blob: 0f583005208b066ccc37646097c955a60a32c90f [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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070050import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070053import android.util.FloatProperty;
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;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070063import android.view.AccessibilityIterators.TextSegmentIterator;
64import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
65import android.view.AccessibilityIterators.WordTextSegmentIterator;
66import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.accessibility.AccessibilityEvent;
68import android.view.accessibility.AccessibilityEventSource;
69import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070070import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070071import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070073import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080074import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.inputmethod.InputConnection;
77import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.ScrollBarDrawable;
79
Romain Guy1ef3fdb2011-09-09 15:30:30 -070080import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070081import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070082
Doug Feltcb3791202011-07-07 11:57:48 -070083import com.android.internal.R;
84import com.android.internal.util.Predicate;
85import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070086import com.google.android.collect.Lists;
87import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070088
Christopher Tatea0374192010-10-05 13:06:41 -070089import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070090import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070091import java.lang.reflect.InvocationTargetException;
92import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070093import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.util.ArrayList;
95import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070096import java.util.Collections;
97import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070098import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080099import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -0700100import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102/**
103 * <p>
104 * This class represents the basic building block for user interface components. A View
105 * occupies a rectangular area on the screen and is responsible for drawing and
106 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700107 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
109 * are invisible containers that hold other Views (or other ViewGroups) and define
110 * their layout properties.
111 * </p>
112 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700113 * <div class="special reference">
114 * <h3>Developer Guides</h3>
115 * <p>For information about using this class to develop your application's user interface,
116 * 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 -0800117 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * <a name="Using"></a>
120 * <h3>Using Views</h3>
121 * <p>
122 * All of the views in a window are arranged in a single tree. You can add views
123 * either from code or by specifying a tree of views in one or more XML layout
124 * files. There are many specialized subclasses of views that act as controls or
125 * are capable of displaying text, images, or other content.
126 * </p>
127 * <p>
128 * Once you have created a tree of views, there are typically a few types of
129 * common operations you may wish to perform:
130 * <ul>
131 * <li><strong>Set properties:</strong> for example setting the text of a
132 * {@link android.widget.TextView}. The available properties and the methods
133 * that set them will vary among the different subclasses of views. Note that
134 * properties that are known at build time can be set in the XML layout
135 * files.</li>
136 * <li><strong>Set focus:</strong> The framework will handled moving focus in
137 * response to user input. To force focus to a specific view, call
138 * {@link #requestFocus}.</li>
139 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
140 * that will be notified when something interesting happens to the view. For
141 * example, all views will let you set a listener to be notified when the view
142 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700143 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700144 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700145 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700147 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * </ul>
149 * </p>
150 * <p><em>
151 * Note: The Android framework is responsible for measuring, laying out and
152 * drawing views. You should not call methods that perform these actions on
153 * views yourself unless you are actually implementing a
154 * {@link android.view.ViewGroup}.
155 * </em></p>
156 *
157 * <a name="Lifecycle"></a>
158 * <h3>Implementing a Custom View</h3>
159 *
160 * <p>
161 * To implement a custom view, you will usually begin by providing overrides for
162 * some of the standard methods that the framework calls on all views. You do
163 * not need to override all of these methods. In fact, you can start by just
164 * overriding {@link #onDraw(android.graphics.Canvas)}.
165 * <table border="2" width="85%" align="center" cellpadding="5">
166 * <thead>
167 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
168 * </thead>
169 *
170 * <tbody>
171 * <tr>
172 * <td rowspan="2">Creation</td>
173 * <td>Constructors</td>
174 * <td>There is a form of the constructor that are called when the view
175 * is created from code and a form that is called when the view is
176 * inflated from a layout file. The second form should parse and apply
177 * any attributes defined in the layout file.
178 * </td>
179 * </tr>
180 * <tr>
181 * <td><code>{@link #onFinishInflate()}</code></td>
182 * <td>Called after a view and all of its children has been inflated
183 * from XML.</td>
184 * </tr>
185 *
186 * <tr>
187 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called to determine the size requirements for this view and all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when this view should assign a size and position to all
196 * of its children.
197 * </td>
198 * </tr>
199 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when the size of this view has changed.
202 * </td>
203 * </tr>
204 *
205 * <tr>
206 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when the view should render its content.
209 * </td>
210 * </tr>
211 *
212 * <tr>
213 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900215 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * </td>
217 * </tr>
218 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700219 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900220 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * </td>
222 * </tr>
223 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when a trackball motion event occurs.
226 * </td>
227 * </tr>
228 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when a touch screen motion event occurs.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700242 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * <td>Called when the window containing the view gains or loses focus.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td rowspan="3">Attaching</td>
249 * <td><code>{@link #onAttachedToWindow()}</code></td>
250 * <td>Called when the view is attached to a window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
255 * <td><code>{@link #onDetachedFromWindow}</code></td>
256 * <td>Called when the view is detached from its window.
257 * </td>
258 * </tr>
259 *
260 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700261 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 * <td>Called when the visibility of the window containing the view
263 * has changed.
264 * </td>
265 * </tr>
266 * </tbody>
267 *
268 * </table>
269 * </p>
270 *
271 * <a name="IDs"></a>
272 * <h3>IDs</h3>
273 * Views may have an integer id associated with them. These ids are typically
274 * assigned in the layout XML files, and are used to find specific views within
275 * the view tree. A common pattern is to:
276 * <ul>
277 * <li>Define a Button in the layout file and assign it a unique ID.
278 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700279 * &lt;Button
280 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * android:layout_width="wrap_content"
282 * android:layout_height="wrap_content"
283 * android:text="@string/my_button_text"/&gt;
284 * </pre></li>
285 * <li>From the onCreate method of an Activity, find the Button
286 * <pre class="prettyprint">
287 * Button myButton = (Button) findViewById(R.id.my_button);
288 * </pre></li>
289 * </ul>
290 * <p>
291 * View IDs need not be unique throughout the tree, but it is good practice to
292 * ensure that they are at least unique within the part of the tree you are
293 * searching.
294 * </p>
295 *
296 * <a name="Position"></a>
297 * <h3>Position</h3>
298 * <p>
299 * The geometry of a view is that of a rectangle. A view has a location,
300 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
301 * two dimensions, expressed as a width and a height. The unit for location
302 * and dimensions is the pixel.
303 * </p>
304 *
305 * <p>
306 * It is possible to retrieve the location of a view by invoking the methods
307 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
308 * coordinate of the rectangle representing the view. The latter returns the
309 * top, or Y, coordinate of the rectangle representing the view. These methods
310 * both return the location of the view relative to its parent. For instance,
311 * when getLeft() returns 20, that means the view is located 20 pixels to the
312 * right of the left edge of its direct parent.
313 * </p>
314 *
315 * <p>
316 * In addition, several convenience methods are offered to avoid unnecessary
317 * computations, namely {@link #getRight()} and {@link #getBottom()}.
318 * These methods return the coordinates of the right and bottom edges of the
319 * rectangle representing the view. For instance, calling {@link #getRight()}
320 * is similar to the following computation: <code>getLeft() + getWidth()</code>
321 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
322 * </p>
323 *
324 * <a name="SizePaddingMargins"></a>
325 * <h3>Size, padding and margins</h3>
326 * <p>
327 * The size of a view is expressed with a width and a height. A view actually
328 * possess two pairs of width and height values.
329 * </p>
330 *
331 * <p>
332 * The first pair is known as <em>measured width</em> and
333 * <em>measured height</em>. These dimensions define how big a view wants to be
334 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
335 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
336 * and {@link #getMeasuredHeight()}.
337 * </p>
338 *
339 * <p>
340 * The second pair is simply known as <em>width</em> and <em>height</em>, or
341 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
342 * dimensions define the actual size of the view on screen, at drawing time and
343 * after layout. These values may, but do not have to, be different from the
344 * measured width and height. The width and height can be obtained by calling
345 * {@link #getWidth()} and {@link #getHeight()}.
346 * </p>
347 *
348 * <p>
349 * To measure its dimensions, a view takes into account its padding. The padding
350 * is expressed in pixels for the left, top, right and bottom parts of the view.
351 * Padding can be used to offset the content of the view by a specific amount of
352 * pixels. For instance, a left padding of 2 will push the view's content by
353 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700354 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
355 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
356 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
357 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * </p>
359 *
360 * <p>
361 * Even though a view can define a padding, it does not provide any support for
362 * margins. However, view groups provide such a support. Refer to
363 * {@link android.view.ViewGroup} and
364 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
365 * </p>
366 *
367 * <a name="Layout"></a>
368 * <h3>Layout</h3>
369 * <p>
370 * Layout is a two pass process: a measure pass and a layout pass. The measuring
371 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
372 * of the view tree. Each view pushes dimension specifications down the tree
373 * during the recursion. At the end of the measure pass, every view has stored
374 * its measurements. The second pass happens in
375 * {@link #layout(int,int,int,int)} and is also top-down. During
376 * this pass each parent is responsible for positioning all of its children
377 * using the sizes computed in the measure pass.
378 * </p>
379 *
380 * <p>
381 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
382 * {@link #getMeasuredHeight()} values must be set, along with those for all of
383 * that view's descendants. A view's measured width and measured height values
384 * must respect the constraints imposed by the view's parents. This guarantees
385 * that at the end of the measure pass, all parents accept all of their
386 * children's measurements. A parent view may call measure() more than once on
387 * its children. For example, the parent may measure each child once with
388 * unspecified dimensions to find out how big they want to be, then call
389 * measure() on them again with actual numbers if the sum of all the children's
390 * unconstrained sizes is too big or too small.
391 * </p>
392 *
393 * <p>
394 * The measure pass uses two classes to communicate dimensions. The
395 * {@link MeasureSpec} class is used by views to tell their parents how they
396 * want to be measured and positioned. The base LayoutParams class just
397 * describes how big the view wants to be for both width and height. For each
398 * dimension, it can specify one of:
399 * <ul>
400 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800401 * <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 -0800402 * (minus padding)
403 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
404 * enclose its content (plus padding).
405 * </ul>
406 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
407 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
408 * an X and Y value.
409 * </p>
410 *
411 * <p>
412 * MeasureSpecs are used to push requirements down the tree from parent to
413 * child. A MeasureSpec can be in one of three modes:
414 * <ul>
415 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
416 * of a child view. For example, a LinearLayout may call measure() on its child
417 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
418 * tall the child view wants to be given a width of 240 pixels.
419 * <li>EXACTLY: This is used by the parent to impose an exact size on the
420 * child. The child must use this size, and guarantee that all of its
421 * descendants will fit within this size.
422 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
423 * child. The child must gurantee that it and all of its descendants will fit
424 * within this size.
425 * </ul>
426 * </p>
427 *
428 * <p>
429 * To intiate a layout, call {@link #requestLayout}. This method is typically
430 * called by a view on itself when it believes that is can no longer fit within
431 * its current bounds.
432 * </p>
433 *
434 * <a name="Drawing"></a>
435 * <h3>Drawing</h3>
436 * <p>
437 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700438 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * this means that parents will draw before (i.e., behind) their children, with
440 * siblings drawn in the order they appear in the tree.
441 * If you set a background drawable for a View, then the View will draw it for you
442 * before calling back to its <code>onDraw()</code> method.
443 * </p>
444 *
445 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700446 * 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 -0800447 * </p>
448 *
449 * <p>
450 * To force a view to draw, call {@link #invalidate()}.
451 * </p>
452 *
453 * <a name="EventHandlingThreading"></a>
454 * <h3>Event Handling and Threading</h3>
455 * <p>
456 * The basic cycle of a view is as follows:
457 * <ol>
458 * <li>An event comes in and is dispatched to the appropriate view. The view
459 * handles the event and notifies any listeners.</li>
460 * <li>If in the course of processing the event, the view's bounds may need
461 * to be changed, the view will call {@link #requestLayout()}.</li>
462 * <li>Similarly, if in the course of processing the event the view's appearance
463 * may need to be changed, the view will call {@link #invalidate()}.</li>
464 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
465 * the framework will take care of measuring, laying out, and drawing the tree
466 * as appropriate.</li>
467 * </ol>
468 * </p>
469 *
470 * <p><em>Note: The entire view tree is single threaded. You must always be on
471 * the UI thread when calling any method on any view.</em>
472 * If you are doing work on other threads and want to update the state of a view
473 * from that thread, you should use a {@link Handler}.
474 * </p>
475 *
476 * <a name="FocusHandling"></a>
477 * <h3>Focus Handling</h3>
478 * <p>
479 * The framework will handle routine focus movement in response to user input.
480 * This includes changing the focus as views are removed or hidden, or as new
481 * views become available. Views indicate their willingness to take focus
482 * through the {@link #isFocusable} method. To change whether a view can take
483 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
484 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
485 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
486 * </p>
487 * <p>
488 * Focus movement is based on an algorithm which finds the nearest neighbor in a
489 * given direction. In rare cases, the default algorithm may not match the
490 * intended behavior of the developer. In these situations, you can provide
491 * explicit overrides by using these XML attributes in the layout file:
492 * <pre>
493 * nextFocusDown
494 * nextFocusLeft
495 * nextFocusRight
496 * nextFocusUp
497 * </pre>
498 * </p>
499 *
500 *
501 * <p>
502 * To get a particular view to take focus, call {@link #requestFocus()}.
503 * </p>
504 *
505 * <a name="TouchMode"></a>
506 * <h3>Touch Mode</h3>
507 * <p>
508 * When a user is navigating a user interface via directional keys such as a D-pad, it is
509 * necessary to give focus to actionable items such as buttons so the user can see
510 * what will take input. If the device has touch capabilities, however, and the user
511 * begins interacting with the interface by touching it, it is no longer necessary to
512 * always highlight, or give focus to, a particular view. This motivates a mode
513 * for interaction named 'touch mode'.
514 * </p>
515 * <p>
516 * For a touch capable device, once the user touches the screen, the device
517 * will enter touch mode. From this point onward, only views for which
518 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
519 * Other views that are touchable, like buttons, will not take focus when touched; they will
520 * only fire the on click listeners.
521 * </p>
522 * <p>
523 * Any time a user hits a directional key, such as a D-pad direction, the view device will
524 * exit touch mode, and find a view to take focus, so that the user may resume interacting
525 * with the user interface without touching the screen again.
526 * </p>
527 * <p>
528 * The touch mode state is maintained across {@link android.app.Activity}s. Call
529 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
530 * </p>
531 *
532 * <a name="Scrolling"></a>
533 * <h3>Scrolling</h3>
534 * <p>
535 * The framework provides basic support for views that wish to internally
536 * scroll their content. This includes keeping track of the X and Y scroll
537 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800538 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700539 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 * </p>
541 *
542 * <a name="Tags"></a>
543 * <h3>Tags</h3>
544 * <p>
545 * Unlike IDs, tags are not used to identify views. Tags are essentially an
546 * extra piece of information that can be associated with a view. They are most
547 * often used as a convenience to store data related to views in the views
548 * themselves rather than by putting them in a separate structure.
549 * </p>
550 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700551 * <a name="Properties"></a>
552 * <h3>Properties</h3>
553 * <p>
554 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
555 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
556 * available both in the {@link Property} form as well as in similarly-named setter/getter
557 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
558 * be used to set persistent state associated with these rendering-related properties on the view.
559 * The properties and methods can also be used in conjunction with
560 * {@link android.animation.Animator Animator}-based animations, described more in the
561 * <a href="#Animation">Animation</a> section.
562 * </p>
563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * <a name="Animation"></a>
565 * <h3>Animation</h3>
566 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700567 * Starting with Android 3.0, the preferred way of animating views is to use the
568 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
569 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
570 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
571 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
572 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
573 * makes animating these View properties particularly easy and efficient.
574 * </p>
575 * <p>
576 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * You can attach an {@link Animation} object to a view using
578 * {@link #setAnimation(Animation)} or
579 * {@link #startAnimation(Animation)}. The animation can alter the scale,
580 * rotation, translation and alpha of a view over time. If the animation is
581 * attached to a view that has children, the animation will affect the entire
582 * subtree rooted by that node. When an animation is started, the framework will
583 * take care of redrawing the appropriate views until the animation completes.
584 * </p>
585 *
Jeff Brown85a31762010-09-01 17:01:00 -0700586 * <a name="Security"></a>
587 * <h3>Security</h3>
588 * <p>
589 * Sometimes it is essential that an application be able to verify that an action
590 * is being performed with the full knowledge and consent of the user, such as
591 * granting a permission request, making a purchase or clicking on an advertisement.
592 * Unfortunately, a malicious application could try to spoof the user into
593 * performing these actions, unaware, by concealing the intended purpose of the view.
594 * As a remedy, the framework offers a touch filtering mechanism that can be used to
595 * improve the security of views that provide access to sensitive functionality.
596 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700597 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800598 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700599 * will discard touches that are received whenever the view's window is obscured by
600 * another visible window. As a result, the view will not receive touches whenever a
601 * toast, dialog or other window appears above the view's window.
602 * </p><p>
603 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700604 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
605 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700606 * </p>
607 *
Romain Guy171c5922011-01-06 10:04:23 -0800608 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_background
610 * @attr ref android.R.styleable#View_clickable
611 * @attr ref android.R.styleable#View_contentDescription
612 * @attr ref android.R.styleable#View_drawingCacheQuality
613 * @attr ref android.R.styleable#View_duplicateParentState
614 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700615 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700616 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700618 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_isScrollContainer
621 * @attr ref android.R.styleable#View_focusable
622 * @attr ref android.R.styleable#View_focusableInTouchMode
623 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
624 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800625 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_longClickable
627 * @attr ref android.R.styleable#View_minHeight
628 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * @attr ref android.R.styleable#View_nextFocusDown
630 * @attr ref android.R.styleable#View_nextFocusLeft
631 * @attr ref android.R.styleable#View_nextFocusRight
632 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_onClick
634 * @attr ref android.R.styleable#View_padding
635 * @attr ref android.R.styleable#View_paddingBottom
636 * @attr ref android.R.styleable#View_paddingLeft
637 * @attr ref android.R.styleable#View_paddingRight
638 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800639 * @attr ref android.R.styleable#View_paddingStart
640 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800642 * @attr ref android.R.styleable#View_rotation
643 * @attr ref android.R.styleable#View_rotationX
644 * @attr ref android.R.styleable#View_rotationY
645 * @attr ref android.R.styleable#View_scaleX
646 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollX
648 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_scrollbarSize
650 * @attr ref android.R.styleable#View_scrollbarStyle
651 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700652 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
653 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
655 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * @attr ref android.R.styleable#View_scrollbarThumbVertical
657 * @attr ref android.R.styleable#View_scrollbarTrackVertical
658 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
659 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_soundEffectsEnabled
661 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700662 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800663 * @attr ref android.R.styleable#View_transformPivotX
664 * @attr ref android.R.styleable#View_transformPivotY
665 * @attr ref android.R.styleable#View_translationX
666 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700667 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 *
669 * @see android.view.ViewGroup
670 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700671public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700672 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 private static final boolean DBG = false;
674
675 /**
676 * The logging tag used by this class with android.util.Log.
677 */
678 protected static final String VIEW_LOG_TAG = "View";
679
680 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700681 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700682 *
683 * @hide
684 */
685 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
686
687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * Used to mark a View that has no ID.
689 */
690 public static final int NO_ID = -1;
691
692 /**
693 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
694 * calling setFlags.
695 */
696 private static final int NOT_FOCUSABLE = 0x00000000;
697
698 /**
699 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
700 * setFlags.
701 */
702 private static final int FOCUSABLE = 0x00000001;
703
704 /**
705 * Mask for use with setFlags indicating bits used for focus.
706 */
707 private static final int FOCUSABLE_MASK = 0x00000001;
708
709 /**
710 * This view will adjust its padding to fit sytem windows (e.g. status bar)
711 */
712 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
713
714 /**
Scott Main812634c22011-07-27 13:22:35 -0700715 * This view is visible.
716 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
717 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final int VISIBLE = 0x00000000;
720
721 /**
722 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700723 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
724 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public static final int INVISIBLE = 0x00000004;
727
728 /**
729 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700730 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
731 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 */
733 public static final int GONE = 0x00000008;
734
735 /**
736 * Mask for use with setFlags indicating bits used for visibility.
737 * {@hide}
738 */
739 static final int VISIBILITY_MASK = 0x0000000C;
740
741 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
742
743 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700744 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * Use with ENABLED_MASK when calling setFlags.
746 * {@hide}
747 */
748 static final int ENABLED = 0x00000000;
749
750 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700751 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 * Use with ENABLED_MASK when calling setFlags.
753 * {@hide}
754 */
755 static final int DISABLED = 0x00000020;
756
757 /**
758 * Mask for use with setFlags indicating bits used for indicating whether
759 * this view is enabled
760 * {@hide}
761 */
762 static final int ENABLED_MASK = 0x00000020;
763
764 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700765 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
766 * called and further optimizations will be performed. It is okay to have
767 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 * {@hide}
769 */
770 static final int WILL_NOT_DRAW = 0x00000080;
771
772 /**
773 * Mask for use with setFlags indicating bits used for indicating whether
774 * this view is will draw
775 * {@hide}
776 */
777 static final int DRAW_MASK = 0x00000080;
778
779 /**
780 * <p>This view doesn't show scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_NONE = 0x00000000;
784
785 /**
786 * <p>This view shows horizontal scrollbars.</p>
787 * {@hide}
788 */
789 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
790
791 /**
792 * <p>This view shows vertical scrollbars.</p>
793 * {@hide}
794 */
795 static final int SCROLLBARS_VERTICAL = 0x00000200;
796
797 /**
798 * <p>Mask for use with setFlags indicating bits used for indicating which
799 * scrollbars are enabled.</p>
800 * {@hide}
801 */
802 static final int SCROLLBARS_MASK = 0x00000300;
803
Jeff Brown85a31762010-09-01 17:01:00 -0700804 /**
805 * Indicates that the view should filter touches when its window is obscured.
806 * Refer to the class comments for more information about this security feature.
807 * {@hide}
808 */
809 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
810
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700811 /**
812 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
813 * that they are optional and should be skipped if the window has
814 * requested system UI flags that ignore those insets for layout.
815 */
816 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 /**
819 * <p>This view doesn't show fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_NONE = 0x00000000;
823
824 /**
825 * <p>This view shows horizontal fading edges.</p>
826 * {@hide}
827 */
828 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
829
830 /**
831 * <p>This view shows vertical fading edges.</p>
832 * {@hide}
833 */
834 static final int FADING_EDGE_VERTICAL = 0x00002000;
835
836 /**
837 * <p>Mask for use with setFlags indicating bits used for indicating which
838 * fading edges are enabled.</p>
839 * {@hide}
840 */
841 static final int FADING_EDGE_MASK = 0x00003000;
842
843 /**
844 * <p>Indicates this view can be clicked. When clickable, a View reacts
845 * to clicks by notifying the OnClickListener.<p>
846 * {@hide}
847 */
848 static final int CLICKABLE = 0x00004000;
849
850 /**
851 * <p>Indicates this view is caching its drawing into a bitmap.</p>
852 * {@hide}
853 */
854 static final int DRAWING_CACHE_ENABLED = 0x00008000;
855
856 /**
857 * <p>Indicates that no icicle should be saved for this view.<p>
858 * {@hide}
859 */
860 static final int SAVE_DISABLED = 0x000010000;
861
862 /**
863 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
864 * property.</p>
865 * {@hide}
866 */
867 static final int SAVE_DISABLED_MASK = 0x000010000;
868
869 /**
870 * <p>Indicates that no drawing cache should ever be created for this view.<p>
871 * {@hide}
872 */
873 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
874
875 /**
876 * <p>Indicates this view can take / keep focus when int touch mode.</p>
877 * {@hide}
878 */
879 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
880
881 /**
882 * <p>Enables low quality mode for the drawing cache.</p>
883 */
884 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
885
886 /**
887 * <p>Enables high quality mode for the drawing cache.</p>
888 */
889 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
890
891 /**
892 * <p>Enables automatic quality mode for the drawing cache.</p>
893 */
894 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
895
896 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
897 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
898 };
899
900 /**
901 * <p>Mask for use with setFlags indicating bits used for the cache
902 * quality property.</p>
903 * {@hide}
904 */
905 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
906
907 /**
908 * <p>
909 * Indicates this view can be long clicked. When long clickable, a View
910 * reacts to long clicks by notifying the OnLongClickListener or showing a
911 * context menu.
912 * </p>
913 * {@hide}
914 */
915 static final int LONG_CLICKABLE = 0x00200000;
916
917 /**
918 * <p>Indicates that this view gets its drawable states from its direct parent
919 * and ignores its original internal states.</p>
920 *
921 * @hide
922 */
923 static final int DUPLICATE_PARENT_STATE = 0x00400000;
924
925 /**
926 * The scrollbar style to display the scrollbars inside the content area,
927 * without increasing the padding. The scrollbars will be overlaid with
928 * translucency on the view's content.
929 */
930 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
931
932 /**
933 * The scrollbar style to display the scrollbars inside the padded area,
934 * increasing the padding of the view. The scrollbars will not overlap the
935 * content area of the view.
936 */
937 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
938
939 /**
940 * The scrollbar style to display the scrollbars at the edge of the view,
941 * without increasing the padding. The scrollbars will be overlaid with
942 * translucency.
943 */
944 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
945
946 /**
947 * The scrollbar style to display the scrollbars at the edge of the view,
948 * increasing the padding of the view. The scrollbars will only overlap the
949 * background, if any.
950 */
951 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
952
953 /**
954 * Mask to check if the scrollbar style is overlay or inset.
955 * {@hide}
956 */
957 static final int SCROLLBARS_INSET_MASK = 0x01000000;
958
959 /**
960 * Mask to check if the scrollbar style is inside or outside.
961 * {@hide}
962 */
963 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
964
965 /**
966 * Mask for scrollbar style.
967 * {@hide}
968 */
969 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
970
971 /**
972 * View flag indicating that the screen should remain on while the
973 * window containing this view is visible to the user. This effectively
974 * takes care of automatically setting the WindowManager's
975 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
976 */
977 public static final int KEEP_SCREEN_ON = 0x04000000;
978
979 /**
980 * View flag indicating whether this view should have sound effects enabled
981 * for events such as clicking and touching.
982 */
983 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
984
985 /**
986 * View flag indicating whether this view should have haptic feedback
987 * enabled for events such as long presses.
988 */
989 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
990
991 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700992 * <p>Indicates that the view hierarchy should stop saving state when
993 * it reaches this view. If state saving is initiated immediately at
994 * the view, it will be allowed.
995 * {@hide}
996 */
997 static final int PARENT_SAVE_DISABLED = 0x20000000;
998
999 /**
1000 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1001 * {@hide}
1002 */
1003 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1004
1005 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add all focusable Views regardless if they are focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_ALL = 0x00000000;
1010
1011 /**
1012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add only Views focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_BACKWARD = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_FORWARD = 0x00000002;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_LEFT = 0x00000011;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_UP = 0x00000021;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_RIGHT = 0x00000042;
1043
1044 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001045 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final int FOCUS_DOWN = 0x00000082;
1048
Svetoslav Ganov42138042012-03-20 11:51:39 -07001049 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1052 */
1053 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1054
1055 /**
1056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1058 */
1059 public static final int MEASURED_STATE_MASK = 0xff000000;
1060
1061 /**
1062 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1063 * for functions that combine both width and height into a single int,
1064 * such as {@link #getMeasuredState()} and the childState argument of
1065 * {@link #resolveSizeAndState(int, int, int)}.
1066 */
1067 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1068
1069 /**
1070 * Bit of {@link #getMeasuredWidthAndState()} and
1071 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1072 * is smaller that the space the view would like to have.
1073 */
1074 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1075
1076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 * Base View state sets
1078 */
1079 // Singles
1080 /**
1081 * Indicates the view has no states set. States are used with
1082 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1083 * view depending on its state.
1084 *
1085 * @see android.graphics.drawable.Drawable
1086 * @see #getDrawableState()
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled. States are used with
1091 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1092 * view depending on its state.
1093 *
1094 * @see android.graphics.drawable.Drawable
1095 * @see #getDrawableState()
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is focused. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is selected. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is pressed. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 * @hide
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view's window has focus. States are used with
1128 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1129 * view depending on its state.
1130 *
1131 * @see android.graphics.drawable.Drawable
1132 * @see #getDrawableState()
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 // Doubles
1136 /**
1137 * Indicates the view is enabled and has the focus.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and selected.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is enabled and that its window has focus.
1152 *
1153 * @see #ENABLED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is focused and selected.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #SELECTED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view has the focus and that its window has the focus.
1166 *
1167 * @see #FOCUSED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is selected and that its window has the focus.
1173 *
1174 * @see #SELECTED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // Triples
1179 /**
1180 * Indicates the view is enabled, focused and selected.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, focused and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #FOCUSED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is enabled, selected and its window has the focus.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is focused, selected and its window has the focus.
1205 *
1206 * @see #FOCUSED_STATE_SET
1207 * @see #SELECTED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, focused, selected and its window
1213 * has the focus.
1214 *
1215 * @see #ENABLED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and its window has the focus.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed and selected.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed, selected and its window has the focus.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #SELECTED_STATE_SET
1240 * @see #WINDOW_FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed and focused.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and its window has the focus.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #FOCUSED_STATE_SET
1255 * @see #WINDOW_FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused and selected.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #SELECTED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused, selected and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #SELECTED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed and enabled.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #WINDOW_FOCUSED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled and selected.
1292 *
1293 * @see #PRESSED_STATE_SET
1294 * @see #ENABLED_STATE_SET
1295 * @see #SELECTED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled, selected and its window has the
1300 * focus.
1301 *
1302 * @see #PRESSED_STATE_SET
1303 * @see #ENABLED_STATE_SET
1304 * @see #SELECTED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and focused.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #FOCUSED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, focused and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #FOCUSED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled, focused and selected.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #SELECTED_STATE_SET
1332 * @see #FOCUSED_STATE_SET
1333 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * Indicates the view is pressed, enabled, focused, selected and its window
1337 * has the focus.
1338 *
1339 * @see #PRESSED_STATE_SET
1340 * @see #ENABLED_STATE_SET
1341 * @see #SELECTED_STATE_SET
1342 * @see #FOCUSED_STATE_SET
1343 * @see #WINDOW_FOCUSED_STATE_SET
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346
1347 /**
1348 * The order here is very important to {@link #getDrawableState()}
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 private static final int[][] VIEW_STATE_SETS;
1351
Romain Guyb051e892010-09-28 19:09:36 -07001352 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1353 static final int VIEW_STATE_SELECTED = 1 << 1;
1354 static final int VIEW_STATE_FOCUSED = 1 << 2;
1355 static final int VIEW_STATE_ENABLED = 1 << 3;
1356 static final int VIEW_STATE_PRESSED = 1 << 4;
1357 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001358 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001359 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001360 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1361 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362
1363 static final int[] VIEW_STATE_IDS = new int[] {
1364 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1365 R.attr.state_selected, VIEW_STATE_SELECTED,
1366 R.attr.state_focused, VIEW_STATE_FOCUSED,
1367 R.attr.state_enabled, VIEW_STATE_ENABLED,
1368 R.attr.state_pressed, VIEW_STATE_PRESSED,
1369 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001371 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001372 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001373 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 };
1375
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001377 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1378 throw new IllegalStateException(
1379 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1380 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001384 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001386 orderedIds[i * 2] = viewState;
1387 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 }
1389 }
1390 }
Romain Guyb051e892010-09-28 19:09:36 -07001391 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1392 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1393 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 int numBits = Integer.bitCount(i);
1395 int[] set = new int[numBits];
1396 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001397 for (int j = 0; j < orderedIds.length; j += 2) {
1398 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 set[pos++] = orderedIds[j];
1400 }
1401 }
1402 VIEW_STATE_SETS[i] = set;
1403 }
1404
1405 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1406 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1407 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1408 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1410 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1411 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1413 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1415 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_FOCUSED];
1418 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1419 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1423 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1436 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1437
1438 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1439 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1443 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1456 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 }
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001484 * Accessibility event types that are dispatched for text population.
1485 */
1486 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1487 AccessibilityEvent.TYPE_VIEW_CLICKED
1488 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1489 | AccessibilityEvent.TYPE_VIEW_SELECTED
1490 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1491 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1492 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001493 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001494 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001495 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001496 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1497 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001498
1499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 * Temporary Rect currently for use in setBackground(). This will probably
1501 * be extended in the future to hold our own class with more than just
1502 * a Rect. :)
1503 */
1504 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001505
1506 /**
1507 * Map used to store views' tags.
1508 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001509 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001512 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001513 */
1514 private static int sNextAccessibilityViewId;
1515
1516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * The animation currently associated with this view.
1518 * @hide
1519 */
1520 protected Animation mCurrentAnimation = null;
1521
1522 /**
1523 * Width as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
1530 * Height as measured during measure pass.
1531 * {@hide}
1532 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001533 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001534 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535
1536 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001537 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1538 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1539 * its display list. This flag, used only when hw accelerated, allows us to clear the
1540 * flag while retaining this information until it's needed (at getDisplayList() time and
1541 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1542 *
1543 * {@hide}
1544 */
1545 boolean mRecreateDisplayList = false;
1546
1547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * The view's identifier.
1549 * {@hide}
1550 *
1551 * @see #setId(int)
1552 * @see #getId()
1553 */
1554 @ViewDebug.ExportedProperty(resolveId = true)
1555 int mID = NO_ID;
1556
1557 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001558 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001559 */
1560 int mAccessibilityViewId = NO_ID;
1561
1562 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001563 * @hide
1564 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001565 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001566
1567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 * The view's tag.
1569 * {@hide}
1570 *
1571 * @see #setTag(Object)
1572 * @see #getTag()
1573 */
1574 protected Object mTag;
1575
1576 // for mPrivateFlags:
1577 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001578 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001584 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001588 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 /**
1590 * When this flag is set, this view is running an animation on behalf of its
1591 * children and should therefore not cancel invalidate requests, even if they
1592 * lie outside of this view's bounds.
1593 *
1594 * {@hide}
1595 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001608 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001609 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001610 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611
Dianne Hackborn4702a852012-08-17 15:18:29 -07001612 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
1614 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 /**
1617 * Flag used to indicate that this view should be drawn once more (and only once
1618 * more) after its animation has completed.
1619 * {@hide}
1620 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001621 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622
Dianne Hackborn4702a852012-08-17 15:18:29 -07001623 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
1625 /**
1626 * Indicates that the View returned true when onSetAlpha() was called and that
1627 * the alpha must be restored.
1628 * {@hide}
1629 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001630 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 /**
1633 * Set by {@link #setScrollContainer(boolean)}.
1634 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001635 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 /**
1638 * Set by {@link #setScrollContainer(boolean)}.
1639 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001640 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
1642 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001643 * View flag indicating whether this view was invalidated (fully or partially.)
1644 *
1645 * @hide
1646 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001647 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001648
1649 /**
1650 * View flag indicating whether this view was invalidated by an opaque
1651 * invalidate request.
1652 *
1653 * @hide
1654 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001655 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001656
1657 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001658 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001659 *
1660 * @hide
1661 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001662 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001663
1664 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001665 * Indicates whether the background is opaque.
1666 *
1667 * @hide
1668 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001669 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001670
1671 /**
1672 * Indicates whether the scrollbars are opaque.
1673 *
1674 * @hide
1675 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001676 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001677
1678 /**
1679 * Indicates whether the view is opaque.
1680 *
1681 * @hide
1682 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001683 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powelle14579b2009-12-16 18:39:52 -08001685 /**
1686 * Indicates a prepressed state;
1687 * the short time between ACTION_DOWN and recognizing
1688 * a 'real' press. Prepressed is used to recognize quick taps
1689 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001690 *
Adam Powelle14579b2009-12-16 18:39:52 -08001691 * @hide
1692 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001693 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powellc9fbaab2010-02-16 17:16:19 -08001695 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001696 * Indicates whether the view is temporarily detached.
1697 *
1698 * @hide
1699 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001700 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001701
Adam Powell8568c3a2010-04-19 14:26:11 -07001702 /**
1703 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001704 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001705 * @hide
1706 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001707 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001708
1709 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001710 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1711 * @hide
1712 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001713 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001714
1715 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001716 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1717 * for transform operations
1718 *
1719 * @hide
1720 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001721 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001722
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001723 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001724 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001725
Chet Haasefd2b0022010-08-06 13:08:56 -07001726 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001727 * Indicates that this view was specifically invalidated, not just dirtied because some
1728 * child view was invalidated. The flag is used to determine when we need to recreate
1729 * a view's display list (as opposed to just returning a reference to its existing
1730 * display list).
1731 *
1732 * @hide
1733 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001734 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001735
Jeff Sharkey36901b62012-09-19 19:06:22 -07001736 /**
1737 * Masks for mPrivateFlags2, as generated by dumpFlags():
1738 *
1739 * -------|-------|-------|-------|
1740 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1741 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1742 * 1 PFLAG2_DRAG_CAN_ACCEPT
1743 * 1 PFLAG2_DRAG_HOVERED
1744 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1745 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1746 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1747 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1748 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1749 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1750 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1751 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1752 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1753 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1754 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1755 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1756 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1757 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1758 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1759 * 111 PFLAG2_TEXT_DIRECTION_MASK
1760 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1761 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1762 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1763 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1764 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1765 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1766 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1767 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1768 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1769 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1770 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1771 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1772 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1773 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1774 * 1 PFLAG2_HAS_TRANSIENT_STATE
1775 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1776 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1777 * 1 PFLAG2_VIEW_QUICK_REJECTED
1778 * 1 PFLAG2_PADDING_RESOLVED
1779 * -------|-------|-------|-------|
1780 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001781
1782 /**
1783 * Indicates that this view has reported that it can accept the current drag's content.
1784 * Cleared when the drag operation concludes.
1785 * @hide
1786 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001787 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001788
1789 /**
1790 * Indicates that this view is currently directly under the drag location in a
1791 * drag-and-drop operation involving content that it can accept. Cleared when
1792 * the drag exits the view, or when the drag operation concludes.
1793 * @hide
1794 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001795 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001796
Cibu Johny86666632010-02-22 13:01:02 -08001797 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 * Horizontal layout direction of this view is from Left to Right.
1799 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001800 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001801 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001802
1803 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001804 * Horizontal layout direction of this view is from Right to Left.
1805 * Use with {@link #setLayoutDirection}.
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001808
1809 /**
1810 * Horizontal layout direction of this view is inherited from its parent.
1811 * Use with {@link #setLayoutDirection}.
1812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Horizontal layout direction of this view is from deduced from the default language
1817 * script for the locale. Use with {@link #setLayoutDirection}.
1818 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001819 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001820
1821 /**
1822 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001823 * @hide
1824 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001825 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Mask for use with private flags indicating bits used for horizontal layout direction.
1829 * @hide
1830 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001831 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001832
1833 /**
1834 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1835 * right-to-left direction.
1836 * @hide
1837 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001838 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Indicates whether the view horizontal layout direction has been resolved.
1842 * @hide
1843 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001844 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /**
1847 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1848 * @hide
1849 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001850 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1851 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001852
1853 /*
1854 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1855 * flag value.
1856 * @hide
1857 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001858 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1859 LAYOUT_DIRECTION_LTR,
1860 LAYOUT_DIRECTION_RTL,
1861 LAYOUT_DIRECTION_INHERIT,
1862 LAYOUT_DIRECTION_LOCALE
1863 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001864
1865 /**
1866 * Default horizontal layout direction.
1867 * @hide
1868 */
1869 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001870
Adam Powell539ee872012-02-03 19:00:49 -08001871 /**
1872 * Indicates that the view is tracking some sort of transient state
1873 * that the app should not need to be aware of, but that the framework
1874 * should take special care to preserve.
1875 *
1876 * @hide
1877 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001878 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001879
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001880 /**
1881 * Text direction is inherited thru {@link ViewGroup}
1882 */
1883 public static final int TEXT_DIRECTION_INHERIT = 0;
1884
1885 /**
1886 * Text direction is using "first strong algorithm". The first strong directional character
1887 * determines the paragraph direction. If there is no strong directional character, the
1888 * paragraph direction is the view's resolved layout direction.
1889 */
1890 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1891
1892 /**
1893 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1894 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1895 * If there are neither, the paragraph direction is the view's resolved layout direction.
1896 */
1897 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1898
1899 /**
1900 * Text direction is forced to LTR.
1901 */
1902 public static final int TEXT_DIRECTION_LTR = 3;
1903
1904 /**
1905 * Text direction is forced to RTL.
1906 */
1907 public static final int TEXT_DIRECTION_RTL = 4;
1908
1909 /**
1910 * Text direction is coming from the system Locale.
1911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
1916 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001917 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001918
1919 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001920 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1921 * @hide
1922 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001923 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001924
1925 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001926 * Mask for use with private flags indicating bits used for text direction.
1927 * @hide
1928 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001929 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1930 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001937 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001950 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1951 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001952
1953 /**
1954 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1955 * @hide
1956 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001957 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001958
1959 /**
1960 * Mask for use with private flags indicating bits used for resolved text direction.
1961 * @hide
1962 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001963 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1964 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001965
1966 /**
1967 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1968 * @hide
1969 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001970 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1971 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001972
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001973 /*
1974 * Default text alignment. The text alignment of this View is inherited from its parent.
1975 * Use with {@link #setTextAlignment(int)}
1976 */
1977 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1978
1979 /**
1980 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1981 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1982 *
1983 * Use with {@link #setTextAlignment(int)}
1984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
1991 */
1992 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1993
1994 /**
1995 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1996 *
1997 * Use with {@link #setTextAlignment(int)}
1998 */
1999 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2000
2001 /**
2002 * Center the paragraph, e.g. ALIGN_CENTER.
2003 *
2004 * Use with {@link #setTextAlignment(int)}
2005 */
2006 public static final int TEXT_ALIGNMENT_CENTER = 4;
2007
2008 /**
2009 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2010 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2011 *
2012 * Use with {@link #setTextAlignment(int)}
2013 */
2014 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2015
2016 /**
2017 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2018 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2019 *
2020 * Use with {@link #setTextAlignment(int)}
2021 */
2022 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2023
2024 /**
2025 * Default text alignment is inherited
2026 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002027 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002028
2029 /**
2030 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2031 * @hide
2032 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002033 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002034
2035 /**
2036 * Mask for use with private flags indicating bits used for text alignment.
2037 * @hide
2038 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002039 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002040
2041 /**
2042 * Array of text direction flags for mapping attribute "textAlignment" to correct
2043 * flag value.
2044 * @hide
2045 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002046 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2047 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2053 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002054 };
2055
2056 /**
2057 * Indicates whether the view text alignment has been resolved.
2058 * @hide
2059 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002060 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002061
2062 /**
2063 * Bit shift to get the resolved text alignment.
2064 * @hide
2065 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002066 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002067
2068 /**
2069 * Mask for use with private flags indicating bits used for text alignment.
2070 * @hide
2071 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002072 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2073 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002074
2075 /**
2076 * Indicates whether if the view text alignment has been resolved to gravity
2077 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002078 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2079 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002080
Svetoslav Ganov42138042012-03-20 11:51:39 -07002081 // Accessiblity constants for mPrivateFlags2
2082
2083 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002084 * Shift for the bits in {@link #mPrivateFlags2} related to the
2085 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002087 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002088
2089 /**
2090 * Automatically determine whether a view is important for accessibility.
2091 */
2092 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2093
2094 /**
2095 * The view is important for accessibility.
2096 */
2097 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2098
2099 /**
2100 * The view is not important for accessibility.
2101 */
2102 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2103
2104 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002105 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002106 */
2107 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2108
2109 /**
2110 * Mask for obtainig the bits which specify how to determine
2111 * whether a view is important for accessibility.
2112 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002113 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002114 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002115 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002116
2117 /**
2118 * Flag indicating whether a view has accessibility focus.
2119 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002120 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002121
2122 /**
2123 * Flag indicating whether a view state for accessibility has changed.
2124 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002125 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2126 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002127
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002128 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002129 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2130 * is used to check whether later changes to the view's transform should invalidate the
2131 * view to force the quickReject test to run again.
2132 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002133 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002134
Adam Powell0090f202012-08-07 17:15:30 -07002135 /**
2136 * Flag indicating that start/end padding has been resolved into left/right padding
2137 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2138 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2139 * during measurement. In some special cases this is required such as when an adapter-based
2140 * view measures prospective children without attaching them to a window.
2141 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002142 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002143
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002144 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002145
Christopher Tate3d4bf172011-03-28 16:16:46 -07002146 /* End of masks for mPrivateFlags2 */
2147
Chet Haase21433372012-06-05 07:54:09 -07002148 /* Masks for mPrivateFlags3 */
2149
2150 /**
2151 * Flag indicating that view has a transform animation set on it. This is used to track whether
2152 * an animation is cleared between successive frames, in order to tell the associated
2153 * DisplayList to clear its animation matrix.
2154 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002155 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002156
2157 /**
2158 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2159 * animation is cleared between successive frames, in order to tell the associated
2160 * DisplayList to restore its alpha value.
2161 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002162 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002163
2164
2165 /* End of masks for mPrivateFlags3 */
2166
Dianne Hackborn4702a852012-08-17 15:18:29 -07002167 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002168
Chet Haasedaf98e92011-01-10 14:10:36 -08002169 /**
Adam Powell637d3372010-08-25 14:37:03 -07002170 * Always allow a user to over-scroll this view, provided it is a
2171 * view that can scroll.
2172 *
2173 * @see #getOverScrollMode()
2174 * @see #setOverScrollMode(int)
2175 */
2176 public static final int OVER_SCROLL_ALWAYS = 0;
2177
2178 /**
2179 * Allow a user to over-scroll this view only if the content is large
2180 * enough to meaningfully scroll, provided it is a view that can scroll.
2181 *
2182 * @see #getOverScrollMode()
2183 * @see #setOverScrollMode(int)
2184 */
2185 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2186
2187 /**
2188 * Never allow a user to over-scroll this view.
2189 *
2190 * @see #getOverScrollMode()
2191 * @see #setOverScrollMode(int)
2192 */
2193 public static final int OVER_SCROLL_NEVER = 2;
2194
2195 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002196 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2197 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002198 *
Joe Malin32736f02011-01-19 16:14:20 -08002199 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002200 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002201 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002202
2203 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002204 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2205 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002206 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002207 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002208 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002209 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002210 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002211 *
Joe Malin32736f02011-01-19 16:14:20 -08002212 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002213 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002214 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2215
2216 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002217 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2218 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002219 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002220 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002221 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2222 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2223 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002224 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002225 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2226 * window flags) for displaying content using every last pixel on the display.
2227 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002228 * <p>There is a limitation: because navigation controls are so important, the least user
2229 * interaction will cause them to reappear immediately. When this happens, both
2230 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2231 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002232 *
2233 * @see #setSystemUiVisibility(int)
2234 */
2235 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2236
2237 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002238 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2239 * into the normal fullscreen mode so that its content can take over the screen
2240 * while still allowing the user to interact with the application.
2241 *
2242 * <p>This has the same visual effect as
2243 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2244 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2245 * meaning that non-critical screen decorations (such as the status bar) will be
2246 * hidden while the user is in the View's window, focusing the experience on
2247 * that content. Unlike the window flag, if you are using ActionBar in
2248 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2249 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2250 * hide the action bar.
2251 *
2252 * <p>This approach to going fullscreen is best used over the window flag when
2253 * it is a transient state -- that is, the application does this at certain
2254 * points in its user interaction where it wants to allow the user to focus
2255 * on content, but not as a continuous state. For situations where the application
2256 * would like to simply stay full screen the entire time (such as a game that
2257 * wants to take over the screen), the
2258 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2259 * is usually a better approach. The state set here will be removed by the system
2260 * in various situations (such as the user moving to another application) like
2261 * the other system UI states.
2262 *
2263 * <p>When using this flag, the application should provide some easy facility
2264 * for the user to go out of it. A common example would be in an e-book
2265 * reader, where tapping on the screen brings back whatever screen and UI
2266 * decorations that had been hidden while the user was immersed in reading
2267 * the book.
2268 *
2269 * @see #setSystemUiVisibility(int)
2270 */
2271 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2272
2273 /**
2274 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2275 * flags, we would like a stable view of the content insets given to
2276 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2277 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002278 * as a continuous state. In the stock Android UI this is the space for
2279 * the system bar, nav bar, and status bar, but not more transient elements
2280 * such as an input method.
2281 *
2282 * The stable layout your UI sees is based on the system UI modes you can
2283 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2284 * then you will get a stable layout for changes of the
2285 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2286 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2287 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2288 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2289 * with a stable layout. (Note that you should avoid using
2290 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2291 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002292 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002293 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2294 * then a hidden status bar will be considered a "stable" state for purposes
2295 * here. This allows your UI to continually hide the status bar, while still
2296 * using the system UI flags to hide the action bar while still retaining
2297 * a stable layout. Note that changing the window fullscreen flag will never
2298 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002299 *
2300 * <p>If you are using ActionBar in
2301 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2302 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2303 * insets it adds to those given to the application.
2304 */
2305 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2306
2307 /**
2308 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2309 * to be layed out as if it has requested
2310 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2311 * allows it to avoid artifacts when switching in and out of that mode, at
2312 * the expense that some of its user interface may be covered by screen
2313 * decorations when they are shown. You can perform layout of your inner
2314 * UI elements to account for the navagation system UI through the
2315 * {@link #fitSystemWindows(Rect)} method.
2316 */
2317 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2318
2319 /**
2320 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2321 * to be layed out as if it has requested
2322 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2323 * allows it to avoid artifacts when switching in and out of that mode, at
2324 * the expense that some of its user interface may be covered by screen
2325 * decorations when they are shown. You can perform layout of your inner
2326 * UI elements to account for non-fullscreen system UI through the
2327 * {@link #fitSystemWindows(Rect)} method.
2328 */
2329 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2330
2331 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002332 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2333 */
2334 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2335
2336 /**
2337 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2338 */
2339 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002340
2341 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002342 * @hide
2343 *
2344 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2345 * out of the public fields to keep the undefined bits out of the developer's way.
2346 *
2347 * Flag to make the status bar not expandable. Unless you also
2348 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2349 */
2350 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2351
2352 /**
2353 * @hide
2354 *
2355 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2356 * out of the public fields to keep the undefined bits out of the developer's way.
2357 *
2358 * Flag to hide notification icons and scrolling ticker text.
2359 */
2360 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2361
2362 /**
2363 * @hide
2364 *
2365 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2366 * out of the public fields to keep the undefined bits out of the developer's way.
2367 *
2368 * Flag to disable incoming notification alerts. This will not block
2369 * icons, but it will block sound, vibrating and other visual or aural notifications.
2370 */
2371 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2372
2373 /**
2374 * @hide
2375 *
2376 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2377 * out of the public fields to keep the undefined bits out of the developer's way.
2378 *
2379 * Flag to hide only the scrolling ticker. Note that
2380 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2381 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2382 */
2383 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2384
2385 /**
2386 * @hide
2387 *
2388 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2389 * out of the public fields to keep the undefined bits out of the developer's way.
2390 *
2391 * Flag to hide the center system info area.
2392 */
2393 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2394
2395 /**
2396 * @hide
2397 *
2398 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2399 * out of the public fields to keep the undefined bits out of the developer's way.
2400 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002401 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002402 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2403 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002404 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002405
2406 /**
2407 * @hide
2408 *
2409 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2410 * out of the public fields to keep the undefined bits out of the developer's way.
2411 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002412 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002413 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2414 */
2415 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2416
2417 /**
2418 * @hide
2419 *
2420 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2421 * out of the public fields to keep the undefined bits out of the developer's way.
2422 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002423 * Flag to hide only the clock. You might use this if your activity has
2424 * its own clock making the status bar's clock redundant.
2425 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002426 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2427
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002428 /**
2429 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002430 *
2431 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2432 * out of the public fields to keep the undefined bits out of the developer's way.
2433 *
2434 * Flag to hide only the recent apps button. Don't use this
2435 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2436 */
2437 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2438
2439 /**
2440 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002441 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002442 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002443
2444 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002445 * These are the system UI flags that can be cleared by events outside
2446 * of an application. Currently this is just the ability to tap on the
2447 * screen while hiding the navigation bar to have it return.
2448 * @hide
2449 */
2450 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002451 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2452 | SYSTEM_UI_FLAG_FULLSCREEN;
2453
2454 /**
2455 * Flags that can impact the layout in relation to system UI.
2456 */
2457 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2458 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2459 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002460
2461 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002462 * Find views that render the specified text.
2463 *
2464 * @see #findViewsWithText(ArrayList, CharSequence, int)
2465 */
2466 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2467
2468 /**
2469 * Find find views that contain the specified content description.
2470 *
2471 * @see #findViewsWithText(ArrayList, CharSequence, int)
2472 */
2473 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2474
2475 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002476 * Find views that contain {@link AccessibilityNodeProvider}. Such
2477 * a View is a root of virtual view hierarchy and may contain the searched
2478 * text. If this flag is set Views with providers are automatically
2479 * added and it is a responsibility of the client to call the APIs of
2480 * the provider to determine whether the virtual tree rooted at this View
2481 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2482 * represeting the virtual views with this text.
2483 *
2484 * @see #findViewsWithText(ArrayList, CharSequence, int)
2485 *
2486 * @hide
2487 */
2488 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2489
2490 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002491 * The undefined cursor position.
2492 */
2493 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2494
2495 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002496 * Indicates that the screen has changed state and is now off.
2497 *
2498 * @see #onScreenStateChanged(int)
2499 */
2500 public static final int SCREEN_STATE_OFF = 0x0;
2501
2502 /**
2503 * Indicates that the screen has changed state and is now on.
2504 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002505 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002506 */
2507 public static final int SCREEN_STATE_ON = 0x1;
2508
2509 /**
Adam Powell637d3372010-08-25 14:37:03 -07002510 * Controls the over-scroll mode for this view.
2511 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2512 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2513 * and {@link #OVER_SCROLL_NEVER}.
2514 */
2515 private int mOverScrollMode;
2516
2517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 * The parent this view is attached to.
2519 * {@hide}
2520 *
2521 * @see #getParent()
2522 */
2523 protected ViewParent mParent;
2524
2525 /**
2526 * {@hide}
2527 */
2528 AttachInfo mAttachInfo;
2529
2530 /**
2531 * {@hide}
2532 */
Romain Guy809a7f62009-05-14 15:44:42 -07002533 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002534 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002535 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002536 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002537 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002538 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002539 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002540 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2541 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2542 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2543 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002544 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002546 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002547 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548
2549 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002550 * This view's request for the visibility of the status bar.
2551 * @hide
2552 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002553 @ViewDebug.ExportedProperty(flagMapping = {
2554 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2555 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2556 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2557 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2558 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2559 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2560 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2561 equals = SYSTEM_UI_FLAG_VISIBLE,
2562 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2563 })
Joe Onorato664644d2011-01-23 17:53:23 -08002564 int mSystemUiVisibility;
2565
2566 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002567 * Reference count for transient state.
2568 * @see #setHasTransientState(boolean)
2569 */
2570 int mTransientStateCount = 0;
2571
2572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 * Count of how many windows this view has been attached to.
2574 */
2575 int mWindowAttachCount;
2576
2577 /**
2578 * The layout parameters associated with this view and used by the parent
2579 * {@link android.view.ViewGroup} to determine how this view should be
2580 * laid out.
2581 * {@hide}
2582 */
2583 protected ViewGroup.LayoutParams mLayoutParams;
2584
2585 /**
2586 * The view flags hold various views states.
2587 * {@hide}
2588 */
2589 @ViewDebug.ExportedProperty
2590 int mViewFlags;
2591
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002592 static class TransformationInfo {
2593 /**
2594 * The transform matrix for the View. This transform is calculated internally
2595 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2596 * is used by default. Do *not* use this variable directly; instead call
2597 * getMatrix(), which will automatically recalculate the matrix if necessary
2598 * to get the correct matrix based on the latest rotation and scale properties.
2599 */
2600 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002601
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002602 /**
2603 * The transform matrix for the View. This transform is calculated internally
2604 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2605 * is used by default. Do *not* use this variable directly; instead call
2606 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2607 * to get the correct matrix based on the latest rotation and scale properties.
2608 */
2609 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002610
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002611 /**
2612 * An internal variable that tracks whether we need to recalculate the
2613 * transform matrix, based on whether the rotation or scaleX/Y properties
2614 * have changed since the matrix was last calculated.
2615 */
2616 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002617
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002618 /**
2619 * An internal variable that tracks whether we need to recalculate the
2620 * transform matrix, based on whether the rotation or scaleX/Y properties
2621 * have changed since the matrix was last calculated.
2622 */
2623 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002624
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002625 /**
2626 * A variable that tracks whether we need to recalculate the
2627 * transform matrix, based on whether the rotation or scaleX/Y properties
2628 * have changed since the matrix was last calculated. This variable
2629 * is only valid after a call to updateMatrix() or to a function that
2630 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2631 */
2632 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002633
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002634 /**
2635 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2636 */
2637 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002638
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002639 /**
2640 * This matrix is used when computing the matrix for 3D rotations.
2641 */
2642 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002643
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002644 /**
2645 * These prev values are used to recalculate a centered pivot point when necessary. The
2646 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2647 * set), so thes values are only used then as well.
2648 */
2649 private int mPrevWidth = -1;
2650 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002651
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002652 /**
2653 * The degrees rotation around the vertical axis through the pivot point.
2654 */
2655 @ViewDebug.ExportedProperty
2656 float mRotationY = 0f;
2657
2658 /**
2659 * The degrees rotation around the horizontal axis through the pivot point.
2660 */
2661 @ViewDebug.ExportedProperty
2662 float mRotationX = 0f;
2663
2664 /**
2665 * The degrees rotation around the pivot point.
2666 */
2667 @ViewDebug.ExportedProperty
2668 float mRotation = 0f;
2669
2670 /**
2671 * The amount of translation of the object away from its left property (post-layout).
2672 */
2673 @ViewDebug.ExportedProperty
2674 float mTranslationX = 0f;
2675
2676 /**
2677 * The amount of translation of the object away from its top property (post-layout).
2678 */
2679 @ViewDebug.ExportedProperty
2680 float mTranslationY = 0f;
2681
2682 /**
2683 * The amount of scale in the x direction around the pivot point. A
2684 * value of 1 means no scaling is applied.
2685 */
2686 @ViewDebug.ExportedProperty
2687 float mScaleX = 1f;
2688
2689 /**
2690 * The amount of scale in the y direction around the pivot point. A
2691 * value of 1 means no scaling is applied.
2692 */
2693 @ViewDebug.ExportedProperty
2694 float mScaleY = 1f;
2695
2696 /**
Chet Haasea33de552012-02-03 16:28:24 -08002697 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002698 */
2699 @ViewDebug.ExportedProperty
2700 float mPivotX = 0f;
2701
2702 /**
Chet Haasea33de552012-02-03 16:28:24 -08002703 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002704 */
2705 @ViewDebug.ExportedProperty
2706 float mPivotY = 0f;
2707
2708 /**
2709 * The opacity of the View. This is a value from 0 to 1, where 0 means
2710 * completely transparent and 1 means completely opaque.
2711 */
2712 @ViewDebug.ExportedProperty
2713 float mAlpha = 1f;
2714 }
2715
2716 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002717
Joe Malin32736f02011-01-19 16:14:20 -08002718 private boolean mLastIsOpaque;
2719
Chet Haasefd2b0022010-08-06 13:08:56 -07002720 /**
2721 * Convenience value to check for float values that are close enough to zero to be considered
2722 * zero.
2723 */
Romain Guy2542d192010-08-18 11:47:12 -07002724 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002725
2726 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 * The distance in pixels from the left edge of this view's parent
2728 * to the left edge of this view.
2729 * {@hide}
2730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002731 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 protected int mLeft;
2733 /**
2734 * The distance in pixels from the left edge of this view's parent
2735 * to the right edge of this view.
2736 * {@hide}
2737 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002738 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 protected int mRight;
2740 /**
2741 * The distance in pixels from the top edge of this view's parent
2742 * to the top edge of this view.
2743 * {@hide}
2744 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002745 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 protected int mTop;
2747 /**
2748 * The distance in pixels from the top edge of this view's parent
2749 * to the bottom edge of this view.
2750 * {@hide}
2751 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002752 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 protected int mBottom;
2754
2755 /**
2756 * The offset, in pixels, by which the content of this view is scrolled
2757 * horizontally.
2758 * {@hide}
2759 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002760 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 protected int mScrollX;
2762 /**
2763 * The offset, in pixels, by which the content of this view is scrolled
2764 * vertically.
2765 * {@hide}
2766 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002767 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 protected int mScrollY;
2769
2770 /**
2771 * The left padding in pixels, that is the distance in pixels between the
2772 * left edge of this view and the left edge of its content.
2773 * {@hide}
2774 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002775 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776 protected int mPaddingLeft;
2777 /**
2778 * The right padding in pixels, that is the distance in pixels between the
2779 * right edge of this view and the right edge of its content.
2780 * {@hide}
2781 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002782 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 protected int mPaddingRight;
2784 /**
2785 * The top padding in pixels, that is the distance in pixels between the
2786 * top edge of this view and the top edge of its content.
2787 * {@hide}
2788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 protected int mPaddingTop;
2791 /**
2792 * The bottom padding in pixels, that is the distance in pixels between the
2793 * bottom edge of this view and the bottom edge of its content.
2794 * {@hide}
2795 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002796 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 protected int mPaddingBottom;
2798
2799 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002800 * The layout insets in pixels, that is the distance in pixels between the
2801 * visible edges of this view its bounds.
2802 */
2803 private Insets mLayoutInsets;
2804
2805 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002806 * Briefly describes the view and is primarily used for accessibility support.
2807 */
2808 private CharSequence mContentDescription;
2809
2810 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002811 * Specifies the id of a view for which this view serves as a label for
2812 * accessibility purposes.
2813 */
2814 private int mLabelForId = View.NO_ID;
2815
2816 /**
2817 * Predicate for matching labeled view id with its label for
2818 * accessibility purposes.
2819 */
2820 private MatchLabelForPredicate mMatchLabelForPredicate;
2821
2822 /**
2823 * Predicate for matching a view by its id.
2824 */
2825 private MatchIdPredicate mMatchIdPredicate;
2826
2827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002829 *
2830 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002832 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002833 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834
2835 /**
2836 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002837 *
2838 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002840 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002841 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002843 /**
Adam Powell20232d02010-12-08 21:08:53 -08002844 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002845 *
2846 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002847 */
2848 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002849 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002850
2851 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002852 * Cache the paddingStart set by the user to append to the scrollbar's size.
2853 *
2854 */
2855 @ViewDebug.ExportedProperty(category = "padding")
2856 int mUserPaddingStart;
2857
2858 /**
2859 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2860 *
2861 */
2862 @ViewDebug.ExportedProperty(category = "padding")
2863 int mUserPaddingEnd;
2864
2865 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002866 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002867 *
2868 * @hide
2869 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002870 int mUserPaddingLeftInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002871
2872 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002873 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002874 *
2875 * @hide
2876 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002877 int mUserPaddingRightInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002878
2879 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002880 * Default undefined padding
2881 */
2882 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2883
2884 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002885 * @hide
2886 */
2887 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2888 /**
2889 * @hide
2890 */
2891 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892
Philip Milne6c8ea062012-04-03 17:38:43 -07002893 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894
2895 private int mBackgroundResource;
2896 private boolean mBackgroundSizeChanged;
2897
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002898 static class ListenerInfo {
2899 /**
2900 * Listener used to dispatch focus change events.
2901 * This field should be made private, so it is hidden from the SDK.
2902 * {@hide}
2903 */
2904 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002906 /**
2907 * Listeners for layout change events.
2908 */
2909 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002910
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002911 /**
2912 * Listeners for attach events.
2913 */
2914 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002915
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002916 /**
2917 * Listener used to dispatch click events.
2918 * This field should be made private, so it is hidden from the SDK.
2919 * {@hide}
2920 */
2921 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002923 /**
2924 * Listener used to dispatch long click events.
2925 * This field should be made private, so it is hidden from the SDK.
2926 * {@hide}
2927 */
2928 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002930 /**
2931 * Listener used to build the context menu.
2932 * This field should be made private, so it is hidden from the SDK.
2933 * {@hide}
2934 */
2935 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002937 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002939 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002941 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002942
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002943 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002944
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002945 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002946
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002947 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2948 }
2949
2950 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 /**
2953 * The application environment this view lives in.
2954 * This field should be made private, so it is hidden from the SDK.
2955 * {@hide}
2956 */
2957 protected Context mContext;
2958
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002959 private final Resources mResources;
2960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 private ScrollabilityCache mScrollCache;
2962
2963 private int[] mDrawableState = null;
2964
Romain Guy0211a0a2011-02-14 16:34:59 -08002965 /**
2966 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002967 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002968 * @hide
2969 */
2970 public boolean mCachingFailed;
2971
Romain Guy02890fd2010-08-06 17:58:44 -07002972 private Bitmap mDrawingCache;
2973 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002974 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002975 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976
2977 /**
2978 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2979 * the user may specify which view to go to next.
2980 */
2981 private int mNextFocusLeftId = View.NO_ID;
2982
2983 /**
2984 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2985 * the user may specify which view to go to next.
2986 */
2987 private int mNextFocusRightId = View.NO_ID;
2988
2989 /**
2990 * When this view has focus and the next focus is {@link #FOCUS_UP},
2991 * the user may specify which view to go to next.
2992 */
2993 private int mNextFocusUpId = View.NO_ID;
2994
2995 /**
2996 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2997 * the user may specify which view to go to next.
2998 */
2999 private int mNextFocusDownId = View.NO_ID;
3000
Jeff Brown4e6319b2010-12-13 10:36:51 -08003001 /**
3002 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3003 * the user may specify which view to go to next.
3004 */
3005 int mNextFocusForwardId = View.NO_ID;
3006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003008 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003009 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003010 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 private UnsetPressedState mUnsetPressedState;
3013
3014 /**
3015 * Whether the long press's action has been invoked. The tap's action is invoked on the
3016 * up event while a long press is invoked as soon as the long press duration is reached, so
3017 * a long press could be performed before the tap is checked, in which case the tap's action
3018 * should not be invoked.
3019 */
3020 private boolean mHasPerformedLongPress;
3021
3022 /**
3023 * The minimum height of the view. We'll try our best to have the height
3024 * of this view to at least this amount.
3025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003026 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 private int mMinHeight;
3028
3029 /**
3030 * The minimum width of the view. We'll try our best to have the width
3031 * of this view to at least this amount.
3032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003033 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 private int mMinWidth;
3035
3036 /**
3037 * The delegate to handle touch events that are physically in this view
3038 * but should be handled by another view.
3039 */
3040 private TouchDelegate mTouchDelegate = null;
3041
3042 /**
3043 * Solid color to use as a background when creating the drawing cache. Enables
3044 * the cache to use 16 bit bitmaps instead of 32 bit.
3045 */
3046 private int mDrawingCacheBackgroundColor = 0;
3047
3048 /**
3049 * Special tree observer used when mAttachInfo is null.
3050 */
3051 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003052
Adam Powelle14579b2009-12-16 18:39:52 -08003053 /**
3054 * Cache the touch slop from the context that created the view.
3055 */
3056 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003059 * Object that handles automatic animation of view properties.
3060 */
3061 private ViewPropertyAnimator mAnimator = null;
3062
3063 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003064 * Flag indicating that a drag can cross window boundaries. When
3065 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3066 * with this flag set, all visible applications will be able to participate
3067 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003068 *
3069 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003070 */
3071 public static final int DRAG_FLAG_GLOBAL = 1;
3072
3073 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003074 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3075 */
3076 private float mVerticalScrollFactor;
3077
3078 /**
Adam Powell20232d02010-12-08 21:08:53 -08003079 * Position of the vertical scroll bar.
3080 */
3081 private int mVerticalScrollbarPosition;
3082
3083 /**
3084 * Position the scroll bar at the default position as determined by the system.
3085 */
3086 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3087
3088 /**
3089 * Position the scroll bar along the left edge.
3090 */
3091 public static final int SCROLLBAR_POSITION_LEFT = 1;
3092
3093 /**
3094 * Position the scroll bar along the right edge.
3095 */
3096 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3097
3098 /**
Romain Guy171c5922011-01-06 10:04:23 -08003099 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003100 *
3101 * @see #getLayerType()
3102 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003103 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003104 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003105 */
3106 public static final int LAYER_TYPE_NONE = 0;
3107
3108 /**
3109 * <p>Indicates that the view has a software layer. A software layer is backed
3110 * by a bitmap and causes the view to be rendered using Android's software
3111 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003112 *
Romain Guy171c5922011-01-06 10:04:23 -08003113 * <p>Software layers have various usages:</p>
3114 * <p>When the application is not using hardware acceleration, a software layer
3115 * is useful to apply a specific color filter and/or blending mode and/or
3116 * translucency to a view and all its children.</p>
3117 * <p>When the application is using hardware acceleration, a software layer
3118 * is useful to render drawing primitives not supported by the hardware
3119 * accelerated pipeline. It can also be used to cache a complex view tree
3120 * into a texture and reduce the complexity of drawing operations. For instance,
3121 * when animating a complex view tree with a translation, a software layer can
3122 * be used to render the view tree only once.</p>
3123 * <p>Software layers should be avoided when the affected view tree updates
3124 * often. Every update will require to re-render the software layer, which can
3125 * potentially be slow (particularly when hardware acceleration is turned on
3126 * since the layer will have to be uploaded into a hardware texture after every
3127 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003128 *
3129 * @see #getLayerType()
3130 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003131 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003132 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003133 */
3134 public static final int LAYER_TYPE_SOFTWARE = 1;
3135
3136 /**
3137 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3138 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3139 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3140 * rendering pipeline, but only if hardware acceleration is turned on for the
3141 * view hierarchy. When hardware acceleration is turned off, hardware layers
3142 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003143 *
Romain Guy171c5922011-01-06 10:04:23 -08003144 * <p>A hardware layer is useful to apply a specific color filter and/or
3145 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003146 * <p>A hardware layer can be used to cache a complex view tree into a
3147 * texture and reduce the complexity of drawing operations. For instance,
3148 * when animating a complex view tree with a translation, a hardware layer can
3149 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003150 * <p>A hardware layer can also be used to increase the rendering quality when
3151 * rotation transformations are applied on a view. It can also be used to
3152 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003153 *
3154 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003155 * @see #setLayerType(int, android.graphics.Paint)
3156 * @see #LAYER_TYPE_NONE
3157 * @see #LAYER_TYPE_SOFTWARE
3158 */
3159 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003160
Romain Guy3aaff3a2011-01-12 14:18:47 -08003161 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3162 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3163 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3164 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3165 })
Romain Guy171c5922011-01-06 10:04:23 -08003166 int mLayerType = LAYER_TYPE_NONE;
3167 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003168 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003169
3170 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003171 * Set to true when the view is sending hover accessibility events because it
3172 * is the innermost hovered view.
3173 */
3174 private boolean mSendingHoverAccessibilityEvents;
3175
Dianne Hackborn4702a852012-08-17 15:18:29 -07003176 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003177 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003178 */
3179 AccessibilityDelegate mAccessibilityDelegate;
3180
3181 /**
3182 * Consistency verifier for debugging purposes.
3183 * @hide
3184 */
3185 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3186 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3187 new InputEventConsistencyVerifier(this, 0) : null;
3188
Adam Powella9108a22012-07-18 11:18:09 -07003189 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3190
Jeff Brown87b7f802011-06-21 18:35:45 -07003191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 * Simple constructor to use when creating a view from code.
3193 *
3194 * @param context The Context the view is running in, through which it can
3195 * access the current theme, resources, etc.
3196 */
3197 public View(Context context) {
3198 mContext = context;
3199 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003200 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003201 // Set layout and text direction defaults
Dianne Hackborn4702a852012-08-17 15:18:29 -07003202 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
3203 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
3204 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
3205 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003206 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003207 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003208 mUserPaddingStart = UNDEFINED_PADDING;
3209 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 }
3211
3212 /**
3213 * Constructor that is called when inflating a view from XML. This is called
3214 * when a view is being constructed from an XML file, supplying attributes
3215 * that were specified in the XML file. This version uses a default style of
3216 * 0, so the only attribute values applied are those in the Context's Theme
3217 * and the given AttributeSet.
3218 *
3219 * <p>
3220 * The method onFinishInflate() will be called after all children have been
3221 * added.
3222 *
3223 * @param context The Context the view is running in, through which it can
3224 * access the current theme, resources, etc.
3225 * @param attrs The attributes of the XML tag that is inflating the view.
3226 * @see #View(Context, AttributeSet, int)
3227 */
3228 public View(Context context, AttributeSet attrs) {
3229 this(context, attrs, 0);
3230 }
3231
3232 /**
3233 * Perform inflation from XML and apply a class-specific base style. This
3234 * constructor of View allows subclasses to use their own base style when
3235 * they are inflating. For example, a Button class's constructor would call
3236 * this version of the super class constructor and supply
3237 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3238 * the theme's button style to modify all of the base view attributes (in
3239 * particular its background) as well as the Button class's attributes.
3240 *
3241 * @param context The Context the view is running in, through which it can
3242 * access the current theme, resources, etc.
3243 * @param attrs The attributes of the XML tag that is inflating the view.
3244 * @param defStyle The default style to apply to this view. If 0, no style
3245 * will be applied (beyond what is included in the theme). This may
3246 * either be an attribute resource, whose value will be retrieved
3247 * from the current theme, or an explicit style resource.
3248 * @see #View(Context, AttributeSet)
3249 */
3250 public View(Context context, AttributeSet attrs, int defStyle) {
3251 this(context);
3252
3253 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3254 defStyle, 0);
3255
3256 Drawable background = null;
3257
3258 int leftPadding = -1;
3259 int topPadding = -1;
3260 int rightPadding = -1;
3261 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003262 int startPadding = UNDEFINED_PADDING;
3263 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264
3265 int padding = -1;
3266
3267 int viewFlagValues = 0;
3268 int viewFlagMasks = 0;
3269
3270 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 int x = 0;
3273 int y = 0;
3274
Chet Haase73066682010-11-29 15:55:32 -08003275 float tx = 0;
3276 float ty = 0;
3277 float rotation = 0;
3278 float rotationX = 0;
3279 float rotationY = 0;
3280 float sx = 1f;
3281 float sy = 1f;
3282 boolean transformSet = false;
3283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003285 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003286 boolean initializeScrollbars = false;
3287
3288 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 final int N = a.getIndexCount();
3291 for (int i = 0; i < N; i++) {
3292 int attr = a.getIndex(i);
3293 switch (attr) {
3294 case com.android.internal.R.styleable.View_background:
3295 background = a.getDrawable(attr);
3296 break;
3297 case com.android.internal.R.styleable.View_padding:
3298 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003299 mUserPaddingLeftInitial = padding;
3300 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 break;
3302 case com.android.internal.R.styleable.View_paddingLeft:
3303 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003304 mUserPaddingLeftInitial = leftPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 break;
3306 case com.android.internal.R.styleable.View_paddingTop:
3307 topPadding = a.getDimensionPixelSize(attr, -1);
3308 break;
3309 case com.android.internal.R.styleable.View_paddingRight:
3310 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003311 mUserPaddingRightInitial = rightPadding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 break;
3313 case com.android.internal.R.styleable.View_paddingBottom:
3314 bottomPadding = a.getDimensionPixelSize(attr, -1);
3315 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003316 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003317 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003318 break;
3319 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003320 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003321 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 case com.android.internal.R.styleable.View_scrollX:
3323 x = a.getDimensionPixelOffset(attr, 0);
3324 break;
3325 case com.android.internal.R.styleable.View_scrollY:
3326 y = a.getDimensionPixelOffset(attr, 0);
3327 break;
Chet Haase73066682010-11-29 15:55:32 -08003328 case com.android.internal.R.styleable.View_alpha:
3329 setAlpha(a.getFloat(attr, 1f));
3330 break;
3331 case com.android.internal.R.styleable.View_transformPivotX:
3332 setPivotX(a.getDimensionPixelOffset(attr, 0));
3333 break;
3334 case com.android.internal.R.styleable.View_transformPivotY:
3335 setPivotY(a.getDimensionPixelOffset(attr, 0));
3336 break;
3337 case com.android.internal.R.styleable.View_translationX:
3338 tx = a.getDimensionPixelOffset(attr, 0);
3339 transformSet = true;
3340 break;
3341 case com.android.internal.R.styleable.View_translationY:
3342 ty = a.getDimensionPixelOffset(attr, 0);
3343 transformSet = true;
3344 break;
3345 case com.android.internal.R.styleable.View_rotation:
3346 rotation = a.getFloat(attr, 0);
3347 transformSet = true;
3348 break;
3349 case com.android.internal.R.styleable.View_rotationX:
3350 rotationX = a.getFloat(attr, 0);
3351 transformSet = true;
3352 break;
3353 case com.android.internal.R.styleable.View_rotationY:
3354 rotationY = a.getFloat(attr, 0);
3355 transformSet = true;
3356 break;
3357 case com.android.internal.R.styleable.View_scaleX:
3358 sx = a.getFloat(attr, 1f);
3359 transformSet = true;
3360 break;
3361 case com.android.internal.R.styleable.View_scaleY:
3362 sy = a.getFloat(attr, 1f);
3363 transformSet = true;
3364 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 case com.android.internal.R.styleable.View_id:
3366 mID = a.getResourceId(attr, NO_ID);
3367 break;
3368 case com.android.internal.R.styleable.View_tag:
3369 mTag = a.getText(attr);
3370 break;
3371 case com.android.internal.R.styleable.View_fitsSystemWindows:
3372 if (a.getBoolean(attr, false)) {
3373 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3374 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3375 }
3376 break;
3377 case com.android.internal.R.styleable.View_focusable:
3378 if (a.getBoolean(attr, false)) {
3379 viewFlagValues |= FOCUSABLE;
3380 viewFlagMasks |= FOCUSABLE_MASK;
3381 }
3382 break;
3383 case com.android.internal.R.styleable.View_focusableInTouchMode:
3384 if (a.getBoolean(attr, false)) {
3385 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3386 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3387 }
3388 break;
3389 case com.android.internal.R.styleable.View_clickable:
3390 if (a.getBoolean(attr, false)) {
3391 viewFlagValues |= CLICKABLE;
3392 viewFlagMasks |= CLICKABLE;
3393 }
3394 break;
3395 case com.android.internal.R.styleable.View_longClickable:
3396 if (a.getBoolean(attr, false)) {
3397 viewFlagValues |= LONG_CLICKABLE;
3398 viewFlagMasks |= LONG_CLICKABLE;
3399 }
3400 break;
3401 case com.android.internal.R.styleable.View_saveEnabled:
3402 if (!a.getBoolean(attr, true)) {
3403 viewFlagValues |= SAVE_DISABLED;
3404 viewFlagMasks |= SAVE_DISABLED_MASK;
3405 }
3406 break;
3407 case com.android.internal.R.styleable.View_duplicateParentState:
3408 if (a.getBoolean(attr, false)) {
3409 viewFlagValues |= DUPLICATE_PARENT_STATE;
3410 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3411 }
3412 break;
3413 case com.android.internal.R.styleable.View_visibility:
3414 final int visibility = a.getInt(attr, 0);
3415 if (visibility != 0) {
3416 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3417 viewFlagMasks |= VISIBILITY_MASK;
3418 }
3419 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003420 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003421 // Clear any layout direction flags (included resolved bits) already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003422 mPrivateFlags2 &= ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003423 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003424 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003425 final int value = (layoutDirection != -1) ?
3426 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003427 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003428 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 case com.android.internal.R.styleable.View_drawingCacheQuality:
3430 final int cacheQuality = a.getInt(attr, 0);
3431 if (cacheQuality != 0) {
3432 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3433 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3434 }
3435 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003436 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003437 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003438 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003439 case com.android.internal.R.styleable.View_labelFor:
3440 setLabelFor(a.getResourceId(attr, NO_ID));
3441 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3443 if (!a.getBoolean(attr, true)) {
3444 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3445 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3446 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003447 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3449 if (!a.getBoolean(attr, true)) {
3450 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3451 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3452 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003453 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 case R.styleable.View_scrollbars:
3455 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3456 if (scrollbars != SCROLLBARS_NONE) {
3457 viewFlagValues |= scrollbars;
3458 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003459 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 }
3461 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003462 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003464 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003465 // Ignore the attribute starting with ICS
3466 break;
3467 }
3468 // With builds < ICS, fall through and apply fading edges
3469 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3471 if (fadingEdge != FADING_EDGE_NONE) {
3472 viewFlagValues |= fadingEdge;
3473 viewFlagMasks |= FADING_EDGE_MASK;
3474 initializeFadingEdge(a);
3475 }
3476 break;
3477 case R.styleable.View_scrollbarStyle:
3478 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3479 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3480 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3481 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3482 }
3483 break;
3484 case R.styleable.View_isScrollContainer:
3485 setScrollContainer = true;
3486 if (a.getBoolean(attr, false)) {
3487 setScrollContainer(true);
3488 }
3489 break;
3490 case com.android.internal.R.styleable.View_keepScreenOn:
3491 if (a.getBoolean(attr, false)) {
3492 viewFlagValues |= KEEP_SCREEN_ON;
3493 viewFlagMasks |= KEEP_SCREEN_ON;
3494 }
3495 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003496 case R.styleable.View_filterTouchesWhenObscured:
3497 if (a.getBoolean(attr, false)) {
3498 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3499 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3500 }
3501 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 case R.styleable.View_nextFocusLeft:
3503 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3504 break;
3505 case R.styleable.View_nextFocusRight:
3506 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3507 break;
3508 case R.styleable.View_nextFocusUp:
3509 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3510 break;
3511 case R.styleable.View_nextFocusDown:
3512 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3513 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003514 case R.styleable.View_nextFocusForward:
3515 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3516 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 case R.styleable.View_minWidth:
3518 mMinWidth = a.getDimensionPixelSize(attr, 0);
3519 break;
3520 case R.styleable.View_minHeight:
3521 mMinHeight = a.getDimensionPixelSize(attr, 0);
3522 break;
Romain Guy9a817362009-05-01 10:57:14 -07003523 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003524 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003525 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003526 + "be used within a restricted context");
3527 }
3528
Romain Guy9a817362009-05-01 10:57:14 -07003529 final String handlerName = a.getString(attr);
3530 if (handlerName != null) {
3531 setOnClickListener(new OnClickListener() {
3532 private Method mHandler;
3533
3534 public void onClick(View v) {
3535 if (mHandler == null) {
3536 try {
3537 mHandler = getContext().getClass().getMethod(handlerName,
3538 View.class);
3539 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003540 int id = getId();
3541 String idText = id == NO_ID ? "" : " with id '"
3542 + getContext().getResources().getResourceEntryName(
3543 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003544 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003545 handlerName + "(View) in the activity "
3546 + getContext().getClass() + " for onClick handler"
3547 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003548 }
3549 }
3550
3551 try {
3552 mHandler.invoke(getContext(), View.this);
3553 } catch (IllegalAccessException e) {
3554 throw new IllegalStateException("Could not execute non "
3555 + "public method of the activity", e);
3556 } catch (InvocationTargetException e) {
3557 throw new IllegalStateException("Could not execute "
3558 + "method of the activity", e);
3559 }
3560 }
3561 });
3562 }
3563 break;
Adam Powell637d3372010-08-25 14:37:03 -07003564 case R.styleable.View_overScrollMode:
3565 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3566 break;
Adam Powell20232d02010-12-08 21:08:53 -08003567 case R.styleable.View_verticalScrollbarPosition:
3568 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3569 break;
Romain Guy171c5922011-01-06 10:04:23 -08003570 case R.styleable.View_layerType:
3571 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3572 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003573 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003574 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003575 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003576 // Set the text direction flags depending on the value of the attribute
3577 final int textDirection = a.getInt(attr, -1);
3578 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003579 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003580 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003581 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003582 case R.styleable.View_textAlignment:
3583 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003584 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003585 // Set the text alignment flag depending on the value of the attribute
3586 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003587 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003588 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003589 case R.styleable.View_importantForAccessibility:
3590 setImportantForAccessibility(a.getInt(attr,
3591 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003592 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 }
3594 }
3595
Adam Powell637d3372010-08-25 14:37:03 -07003596 setOverScrollMode(overScrollMode);
3597
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003598 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3599 // the resolved layout direction). Those cached values will be used later during padding
3600 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003601 mUserPaddingStart = startPadding;
3602 mUserPaddingEnd = endPadding;
3603
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003604 if (background != null) {
3605 setBackground(background);
3606 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003608 if (padding >= 0) {
3609 leftPadding = padding;
3610 topPadding = padding;
3611 rightPadding = padding;
3612 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003613 mUserPaddingLeftInitial = padding;
3614 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 }
3616
3617 // If the user specified the padding (either with android:padding or
3618 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3619 // use the default padding or the padding from the background drawable
3620 // (stored at this point in mPadding*)
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003621 mUserPaddingLeftInitial = leftPadding >= 0 ? leftPadding : mPaddingLeft;
3622 mUserPaddingRightInitial = rightPadding >= 0 ? rightPadding : mPaddingRight;
3623 internalSetPadding(mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003625 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3627
3628 if (viewFlagMasks != 0) {
3629 setFlags(viewFlagValues, viewFlagMasks);
3630 }
3631
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003632 if (initializeScrollbars) {
3633 initializeScrollbars(a);
3634 }
3635
3636 a.recycle();
3637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 // Needs to be called after mViewFlags is set
3639 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3640 recomputePadding();
3641 }
3642
3643 if (x != 0 || y != 0) {
3644 scrollTo(x, y);
3645 }
3646
Chet Haase73066682010-11-29 15:55:32 -08003647 if (transformSet) {
3648 setTranslationX(tx);
3649 setTranslationY(ty);
3650 setRotation(rotation);
3651 setRotationX(rotationX);
3652 setRotationY(rotationY);
3653 setScaleX(sx);
3654 setScaleY(sy);
3655 }
3656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3658 setScrollContainer(true);
3659 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003660
3661 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 }
3663
3664 /**
3665 * Non-public constructor for use in testing
3666 */
3667 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003668 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670
Dianne Hackborn4702a852012-08-17 15:18:29 -07003671 public String toString() {
3672 StringBuilder out = new StringBuilder(128);
3673 out.append(getClass().getName());
3674 out.append('{');
3675 out.append(Integer.toHexString(System.identityHashCode(this)));
3676 out.append(' ');
3677 switch (mViewFlags&VISIBILITY_MASK) {
3678 case VISIBLE: out.append('V'); break;
3679 case INVISIBLE: out.append('I'); break;
3680 case GONE: out.append('G'); break;
3681 default: out.append('.'); break;
3682 }
3683 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3684 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3685 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3686 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3687 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3688 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3689 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3690 out.append(' ');
3691 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3692 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3693 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3694 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3695 out.append('p');
3696 } else {
3697 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3698 }
3699 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3700 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3701 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3702 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3703 out.append(' ');
3704 out.append(mLeft);
3705 out.append(',');
3706 out.append(mTop);
3707 out.append('-');
3708 out.append(mRight);
3709 out.append(',');
3710 out.append(mBottom);
3711 final int id = getId();
3712 if (id != NO_ID) {
3713 out.append(" #");
3714 out.append(Integer.toHexString(id));
3715 final Resources r = mResources;
3716 if (id != 0 && r != null) {
3717 try {
3718 String pkgname;
3719 switch (id&0xff000000) {
3720 case 0x7f000000:
3721 pkgname="app";
3722 break;
3723 case 0x01000000:
3724 pkgname="android";
3725 break;
3726 default:
3727 pkgname = r.getResourcePackageName(id);
3728 break;
3729 }
3730 String typename = r.getResourceTypeName(id);
3731 String entryname = r.getResourceEntryName(id);
3732 out.append(" ");
3733 out.append(pkgname);
3734 out.append(":");
3735 out.append(typename);
3736 out.append("/");
3737 out.append(entryname);
3738 } catch (Resources.NotFoundException e) {
3739 }
3740 }
3741 }
3742 out.append("}");
3743 return out.toString();
3744 }
3745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 /**
3747 * <p>
3748 * Initializes the fading edges from a given set of styled attributes. This
3749 * method should be called by subclasses that need fading edges and when an
3750 * instance of these subclasses is created programmatically rather than
3751 * being inflated from XML. This method is automatically called when the XML
3752 * is inflated.
3753 * </p>
3754 *
3755 * @param a the styled attributes set to initialize the fading edges from
3756 */
3757 protected void initializeFadingEdge(TypedArray a) {
3758 initScrollCache();
3759
3760 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3761 R.styleable.View_fadingEdgeLength,
3762 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3763 }
3764
3765 /**
3766 * Returns the size of the vertical faded edges used to indicate that more
3767 * content in this view is visible.
3768 *
3769 * @return The size in pixels of the vertical faded edge or 0 if vertical
3770 * faded edges are not enabled for this view.
3771 * @attr ref android.R.styleable#View_fadingEdgeLength
3772 */
3773 public int getVerticalFadingEdgeLength() {
3774 if (isVerticalFadingEdgeEnabled()) {
3775 ScrollabilityCache cache = mScrollCache;
3776 if (cache != null) {
3777 return cache.fadingEdgeLength;
3778 }
3779 }
3780 return 0;
3781 }
3782
3783 /**
3784 * Set the size of the faded edge used to indicate that more content in this
3785 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003786 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3787 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3788 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 *
3790 * @param length The size in pixels of the faded edge used to indicate that more
3791 * content in this view is visible.
3792 */
3793 public void setFadingEdgeLength(int length) {
3794 initScrollCache();
3795 mScrollCache.fadingEdgeLength = length;
3796 }
3797
3798 /**
3799 * Returns the size of the horizontal faded edges used to indicate that more
3800 * content in this view is visible.
3801 *
3802 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3803 * faded edges are not enabled for this view.
3804 * @attr ref android.R.styleable#View_fadingEdgeLength
3805 */
3806 public int getHorizontalFadingEdgeLength() {
3807 if (isHorizontalFadingEdgeEnabled()) {
3808 ScrollabilityCache cache = mScrollCache;
3809 if (cache != null) {
3810 return cache.fadingEdgeLength;
3811 }
3812 }
3813 return 0;
3814 }
3815
3816 /**
3817 * Returns the width of the vertical scrollbar.
3818 *
3819 * @return The width in pixels of the vertical scrollbar or 0 if there
3820 * is no vertical scrollbar.
3821 */
3822 public int getVerticalScrollbarWidth() {
3823 ScrollabilityCache cache = mScrollCache;
3824 if (cache != null) {
3825 ScrollBarDrawable scrollBar = cache.scrollBar;
3826 if (scrollBar != null) {
3827 int size = scrollBar.getSize(true);
3828 if (size <= 0) {
3829 size = cache.scrollBarSize;
3830 }
3831 return size;
3832 }
3833 return 0;
3834 }
3835 return 0;
3836 }
3837
3838 /**
3839 * Returns the height of the horizontal scrollbar.
3840 *
3841 * @return The height in pixels of the horizontal scrollbar or 0 if
3842 * there is no horizontal scrollbar.
3843 */
3844 protected int getHorizontalScrollbarHeight() {
3845 ScrollabilityCache cache = mScrollCache;
3846 if (cache != null) {
3847 ScrollBarDrawable scrollBar = cache.scrollBar;
3848 if (scrollBar != null) {
3849 int size = scrollBar.getSize(false);
3850 if (size <= 0) {
3851 size = cache.scrollBarSize;
3852 }
3853 return size;
3854 }
3855 return 0;
3856 }
3857 return 0;
3858 }
3859
3860 /**
3861 * <p>
3862 * Initializes the scrollbars from a given set of styled attributes. This
3863 * method should be called by subclasses that need scrollbars and when an
3864 * instance of these subclasses is created programmatically rather than
3865 * being inflated from XML. This method is automatically called when the XML
3866 * is inflated.
3867 * </p>
3868 *
3869 * @param a the styled attributes set to initialize the scrollbars from
3870 */
3871 protected void initializeScrollbars(TypedArray a) {
3872 initScrollCache();
3873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003875
Mike Cleronf116bf82009-09-27 19:14:12 -07003876 if (scrollabilityCache.scrollBar == null) {
3877 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3878 }
Joe Malin32736f02011-01-19 16:14:20 -08003879
Romain Guy8bda2482010-03-02 11:42:11 -08003880 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881
Mike Cleronf116bf82009-09-27 19:14:12 -07003882 if (!fadeScrollbars) {
3883 scrollabilityCache.state = ScrollabilityCache.ON;
3884 }
3885 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003886
3887
Mike Cleronf116bf82009-09-27 19:14:12 -07003888 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3889 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3890 .getScrollBarFadeDuration());
3891 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3892 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3893 ViewConfiguration.getScrollDefaultDelay());
3894
Joe Malin32736f02011-01-19 16:14:20 -08003895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3897 com.android.internal.R.styleable.View_scrollbarSize,
3898 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3899
3900 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3901 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3902
3903 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3904 if (thumb != null) {
3905 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3906 }
3907
3908 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3909 false);
3910 if (alwaysDraw) {
3911 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3912 }
3913
3914 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3915 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3916
3917 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3918 if (thumb != null) {
3919 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3920 }
3921
3922 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3923 false);
3924 if (alwaysDraw) {
3925 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3926 }
3927
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003928 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003929 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003930 if (track != null) {
3931 track.setLayoutDirection(layoutDirection);
3932 }
3933 if (thumb != null) {
3934 thumb.setLayoutDirection(layoutDirection);
3935 }
3936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003938 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940
3941 /**
3942 * <p>
3943 * Initalizes the scrollability cache if necessary.
3944 * </p>
3945 */
3946 private void initScrollCache() {
3947 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003948 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 }
3950 }
3951
Philip Milne6c8ea062012-04-03 17:38:43 -07003952 private ScrollabilityCache getScrollCache() {
3953 initScrollCache();
3954 return mScrollCache;
3955 }
3956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 /**
Adam Powell20232d02010-12-08 21:08:53 -08003958 * Set the position of the vertical scroll bar. Should be one of
3959 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3960 * {@link #SCROLLBAR_POSITION_RIGHT}.
3961 *
3962 * @param position Where the vertical scroll bar should be positioned.
3963 */
3964 public void setVerticalScrollbarPosition(int position) {
3965 if (mVerticalScrollbarPosition != position) {
3966 mVerticalScrollbarPosition = position;
3967 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003968 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003969 }
3970 }
3971
3972 /**
3973 * @return The position where the vertical scroll bar will show, if applicable.
3974 * @see #setVerticalScrollbarPosition(int)
3975 */
3976 public int getVerticalScrollbarPosition() {
3977 return mVerticalScrollbarPosition;
3978 }
3979
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003980 ListenerInfo getListenerInfo() {
3981 if (mListenerInfo != null) {
3982 return mListenerInfo;
3983 }
3984 mListenerInfo = new ListenerInfo();
3985 return mListenerInfo;
3986 }
3987
Adam Powell20232d02010-12-08 21:08:53 -08003988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 * Register a callback to be invoked when focus of this view changed.
3990 *
3991 * @param l The callback that will run.
3992 */
3993 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003994 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996
3997 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003998 * Add a listener that will be called when the bounds of the view change due to
3999 * layout processing.
4000 *
4001 * @param listener The listener that will be called when layout bounds change.
4002 */
4003 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004004 ListenerInfo li = getListenerInfo();
4005 if (li.mOnLayoutChangeListeners == null) {
4006 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004007 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004008 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4009 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004010 }
Chet Haase21cd1382010-09-01 17:42:29 -07004011 }
4012
4013 /**
4014 * Remove a listener for layout changes.
4015 *
4016 * @param listener The listener for layout bounds change.
4017 */
4018 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004019 ListenerInfo li = mListenerInfo;
4020 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004021 return;
4022 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004023 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004024 }
4025
4026 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004027 * Add a listener for attach state changes.
4028 *
4029 * This listener will be called whenever this view is attached or detached
4030 * from a window. Remove the listener using
4031 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4032 *
4033 * @param listener Listener to attach
4034 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4035 */
4036 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004037 ListenerInfo li = getListenerInfo();
4038 if (li.mOnAttachStateChangeListeners == null) {
4039 li.mOnAttachStateChangeListeners
4040 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004041 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004042 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004043 }
4044
4045 /**
4046 * Remove a listener for attach state changes. The listener will receive no further
4047 * notification of window attach/detach events.
4048 *
4049 * @param listener Listener to remove
4050 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4051 */
4052 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004053 ListenerInfo li = mListenerInfo;
4054 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004055 return;
4056 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004057 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004058 }
4059
4060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 * Returns the focus-change callback registered for this view.
4062 *
4063 * @return The callback, or null if one is not registered.
4064 */
4065 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004066 ListenerInfo li = mListenerInfo;
4067 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004068 }
4069
4070 /**
4071 * Register a callback to be invoked when this view is clicked. If this view is not
4072 * clickable, it becomes clickable.
4073 *
4074 * @param l The callback that will run
4075 *
4076 * @see #setClickable(boolean)
4077 */
4078 public void setOnClickListener(OnClickListener l) {
4079 if (!isClickable()) {
4080 setClickable(true);
4081 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004082 getListenerInfo().mOnClickListener = l;
4083 }
4084
4085 /**
4086 * Return whether this view has an attached OnClickListener. Returns
4087 * true if there is a listener, false if there is none.
4088 */
4089 public boolean hasOnClickListeners() {
4090 ListenerInfo li = mListenerInfo;
4091 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
4093
4094 /**
4095 * Register a callback to be invoked when this view is clicked and held. If this view is not
4096 * long clickable, it becomes long clickable.
4097 *
4098 * @param l The callback that will run
4099 *
4100 * @see #setLongClickable(boolean)
4101 */
4102 public void setOnLongClickListener(OnLongClickListener l) {
4103 if (!isLongClickable()) {
4104 setLongClickable(true);
4105 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004106 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
4108
4109 /**
4110 * Register a callback to be invoked when the context menu for this view is
4111 * being built. If this view is not long clickable, it becomes long clickable.
4112 *
4113 * @param l The callback that will run
4114 *
4115 */
4116 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4117 if (!isLongClickable()) {
4118 setLongClickable(true);
4119 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004120 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
4122
4123 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004124 * Call this view's OnClickListener, if it is defined. Performs all normal
4125 * actions associated with clicking: reporting accessibility event, playing
4126 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 *
4128 * @return True there was an assigned OnClickListener that was called, false
4129 * otherwise is returned.
4130 */
4131 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004132 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4133
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004134 ListenerInfo li = mListenerInfo;
4135 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004137 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 return true;
4139 }
4140
4141 return false;
4142 }
4143
4144 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004145 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4146 * this only calls the listener, and does not do any associated clicking
4147 * actions like reporting an accessibility event.
4148 *
4149 * @return True there was an assigned OnClickListener that was called, false
4150 * otherwise is returned.
4151 */
4152 public boolean callOnClick() {
4153 ListenerInfo li = mListenerInfo;
4154 if (li != null && li.mOnClickListener != null) {
4155 li.mOnClickListener.onClick(this);
4156 return true;
4157 }
4158 return false;
4159 }
4160
4161 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004162 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4163 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004164 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004165 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 */
4167 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004168 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004171 ListenerInfo li = mListenerInfo;
4172 if (li != null && li.mOnLongClickListener != null) {
4173 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004174 }
4175 if (!handled) {
4176 handled = showContextMenu();
4177 }
4178 if (handled) {
4179 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4180 }
4181 return handled;
4182 }
4183
4184 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004185 * Performs button-related actions during a touch down event.
4186 *
4187 * @param event The event.
4188 * @return True if the down was consumed.
4189 *
4190 * @hide
4191 */
4192 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4193 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4194 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4195 return true;
4196 }
4197 }
4198 return false;
4199 }
4200
4201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * Bring up the context menu for this view.
4203 *
4204 * @return Whether a context menu was displayed.
4205 */
4206 public boolean showContextMenu() {
4207 return getParent().showContextMenuForChild(this);
4208 }
4209
4210 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004211 * Bring up the context menu for this view, referring to the item under the specified point.
4212 *
4213 * @param x The referenced x coordinate.
4214 * @param y The referenced y coordinate.
4215 * @param metaState The keyboard modifiers that were pressed.
4216 * @return Whether a context menu was displayed.
4217 *
4218 * @hide
4219 */
4220 public boolean showContextMenu(float x, float y, int metaState) {
4221 return showContextMenu();
4222 }
4223
4224 /**
Adam Powell6e346362010-07-23 10:18:23 -07004225 * Start an action mode.
4226 *
4227 * @param callback Callback that will control the lifecycle of the action mode
4228 * @return The new action mode if it is started, null otherwise
4229 *
4230 * @see ActionMode
4231 */
4232 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004233 ViewParent parent = getParent();
4234 if (parent == null) return null;
4235 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004236 }
4237
4238 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004239 * Register a callback to be invoked when a hardware key is pressed in this view.
4240 * Key presses in software input methods will generally not trigger the methods of
4241 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 * @param l the key listener to attach to this view
4243 */
4244 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004245 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
4247
4248 /**
4249 * Register a callback to be invoked when a touch event is sent to this view.
4250 * @param l the touch listener to attach to this view
4251 */
4252 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004253 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 }
4255
4256 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004257 * Register a callback to be invoked when a generic motion event is sent to this view.
4258 * @param l the generic motion listener to attach to this view
4259 */
4260 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004261 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004262 }
4263
4264 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004265 * Register a callback to be invoked when a hover event is sent to this view.
4266 * @param l the hover listener to attach to this view
4267 */
4268 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004269 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004270 }
4271
4272 /**
Joe Malin32736f02011-01-19 16:14:20 -08004273 * Register a drag event listener callback object for this View. The parameter is
4274 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4275 * View, the system calls the
4276 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4277 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004278 */
4279 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004280 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004281 }
4282
4283 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004284 * Give this view focus. This will cause
4285 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 *
4287 * Note: this does not check whether this {@link View} should get focus, it just
4288 * gives it focus no matter what. It should only be called internally by framework
4289 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4290 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004291 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4292 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 * focus moved when requestFocus() is called. It may not always
4294 * apply, in which case use the default View.FOCUS_DOWN.
4295 * @param previouslyFocusedRect The rectangle of the view that had focus
4296 * prior in this View's coordinate system.
4297 */
4298 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4299 if (DBG) {
4300 System.out.println(this + " requestFocus()");
4301 }
4302
Dianne Hackborn4702a852012-08-17 15:18:29 -07004303 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4304 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305
4306 if (mParent != null) {
4307 mParent.requestChildFocus(this, this);
4308 }
4309
4310 onFocusChanged(true, direction, previouslyFocusedRect);
4311 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004312
4313 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4314 notifyAccessibilityStateChanged();
4315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
4317 }
4318
4319 /**
4320 * Request that a rectangle of this view be visible on the screen,
4321 * scrolling if necessary just enough.
4322 *
4323 * <p>A View should call this if it maintains some notion of which part
4324 * of its content is interesting. For example, a text editing view
4325 * should call this when its cursor moves.
4326 *
4327 * @param rectangle The rectangle.
4328 * @return Whether any parent scrolled.
4329 */
4330 public boolean requestRectangleOnScreen(Rect rectangle) {
4331 return requestRectangleOnScreen(rectangle, false);
4332 }
4333
4334 /**
4335 * Request that a rectangle of this view be visible on the screen,
4336 * scrolling if necessary just enough.
4337 *
4338 * <p>A View should call this if it maintains some notion of which part
4339 * of its content is interesting. For example, a text editing view
4340 * should call this when its cursor moves.
4341 *
4342 * <p>When <code>immediate</code> is set to true, scrolling will not be
4343 * animated.
4344 *
4345 * @param rectangle The rectangle.
4346 * @param immediate True to forbid animated scrolling, false otherwise
4347 * @return Whether any parent scrolled.
4348 */
4349 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004350 if (mAttachInfo == null) {
4351 return false;
4352 }
4353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004355
4356 RectF position = mAttachInfo.mTmpTransformRect;
4357 position.set(rectangle);
4358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 ViewParent parent = mParent;
4360 boolean scrolled = false;
4361 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004362 rectangle.set((int) position.left, (int) position.top,
4363 (int) position.right, (int) position.bottom);
4364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 scrolled |= parent.requestChildRectangleOnScreen(child,
4366 rectangle, immediate);
4367
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004368 if (!child.hasIdentityMatrix()) {
4369 child.getMatrix().mapRect(position);
4370 }
4371
4372 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004373
4374 if (!(parent instanceof View)) {
4375 break;
4376 }
Romain Guy8506ab42009-06-11 17:35:47 -07004377
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004378 View parentView = (View) parent;
4379
4380 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4381
4382 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 parent = child.getParent();
4384 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 return scrolled;
4387 }
4388
4389 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004390 * Called when this view wants to give up focus. If focus is cleared
4391 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4392 * <p>
4393 * <strong>Note:</strong> When a View clears focus the framework is trying
4394 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004395 * View is the first from the top that can take focus, then all callbacks
4396 * related to clearing focus will be invoked after wich the framework will
4397 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004398 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 */
4400 public void clearFocus() {
4401 if (DBG) {
4402 System.out.println(this + " clearFocus()");
4403 }
4404
Dianne Hackborn4702a852012-08-17 15:18:29 -07004405 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4406 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407
4408 if (mParent != null) {
4409 mParent.clearChildFocus(this);
4410 }
4411
4412 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004415
4416 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004417
4418 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4419 notifyAccessibilityStateChanged();
4420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
4422 }
4423
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004424 void ensureInputFocusOnFirstFocusable() {
4425 View root = getRootView();
4426 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004427 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 }
4429 }
4430
4431 /**
4432 * Called internally by the view system when a new view is getting focus.
4433 * This is what clears the old focus.
4434 */
4435 void unFocus() {
4436 if (DBG) {
4437 System.out.println(this + " unFocus()");
4438 }
4439
Dianne Hackborn4702a852012-08-17 15:18:29 -07004440 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4441 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442
4443 onFocusChanged(false, 0, null);
4444 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004445
4446 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4447 notifyAccessibilityStateChanged();
4448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 }
4450 }
4451
4452 /**
4453 * Returns true if this view has focus iteself, or is the ancestor of the
4454 * view that has focus.
4455 *
4456 * @return True if this view has or contains focus, false otherwise.
4457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004458 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004460 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 }
4462
4463 /**
4464 * Returns true if this view is focusable or if it contains a reachable View
4465 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4466 * is a View whose parents do not block descendants focus.
4467 *
4468 * Only {@link #VISIBLE} views are considered focusable.
4469 *
4470 * @return True if the view is focusable or if the view contains a focusable
4471 * View, false otherwise.
4472 *
4473 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4474 */
4475 public boolean hasFocusable() {
4476 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4477 }
4478
4479 /**
4480 * Called by the view system when the focus state of this view changes.
4481 * When the focus change event is caused by directional navigation, direction
4482 * and previouslyFocusedRect provide insight into where the focus is coming from.
4483 * When overriding, be sure to call up through to the super class so that
4484 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004485 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 * @param gainFocus True if the View has focus; false otherwise.
4487 * @param direction The direction focus has moved when requestFocus()
4488 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004489 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4490 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4491 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4493 * system, of the previously focused view. If applicable, this will be
4494 * passed in as finer grained information about where the focus is coming
4495 * from (in addition to direction). Will be <code>null</code> otherwise.
4496 */
4497 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004498 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004499 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4500 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004501 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004502 }
4503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 InputMethodManager imm = InputMethodManager.peekInstance();
4505 if (!gainFocus) {
4506 if (isPressed()) {
4507 setPressed(false);
4508 }
4509 if (imm != null && mAttachInfo != null
4510 && mAttachInfo.mHasWindowFocus) {
4511 imm.focusOut(this);
4512 }
Romain Guya2431d02009-04-30 16:30:00 -07004513 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 } else if (imm != null && mAttachInfo != null
4515 && mAttachInfo.mHasWindowFocus) {
4516 imm.focusIn(this);
4517 }
Romain Guy8506ab42009-06-11 17:35:47 -07004518
Romain Guy0fd89bf2011-01-26 15:41:30 -08004519 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004520 ListenerInfo li = mListenerInfo;
4521 if (li != null && li.mOnFocusChangeListener != null) {
4522 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
Joe Malin32736f02011-01-19 16:14:20 -08004524
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004525 if (mAttachInfo != null) {
4526 mAttachInfo.mKeyDispatchState.reset(this);
4527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 }
4529
4530 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004531 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004532 * not enabled this method has no effect. The default implementation calls
4533 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4534 * to populate information about the event source (this View), then calls
4535 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4536 * populate the text content of the event source including its descendants,
4537 * and last calls
4538 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4539 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004540 * <p>
4541 * If an {@link AccessibilityDelegate} has been specified via calling
4542 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4543 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4544 * responsible for handling this call.
4545 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004546 *
Scott Mainb303d832011-10-12 16:45:18 -07004547 * @param eventType The type of the event to send, as defined by several types from
4548 * {@link android.view.accessibility.AccessibilityEvent}, such as
4549 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4550 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004551 *
4552 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4553 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4554 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004555 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004556 */
4557 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004558 if (mAccessibilityDelegate != null) {
4559 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4560 } else {
4561 sendAccessibilityEventInternal(eventType);
4562 }
4563 }
4564
4565 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004566 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4567 * {@link AccessibilityEvent} to make an announcement which is related to some
4568 * sort of a context change for which none of the events representing UI transitions
4569 * is a good fit. For example, announcing a new page in a book. If accessibility
4570 * is not enabled this method does nothing.
4571 *
4572 * @param text The announcement text.
4573 */
4574 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004575 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004576 AccessibilityEvent event = AccessibilityEvent.obtain(
4577 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004578 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004579 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004580 event.setContentDescription(null);
4581 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004582 }
4583 }
4584
4585 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004586 * @see #sendAccessibilityEvent(int)
4587 *
4588 * Note: Called from the default {@link AccessibilityDelegate}.
4589 */
4590 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004591 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4592 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4593 }
4594 }
4595
4596 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004597 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4598 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004599 * perform a check whether accessibility is enabled.
4600 * <p>
4601 * If an {@link AccessibilityDelegate} has been specified via calling
4602 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4603 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4604 * is responsible for handling this call.
4605 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004606 *
4607 * @param event The event to send.
4608 *
4609 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004610 */
4611 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004612 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004613 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004614 } else {
4615 sendAccessibilityEventUncheckedInternal(event);
4616 }
4617 }
4618
4619 /**
4620 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4621 *
4622 * Note: Called from the default {@link AccessibilityDelegate}.
4623 */
4624 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004625 if (!isShown()) {
4626 return;
4627 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004628 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004629 // Only a subset of accessibility events populates text content.
4630 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4631 dispatchPopulateAccessibilityEvent(event);
4632 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004633 // In the beginning we called #isShown(), so we know that getParent() is not null.
4634 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004635 }
4636
4637 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004638 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4639 * to its children for adding their text content to the event. Note that the
4640 * event text is populated in a separate dispatch path since we add to the
4641 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004642 * A typical implementation will call
4643 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4644 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4645 * on each child. Override this method if custom population of the event text
4646 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004647 * <p>
4648 * If an {@link AccessibilityDelegate} has been specified via calling
4649 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4650 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4651 * is responsible for handling this call.
4652 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004653 * <p>
4654 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4655 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4656 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004657 *
4658 * @param event The event.
4659 *
4660 * @return True if the event population was completed.
4661 */
4662 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004663 if (mAccessibilityDelegate != null) {
4664 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4665 } else {
4666 return dispatchPopulateAccessibilityEventInternal(event);
4667 }
4668 }
4669
4670 /**
4671 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4672 *
4673 * Note: Called from the default {@link AccessibilityDelegate}.
4674 */
4675 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004676 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004677 return false;
4678 }
4679
4680 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004681 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004682 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004683 * text content. While this method is free to modify event
4684 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004685 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4686 * <p>
4687 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004688 * to the text added by the super implementation:
4689 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004690 * super.onPopulateAccessibilityEvent(event);
4691 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4692 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4693 * mCurrentDate.getTimeInMillis(), flags);
4694 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004695 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004696 * <p>
4697 * If an {@link AccessibilityDelegate} has been specified via calling
4698 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4699 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4700 * is responsible for handling this call.
4701 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004702 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4703 * information to the event, in case the default implementation has basic information to add.
4704 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004705 *
4706 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004707 *
4708 * @see #sendAccessibilityEvent(int)
4709 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004710 */
4711 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004712 if (mAccessibilityDelegate != null) {
4713 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4714 } else {
4715 onPopulateAccessibilityEventInternal(event);
4716 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004717 }
4718
4719 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004720 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4721 *
4722 * Note: Called from the default {@link AccessibilityDelegate}.
4723 */
4724 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4725
4726 }
4727
4728 /**
4729 * Initializes an {@link AccessibilityEvent} with information about
4730 * this View which is the event source. In other words, the source of
4731 * an accessibility event is the view whose state change triggered firing
4732 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004733 * <p>
4734 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004735 * to properties set by the super implementation:
4736 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4737 * super.onInitializeAccessibilityEvent(event);
4738 * event.setPassword(true);
4739 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004740 * <p>
4741 * If an {@link AccessibilityDelegate} has been specified via calling
4742 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4743 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4744 * is responsible for handling this call.
4745 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004746 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4747 * information to the event, in case the default implementation has basic information to add.
4748 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004749 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004750 *
4751 * @see #sendAccessibilityEvent(int)
4752 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4753 */
4754 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004755 if (mAccessibilityDelegate != null) {
4756 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4757 } else {
4758 onInitializeAccessibilityEventInternal(event);
4759 }
4760 }
4761
4762 /**
4763 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4764 *
4765 * Note: Called from the default {@link AccessibilityDelegate}.
4766 */
4767 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004768 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004769 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004770 event.setPackageName(getContext().getPackageName());
4771 event.setEnabled(isEnabled());
4772 event.setContentDescription(mContentDescription);
4773
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004774 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004775 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004776 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4777 FOCUSABLES_ALL);
4778 event.setItemCount(focusablesTempList.size());
4779 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4780 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004781 }
4782 }
4783
4784 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004785 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4786 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4787 * This method is responsible for obtaining an accessibility node info from a
4788 * pool of reusable instances and calling
4789 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4790 * initialize the former.
4791 * <p>
4792 * Note: The client is responsible for recycling the obtained instance by calling
4793 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4794 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004795 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004796 * @return A populated {@link AccessibilityNodeInfo}.
4797 *
4798 * @see AccessibilityNodeInfo
4799 */
4800 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004801 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4802 if (provider != null) {
4803 return provider.createAccessibilityNodeInfo(View.NO_ID);
4804 } else {
4805 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4806 onInitializeAccessibilityNodeInfo(info);
4807 return info;
4808 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004809 }
4810
4811 /**
4812 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4813 * The base implementation sets:
4814 * <ul>
4815 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004816 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4817 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004818 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4819 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4820 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4821 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4822 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4823 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4824 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4825 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4826 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4827 * </ul>
4828 * <p>
4829 * Subclasses should override this method, call the super implementation,
4830 * and set additional attributes.
4831 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004832 * <p>
4833 * If an {@link AccessibilityDelegate} has been specified via calling
4834 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4835 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4836 * is responsible for handling this call.
4837 * </p>
4838 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004839 * @param info The instance to initialize.
4840 */
4841 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004842 if (mAccessibilityDelegate != null) {
4843 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4844 } else {
4845 onInitializeAccessibilityNodeInfoInternal(info);
4846 }
4847 }
4848
4849 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004850 * Gets the location of this view in screen coordintates.
4851 *
4852 * @param outRect The output location
4853 */
4854 private void getBoundsOnScreen(Rect outRect) {
4855 if (mAttachInfo == null) {
4856 return;
4857 }
4858
4859 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004860 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004861
4862 if (!hasIdentityMatrix()) {
4863 getMatrix().mapRect(position);
4864 }
4865
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004866 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004867
4868 ViewParent parent = mParent;
4869 while (parent instanceof View) {
4870 View parentView = (View) parent;
4871
4872 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4873
4874 if (!parentView.hasIdentityMatrix()) {
4875 parentView.getMatrix().mapRect(position);
4876 }
4877
4878 position.offset(parentView.mLeft, parentView.mTop);
4879
4880 parent = parentView.mParent;
4881 }
4882
4883 if (parent instanceof ViewRootImpl) {
4884 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4885 position.offset(0, -viewRootImpl.mCurScrollY);
4886 }
4887
4888 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4889
4890 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4891 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4892 }
4893
4894 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004895 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4896 *
4897 * Note: Called from the default {@link AccessibilityDelegate}.
4898 */
4899 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004900 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004901
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004902 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004903 info.setBoundsInParent(bounds);
4904
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004905 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004906 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004907
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004908 ViewParent parent = getParentForAccessibility();
4909 if (parent instanceof View) {
4910 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004911 }
4912
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004913 if (mID != View.NO_ID) {
4914 View rootView = getRootView();
4915 if (rootView == null) {
4916 rootView = this;
4917 }
4918 View label = rootView.findLabelForView(this, mID);
4919 if (label != null) {
4920 info.setLabeledBy(label);
4921 }
4922 }
4923
4924 if (mLabelForId != View.NO_ID) {
4925 View rootView = getRootView();
4926 if (rootView == null) {
4927 rootView = this;
4928 }
4929 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4930 if (labeled != null) {
4931 info.setLabelFor(labeled);
4932 }
4933 }
4934
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004935 info.setVisibleToUser(isVisibleToUser());
4936
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004937 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004938 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004939 info.setContentDescription(getContentDescription());
4940
4941 info.setEnabled(isEnabled());
4942 info.setClickable(isClickable());
4943 info.setFocusable(isFocusable());
4944 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004945 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004946 info.setSelected(isSelected());
4947 info.setLongClickable(isLongClickable());
4948
4949 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004950 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004951 // we should report as selectable view in an AdapterView.
4952 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4953 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4954
4955 if (isFocusable()) {
4956 if (isFocused()) {
4957 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4958 } else {
4959 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4960 }
4961 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004962
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004963 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07004964 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07004965 } else {
4966 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4967 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004968
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004969 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004970 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4971 }
4972
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07004973 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004974 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4975 }
4976
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004977 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004978 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4979 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4980 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07004981 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
4982 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004983 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004984 }
4985
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004986 private View findLabelForView(View view, int labeledId) {
4987 if (mMatchLabelForPredicate == null) {
4988 mMatchLabelForPredicate = new MatchLabelForPredicate();
4989 }
4990 mMatchLabelForPredicate.mLabeledId = labeledId;
4991 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
4992 }
4993
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004994 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004995 * Computes whether this view is visible to the user. Such a view is
4996 * attached, visible, all its predecessors are visible, it is not clipped
4997 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004998 *
4999 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005000 *
5001 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005002 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005003 protected boolean isVisibleToUser() {
5004 return isVisibleToUser(null);
5005 }
5006
5007 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005008 * Computes whether the given portion of this view is visible to the user.
5009 * Such a view is attached, visible, all its predecessors are visible,
5010 * has an alpha greater than zero, and the specified portion is not
5011 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005012 *
5013 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5014 * <code>null</code>, and the entire view will be tested in this case.
5015 * When <code>true</code> is returned by the function, the actual visible
5016 * region will be stored in this parameter; that is, if boundInView is fully
5017 * contained within the view, no modification will be made, otherwise regions
5018 * outside of the visible area of the view will be clipped.
5019 *
5020 * @return Whether the specified portion of the view is visible on the screen.
5021 *
5022 * @hide
5023 */
5024 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005025 if (mAttachInfo != null) {
5026 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5027 Point offset = mAttachInfo.mPoint;
5028 // The first two checks are made also made by isShown() which
5029 // however traverses the tree up to the parent to catch that.
5030 // Therefore, we do some fail fast check to minimize the up
5031 // tree traversal.
5032 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
5033 && getAlpha() > 0
5034 && isShown()
5035 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005036 if (isVisible && boundInView != null) {
5037 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07005038 // isVisible is always true here, use a simple assignment
5039 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005040 }
5041 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07005042 }
5043
5044 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005045 }
5046
5047 /**
alanvb72fe7a2012-08-27 16:44:25 -07005048 * Returns the delegate for implementing accessibility support via
5049 * composition. For more details see {@link AccessibilityDelegate}.
5050 *
5051 * @return The delegate, or null if none set.
5052 *
5053 * @hide
5054 */
5055 public AccessibilityDelegate getAccessibilityDelegate() {
5056 return mAccessibilityDelegate;
5057 }
5058
5059 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005060 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005061 * opposed to inheritance. The delegate's primary use is for implementing
5062 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5063 *
5064 * @param delegate The delegate instance.
5065 *
5066 * @see AccessibilityDelegate
5067 */
5068 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5069 mAccessibilityDelegate = delegate;
5070 }
5071
5072 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005073 * Gets the provider for managing a virtual view hierarchy rooted at this View
5074 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5075 * that explore the window content.
5076 * <p>
5077 * If this method returns an instance, this instance is responsible for managing
5078 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5079 * View including the one representing the View itself. Similarly the returned
5080 * instance is responsible for performing accessibility actions on any virtual
5081 * view or the root view itself.
5082 * </p>
5083 * <p>
5084 * If an {@link AccessibilityDelegate} has been specified via calling
5085 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5086 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5087 * is responsible for handling this call.
5088 * </p>
5089 *
5090 * @return The provider.
5091 *
5092 * @see AccessibilityNodeProvider
5093 */
5094 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5095 if (mAccessibilityDelegate != null) {
5096 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5097 } else {
5098 return null;
5099 }
5100 }
5101
5102 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005103 * Gets the unique identifier of this view on the screen for accessibility purposes.
5104 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5105 *
5106 * @return The view accessibility id.
5107 *
5108 * @hide
5109 */
5110 public int getAccessibilityViewId() {
5111 if (mAccessibilityViewId == NO_ID) {
5112 mAccessibilityViewId = sNextAccessibilityViewId++;
5113 }
5114 return mAccessibilityViewId;
5115 }
5116
5117 /**
5118 * Gets the unique identifier of the window in which this View reseides.
5119 *
5120 * @return The window accessibility id.
5121 *
5122 * @hide
5123 */
5124 public int getAccessibilityWindowId() {
5125 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5126 }
5127
5128 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005129 * Gets the {@link View} description. It briefly describes the view and is
5130 * primarily used for accessibility support. Set this property to enable
5131 * better accessibility support for your application. This is especially
5132 * true for views that do not have textual representation (For example,
5133 * ImageButton).
5134 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005135 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005136 *
5137 * @attr ref android.R.styleable#View_contentDescription
5138 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005139 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005140 public CharSequence getContentDescription() {
5141 return mContentDescription;
5142 }
5143
5144 /**
5145 * Sets the {@link View} description. It briefly describes the view and is
5146 * primarily used for accessibility support. Set this property to enable
5147 * better accessibility support for your application. This is especially
5148 * true for views that do not have textual representation (For example,
5149 * ImageButton).
5150 *
5151 * @param contentDescription The content description.
5152 *
5153 * @attr ref android.R.styleable#View_contentDescription
5154 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005155 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005156 public void setContentDescription(CharSequence contentDescription) {
5157 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005158 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5159 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5160 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5161 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005162 }
5163
5164 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005165 * Gets the id of a view for which this view serves as a label for
5166 * accessibility purposes.
5167 *
5168 * @return The labeled view id.
5169 */
5170 @ViewDebug.ExportedProperty(category = "accessibility")
5171 public int getLabelFor() {
5172 return mLabelForId;
5173 }
5174
5175 /**
5176 * Sets the id of a view for which this view serves as a label for
5177 * accessibility purposes.
5178 *
5179 * @param id The labeled view id.
5180 */
5181 @RemotableViewMethod
5182 public void setLabelFor(int id) {
5183 mLabelForId = id;
5184 if (mLabelForId != View.NO_ID
5185 && mID == View.NO_ID) {
5186 mID = generateViewId();
5187 }
5188 }
5189
5190 /**
Romain Guya2431d02009-04-30 16:30:00 -07005191 * Invoked whenever this view loses focus, either by losing window focus or by losing
5192 * focus within its window. This method can be used to clear any state tied to the
5193 * focus. For instance, if a button is held pressed with the trackball and the window
5194 * loses focus, this method can be used to cancel the press.
5195 *
5196 * Subclasses of View overriding this method should always call super.onFocusLost().
5197 *
5198 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005199 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005200 *
5201 * @hide pending API council approval
5202 */
5203 protected void onFocusLost() {
5204 resetPressedState();
5205 }
5206
5207 private void resetPressedState() {
5208 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5209 return;
5210 }
5211
5212 if (isPressed()) {
5213 setPressed(false);
5214
5215 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005216 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005217 }
5218 }
5219 }
5220
5221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222 * Returns true if this view has focus
5223 *
5224 * @return True if this view has focus, false otherwise.
5225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005226 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005228 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 }
5230
5231 /**
5232 * Find the view in the hierarchy rooted at this view that currently has
5233 * focus.
5234 *
5235 * @return The view that currently has focus, or null if no focused view can
5236 * be found.
5237 */
5238 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005239 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 }
5241
5242 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005243 * Indicates whether this view is one of the set of scrollable containers in
5244 * its window.
5245 *
5246 * @return whether this view is one of the set of scrollable containers in
5247 * its window
5248 *
5249 * @attr ref android.R.styleable#View_isScrollContainer
5250 */
5251 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005252 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005253 }
5254
5255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 * Change whether this view is one of the set of scrollable containers in
5257 * its window. This will be used to determine whether the window can
5258 * resize or must pan when a soft input area is open -- scrollable
5259 * containers allow the window to use resize mode since the container
5260 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005261 *
5262 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 */
5264 public void setScrollContainer(boolean isScrollContainer) {
5265 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005266 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005268 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005270 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005272 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 mAttachInfo.mScrollContainers.remove(this);
5274 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005275 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 }
5277 }
5278
5279 /**
5280 * Returns the quality of the drawing cache.
5281 *
5282 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5283 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5284 *
5285 * @see #setDrawingCacheQuality(int)
5286 * @see #setDrawingCacheEnabled(boolean)
5287 * @see #isDrawingCacheEnabled()
5288 *
5289 * @attr ref android.R.styleable#View_drawingCacheQuality
5290 */
5291 public int getDrawingCacheQuality() {
5292 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5293 }
5294
5295 /**
5296 * Set the drawing cache quality of this view. This value is used only when the
5297 * drawing cache is enabled
5298 *
5299 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5300 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5301 *
5302 * @see #getDrawingCacheQuality()
5303 * @see #setDrawingCacheEnabled(boolean)
5304 * @see #isDrawingCacheEnabled()
5305 *
5306 * @attr ref android.R.styleable#View_drawingCacheQuality
5307 */
5308 public void setDrawingCacheQuality(int quality) {
5309 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5310 }
5311
5312 /**
5313 * Returns whether the screen should remain on, corresponding to the current
5314 * value of {@link #KEEP_SCREEN_ON}.
5315 *
5316 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5317 *
5318 * @see #setKeepScreenOn(boolean)
5319 *
5320 * @attr ref android.R.styleable#View_keepScreenOn
5321 */
5322 public boolean getKeepScreenOn() {
5323 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5324 }
5325
5326 /**
5327 * Controls whether the screen should remain on, modifying the
5328 * value of {@link #KEEP_SCREEN_ON}.
5329 *
5330 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5331 *
5332 * @see #getKeepScreenOn()
5333 *
5334 * @attr ref android.R.styleable#View_keepScreenOn
5335 */
5336 public void setKeepScreenOn(boolean keepScreenOn) {
5337 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5338 }
5339
5340 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005341 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5342 * @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 -08005343 *
5344 * @attr ref android.R.styleable#View_nextFocusLeft
5345 */
5346 public int getNextFocusLeftId() {
5347 return mNextFocusLeftId;
5348 }
5349
5350 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005351 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5352 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5353 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 *
5355 * @attr ref android.R.styleable#View_nextFocusLeft
5356 */
5357 public void setNextFocusLeftId(int nextFocusLeftId) {
5358 mNextFocusLeftId = nextFocusLeftId;
5359 }
5360
5361 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005362 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5363 * @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 -08005364 *
5365 * @attr ref android.R.styleable#View_nextFocusRight
5366 */
5367 public int getNextFocusRightId() {
5368 return mNextFocusRightId;
5369 }
5370
5371 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005372 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5373 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5374 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 *
5376 * @attr ref android.R.styleable#View_nextFocusRight
5377 */
5378 public void setNextFocusRightId(int nextFocusRightId) {
5379 mNextFocusRightId = nextFocusRightId;
5380 }
5381
5382 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005383 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5384 * @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 -08005385 *
5386 * @attr ref android.R.styleable#View_nextFocusUp
5387 */
5388 public int getNextFocusUpId() {
5389 return mNextFocusUpId;
5390 }
5391
5392 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005393 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5394 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5395 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 *
5397 * @attr ref android.R.styleable#View_nextFocusUp
5398 */
5399 public void setNextFocusUpId(int nextFocusUpId) {
5400 mNextFocusUpId = nextFocusUpId;
5401 }
5402
5403 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005404 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5405 * @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 -08005406 *
5407 * @attr ref android.R.styleable#View_nextFocusDown
5408 */
5409 public int getNextFocusDownId() {
5410 return mNextFocusDownId;
5411 }
5412
5413 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005414 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5415 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5416 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 *
5418 * @attr ref android.R.styleable#View_nextFocusDown
5419 */
5420 public void setNextFocusDownId(int nextFocusDownId) {
5421 mNextFocusDownId = nextFocusDownId;
5422 }
5423
5424 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005425 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5426 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5427 *
5428 * @attr ref android.R.styleable#View_nextFocusForward
5429 */
5430 public int getNextFocusForwardId() {
5431 return mNextFocusForwardId;
5432 }
5433
5434 /**
5435 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5436 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5437 * decide automatically.
5438 *
5439 * @attr ref android.R.styleable#View_nextFocusForward
5440 */
5441 public void setNextFocusForwardId(int nextFocusForwardId) {
5442 mNextFocusForwardId = nextFocusForwardId;
5443 }
5444
5445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 * Returns the visibility of this view and all of its ancestors
5447 *
5448 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5449 */
5450 public boolean isShown() {
5451 View current = this;
5452 //noinspection ConstantConditions
5453 do {
5454 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5455 return false;
5456 }
5457 ViewParent parent = current.mParent;
5458 if (parent == null) {
5459 return false; // We are not attached to the view root
5460 }
5461 if (!(parent instanceof View)) {
5462 return true;
5463 }
5464 current = (View) parent;
5465 } while (current != null);
5466
5467 return false;
5468 }
5469
5470 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005471 * Called by the view hierarchy when the content insets for a window have
5472 * changed, to allow it to adjust its content to fit within those windows.
5473 * The content insets tell you the space that the status bar, input method,
5474 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005476 * <p>You do not normally need to deal with this function, since the default
5477 * window decoration given to applications takes care of applying it to the
5478 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5479 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5480 * and your content can be placed under those system elements. You can then
5481 * use this method within your view hierarchy if you have parts of your UI
5482 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005484 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005485 * inset's to the view's padding, consuming that content (modifying the
5486 * insets to be 0), and returning true. This behavior is off by default, but can
5487 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5488 *
5489 * <p>This function's traversal down the hierarchy is depth-first. The same content
5490 * insets object is propagated down the hierarchy, so any changes made to it will
5491 * be seen by all following views (including potentially ones above in
5492 * the hierarchy since this is a depth-first traversal). The first view
5493 * that returns true will abort the entire traversal.
5494 *
5495 * <p>The default implementation works well for a situation where it is
5496 * used with a container that covers the entire window, allowing it to
5497 * apply the appropriate insets to its content on all edges. If you need
5498 * a more complicated layout (such as two different views fitting system
5499 * windows, one on the top of the window, and one on the bottom),
5500 * you can override the method and handle the insets however you would like.
5501 * Note that the insets provided by the framework are always relative to the
5502 * far edges of the window, not accounting for the location of the called view
5503 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005504 * where the layout will place the view, as it is done before layout happens.)
5505 *
5506 * <p>Note: unlike many View methods, there is no dispatch phase to this
5507 * call. If you are overriding it in a ViewGroup and want to allow the
5508 * call to continue to your children, you must be sure to call the super
5509 * implementation.
5510 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005511 * <p>Here is a sample layout that makes use of fitting system windows
5512 * to have controls for a video view placed inside of the window decorations
5513 * that it hides and shows. This can be used with code like the second
5514 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5515 *
5516 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5517 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005518 * @param insets Current content insets of the window. Prior to
5519 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5520 * the insets or else you and Android will be unhappy.
5521 *
5522 * @return Return true if this view applied the insets and it should not
5523 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005524 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005525 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005526 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 */
5528 protected boolean fitSystemWindows(Rect insets) {
5529 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005530 mUserPaddingStart = UNDEFINED_PADDING;
5531 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005532 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5533 || mAttachInfo == null
5534 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5535 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5536 return true;
5537 } else {
5538 internalSetPadding(0, 0, 0, 0);
5539 return false;
5540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 }
5542 return false;
5543 }
5544
5545 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005546 * Sets whether or not this view should account for system screen decorations
5547 * such as the status bar and inset its content; that is, controlling whether
5548 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5549 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005550 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005551 * <p>Note that if you are providing your own implementation of
5552 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5553 * flag to true -- your implementation will be overriding the default
5554 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005555 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005556 * @param fitSystemWindows If true, then the default implementation of
5557 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005558 *
5559 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005560 * @see #getFitsSystemWindows()
5561 * @see #fitSystemWindows(Rect)
5562 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005563 */
5564 public void setFitsSystemWindows(boolean fitSystemWindows) {
5565 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5566 }
5567
5568 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005569 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005570 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5571 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005572 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005573 * @return Returns true if the default implementation of
5574 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005575 *
5576 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005577 * @see #setFitsSystemWindows()
5578 * @see #fitSystemWindows(Rect)
5579 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005580 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005581 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005582 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5583 }
5584
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005585 /** @hide */
5586 public boolean fitsSystemWindows() {
5587 return getFitsSystemWindows();
5588 }
5589
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005590 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005591 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5592 */
5593 public void requestFitSystemWindows() {
5594 if (mParent != null) {
5595 mParent.requestFitSystemWindows();
5596 }
5597 }
5598
5599 /**
5600 * For use by PhoneWindow to make its own system window fitting optional.
5601 * @hide
5602 */
5603 public void makeOptionalFitsSystemWindows() {
5604 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5605 }
5606
5607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 * Returns the visibility status for this view.
5609 *
5610 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5611 * @attr ref android.R.styleable#View_visibility
5612 */
5613 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005614 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5615 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5616 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 })
5618 public int getVisibility() {
5619 return mViewFlags & VISIBILITY_MASK;
5620 }
5621
5622 /**
5623 * Set the enabled state of this view.
5624 *
5625 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5626 * @attr ref android.R.styleable#View_visibility
5627 */
5628 @RemotableViewMethod
5629 public void setVisibility(int visibility) {
5630 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005631 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633
5634 /**
5635 * Returns the enabled status for this view. The interpretation of the
5636 * enabled state varies by subclass.
5637 *
5638 * @return True if this view is enabled, false otherwise.
5639 */
5640 @ViewDebug.ExportedProperty
5641 public boolean isEnabled() {
5642 return (mViewFlags & ENABLED_MASK) == ENABLED;
5643 }
5644
5645 /**
5646 * Set the enabled state of this view. The interpretation of the enabled
5647 * state varies by subclass.
5648 *
5649 * @param enabled True if this view is enabled, false otherwise.
5650 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005651 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005653 if (enabled == isEnabled()) return;
5654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5656
5657 /*
5658 * The View most likely has to change its appearance, so refresh
5659 * the drawable state.
5660 */
5661 refreshDrawableState();
5662
5663 // Invalidate too, since the default behavior for views is to be
5664 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005665 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 }
5667
5668 /**
5669 * Set whether this view can receive the focus.
5670 *
5671 * Setting this to false will also ensure that this view is not focusable
5672 * in touch mode.
5673 *
5674 * @param focusable If true, this view can receive the focus.
5675 *
5676 * @see #setFocusableInTouchMode(boolean)
5677 * @attr ref android.R.styleable#View_focusable
5678 */
5679 public void setFocusable(boolean focusable) {
5680 if (!focusable) {
5681 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5682 }
5683 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5684 }
5685
5686 /**
5687 * Set whether this view can receive focus while in touch mode.
5688 *
5689 * Setting this to true will also ensure that this view is focusable.
5690 *
5691 * @param focusableInTouchMode If true, this view can receive the focus while
5692 * in touch mode.
5693 *
5694 * @see #setFocusable(boolean)
5695 * @attr ref android.R.styleable#View_focusableInTouchMode
5696 */
5697 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5698 // Focusable in touch mode should always be set before the focusable flag
5699 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5700 // which, in touch mode, will not successfully request focus on this view
5701 // because the focusable in touch mode flag is not set
5702 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5703 if (focusableInTouchMode) {
5704 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5705 }
5706 }
5707
5708 /**
5709 * Set whether this view should have sound effects enabled for events such as
5710 * clicking and touching.
5711 *
5712 * <p>You may wish to disable sound effects for a view if you already play sounds,
5713 * for instance, a dial key that plays dtmf tones.
5714 *
5715 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5716 * @see #isSoundEffectsEnabled()
5717 * @see #playSoundEffect(int)
5718 * @attr ref android.R.styleable#View_soundEffectsEnabled
5719 */
5720 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5721 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5722 }
5723
5724 /**
5725 * @return whether this view should have sound effects enabled for events such as
5726 * clicking and touching.
5727 *
5728 * @see #setSoundEffectsEnabled(boolean)
5729 * @see #playSoundEffect(int)
5730 * @attr ref android.R.styleable#View_soundEffectsEnabled
5731 */
5732 @ViewDebug.ExportedProperty
5733 public boolean isSoundEffectsEnabled() {
5734 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5735 }
5736
5737 /**
5738 * Set whether this view should have haptic feedback for events such as
5739 * long presses.
5740 *
5741 * <p>You may wish to disable haptic feedback if your view already controls
5742 * its own haptic feedback.
5743 *
5744 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5745 * @see #isHapticFeedbackEnabled()
5746 * @see #performHapticFeedback(int)
5747 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5748 */
5749 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5750 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5751 }
5752
5753 /**
5754 * @return whether this view should have haptic feedback enabled for events
5755 * long presses.
5756 *
5757 * @see #setHapticFeedbackEnabled(boolean)
5758 * @see #performHapticFeedback(int)
5759 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5760 */
5761 @ViewDebug.ExportedProperty
5762 public boolean isHapticFeedbackEnabled() {
5763 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5764 }
5765
5766 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005767 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005768 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005769 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5770 * {@link #LAYOUT_DIRECTION_RTL},
5771 * {@link #LAYOUT_DIRECTION_INHERIT} or
5772 * {@link #LAYOUT_DIRECTION_LOCALE}.
5773 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005774 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005775 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005776 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5777 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5778 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5779 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005780 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005781 private int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005782 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005783 }
5784
5785 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005786 * Set the layout direction for this view. This will propagate a reset of layout direction
5787 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005788 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005789 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5790 * {@link #LAYOUT_DIRECTION_RTL},
5791 * {@link #LAYOUT_DIRECTION_INHERIT} or
5792 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005793 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005794 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005795 */
5796 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005797 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005798 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005799 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005800 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005801 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005802 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005803 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005804 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005805 resolveRtlProperties();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005806 // Notify changes
5807 onRtlPropertiesChanged();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005808 // ... and ask for a layout pass
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005809 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005810 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005811 }
5812
5813 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005814 * Returns the resolved layout direction for this view.
5815 *
5816 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005817 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005818 */
5819 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005820 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5821 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005822 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005823 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005824 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5825 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005826 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005827 return LAYOUT_DIRECTION_LTR;
5828 }
Fabrice Di Megliob93911f2012-06-26 19:43:15 -07005829 // The layout direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -07005830 if ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) != PFLAG2_LAYOUT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005831 resolveLayoutDirection();
5832 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005833 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) == PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005834 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5835 }
5836
5837 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005838 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5839 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005840 *
5841 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005842 */
5843 @ViewDebug.ExportedProperty(category = "layout")
5844 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005845 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005846 }
5847
5848 /**
Adam Powell539ee872012-02-03 19:00:49 -08005849 * Indicates whether the view is currently tracking transient state that the
5850 * app should not need to concern itself with saving and restoring, but that
5851 * the framework should take special note to preserve when possible.
5852 *
Adam Powell785c4472012-05-02 21:25:39 -07005853 * <p>A view with transient state cannot be trivially rebound from an external
5854 * data source, such as an adapter binding item views in a list. This may be
5855 * because the view is performing an animation, tracking user selection
5856 * of content, or similar.</p>
5857 *
Adam Powell539ee872012-02-03 19:00:49 -08005858 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005859 */
5860 @ViewDebug.ExportedProperty(category = "layout")
5861 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005862 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005863 }
5864
5865 /**
5866 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005867 * framework should attempt to preserve when possible. This flag is reference counted,
5868 * so every call to setHasTransientState(true) should be paired with a later call
5869 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005870 *
Adam Powell785c4472012-05-02 21:25:39 -07005871 * <p>A view with transient state cannot be trivially rebound from an external
5872 * data source, such as an adapter binding item views in a list. This may be
5873 * because the view is performing an animation, tracking user selection
5874 * of content, or similar.</p>
5875 *
Adam Powell539ee872012-02-03 19:00:49 -08005876 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005877 */
5878 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005879 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5880 mTransientStateCount - 1;
5881 if (mTransientStateCount < 0) {
5882 mTransientStateCount = 0;
5883 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5884 "unmatched pair of setHasTransientState calls");
5885 }
5886 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005887 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005888 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005889 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5890 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005891 if (mParent != null) {
5892 try {
5893 mParent.childHasTransientStateChanged(this, hasTransientState);
5894 } catch (AbstractMethodError e) {
5895 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5896 " does not fully implement ViewParent", e);
5897 }
Adam Powell539ee872012-02-03 19:00:49 -08005898 }
5899 }
5900 }
5901
5902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005903 * If this view doesn't do any drawing on its own, set this flag to
5904 * allow further optimizations. By default, this flag is not set on
5905 * View, but could be set on some View subclasses such as ViewGroup.
5906 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005907 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5908 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 *
5910 * @param willNotDraw whether or not this View draw on its own
5911 */
5912 public void setWillNotDraw(boolean willNotDraw) {
5913 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5914 }
5915
5916 /**
5917 * Returns whether or not this View draws on its own.
5918 *
5919 * @return true if this view has nothing to draw, false otherwise
5920 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005921 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 public boolean willNotDraw() {
5923 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5924 }
5925
5926 /**
5927 * When a View's drawing cache is enabled, drawing is redirected to an
5928 * offscreen bitmap. Some views, like an ImageView, must be able to
5929 * bypass this mechanism if they already draw a single bitmap, to avoid
5930 * unnecessary usage of the memory.
5931 *
5932 * @param willNotCacheDrawing true if this view does not cache its
5933 * drawing, false otherwise
5934 */
5935 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5936 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5937 }
5938
5939 /**
5940 * Returns whether or not this View can cache its drawing or not.
5941 *
5942 * @return true if this view does not cache its drawing, false otherwise
5943 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005944 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005945 public boolean willNotCacheDrawing() {
5946 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5947 }
5948
5949 /**
5950 * Indicates whether this view reacts to click events or not.
5951 *
5952 * @return true if the view is clickable, false otherwise
5953 *
5954 * @see #setClickable(boolean)
5955 * @attr ref android.R.styleable#View_clickable
5956 */
5957 @ViewDebug.ExportedProperty
5958 public boolean isClickable() {
5959 return (mViewFlags & CLICKABLE) == CLICKABLE;
5960 }
5961
5962 /**
5963 * Enables or disables click events for this view. When a view
5964 * is clickable it will change its state to "pressed" on every click.
5965 * Subclasses should set the view clickable to visually react to
5966 * user's clicks.
5967 *
5968 * @param clickable true to make the view clickable, false otherwise
5969 *
5970 * @see #isClickable()
5971 * @attr ref android.R.styleable#View_clickable
5972 */
5973 public void setClickable(boolean clickable) {
5974 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5975 }
5976
5977 /**
5978 * Indicates whether this view reacts to long click events or not.
5979 *
5980 * @return true if the view is long clickable, false otherwise
5981 *
5982 * @see #setLongClickable(boolean)
5983 * @attr ref android.R.styleable#View_longClickable
5984 */
5985 public boolean isLongClickable() {
5986 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5987 }
5988
5989 /**
5990 * Enables or disables long click events for this view. When a view is long
5991 * clickable it reacts to the user holding down the button for a longer
5992 * duration than a tap. This event can either launch the listener or a
5993 * context menu.
5994 *
5995 * @param longClickable true to make the view long clickable, false otherwise
5996 * @see #isLongClickable()
5997 * @attr ref android.R.styleable#View_longClickable
5998 */
5999 public void setLongClickable(boolean longClickable) {
6000 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6001 }
6002
6003 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006004 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 *
6006 * @see #isClickable()
6007 * @see #setClickable(boolean)
6008 *
6009 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6010 * the View's internal state from a previously set "pressed" state.
6011 */
6012 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006013 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006016 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006018 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006020
6021 if (needsRefresh) {
6022 refreshDrawableState();
6023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 dispatchSetPressed(pressed);
6025 }
6026
6027 /**
6028 * Dispatch setPressed to all of this View's children.
6029 *
6030 * @see #setPressed(boolean)
6031 *
6032 * @param pressed The new pressed state
6033 */
6034 protected void dispatchSetPressed(boolean pressed) {
6035 }
6036
6037 /**
6038 * Indicates whether the view is currently in pressed state. Unless
6039 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6040 * the pressed state.
6041 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006042 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 * @see #isClickable()
6044 * @see #setClickable(boolean)
6045 *
6046 * @return true if the view is currently pressed, false otherwise
6047 */
6048 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006049 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 }
6051
6052 /**
6053 * Indicates whether this view will save its state (that is,
6054 * whether its {@link #onSaveInstanceState} method will be called).
6055 *
6056 * @return Returns true if the view state saving is enabled, else false.
6057 *
6058 * @see #setSaveEnabled(boolean)
6059 * @attr ref android.R.styleable#View_saveEnabled
6060 */
6061 public boolean isSaveEnabled() {
6062 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6063 }
6064
6065 /**
6066 * Controls whether the saving of this view's state is
6067 * enabled (that is, whether its {@link #onSaveInstanceState} method
6068 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006069 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 * for its state to be saved. This flag can only disable the
6071 * saving of this view; any child views may still have their state saved.
6072 *
6073 * @param enabled Set to false to <em>disable</em> state saving, or true
6074 * (the default) to allow it.
6075 *
6076 * @see #isSaveEnabled()
6077 * @see #setId(int)
6078 * @see #onSaveInstanceState()
6079 * @attr ref android.R.styleable#View_saveEnabled
6080 */
6081 public void setSaveEnabled(boolean enabled) {
6082 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6083 }
6084
Jeff Brown85a31762010-09-01 17:01:00 -07006085 /**
6086 * Gets whether the framework should discard touches when the view's
6087 * window is obscured by another visible window.
6088 * Refer to the {@link View} security documentation for more details.
6089 *
6090 * @return True if touch filtering is enabled.
6091 *
6092 * @see #setFilterTouchesWhenObscured(boolean)
6093 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6094 */
6095 @ViewDebug.ExportedProperty
6096 public boolean getFilterTouchesWhenObscured() {
6097 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6098 }
6099
6100 /**
6101 * Sets whether the framework should discard touches when the view's
6102 * window is obscured by another visible window.
6103 * Refer to the {@link View} security documentation for more details.
6104 *
6105 * @param enabled True if touch filtering should be enabled.
6106 *
6107 * @see #getFilterTouchesWhenObscured
6108 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6109 */
6110 public void setFilterTouchesWhenObscured(boolean enabled) {
6111 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6112 FILTER_TOUCHES_WHEN_OBSCURED);
6113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114
6115 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006116 * Indicates whether the entire hierarchy under this view will save its
6117 * state when a state saving traversal occurs from its parent. The default
6118 * is true; if false, these views will not be saved unless
6119 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6120 *
6121 * @return Returns true if the view state saving from parent is enabled, else false.
6122 *
6123 * @see #setSaveFromParentEnabled(boolean)
6124 */
6125 public boolean isSaveFromParentEnabled() {
6126 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6127 }
6128
6129 /**
6130 * Controls whether the entire hierarchy under this view will save its
6131 * state when a state saving traversal occurs from its parent. The default
6132 * is true; if false, these views will not be saved unless
6133 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6134 *
6135 * @param enabled Set to false to <em>disable</em> state saving, or true
6136 * (the default) to allow it.
6137 *
6138 * @see #isSaveFromParentEnabled()
6139 * @see #setId(int)
6140 * @see #onSaveInstanceState()
6141 */
6142 public void setSaveFromParentEnabled(boolean enabled) {
6143 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6144 }
6145
6146
6147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006148 * Returns whether this View is able to take focus.
6149 *
6150 * @return True if this view can take focus, or false otherwise.
6151 * @attr ref android.R.styleable#View_focusable
6152 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006153 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006154 public final boolean isFocusable() {
6155 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6156 }
6157
6158 /**
6159 * When a view is focusable, it may not want to take focus when in touch mode.
6160 * For example, a button would like focus when the user is navigating via a D-pad
6161 * so that the user can click on it, but once the user starts touching the screen,
6162 * the button shouldn't take focus
6163 * @return Whether the view is focusable in touch mode.
6164 * @attr ref android.R.styleable#View_focusableInTouchMode
6165 */
6166 @ViewDebug.ExportedProperty
6167 public final boolean isFocusableInTouchMode() {
6168 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6169 }
6170
6171 /**
6172 * Find the nearest view in the specified direction that can take focus.
6173 * This does not actually give focus to that view.
6174 *
6175 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6176 *
6177 * @return The nearest focusable in the specified direction, or null if none
6178 * can be found.
6179 */
6180 public View focusSearch(int direction) {
6181 if (mParent != null) {
6182 return mParent.focusSearch(this, direction);
6183 } else {
6184 return null;
6185 }
6186 }
6187
6188 /**
6189 * This method is the last chance for the focused view and its ancestors to
6190 * respond to an arrow key. This is called when the focused view did not
6191 * consume the key internally, nor could the view system find a new view in
6192 * the requested direction to give focus to.
6193 *
6194 * @param focused The currently focused view.
6195 * @param direction The direction focus wants to move. One of FOCUS_UP,
6196 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6197 * @return True if the this view consumed this unhandled move.
6198 */
6199 public boolean dispatchUnhandledMove(View focused, int direction) {
6200 return false;
6201 }
6202
6203 /**
6204 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006205 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006207 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6208 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 * @return The user specified next view, or null if there is none.
6210 */
6211 View findUserSetNextFocus(View root, int direction) {
6212 switch (direction) {
6213 case FOCUS_LEFT:
6214 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006215 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 case FOCUS_RIGHT:
6217 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006218 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 case FOCUS_UP:
6220 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006221 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006222 case FOCUS_DOWN:
6223 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006224 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006225 case FOCUS_FORWARD:
6226 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006227 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006228 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006229 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006230 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006231 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006232 @Override
6233 public boolean apply(View t) {
6234 return t.mNextFocusForwardId == id;
6235 }
6236 });
6237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 }
6239 return null;
6240 }
6241
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006242 private View findViewInsideOutShouldExist(View root, int id) {
6243 if (mMatchIdPredicate == null) {
6244 mMatchIdPredicate = new MatchIdPredicate();
6245 }
6246 mMatchIdPredicate.mId = id;
6247 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006248 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006249 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006250 }
6251 return result;
6252 }
6253
6254 /**
6255 * Find and return all focusable views that are descendants of this view,
6256 * possibly including this view if it is focusable itself.
6257 *
6258 * @param direction The direction of the focus
6259 * @return A list of focusable views
6260 */
6261 public ArrayList<View> getFocusables(int direction) {
6262 ArrayList<View> result = new ArrayList<View>(24);
6263 addFocusables(result, direction);
6264 return result;
6265 }
6266
6267 /**
6268 * Add any focusable views that are descendants of this view (possibly
6269 * including this view if it is focusable itself) to views. If we are in touch mode,
6270 * only add views that are also focusable in touch mode.
6271 *
6272 * @param views Focusable views found so far
6273 * @param direction The direction of the focus
6274 */
6275 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006276 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278
svetoslavganov75986cf2009-05-14 22:28:01 -07006279 /**
6280 * Adds any focusable views that are descendants of this view (possibly
6281 * including this view if it is focusable itself) to views. This method
6282 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006283 * only views focusable in touch mode if we are in touch mode or
6284 * only views that can take accessibility focus if accessibility is enabeld
6285 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006286 *
6287 * @param views Focusable views found so far or null if all we are interested is
6288 * the number of focusables.
6289 * @param direction The direction of the focus.
6290 * @param focusableMode The type of focusables to be added.
6291 *
6292 * @see #FOCUSABLES_ALL
6293 * @see #FOCUSABLES_TOUCH_MODE
6294 */
6295 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006296 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006297 return;
6298 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006299 if (!isFocusable()) {
6300 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006301 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006302 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6303 && isInTouchMode() && !isFocusableInTouchMode()) {
6304 return;
6305 }
6306 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 }
6308
6309 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006310 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006311 * The search is performed by either the text that the View renders or the content
6312 * description that describes the view for accessibility purposes and the view does
6313 * not render or both. Clients can specify how the search is to be performed via
6314 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6315 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006316 *
6317 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006318 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006319 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006320 * @see #FIND_VIEWS_WITH_TEXT
6321 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6322 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006323 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006324 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006325 if (getAccessibilityNodeProvider() != null) {
6326 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6327 outViews.add(this);
6328 }
6329 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006330 && (searched != null && searched.length() > 0)
6331 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006332 String searchedLowerCase = searched.toString().toLowerCase();
6333 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6334 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6335 outViews.add(this);
6336 }
6337 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006338 }
6339
6340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 * Find and return all touchable views that are descendants of this view,
6342 * possibly including this view if it is touchable itself.
6343 *
6344 * @return A list of touchable views
6345 */
6346 public ArrayList<View> getTouchables() {
6347 ArrayList<View> result = new ArrayList<View>();
6348 addTouchables(result);
6349 return result;
6350 }
6351
6352 /**
6353 * Add any touchable views that are descendants of this view (possibly
6354 * including this view if it is touchable itself) to views.
6355 *
6356 * @param views Touchable views found so far
6357 */
6358 public void addTouchables(ArrayList<View> views) {
6359 final int viewFlags = mViewFlags;
6360
6361 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6362 && (viewFlags & ENABLED_MASK) == ENABLED) {
6363 views.add(this);
6364 }
6365 }
6366
6367 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006368 * Returns whether this View is accessibility focused.
6369 *
6370 * @return True if this View is accessibility focused.
6371 */
6372 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006373 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006374 }
6375
6376 /**
6377 * Call this to try to give accessibility focus to this view.
6378 *
6379 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6380 * returns false or the view is no visible or the view already has accessibility
6381 * focus.
6382 *
6383 * See also {@link #focusSearch(int)}, which is what you call to say that you
6384 * have focus, and you want your parent to look for the next one.
6385 *
6386 * @return Whether this view actually took accessibility focus.
6387 *
6388 * @hide
6389 */
6390 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006391 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6392 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006393 return false;
6394 }
6395 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6396 return false;
6397 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006398 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6399 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006400 ViewRootImpl viewRootImpl = getViewRootImpl();
6401 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006402 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006403 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006404 if (mAttachInfo != null) {
6405 Rect rectangle = mAttachInfo.mTmpInvalRect;
6406 getDrawingRect(rectangle);
6407 requestRectangleOnScreen(rectangle);
6408 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006409 invalidate();
6410 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6411 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006412 return true;
6413 }
6414 return false;
6415 }
6416
6417 /**
6418 * Call this to try to clear accessibility focus of this view.
6419 *
6420 * See also {@link #focusSearch(int)}, which is what you call to say that you
6421 * have focus, and you want your parent to look for the next one.
6422 *
6423 * @hide
6424 */
6425 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006426 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6427 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006428 invalidate();
6429 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6430 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006431 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006432 // Clear the global reference of accessibility focus if this
6433 // view or any of its descendants had accessibility focus.
6434 ViewRootImpl viewRootImpl = getViewRootImpl();
6435 if (viewRootImpl != null) {
6436 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6437 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006438 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006439 }
6440 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006441 }
6442
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006443 private void sendAccessibilityHoverEvent(int eventType) {
6444 // Since we are not delivering to a client accessibility events from not
6445 // important views (unless the clinet request that) we need to fire the
6446 // event from the deepest view exposed to the client. As a consequence if
6447 // the user crosses a not exposed view the client will see enter and exit
6448 // of the exposed predecessor followed by and enter and exit of that same
6449 // predecessor when entering and exiting the not exposed descendant. This
6450 // is fine since the client has a clear idea which view is hovered at the
6451 // price of a couple more events being sent. This is a simple and
6452 // working solution.
6453 View source = this;
6454 while (true) {
6455 if (source.includeForAccessibility()) {
6456 source.sendAccessibilityEvent(eventType);
6457 return;
6458 }
6459 ViewParent parent = source.getParent();
6460 if (parent instanceof View) {
6461 source = (View) parent;
6462 } else {
6463 return;
6464 }
6465 }
6466 }
6467
Svetoslav Ganov42138042012-03-20 11:51:39 -07006468 /**
6469 * Clears accessibility focus without calling any callback methods
6470 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6471 * is used for clearing accessibility focus when giving this focus to
6472 * another view.
6473 */
6474 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006475 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6476 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006477 invalidate();
6478 }
6479 }
6480
6481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006482 * Call this to try to give focus to a specific view or to one of its
6483 * descendants.
6484 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006485 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6486 * false), or if it is focusable and it is not focusable in touch mode
6487 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006488 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006489 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006490 * have focus, and you want your parent to look for the next one.
6491 *
6492 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6493 * {@link #FOCUS_DOWN} and <code>null</code>.
6494 *
6495 * @return Whether this view or one of its descendants actually took focus.
6496 */
6497 public final boolean requestFocus() {
6498 return requestFocus(View.FOCUS_DOWN);
6499 }
6500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 /**
6502 * Call this to try to give focus to a specific view or to one of its
6503 * descendants and give it a hint about what direction focus is heading.
6504 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006505 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6506 * false), or if it is focusable and it is not focusable in touch mode
6507 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006509 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 * have focus, and you want your parent to look for the next one.
6511 *
6512 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6513 * <code>null</code> set for the previously focused rectangle.
6514 *
6515 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6516 * @return Whether this view or one of its descendants actually took focus.
6517 */
6518 public final boolean requestFocus(int direction) {
6519 return requestFocus(direction, null);
6520 }
6521
6522 /**
6523 * Call this to try to give focus to a specific view or to one of its descendants
6524 * and give it hints about the direction and a specific rectangle that the focus
6525 * is coming from. The rectangle can help give larger views a finer grained hint
6526 * about where focus is coming from, and therefore, where to show selection, or
6527 * forward focus change internally.
6528 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006529 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6530 * false), or if it is focusable and it is not focusable in touch mode
6531 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 *
6533 * A View will not take focus if it is not visible.
6534 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006535 * A View will not take focus if one of its parents has
6536 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6537 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006539 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 * have focus, and you want your parent to look for the next one.
6541 *
6542 * You may wish to override this method if your custom {@link View} has an internal
6543 * {@link View} that it wishes to forward the request to.
6544 *
6545 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6546 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6547 * to give a finer grained hint about where focus is coming from. May be null
6548 * if there is no hint.
6549 * @return Whether this view or one of its descendants actually took focus.
6550 */
6551 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006552 return requestFocusNoSearch(direction, previouslyFocusedRect);
6553 }
6554
6555 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 // need to be focusable
6557 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6558 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6559 return false;
6560 }
6561
6562 // need to be focusable in touch mode if in touch mode
6563 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006564 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6565 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 }
6567
6568 // need to not have any parents blocking us
6569 if (hasAncestorThatBlocksDescendantFocus()) {
6570 return false;
6571 }
6572
6573 handleFocusGainInternal(direction, previouslyFocusedRect);
6574 return true;
6575 }
6576
6577 /**
6578 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6579 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6580 * touch mode to request focus when they are touched.
6581 *
6582 * @return Whether this view or one of its descendants actually took focus.
6583 *
6584 * @see #isInTouchMode()
6585 *
6586 */
6587 public final boolean requestFocusFromTouch() {
6588 // Leave touch mode if we need to
6589 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006590 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006591 if (viewRoot != null) {
6592 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006593 }
6594 }
6595 return requestFocus(View.FOCUS_DOWN);
6596 }
6597
6598 /**
6599 * @return Whether any ancestor of this view blocks descendant focus.
6600 */
6601 private boolean hasAncestorThatBlocksDescendantFocus() {
6602 ViewParent ancestor = mParent;
6603 while (ancestor instanceof ViewGroup) {
6604 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6605 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6606 return true;
6607 } else {
6608 ancestor = vgAncestor.getParent();
6609 }
6610 }
6611 return false;
6612 }
6613
6614 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006615 * Gets the mode for determining whether this View is important for accessibility
6616 * which is if it fires accessibility events and if it is reported to
6617 * accessibility services that query the screen.
6618 *
6619 * @return The mode for determining whether a View is important for accessibility.
6620 *
6621 * @attr ref android.R.styleable#View_importantForAccessibility
6622 *
6623 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6624 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6625 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6626 */
6627 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006628 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6629 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6630 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006631 })
6632 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006633 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6634 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006635 }
6636
6637 /**
6638 * Sets how to determine whether this view is important for accessibility
6639 * which is if it fires accessibility events and if it is reported to
6640 * accessibility services that query the screen.
6641 *
6642 * @param mode How to determine whether this view is important for accessibility.
6643 *
6644 * @attr ref android.R.styleable#View_importantForAccessibility
6645 *
6646 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6647 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6648 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6649 */
6650 public void setImportantForAccessibility(int mode) {
6651 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006652 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6653 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6654 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006655 notifyAccessibilityStateChanged();
6656 }
6657 }
6658
6659 /**
6660 * Gets whether this view should be exposed for accessibility.
6661 *
6662 * @return Whether the view is exposed for accessibility.
6663 *
6664 * @hide
6665 */
6666 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006667 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6668 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006669 switch (mode) {
6670 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6671 return true;
6672 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6673 return false;
6674 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006675 return isActionableForAccessibility() || hasListenersForAccessibility()
6676 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006677 default:
6678 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6679 + mode);
6680 }
6681 }
6682
6683 /**
6684 * Gets the parent for accessibility purposes. Note that the parent for
6685 * accessibility is not necessary the immediate parent. It is the first
6686 * predecessor that is important for accessibility.
6687 *
6688 * @return The parent for accessibility purposes.
6689 */
6690 public ViewParent getParentForAccessibility() {
6691 if (mParent instanceof View) {
6692 View parentView = (View) mParent;
6693 if (parentView.includeForAccessibility()) {
6694 return mParent;
6695 } else {
6696 return mParent.getParentForAccessibility();
6697 }
6698 }
6699 return null;
6700 }
6701
6702 /**
6703 * Adds the children of a given View for accessibility. Since some Views are
6704 * not important for accessibility the children for accessibility are not
6705 * necessarily direct children of the riew, rather they are the first level of
6706 * descendants important for accessibility.
6707 *
6708 * @param children The list of children for accessibility.
6709 */
6710 public void addChildrenForAccessibility(ArrayList<View> children) {
6711 if (includeForAccessibility()) {
6712 children.add(this);
6713 }
6714 }
6715
6716 /**
6717 * Whether to regard this view for accessibility. A view is regarded for
6718 * accessibility if it is important for accessibility or the querying
6719 * accessibility service has explicitly requested that view not
6720 * important for accessibility are regarded.
6721 *
6722 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006723 *
6724 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006725 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006726 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006727 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006728 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006729 }
6730 return false;
6731 }
6732
6733 /**
6734 * Returns whether the View is considered actionable from
6735 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006736 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006737 *
6738 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006739 *
6740 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006741 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006742 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006743 return (isClickable() || isLongClickable() || isFocusable());
6744 }
6745
6746 /**
6747 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006748 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006749 *
6750 * @return True if the view is actionable for accessibility.
6751 */
6752 private boolean hasListenersForAccessibility() {
6753 ListenerInfo info = getListenerInfo();
6754 return mTouchDelegate != null || info.mOnKeyListener != null
6755 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6756 || info.mOnHoverListener != null || info.mOnDragListener != null;
6757 }
6758
6759 /**
6760 * Notifies accessibility services that some view's important for
6761 * accessibility state has changed. Note that such notifications
6762 * are made at most once every
6763 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6764 * to avoid unnecessary load to the system. Also once a view has
6765 * made a notifucation this method is a NOP until the notification has
6766 * been sent to clients.
6767 *
6768 * @hide
6769 *
6770 * TODO: Makse sure this method is called for any view state change
6771 * that is interesting for accessilility purposes.
6772 */
6773 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006774 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6775 return;
6776 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006777 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6778 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006779 if (mParent != null) {
6780 mParent.childAccessibilityStateChanged(this);
6781 }
6782 }
6783 }
6784
6785 /**
6786 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006787 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006788 *
6789 * @hide
6790 */
6791 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006792 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006793 }
6794
6795 /**
6796 * Performs the specified accessibility action on the view. For
6797 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006798 * <p>
6799 * If an {@link AccessibilityDelegate} has been specified via calling
6800 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6801 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6802 * is responsible for handling this call.
6803 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006804 *
6805 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006806 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006807 * @return Whether the action was performed.
6808 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006809 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006810 if (mAccessibilityDelegate != null) {
6811 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6812 } else {
6813 return performAccessibilityActionInternal(action, arguments);
6814 }
6815 }
6816
6817 /**
6818 * @see #performAccessibilityAction(int, Bundle)
6819 *
6820 * Note: Called from the default {@link AccessibilityDelegate}.
6821 */
6822 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006823 switch (action) {
6824 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006825 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006826 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006827 }
6828 } break;
6829 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6830 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006831 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006832 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006833 } break;
6834 case AccessibilityNodeInfo.ACTION_FOCUS: {
6835 if (!hasFocus()) {
6836 // Get out of touch mode since accessibility
6837 // wants to move focus around.
6838 getViewRootImpl().ensureTouchMode(false);
6839 return requestFocus();
6840 }
6841 } break;
6842 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6843 if (hasFocus()) {
6844 clearFocus();
6845 return !isFocused();
6846 }
6847 } break;
6848 case AccessibilityNodeInfo.ACTION_SELECT: {
6849 if (!isSelected()) {
6850 setSelected(true);
6851 return isSelected();
6852 }
6853 } break;
6854 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6855 if (isSelected()) {
6856 setSelected(false);
6857 return !isSelected();
6858 }
6859 } break;
6860 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006861 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006862 return requestAccessibilityFocus();
6863 }
6864 } break;
6865 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6866 if (isAccessibilityFocused()) {
6867 clearAccessibilityFocus();
6868 return true;
6869 }
6870 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006871 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6872 if (arguments != null) {
6873 final int granularity = arguments.getInt(
6874 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6875 return nextAtGranularity(granularity);
6876 }
6877 } break;
6878 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6879 if (arguments != null) {
6880 final int granularity = arguments.getInt(
6881 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6882 return previousAtGranularity(granularity);
6883 }
6884 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006885 }
6886 return false;
6887 }
6888
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006889 private boolean nextAtGranularity(int granularity) {
6890 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006891 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006892 return false;
6893 }
6894 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6895 if (iterator == null) {
6896 return false;
6897 }
6898 final int current = getAccessibilityCursorPosition();
6899 final int[] range = iterator.following(current);
6900 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006901 return false;
6902 }
6903 final int start = range[0];
6904 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006905 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006906 sendViewTextTraversedAtGranularityEvent(
6907 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6908 granularity, start, end);
6909 return true;
6910 }
6911
6912 private boolean previousAtGranularity(int granularity) {
6913 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006914 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006915 return false;
6916 }
6917 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6918 if (iterator == null) {
6919 return false;
6920 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006921 int current = getAccessibilityCursorPosition();
6922 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6923 current = text.length();
6924 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6925 // When traversing by character we always put the cursor after the character
6926 // to ease edit and have to compensate before asking the for previous segment.
6927 current--;
6928 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006929 final int[] range = iterator.preceding(current);
6930 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006931 return false;
6932 }
6933 final int start = range[0];
6934 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006935 // Always put the cursor after the character to ease edit.
6936 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6937 setAccessibilityCursorPosition(end);
6938 } else {
6939 setAccessibilityCursorPosition(start);
6940 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006941 sendViewTextTraversedAtGranularityEvent(
6942 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6943 granularity, start, end);
6944 return true;
6945 }
6946
6947 /**
6948 * Gets the text reported for accessibility purposes.
6949 *
6950 * @return The accessibility text.
6951 *
6952 * @hide
6953 */
6954 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07006955 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006956 }
6957
6958 /**
6959 * @hide
6960 */
6961 public int getAccessibilityCursorPosition() {
6962 return mAccessibilityCursorPosition;
6963 }
6964
6965 /**
6966 * @hide
6967 */
6968 public void setAccessibilityCursorPosition(int position) {
6969 mAccessibilityCursorPosition = position;
6970 }
6971
6972 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
6973 int fromIndex, int toIndex) {
6974 if (mParent == null) {
6975 return;
6976 }
6977 AccessibilityEvent event = AccessibilityEvent.obtain(
6978 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
6979 onInitializeAccessibilityEvent(event);
6980 onPopulateAccessibilityEvent(event);
6981 event.setFromIndex(fromIndex);
6982 event.setToIndex(toIndex);
6983 event.setAction(action);
6984 event.setMovementGranularity(granularity);
6985 mParent.requestSendAccessibilityEvent(this, event);
6986 }
6987
6988 /**
6989 * @hide
6990 */
6991 public TextSegmentIterator getIteratorForGranularity(int granularity) {
6992 switch (granularity) {
6993 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
6994 CharSequence text = getIterableTextForAccessibility();
6995 if (text != null && text.length() > 0) {
6996 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07006997 CharacterTextSegmentIterator.getInstance(
6998 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006999 iterator.initialize(text.toString());
7000 return iterator;
7001 }
7002 } break;
7003 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7004 CharSequence text = getIterableTextForAccessibility();
7005 if (text != null && text.length() > 0) {
7006 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007007 WordTextSegmentIterator.getInstance(
7008 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007009 iterator.initialize(text.toString());
7010 return iterator;
7011 }
7012 } break;
7013 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7014 CharSequence text = getIterableTextForAccessibility();
7015 if (text != null && text.length() > 0) {
7016 ParagraphTextSegmentIterator iterator =
7017 ParagraphTextSegmentIterator.getInstance();
7018 iterator.initialize(text.toString());
7019 return iterator;
7020 }
7021 } break;
7022 }
7023 return null;
7024 }
7025
Svetoslav Ganov42138042012-03-20 11:51:39 -07007026 /**
Romain Guya440b002010-02-24 15:57:54 -08007027 * @hide
7028 */
7029 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007030 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007031 clearDisplayList();
7032
Romain Guya440b002010-02-24 15:57:54 -08007033 onStartTemporaryDetach();
7034 }
7035
7036 /**
7037 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007038 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7039 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007040 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041 */
7042 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007043 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007044 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007045 }
7046
7047 /**
7048 * @hide
7049 */
7050 public void dispatchFinishTemporaryDetach() {
7051 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052 }
Romain Guy8506ab42009-06-11 17:35:47 -07007053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 /**
7055 * Called after {@link #onStartTemporaryDetach} when the container is done
7056 * changing the view.
7057 */
7058 public void onFinishTemporaryDetach() {
7059 }
Romain Guy8506ab42009-06-11 17:35:47 -07007060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007061 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007062 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7063 * for this view's window. Returns null if the view is not currently attached
7064 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007065 * just use the standard high-level event callbacks like
7066 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007067 */
7068 public KeyEvent.DispatcherState getKeyDispatcherState() {
7069 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7070 }
Joe Malin32736f02011-01-19 16:14:20 -08007071
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007072 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073 * Dispatch a key event before it is processed by any input method
7074 * associated with the view hierarchy. This can be used to intercept
7075 * key events in special situations before the IME consumes them; a
7076 * typical example would be handling the BACK key to update the application's
7077 * UI instead of allowing the IME to see it and close itself.
7078 *
7079 * @param event The key event to be dispatched.
7080 * @return True if the event was handled, false otherwise.
7081 */
7082 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7083 return onKeyPreIme(event.getKeyCode(), event);
7084 }
7085
7086 /**
7087 * Dispatch a key event to the next view on the focus path. This path runs
7088 * from the top of the view tree down to the currently focused view. If this
7089 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7090 * the next node down the focus path. This method also fires any key
7091 * listeners.
7092 *
7093 * @param event The key event to be dispatched.
7094 * @return True if the event was handled, false otherwise.
7095 */
7096 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007097 if (mInputEventConsistencyVerifier != null) {
7098 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100
Jeff Brown21bc5c92011-02-28 18:27:14 -08007101 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007102 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007103 ListenerInfo li = mListenerInfo;
7104 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7105 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 return true;
7107 }
7108
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007109 if (event.dispatch(this, mAttachInfo != null
7110 ? mAttachInfo.mKeyDispatchState : null, this)) {
7111 return true;
7112 }
7113
7114 if (mInputEventConsistencyVerifier != null) {
7115 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7116 }
7117 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007118 }
7119
7120 /**
7121 * Dispatches a key shortcut event.
7122 *
7123 * @param event The key event to be dispatched.
7124 * @return True if the event was handled by the view, false otherwise.
7125 */
7126 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7127 return onKeyShortcut(event.getKeyCode(), event);
7128 }
7129
7130 /**
7131 * Pass the touch screen motion event down to the target view, or this
7132 * view if it is the target.
7133 *
7134 * @param event The motion event to be dispatched.
7135 * @return True if the event was handled by the view, false otherwise.
7136 */
7137 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007138 if (mInputEventConsistencyVerifier != null) {
7139 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7140 }
7141
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007142 if (onFilterTouchEventForSecurity(event)) {
7143 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007144 ListenerInfo li = mListenerInfo;
7145 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7146 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007147 return true;
7148 }
7149
7150 if (onTouchEvent(event)) {
7151 return true;
7152 }
Jeff Brown85a31762010-09-01 17:01:00 -07007153 }
7154
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007155 if (mInputEventConsistencyVerifier != null) {
7156 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007158 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 }
7160
7161 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007162 * Filter the touch event to apply security policies.
7163 *
7164 * @param event The motion event to be filtered.
7165 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007166 *
Jeff Brown85a31762010-09-01 17:01:00 -07007167 * @see #getFilterTouchesWhenObscured
7168 */
7169 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007170 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007171 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7172 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7173 // Window is obscured, drop this touch.
7174 return false;
7175 }
7176 return true;
7177 }
7178
7179 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 * Pass a trackball motion event down to the focused view.
7181 *
7182 * @param event The motion event to be dispatched.
7183 * @return True if the event was handled by the view, false otherwise.
7184 */
7185 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007186 if (mInputEventConsistencyVerifier != null) {
7187 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7188 }
7189
Romain Guy02ccac62011-06-24 13:20:23 -07007190 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 }
7192
7193 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007194 * Dispatch a generic motion event.
7195 * <p>
7196 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7197 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007198 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007199 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007200 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007201 *
7202 * @param event The motion event to be dispatched.
7203 * @return True if the event was handled by the view, false otherwise.
7204 */
7205 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007206 if (mInputEventConsistencyVerifier != null) {
7207 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7208 }
7209
Jeff Browna032cc02011-03-07 16:56:21 -08007210 final int source = event.getSource();
7211 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7212 final int action = event.getAction();
7213 if (action == MotionEvent.ACTION_HOVER_ENTER
7214 || action == MotionEvent.ACTION_HOVER_MOVE
7215 || action == MotionEvent.ACTION_HOVER_EXIT) {
7216 if (dispatchHoverEvent(event)) {
7217 return true;
7218 }
7219 } else if (dispatchGenericPointerEvent(event)) {
7220 return true;
7221 }
7222 } else if (dispatchGenericFocusedEvent(event)) {
7223 return true;
7224 }
7225
Jeff Brown10b62902011-06-20 16:40:37 -07007226 if (dispatchGenericMotionEventInternal(event)) {
7227 return true;
7228 }
7229
7230 if (mInputEventConsistencyVerifier != null) {
7231 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7232 }
7233 return false;
7234 }
7235
7236 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007237 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007238 ListenerInfo li = mListenerInfo;
7239 if (li != null && li.mOnGenericMotionListener != null
7240 && (mViewFlags & ENABLED_MASK) == ENABLED
7241 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007242 return true;
7243 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007244
7245 if (onGenericMotionEvent(event)) {
7246 return true;
7247 }
7248
7249 if (mInputEventConsistencyVerifier != null) {
7250 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7251 }
7252 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007253 }
7254
7255 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007256 * Dispatch a hover event.
7257 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007258 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007259 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007260 * </p>
7261 *
7262 * @param event The motion event to be dispatched.
7263 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007264 */
7265 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007266 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007267 ListenerInfo li = mListenerInfo;
7268 if (li != null && li.mOnHoverListener != null
7269 && (mViewFlags & ENABLED_MASK) == ENABLED
7270 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007271 return true;
7272 }
7273
Jeff Browna032cc02011-03-07 16:56:21 -08007274 return onHoverEvent(event);
7275 }
7276
7277 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007278 * Returns true if the view has a child to which it has recently sent
7279 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7280 * it does not have a hovered child, then it must be the innermost hovered view.
7281 * @hide
7282 */
7283 protected boolean hasHoveredChild() {
7284 return false;
7285 }
7286
7287 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007288 * Dispatch a generic motion event to the view under the first pointer.
7289 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007290 * Do not call this method directly.
7291 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007292 * </p>
7293 *
7294 * @param event The motion event to be dispatched.
7295 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007296 */
7297 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7298 return false;
7299 }
7300
7301 /**
7302 * Dispatch a generic motion event to the currently focused view.
7303 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007304 * Do not call this method directly.
7305 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007306 * </p>
7307 *
7308 * @param event The motion event to be dispatched.
7309 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007310 */
7311 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7312 return false;
7313 }
7314
7315 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007316 * Dispatch a pointer event.
7317 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007318 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7319 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7320 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007321 * and should not be expected to handle other pointing device features.
7322 * </p>
7323 *
7324 * @param event The motion event to be dispatched.
7325 * @return True if the event was handled by the view, false otherwise.
7326 * @hide
7327 */
7328 public final boolean dispatchPointerEvent(MotionEvent event) {
7329 if (event.isTouchEvent()) {
7330 return dispatchTouchEvent(event);
7331 } else {
7332 return dispatchGenericMotionEvent(event);
7333 }
7334 }
7335
7336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 * Called when the window containing this view gains or loses window focus.
7338 * ViewGroups should override to route to their children.
7339 *
7340 * @param hasFocus True if the window containing this view now has focus,
7341 * false otherwise.
7342 */
7343 public void dispatchWindowFocusChanged(boolean hasFocus) {
7344 onWindowFocusChanged(hasFocus);
7345 }
7346
7347 /**
7348 * Called when the window containing this view gains or loses focus. Note
7349 * that this is separate from view focus: to receive key events, both
7350 * your view and its window must have focus. If a window is displayed
7351 * on top of yours that takes input focus, then your own window will lose
7352 * focus but the view focus will remain unchanged.
7353 *
7354 * @param hasWindowFocus True if the window containing this view now has
7355 * focus, false otherwise.
7356 */
7357 public void onWindowFocusChanged(boolean hasWindowFocus) {
7358 InputMethodManager imm = InputMethodManager.peekInstance();
7359 if (!hasWindowFocus) {
7360 if (isPressed()) {
7361 setPressed(false);
7362 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007363 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 imm.focusOut(this);
7365 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007366 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007367 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007368 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007369 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 imm.focusIn(this);
7371 }
7372 refreshDrawableState();
7373 }
7374
7375 /**
7376 * Returns true if this view is in a window that currently has window focus.
7377 * Note that this is not the same as the view itself having focus.
7378 *
7379 * @return True if this view is in a window that currently has window focus.
7380 */
7381 public boolean hasWindowFocus() {
7382 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7383 }
7384
7385 /**
Adam Powell326d8082009-12-09 15:10:07 -08007386 * Dispatch a view visibility change down the view hierarchy.
7387 * ViewGroups should override to route to their children.
7388 * @param changedView The view whose visibility changed. Could be 'this' or
7389 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007390 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7391 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007392 */
7393 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7394 onVisibilityChanged(changedView, visibility);
7395 }
7396
7397 /**
7398 * Called when the visibility of the view or an ancestor of the view is changed.
7399 * @param changedView The view whose visibility changed. Could be 'this' or
7400 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007401 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7402 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007403 */
7404 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007405 if (visibility == VISIBLE) {
7406 if (mAttachInfo != null) {
7407 initialAwakenScrollBars();
7408 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007409 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007410 }
7411 }
Adam Powell326d8082009-12-09 15:10:07 -08007412 }
7413
7414 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007415 * Dispatch a hint about whether this view is displayed. For instance, when
7416 * a View moves out of the screen, it might receives a display hint indicating
7417 * the view is not displayed. Applications should not <em>rely</em> on this hint
7418 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007419 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007420 * @param hint A hint about whether or not this view is displayed:
7421 * {@link #VISIBLE} or {@link #INVISIBLE}.
7422 */
7423 public void dispatchDisplayHint(int hint) {
7424 onDisplayHint(hint);
7425 }
7426
7427 /**
7428 * Gives this view a hint about whether is displayed or not. For instance, when
7429 * a View moves out of the screen, it might receives a display hint indicating
7430 * the view is not displayed. Applications should not <em>rely</em> on this hint
7431 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007432 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007433 * @param hint A hint about whether or not this view is displayed:
7434 * {@link #VISIBLE} or {@link #INVISIBLE}.
7435 */
7436 protected void onDisplayHint(int hint) {
7437 }
7438
7439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 * Dispatch a window visibility change down the view hierarchy.
7441 * ViewGroups should override to route to their children.
7442 *
7443 * @param visibility The new visibility of the window.
7444 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007445 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 */
7447 public void dispatchWindowVisibilityChanged(int visibility) {
7448 onWindowVisibilityChanged(visibility);
7449 }
7450
7451 /**
7452 * Called when the window containing has change its visibility
7453 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7454 * that this tells you whether or not your window is being made visible
7455 * to the window manager; this does <em>not</em> tell you whether or not
7456 * your window is obscured by other windows on the screen, even if it
7457 * is itself visible.
7458 *
7459 * @param visibility The new visibility of the window.
7460 */
7461 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007462 if (visibility == VISIBLE) {
7463 initialAwakenScrollBars();
7464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 }
7466
7467 /**
7468 * Returns the current visibility of the window this view is attached to
7469 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7470 *
7471 * @return Returns the current visibility of the view's window.
7472 */
7473 public int getWindowVisibility() {
7474 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7475 }
7476
7477 /**
7478 * Retrieve the overall visible display size in which the window this view is
7479 * attached to has been positioned in. This takes into account screen
7480 * decorations above the window, for both cases where the window itself
7481 * is being position inside of them or the window is being placed under
7482 * then and covered insets are used for the window to position its content
7483 * inside. In effect, this tells you the available area where content can
7484 * be placed and remain visible to users.
7485 *
7486 * <p>This function requires an IPC back to the window manager to retrieve
7487 * the requested information, so should not be used in performance critical
7488 * code like drawing.
7489 *
7490 * @param outRect Filled in with the visible display frame. If the view
7491 * is not attached to a window, this is simply the raw display size.
7492 */
7493 public void getWindowVisibleDisplayFrame(Rect outRect) {
7494 if (mAttachInfo != null) {
7495 try {
7496 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7497 } catch (RemoteException e) {
7498 return;
7499 }
7500 // XXX This is really broken, and probably all needs to be done
7501 // in the window manager, and we need to know more about whether
7502 // we want the area behind or in front of the IME.
7503 final Rect insets = mAttachInfo.mVisibleInsets;
7504 outRect.left += insets.left;
7505 outRect.top += insets.top;
7506 outRect.right -= insets.right;
7507 outRect.bottom -= insets.bottom;
7508 return;
7509 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007510 // The view is not attached to a display so we don't have a context.
7511 // Make a best guess about the display size.
7512 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007513 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 }
7515
7516 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007517 * Dispatch a notification about a resource configuration change down
7518 * the view hierarchy.
7519 * ViewGroups should override to route to their children.
7520 *
7521 * @param newConfig The new resource configuration.
7522 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007523 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007524 */
7525 public void dispatchConfigurationChanged(Configuration newConfig) {
7526 onConfigurationChanged(newConfig);
7527 }
7528
7529 /**
7530 * Called when the current configuration of the resources being used
7531 * by the application have changed. You can use this to decide when
7532 * to reload resources that can changed based on orientation and other
7533 * configuration characterstics. You only need to use this if you are
7534 * not relying on the normal {@link android.app.Activity} mechanism of
7535 * recreating the activity instance upon a configuration change.
7536 *
7537 * @param newConfig The new resource configuration.
7538 */
7539 protected void onConfigurationChanged(Configuration newConfig) {
7540 }
7541
7542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 * Private function to aggregate all per-view attributes in to the view
7544 * root.
7545 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007546 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7547 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007548 }
7549
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007550 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7551 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007552 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007553 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007554 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007555 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007556 ListenerInfo li = mListenerInfo;
7557 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007558 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 }
7561 }
7562
7563 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007564 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007565 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007566 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7567 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 ai.mRecomputeGlobalAttributes = true;
7569 }
7570 }
7571 }
7572
7573 /**
7574 * Returns whether the device is currently in touch mode. Touch mode is entered
7575 * once the user begins interacting with the device by touch, and affects various
7576 * things like whether focus is always visible to the user.
7577 *
7578 * @return Whether the device is in touch mode.
7579 */
7580 @ViewDebug.ExportedProperty
7581 public boolean isInTouchMode() {
7582 if (mAttachInfo != null) {
7583 return mAttachInfo.mInTouchMode;
7584 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007585 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586 }
7587 }
7588
7589 /**
7590 * Returns the context the view is running in, through which it can
7591 * access the current theme, resources, etc.
7592 *
7593 * @return The view's Context.
7594 */
7595 @ViewDebug.CapturedViewProperty
7596 public final Context getContext() {
7597 return mContext;
7598 }
7599
7600 /**
7601 * Handle a key event before it is processed by any input method
7602 * associated with the view hierarchy. This can be used to intercept
7603 * key events in special situations before the IME consumes them; a
7604 * typical example would be handling the BACK key to update the application's
7605 * UI instead of allowing the IME to see it and close itself.
7606 *
7607 * @param keyCode The value in event.getKeyCode().
7608 * @param event Description of the key event.
7609 * @return If you handled the event, return true. If you want to allow the
7610 * event to be handled by the next receiver, return false.
7611 */
7612 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7613 return false;
7614 }
7615
7616 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007617 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7618 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7620 * is released, if the view is enabled and clickable.
7621 *
Jean Chalard405bc512012-05-29 19:12:34 +09007622 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7623 * although some may elect to do so in some situations. Do not rely on this to
7624 * catch software key presses.
7625 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 * @param keyCode A key code that represents the button pressed, from
7627 * {@link android.view.KeyEvent}.
7628 * @param event The KeyEvent object that defines the button action.
7629 */
7630 public boolean onKeyDown(int keyCode, KeyEvent event) {
7631 boolean result = false;
7632
7633 switch (keyCode) {
7634 case KeyEvent.KEYCODE_DPAD_CENTER:
7635 case KeyEvent.KEYCODE_ENTER: {
7636 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7637 return true;
7638 }
7639 // Long clickable items don't necessarily have to be clickable
7640 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7641 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7642 (event.getRepeatCount() == 0)) {
7643 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007644 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007645 return true;
7646 }
7647 break;
7648 }
7649 }
7650 return result;
7651 }
7652
7653 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007654 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7655 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7656 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007657 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7658 * although some may elect to do so in some situations. Do not rely on this to
7659 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007660 */
7661 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7662 return false;
7663 }
7664
7665 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007666 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7667 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7669 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007670 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7671 * although some may elect to do so in some situations. Do not rely on this to
7672 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 *
7674 * @param keyCode A key code that represents the button pressed, from
7675 * {@link android.view.KeyEvent}.
7676 * @param event The KeyEvent object that defines the button action.
7677 */
7678 public boolean onKeyUp(int keyCode, KeyEvent event) {
7679 boolean result = false;
7680
7681 switch (keyCode) {
7682 case KeyEvent.KEYCODE_DPAD_CENTER:
7683 case KeyEvent.KEYCODE_ENTER: {
7684 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7685 return true;
7686 }
7687 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7688 setPressed(false);
7689
7690 if (!mHasPerformedLongPress) {
7691 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007692 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693
7694 result = performClick();
7695 }
7696 }
7697 break;
7698 }
7699 }
7700 return result;
7701 }
7702
7703 /**
7704 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7705 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7706 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007707 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7708 * although some may elect to do so in some situations. Do not rely on this to
7709 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 *
7711 * @param keyCode A key code that represents the button pressed, from
7712 * {@link android.view.KeyEvent}.
7713 * @param repeatCount The number of times the action was made.
7714 * @param event The KeyEvent object that defines the button action.
7715 */
7716 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7717 return false;
7718 }
7719
7720 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007721 * Called on the focused view when a key shortcut event is not handled.
7722 * Override this method to implement local key shortcuts for the View.
7723 * Key shortcuts can also be implemented by setting the
7724 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 *
7726 * @param keyCode The value in event.getKeyCode().
7727 * @param event Description of the key event.
7728 * @return If you handled the event, return true. If you want to allow the
7729 * event to be handled by the next receiver, return false.
7730 */
7731 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7732 return false;
7733 }
7734
7735 /**
7736 * Check whether the called view is a text editor, in which case it
7737 * would make sense to automatically display a soft input window for
7738 * it. Subclasses should override this if they implement
7739 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007740 * a call on that method would return a non-null InputConnection, and
7741 * they are really a first-class editor that the user would normally
7742 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007743 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007744 * <p>The default implementation always returns false. This does
7745 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7746 * will not be called or the user can not otherwise perform edits on your
7747 * view; it is just a hint to the system that this is not the primary
7748 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007749 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 * @return Returns true if this view is a text editor, else false.
7751 */
7752 public boolean onCheckIsTextEditor() {
7753 return false;
7754 }
Romain Guy8506ab42009-06-11 17:35:47 -07007755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007756 /**
7757 * Create a new InputConnection for an InputMethod to interact
7758 * with the view. The default implementation returns null, since it doesn't
7759 * support input methods. You can override this to implement such support.
7760 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007762 * <p>When implementing this, you probably also want to implement
7763 * {@link #onCheckIsTextEditor()} to indicate you will return a
7764 * non-null InputConnection.
7765 *
7766 * @param outAttrs Fill in with attribute information about the connection.
7767 */
7768 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7769 return null;
7770 }
7771
7772 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007773 * Called by the {@link android.view.inputmethod.InputMethodManager}
7774 * when a view who is not the current
7775 * input connection target is trying to make a call on the manager. The
7776 * default implementation returns false; you can override this to return
7777 * true for certain views if you are performing InputConnection proxying
7778 * to them.
7779 * @param view The View that is making the InputMethodManager call.
7780 * @return Return true to allow the call, false to reject.
7781 */
7782 public boolean checkInputConnectionProxy(View view) {
7783 return false;
7784 }
Romain Guy8506ab42009-06-11 17:35:47 -07007785
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 * Show the context menu for this view. It is not safe to hold on to the
7788 * menu after returning from this method.
7789 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007790 * You should normally not overload this method. Overload
7791 * {@link #onCreateContextMenu(ContextMenu)} or define an
7792 * {@link OnCreateContextMenuListener} to add items to the context menu.
7793 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 * @param menu The context menu to populate
7795 */
7796 public void createContextMenu(ContextMenu menu) {
7797 ContextMenuInfo menuInfo = getContextMenuInfo();
7798
7799 // Sets the current menu info so all items added to menu will have
7800 // my extra info set.
7801 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7802
7803 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007804 ListenerInfo li = mListenerInfo;
7805 if (li != null && li.mOnCreateContextMenuListener != null) {
7806 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 }
7808
7809 // Clear the extra information so subsequent items that aren't mine don't
7810 // have my extra info.
7811 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7812
7813 if (mParent != null) {
7814 mParent.createContextMenu(menu);
7815 }
7816 }
7817
7818 /**
7819 * Views should implement this if they have extra information to associate
7820 * with the context menu. The return result is supplied as a parameter to
7821 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7822 * callback.
7823 *
7824 * @return Extra information about the item for which the context menu
7825 * should be shown. This information will vary across different
7826 * subclasses of View.
7827 */
7828 protected ContextMenuInfo getContextMenuInfo() {
7829 return null;
7830 }
7831
7832 /**
7833 * Views should implement this if the view itself is going to add items to
7834 * the context menu.
7835 *
7836 * @param menu the context menu to populate
7837 */
7838 protected void onCreateContextMenu(ContextMenu menu) {
7839 }
7840
7841 /**
7842 * Implement this method to handle trackball motion events. The
7843 * <em>relative</em> movement of the trackball since the last event
7844 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7845 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7846 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7847 * they will often be fractional values, representing the more fine-grained
7848 * movement information available from a trackball).
7849 *
7850 * @param event The motion event.
7851 * @return True if the event was handled, false otherwise.
7852 */
7853 public boolean onTrackballEvent(MotionEvent event) {
7854 return false;
7855 }
7856
7857 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007858 * Implement this method to handle generic motion events.
7859 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007860 * Generic motion events describe joystick movements, mouse hovers, track pad
7861 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007862 * {@link MotionEvent#getSource() source} of the motion event specifies
7863 * the class of input that was received. Implementations of this method
7864 * must examine the bits in the source before processing the event.
7865 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007866 * </p><p>
7867 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7868 * are delivered to the view under the pointer. All other generic motion events are
7869 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007870 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007871 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007872 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007873 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7874 * // process the joystick movement...
7875 * return true;
7876 * }
7877 * }
7878 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7879 * switch (event.getAction()) {
7880 * case MotionEvent.ACTION_HOVER_MOVE:
7881 * // process the mouse hover movement...
7882 * return true;
7883 * case MotionEvent.ACTION_SCROLL:
7884 * // process the scroll wheel movement...
7885 * return true;
7886 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007887 * }
7888 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007889 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007890 *
7891 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007892 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007893 */
7894 public boolean onGenericMotionEvent(MotionEvent event) {
7895 return false;
7896 }
7897
7898 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007899 * Implement this method to handle hover events.
7900 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007901 * This method is called whenever a pointer is hovering into, over, or out of the
7902 * bounds of a view and the view is not currently being touched.
7903 * Hover events are represented as pointer events with action
7904 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7905 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7906 * </p>
7907 * <ul>
7908 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7909 * when the pointer enters the bounds of the view.</li>
7910 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7911 * when the pointer has already entered the bounds of the view and has moved.</li>
7912 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7913 * when the pointer has exited the bounds of the view or when the pointer is
7914 * about to go down due to a button click, tap, or similar user action that
7915 * causes the view to be touched.</li>
7916 * </ul>
7917 * <p>
7918 * The view should implement this method to return true to indicate that it is
7919 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007920 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007921 * The default implementation calls {@link #setHovered} to update the hovered state
7922 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007923 * is enabled and is clickable. The default implementation also sends hover
7924 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007925 * </p>
7926 *
7927 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007928 * @return True if the view handled the hover event.
7929 *
7930 * @see #isHovered
7931 * @see #setHovered
7932 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007933 */
7934 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007935 // The root view may receive hover (or touch) events that are outside the bounds of
7936 // the window. This code ensures that we only send accessibility events for
7937 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007938 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007939 if (!mSendingHoverAccessibilityEvents) {
7940 if ((action == MotionEvent.ACTION_HOVER_ENTER
7941 || action == MotionEvent.ACTION_HOVER_MOVE)
7942 && !hasHoveredChild()
7943 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007944 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007945 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007946 }
7947 } else {
7948 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007949 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007950 && !pointInView(event.getX(), event.getY()))) {
7951 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007952 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007953 // If the window does not have input focus we take away accessibility
7954 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007955 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07007956 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007957 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007958 }
Jeff Browna1b24182011-07-28 13:38:24 -07007959 }
7960
Jeff Brown87b7f802011-06-21 18:35:45 -07007961 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007962 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007963 case MotionEvent.ACTION_HOVER_ENTER:
7964 setHovered(true);
7965 break;
7966 case MotionEvent.ACTION_HOVER_EXIT:
7967 setHovered(false);
7968 break;
7969 }
Jeff Browna1b24182011-07-28 13:38:24 -07007970
7971 // Dispatch the event to onGenericMotionEvent before returning true.
7972 // This is to provide compatibility with existing applications that
7973 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7974 // break because of the new default handling for hoverable views
7975 // in onHoverEvent.
7976 // Note that onGenericMotionEvent will be called by default when
7977 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7978 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007979 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007980 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007981
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007982 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007983 }
7984
7985 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007986 * Returns true if the view should handle {@link #onHoverEvent}
7987 * by calling {@link #setHovered} to change its hovered state.
7988 *
7989 * @return True if the view is hoverable.
7990 */
7991 private boolean isHoverable() {
7992 final int viewFlags = mViewFlags;
7993 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7994 return false;
7995 }
7996
7997 return (viewFlags & CLICKABLE) == CLICKABLE
7998 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7999 }
8000
8001 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008002 * Returns true if the view is currently hovered.
8003 *
8004 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008005 *
8006 * @see #setHovered
8007 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008008 */
Jeff Brown10b62902011-06-20 16:40:37 -07008009 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008010 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008011 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008012 }
8013
8014 /**
8015 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008016 * <p>
8017 * Calling this method also changes the drawable state of the view. This
8018 * enables the view to react to hover by using different drawable resources
8019 * to change its appearance.
8020 * </p><p>
8021 * The {@link #onHoverChanged} method is called when the hovered state changes.
8022 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008023 *
8024 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008025 *
8026 * @see #isHovered
8027 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008028 */
8029 public void setHovered(boolean hovered) {
8030 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008031 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8032 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008033 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008034 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008035 }
8036 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008037 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8038 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008039 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008040 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008041 }
8042 }
8043 }
8044
8045 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008046 * Implement this method to handle hover state changes.
8047 * <p>
8048 * This method is called whenever the hover state changes as a result of a
8049 * call to {@link #setHovered}.
8050 * </p>
8051 *
8052 * @param hovered The current hover state, as returned by {@link #isHovered}.
8053 *
8054 * @see #isHovered
8055 * @see #setHovered
8056 */
8057 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008058 }
8059
8060 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 * Implement this method to handle touch screen motion events.
8062 *
8063 * @param event The motion event.
8064 * @return True if the event was handled, false otherwise.
8065 */
8066 public boolean onTouchEvent(MotionEvent event) {
8067 final int viewFlags = mViewFlags;
8068
8069 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008070 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008071 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 // A disabled view that is clickable still consumes the touch
8074 // events, it just doesn't respond to them.
8075 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8076 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8077 }
8078
8079 if (mTouchDelegate != null) {
8080 if (mTouchDelegate.onTouchEvent(event)) {
8081 return true;
8082 }
8083 }
8084
8085 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8086 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8087 switch (event.getAction()) {
8088 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008089 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8090 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 // take focus if we don't have it already and we should in
8092 // touch mode.
8093 boolean focusTaken = false;
8094 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8095 focusTaken = requestFocus();
8096 }
8097
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008098 if (prepressed) {
8099 // The button is being released before we actually
8100 // showed it as pressed. Make it show the pressed
8101 // state now (before scheduling the click) to ensure
8102 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008103 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008104 }
Joe Malin32736f02011-01-19 16:14:20 -08008105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 if (!mHasPerformedLongPress) {
8107 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008108 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109
8110 // Only perform take click actions if we were in the pressed state
8111 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008112 // Use a Runnable and post this rather than calling
8113 // performClick directly. This lets other visual state
8114 // of the view update before click actions start.
8115 if (mPerformClick == null) {
8116 mPerformClick = new PerformClick();
8117 }
8118 if (!post(mPerformClick)) {
8119 performClick();
8120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122 }
8123
8124 if (mUnsetPressedState == null) {
8125 mUnsetPressedState = new UnsetPressedState();
8126 }
8127
Adam Powelle14579b2009-12-16 18:39:52 -08008128 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008129 postDelayed(mUnsetPressedState,
8130 ViewConfiguration.getPressedStateDuration());
8131 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 // If the post failed, unpress right now
8133 mUnsetPressedState.run();
8134 }
Adam Powelle14579b2009-12-16 18:39:52 -08008135 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008136 }
8137 break;
8138
8139 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008140 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008141
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008142 if (performButtonActionOnTouchDown(event)) {
8143 break;
8144 }
8145
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008146 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008147 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008148
8149 // For views inside a scrolling container, delay the pressed feedback for
8150 // a short period in case this is a scroll.
8151 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008152 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008153 if (mPendingCheckForTap == null) {
8154 mPendingCheckForTap = new CheckForTap();
8155 }
8156 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8157 } else {
8158 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008159 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008160 checkForLongClick(0);
8161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 break;
8163
8164 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008165 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008166 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008167 break;
8168
8169 case MotionEvent.ACTION_MOVE:
8170 final int x = (int) event.getX();
8171 final int y = (int) event.getY();
8172
8173 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008174 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008176 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008177 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008178 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008179 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180
Adam Powell4d6f0662012-02-21 15:11:11 -08008181 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 }
8184 break;
8185 }
8186 return true;
8187 }
8188
8189 return false;
8190 }
8191
8192 /**
Adam Powell10298662011-08-14 18:26:30 -07008193 * @hide
8194 */
8195 public boolean isInScrollingContainer() {
8196 ViewParent p = getParent();
8197 while (p != null && p instanceof ViewGroup) {
8198 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8199 return true;
8200 }
8201 p = p.getParent();
8202 }
8203 return false;
8204 }
8205
8206 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008207 * Remove the longpress detection timer.
8208 */
8209 private void removeLongPressCallback() {
8210 if (mPendingCheckForLongPress != null) {
8211 removeCallbacks(mPendingCheckForLongPress);
8212 }
8213 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008214
8215 /**
8216 * Remove the pending click action
8217 */
8218 private void removePerformClickCallback() {
8219 if (mPerformClick != null) {
8220 removeCallbacks(mPerformClick);
8221 }
8222 }
8223
Adam Powelle14579b2009-12-16 18:39:52 -08008224 /**
Romain Guya440b002010-02-24 15:57:54 -08008225 * Remove the prepress detection timer.
8226 */
8227 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008228 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008229 setPressed(false);
8230 removeCallbacks(mUnsetPressedState);
8231 }
8232 }
8233
8234 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008235 * Remove the tap detection timer.
8236 */
8237 private void removeTapCallback() {
8238 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008239 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008240 removeCallbacks(mPendingCheckForTap);
8241 }
8242 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008243
8244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 * Cancels a pending long press. Your subclass can use this if you
8246 * want the context menu to come up if the user presses and holds
8247 * at the same place, but you don't want it to come up if they press
8248 * and then move around enough to cause scrolling.
8249 */
8250 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008251 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008252
8253 /*
8254 * The prepressed state handled by the tap callback is a display
8255 * construct, but the tap callback will post a long press callback
8256 * less its own timeout. Remove it here.
8257 */
8258 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 }
8260
8261 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008262 * Remove the pending callback for sending a
8263 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8264 */
8265 private void removeSendViewScrolledAccessibilityEventCallback() {
8266 if (mSendViewScrolledAccessibilityEvent != null) {
8267 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008268 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008269 }
8270 }
8271
8272 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 * Sets the TouchDelegate for this View.
8274 */
8275 public void setTouchDelegate(TouchDelegate delegate) {
8276 mTouchDelegate = delegate;
8277 }
8278
8279 /**
8280 * Gets the TouchDelegate for this View.
8281 */
8282 public TouchDelegate getTouchDelegate() {
8283 return mTouchDelegate;
8284 }
8285
8286 /**
8287 * Set flags controlling behavior of this view.
8288 *
8289 * @param flags Constant indicating the value which should be set
8290 * @param mask Constant indicating the bit range that should be changed
8291 */
8292 void setFlags(int flags, int mask) {
8293 int old = mViewFlags;
8294 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8295
8296 int changed = mViewFlags ^ old;
8297 if (changed == 0) {
8298 return;
8299 }
8300 int privateFlags = mPrivateFlags;
8301
8302 /* Check if the FOCUSABLE bit has changed */
8303 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008304 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008306 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 /* Give up focus if we are no longer focusable */
8308 clearFocus();
8309 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008310 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 /*
8312 * Tell the view system that we are now available to take focus
8313 * if no one else already has it.
8314 */
8315 if (mParent != null) mParent.focusableViewAvailable(this);
8316 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008317 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8318 notifyAccessibilityStateChanged();
8319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 }
8321
8322 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8323 if ((changed & VISIBILITY_MASK) != 0) {
8324 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008325 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008326 * it was not visible. Marking it drawn ensures that the invalidation will
8327 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008329 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008330 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331
8332 needGlobalAttributesUpdate(true);
8333
8334 // a view becoming visible is worth notifying the parent
8335 // about in case nothing has focus. even if this specific view
8336 // isn't focusable, it may contain something that is, so let
8337 // the root view try to give this focus if nothing else does.
8338 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8339 mParent.focusableViewAvailable(this);
8340 }
8341 }
8342 }
8343
8344 /* Check if the GONE bit has changed */
8345 if ((changed & GONE) != 0) {
8346 needGlobalAttributesUpdate(false);
8347 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348
Romain Guyecd80ee2009-12-03 17:13:02 -08008349 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8350 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008351 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008352 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008353 if (mParent instanceof View) {
8354 // GONE views noop invalidation, so invalidate the parent
8355 ((View) mParent).invalidate(true);
8356 }
8357 // Mark the view drawn to ensure that it gets invalidated properly the next
8358 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008359 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 }
8361 if (mAttachInfo != null) {
8362 mAttachInfo.mViewVisibilityChanged = true;
8363 }
8364 }
8365
8366 /* Check if the VISIBLE bit has changed */
8367 if ((changed & INVISIBLE) != 0) {
8368 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008369 /*
8370 * If this view is becoming invisible, set the DRAWN flag so that
8371 * the next invalidate() will not be skipped.
8372 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008373 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008374
8375 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008376 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008377 if (getRootView() != this) {
8378 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008379 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 }
8381 }
8382 if (mAttachInfo != null) {
8383 mAttachInfo.mViewVisibilityChanged = true;
8384 }
8385 }
8386
Adam Powell326d8082009-12-09 15:10:07 -08008387 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008388 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008389 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8390 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008391 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008392 } else if (mParent != null) {
8393 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008394 }
Adam Powell326d8082009-12-09 15:10:07 -08008395 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8396 }
8397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8399 destroyDrawingCache();
8400 }
8401
8402 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8403 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008404 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008405 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008406 }
8407
8408 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8409 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008410 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 }
8412
8413 if ((changed & DRAW_MASK) != 0) {
8414 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008415 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008416 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8417 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008418 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008419 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 }
8421 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008422 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 }
8424 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008425 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008426 }
8427
8428 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008429 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 mParent.recomputeViewAttributes(this);
8431 }
8432 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008433
8434 if (AccessibilityManager.getInstance(mContext).isEnabled()
8435 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8436 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8437 notifyAccessibilityStateChanged();
8438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 }
8440
8441 /**
8442 * Change the view's z order in the tree, so it's on top of other sibling
8443 * views
8444 */
8445 public void bringToFront() {
8446 if (mParent != null) {
8447 mParent.bringChildToFront(this);
8448 }
8449 }
8450
8451 /**
8452 * This is called in response to an internal scroll in this view (i.e., the
8453 * view scrolled its own contents). This is typically as a result of
8454 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8455 * called.
8456 *
8457 * @param l Current horizontal scroll origin.
8458 * @param t Current vertical scroll origin.
8459 * @param oldl Previous horizontal scroll origin.
8460 * @param oldt Previous vertical scroll origin.
8461 */
8462 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008463 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8464 postSendViewScrolledAccessibilityEventCallback();
8465 }
8466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008467 mBackgroundSizeChanged = true;
8468
8469 final AttachInfo ai = mAttachInfo;
8470 if (ai != null) {
8471 ai.mViewScrollChanged = true;
8472 }
8473 }
8474
8475 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008476 * Interface definition for a callback to be invoked when the layout bounds of a view
8477 * changes due to layout processing.
8478 */
8479 public interface OnLayoutChangeListener {
8480 /**
8481 * Called when the focus state of a view has changed.
8482 *
8483 * @param v The view whose state has changed.
8484 * @param left The new value of the view's left property.
8485 * @param top The new value of the view's top property.
8486 * @param right The new value of the view's right property.
8487 * @param bottom The new value of the view's bottom property.
8488 * @param oldLeft The previous value of the view's left property.
8489 * @param oldTop The previous value of the view's top property.
8490 * @param oldRight The previous value of the view's right property.
8491 * @param oldBottom The previous value of the view's bottom property.
8492 */
8493 void onLayoutChange(View v, int left, int top, int right, int bottom,
8494 int oldLeft, int oldTop, int oldRight, int oldBottom);
8495 }
8496
8497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 * This is called during layout when the size of this view has changed. If
8499 * you were just added to the view hierarchy, you're called with the old
8500 * values of 0.
8501 *
8502 * @param w Current width of this view.
8503 * @param h Current height of this view.
8504 * @param oldw Old width of this view.
8505 * @param oldh Old height of this view.
8506 */
8507 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8508 }
8509
8510 /**
8511 * Called by draw to draw the child views. This may be overridden
8512 * by derived classes to gain control just before its children are drawn
8513 * (but after its own view has been drawn).
8514 * @param canvas the canvas on which to draw the view
8515 */
8516 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 }
8519
8520 /**
8521 * Gets the parent of this view. Note that the parent is a
8522 * ViewParent and not necessarily a View.
8523 *
8524 * @return Parent of this view.
8525 */
8526 public final ViewParent getParent() {
8527 return mParent;
8528 }
8529
8530 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008531 * Set the horizontal scrolled position of your view. This will cause a call to
8532 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8533 * invalidated.
8534 * @param value the x position to scroll to
8535 */
8536 public void setScrollX(int value) {
8537 scrollTo(value, mScrollY);
8538 }
8539
8540 /**
8541 * Set the vertical scrolled position of your view. This will cause a call to
8542 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8543 * invalidated.
8544 * @param value the y position to scroll to
8545 */
8546 public void setScrollY(int value) {
8547 scrollTo(mScrollX, value);
8548 }
8549
8550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 * Return the scrolled left position of this view. This is the left edge of
8552 * the displayed part of your view. You do not need to draw any pixels
8553 * farther left, since those are outside of the frame of your view on
8554 * screen.
8555 *
8556 * @return The left edge of the displayed part of your view, in pixels.
8557 */
8558 public final int getScrollX() {
8559 return mScrollX;
8560 }
8561
8562 /**
8563 * Return the scrolled top position of this view. This is the top edge of
8564 * the displayed part of your view. You do not need to draw any pixels above
8565 * it, since those are outside of the frame of your view on screen.
8566 *
8567 * @return The top edge of the displayed part of your view, in pixels.
8568 */
8569 public final int getScrollY() {
8570 return mScrollY;
8571 }
8572
8573 /**
8574 * Return the width of the your view.
8575 *
8576 * @return The width of your view, in pixels.
8577 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008578 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 public final int getWidth() {
8580 return mRight - mLeft;
8581 }
8582
8583 /**
8584 * Return the height of your view.
8585 *
8586 * @return The height of your view, in pixels.
8587 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008588 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 public final int getHeight() {
8590 return mBottom - mTop;
8591 }
8592
8593 /**
8594 * Return the visible drawing bounds of your view. Fills in the output
8595 * rectangle with the values from getScrollX(), getScrollY(),
8596 * getWidth(), and getHeight().
8597 *
8598 * @param outRect The (scrolled) drawing bounds of the view.
8599 */
8600 public void getDrawingRect(Rect outRect) {
8601 outRect.left = mScrollX;
8602 outRect.top = mScrollY;
8603 outRect.right = mScrollX + (mRight - mLeft);
8604 outRect.bottom = mScrollY + (mBottom - mTop);
8605 }
8606
8607 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008608 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8609 * raw width component (that is the result is masked by
8610 * {@link #MEASURED_SIZE_MASK}).
8611 *
8612 * @return The raw measured width of this view.
8613 */
8614 public final int getMeasuredWidth() {
8615 return mMeasuredWidth & MEASURED_SIZE_MASK;
8616 }
8617
8618 /**
8619 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008620 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008621 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008622 * This should be used during measurement and layout calculations only. Use
8623 * {@link #getWidth()} to see how wide a view is after layout.
8624 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008625 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008626 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008627 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 return mMeasuredWidth;
8629 }
8630
8631 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008632 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8633 * raw width component (that is the result is masked by
8634 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008635 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008636 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 */
8638 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008639 return mMeasuredHeight & MEASURED_SIZE_MASK;
8640 }
8641
8642 /**
8643 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008644 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008645 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8646 * This should be used during measurement and layout calculations only. Use
8647 * {@link #getHeight()} to see how wide a view is after layout.
8648 *
8649 * @return The measured width of this view as a bit mask.
8650 */
8651 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 return mMeasuredHeight;
8653 }
8654
8655 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008656 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8657 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8658 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8659 * and the height component is at the shifted bits
8660 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8661 */
8662 public final int getMeasuredState() {
8663 return (mMeasuredWidth&MEASURED_STATE_MASK)
8664 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8665 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8666 }
8667
8668 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008669 * The transform matrix of this view, which is calculated based on the current
8670 * roation, scale, and pivot properties.
8671 *
8672 * @see #getRotation()
8673 * @see #getScaleX()
8674 * @see #getScaleY()
8675 * @see #getPivotX()
8676 * @see #getPivotY()
8677 * @return The current transform matrix for the view
8678 */
8679 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008680 if (mTransformationInfo != null) {
8681 updateMatrix();
8682 return mTransformationInfo.mMatrix;
8683 }
8684 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008685 }
8686
8687 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008688 * Utility function to determine if the value is far enough away from zero to be
8689 * considered non-zero.
8690 * @param value A floating point value to check for zero-ness
8691 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8692 */
8693 private static boolean nonzero(float value) {
8694 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8695 }
8696
8697 /**
Jeff Brown86671742010-09-30 20:00:15 -07008698 * Returns true if the transform matrix is the identity matrix.
8699 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008700 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008701 * @return True if the transform matrix is the identity matrix, false otherwise.
8702 */
Jeff Brown86671742010-09-30 20:00:15 -07008703 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008704 if (mTransformationInfo != null) {
8705 updateMatrix();
8706 return mTransformationInfo.mMatrixIsIdentity;
8707 }
8708 return true;
8709 }
8710
8711 void ensureTransformationInfo() {
8712 if (mTransformationInfo == null) {
8713 mTransformationInfo = new TransformationInfo();
8714 }
Jeff Brown86671742010-09-30 20:00:15 -07008715 }
8716
8717 /**
8718 * Recomputes the transform matrix if necessary.
8719 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008720 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008721 final TransformationInfo info = mTransformationInfo;
8722 if (info == null) {
8723 return;
8724 }
8725 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008726 // transform-related properties have changed since the last time someone
8727 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008728
8729 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008730 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008731 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8732 info.mPrevWidth = mRight - mLeft;
8733 info.mPrevHeight = mBottom - mTop;
8734 info.mPivotX = info.mPrevWidth / 2f;
8735 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008736 }
8737 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008738 info.mMatrix.reset();
8739 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8740 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8741 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8742 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008743 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008744 if (info.mCamera == null) {
8745 info.mCamera = new Camera();
8746 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008747 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008748 info.mCamera.save();
8749 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8750 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8751 info.mCamera.getMatrix(info.matrix3D);
8752 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8753 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8754 info.mPivotY + info.mTranslationY);
8755 info.mMatrix.postConcat(info.matrix3D);
8756 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008757 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008758 info.mMatrixDirty = false;
8759 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8760 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008761 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008762 }
8763
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008764 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008765 * Utility method to retrieve the inverse of the current mMatrix property.
8766 * We cache the matrix to avoid recalculating it when transform properties
8767 * have not changed.
8768 *
8769 * @return The inverse of the current matrix of this view.
8770 */
Jeff Brown86671742010-09-30 20:00:15 -07008771 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008772 final TransformationInfo info = mTransformationInfo;
8773 if (info != null) {
8774 updateMatrix();
8775 if (info.mInverseMatrixDirty) {
8776 if (info.mInverseMatrix == null) {
8777 info.mInverseMatrix = new Matrix();
8778 }
8779 info.mMatrix.invert(info.mInverseMatrix);
8780 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008781 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008782 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008783 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008784 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008785 }
8786
8787 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008788 * Gets the distance along the Z axis from the camera to this view.
8789 *
8790 * @see #setCameraDistance(float)
8791 *
8792 * @return The distance along the Z axis.
8793 */
8794 public float getCameraDistance() {
8795 ensureTransformationInfo();
8796 final float dpi = mResources.getDisplayMetrics().densityDpi;
8797 final TransformationInfo info = mTransformationInfo;
8798 if (info.mCamera == null) {
8799 info.mCamera = new Camera();
8800 info.matrix3D = new Matrix();
8801 }
8802 return -(info.mCamera.getLocationZ() * dpi);
8803 }
8804
8805 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008806 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8807 * views are drawn) from the camera to this view. The camera's distance
8808 * affects 3D transformations, for instance rotations around the X and Y
8809 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008810 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008811 * use a camera distance that's greater than the height (X axis rotation) or
8812 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008813 *
Romain Guya5364ee2011-02-24 14:46:04 -08008814 * <p>The distance of the camera from the view plane can have an affect on the
8815 * perspective distortion of the view when it is rotated around the x or y axis.
8816 * For example, a large distance will result in a large viewing angle, and there
8817 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008818 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008819 * also result in some drawing artifacts if the rotated view ends up partially
8820 * behind the camera (which is why the recommendation is to use a distance at
8821 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008822 *
Romain Guya5364ee2011-02-24 14:46:04 -08008823 * <p>The distance is expressed in "depth pixels." The default distance depends
8824 * on the screen density. For instance, on a medium density display, the
8825 * default distance is 1280. On a high density display, the default distance
8826 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008827 *
Romain Guya5364ee2011-02-24 14:46:04 -08008828 * <p>If you want to specify a distance that leads to visually consistent
8829 * results across various densities, use the following formula:</p>
8830 * <pre>
8831 * float scale = context.getResources().getDisplayMetrics().density;
8832 * view.setCameraDistance(distance * scale);
8833 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008834 *
Romain Guya5364ee2011-02-24 14:46:04 -08008835 * <p>The density scale factor of a high density display is 1.5,
8836 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008837 *
Romain Guya5364ee2011-02-24 14:46:04 -08008838 * @param distance The distance in "depth pixels", if negative the opposite
8839 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008840 *
8841 * @see #setRotationX(float)
8842 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008843 */
8844 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008845 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008846
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008847 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008848 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008849 final TransformationInfo info = mTransformationInfo;
8850 if (info.mCamera == null) {
8851 info.mCamera = new Camera();
8852 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008853 }
8854
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008855 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8856 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008857
Chet Haase9d1992d2012-03-13 11:03:25 -07008858 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008859 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008860 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008861 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008862 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008863 // View was rejected last time it was drawn by its parent; this may have changed
8864 invalidateParentIfNeeded();
8865 }
Romain Guya5364ee2011-02-24 14:46:04 -08008866 }
8867
8868 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008869 * The degrees that the view is rotated around the pivot point.
8870 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008871 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008872 * @see #getPivotX()
8873 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008874 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008875 * @return The degrees of rotation.
8876 */
Chet Haasea5531132012-02-02 13:41:44 -08008877 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008878 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008879 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008880 }
8881
8882 /**
Chet Haase897247b2010-09-09 14:54:47 -07008883 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8884 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008885 *
8886 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008887 *
8888 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008889 * @see #getPivotX()
8890 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008891 * @see #setRotationX(float)
8892 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008893 *
8894 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008895 */
8896 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 ensureTransformationInfo();
8898 final TransformationInfo info = mTransformationInfo;
8899 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008900 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008901 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008902 info.mRotation = rotation;
8903 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008904 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008905 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008906 mDisplayList.setRotation(rotation);
8907 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008908 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008909 // View was rejected last time it was drawn by its parent; this may have changed
8910 invalidateParentIfNeeded();
8911 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008912 }
8913 }
8914
8915 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008916 * The degrees that the view is rotated around the vertical axis through the pivot point.
8917 *
8918 * @see #getPivotX()
8919 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008920 * @see #setRotationY(float)
8921 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008922 * @return The degrees of Y rotation.
8923 */
Chet Haasea5531132012-02-02 13:41:44 -08008924 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008925 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008926 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008927 }
8928
8929 /**
Chet Haase897247b2010-09-09 14:54:47 -07008930 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8931 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8932 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008933 *
Romain Guya5364ee2011-02-24 14:46:04 -08008934 * When rotating large views, it is recommended to adjust the camera distance
8935 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008936 *
8937 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008938 *
8939 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008940 * @see #getPivotX()
8941 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008942 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008943 * @see #setRotationX(float)
8944 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008945 *
8946 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008947 */
8948 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008949 ensureTransformationInfo();
8950 final TransformationInfo info = mTransformationInfo;
8951 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008952 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008953 info.mRotationY = rotationY;
8954 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008955 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008956 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008957 mDisplayList.setRotationY(rotationY);
8958 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008959 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008960 // View was rejected last time it was drawn by its parent; this may have changed
8961 invalidateParentIfNeeded();
8962 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008963 }
8964 }
8965
8966 /**
8967 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8968 *
8969 * @see #getPivotX()
8970 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008971 * @see #setRotationX(float)
8972 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008973 * @return The degrees of X rotation.
8974 */
Chet Haasea5531132012-02-02 13:41:44 -08008975 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008976 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008977 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008978 }
8979
8980 /**
Chet Haase897247b2010-09-09 14:54:47 -07008981 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8982 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8983 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008984 *
Romain Guya5364ee2011-02-24 14:46:04 -08008985 * When rotating large views, it is recommended to adjust the camera distance
8986 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008987 *
8988 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008989 *
8990 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008991 * @see #getPivotX()
8992 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008993 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008994 * @see #setRotationY(float)
8995 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008996 *
8997 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008998 */
8999 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009000 ensureTransformationInfo();
9001 final TransformationInfo info = mTransformationInfo;
9002 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009003 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009004 info.mRotationX = rotationX;
9005 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009006 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009007 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009008 mDisplayList.setRotationX(rotationX);
9009 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009010 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009011 // View was rejected last time it was drawn by its parent; this may have changed
9012 invalidateParentIfNeeded();
9013 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009014 }
9015 }
9016
9017 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009018 * The amount that the view is scaled in x around the pivot point, as a proportion of
9019 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9020 *
Joe Onorato93162322010-09-16 15:42:01 -04009021 * <p>By default, this is 1.0f.
9022 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009023 * @see #getPivotX()
9024 * @see #getPivotY()
9025 * @return The scaling factor.
9026 */
Chet Haasea5531132012-02-02 13:41:44 -08009027 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009028 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009029 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009030 }
9031
9032 /**
9033 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9034 * the view's unscaled width. A value of 1 means that no scaling is applied.
9035 *
9036 * @param scaleX The scaling factor.
9037 * @see #getPivotX()
9038 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009039 *
9040 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009041 */
9042 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009043 ensureTransformationInfo();
9044 final TransformationInfo info = mTransformationInfo;
9045 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009046 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009047 info.mScaleX = scaleX;
9048 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009049 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009050 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009051 mDisplayList.setScaleX(scaleX);
9052 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009053 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009054 // View was rejected last time it was drawn by its parent; this may have changed
9055 invalidateParentIfNeeded();
9056 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009057 }
9058 }
9059
9060 /**
9061 * The amount that the view is scaled in y around the pivot point, as a proportion of
9062 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9063 *
Joe Onorato93162322010-09-16 15:42:01 -04009064 * <p>By default, this is 1.0f.
9065 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009066 * @see #getPivotX()
9067 * @see #getPivotY()
9068 * @return The scaling factor.
9069 */
Chet Haasea5531132012-02-02 13:41:44 -08009070 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009071 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009072 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009073 }
9074
9075 /**
9076 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9077 * the view's unscaled width. A value of 1 means that no scaling is applied.
9078 *
9079 * @param scaleY The scaling factor.
9080 * @see #getPivotX()
9081 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009082 *
9083 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009084 */
9085 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009086 ensureTransformationInfo();
9087 final TransformationInfo info = mTransformationInfo;
9088 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009089 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009090 info.mScaleY = scaleY;
9091 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009092 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009093 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009094 mDisplayList.setScaleY(scaleY);
9095 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009096 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009097 // View was rejected last time it was drawn by its parent; this may have changed
9098 invalidateParentIfNeeded();
9099 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009100 }
9101 }
9102
9103 /**
9104 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9105 * and {@link #setScaleX(float) scaled}.
9106 *
9107 * @see #getRotation()
9108 * @see #getScaleX()
9109 * @see #getScaleY()
9110 * @see #getPivotY()
9111 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009112 *
9113 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009114 */
Chet Haasea5531132012-02-02 13:41:44 -08009115 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009116 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009117 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009118 }
9119
9120 /**
9121 * Sets the x location of the point around which the view is
9122 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009123 * By default, the pivot point is centered on the object.
9124 * Setting this property disables this behavior and causes the view to use only the
9125 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009126 *
9127 * @param pivotX The x location of the pivot point.
9128 * @see #getRotation()
9129 * @see #getScaleX()
9130 * @see #getScaleY()
9131 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009132 *
9133 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009134 */
9135 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009136 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009137 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009138 final TransformationInfo info = mTransformationInfo;
9139 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009140 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009141 info.mPivotX = pivotX;
9142 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009143 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009144 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009145 mDisplayList.setPivotX(pivotX);
9146 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009147 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009148 // View was rejected last time it was drawn by its parent; this may have changed
9149 invalidateParentIfNeeded();
9150 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009151 }
9152 }
9153
9154 /**
9155 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9156 * and {@link #setScaleY(float) scaled}.
9157 *
9158 * @see #getRotation()
9159 * @see #getScaleX()
9160 * @see #getScaleY()
9161 * @see #getPivotY()
9162 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009163 *
9164 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 */
Chet Haasea5531132012-02-02 13:41:44 -08009166 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009167 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009168 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009169 }
9170
9171 /**
9172 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009173 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9174 * Setting this property disables this behavior and causes the view to use only the
9175 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009176 *
9177 * @param pivotY The y location of the pivot point.
9178 * @see #getRotation()
9179 * @see #getScaleX()
9180 * @see #getScaleY()
9181 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009182 *
9183 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009184 */
9185 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009186 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009187 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009188 final TransformationInfo info = mTransformationInfo;
9189 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009190 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009191 info.mPivotY = pivotY;
9192 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009193 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009194 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009195 mDisplayList.setPivotY(pivotY);
9196 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009197 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009198 // View was rejected last time it was drawn by its parent; this may have changed
9199 invalidateParentIfNeeded();
9200 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009201 }
9202 }
9203
9204 /**
9205 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9206 * completely transparent and 1 means the view is completely opaque.
9207 *
Joe Onorato93162322010-09-16 15:42:01 -04009208 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009209 * @return The opacity of the view.
9210 */
Chet Haasea5531132012-02-02 13:41:44 -08009211 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009212 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009213 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009214 }
9215
9216 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009217 * Returns whether this View has content which overlaps. This function, intended to be
9218 * overridden by specific View types, is an optimization when alpha is set on a view. If
9219 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9220 * and then composited it into place, which can be expensive. If the view has no overlapping
9221 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9222 * An example of overlapping rendering is a TextView with a background image, such as a
9223 * Button. An example of non-overlapping rendering is a TextView with no background, or
9224 * an ImageView with only the foreground image. The default implementation returns true;
9225 * subclasses should override if they have cases which can be optimized.
9226 *
9227 * @return true if the content in this view might overlap, false otherwise.
9228 */
9229 public boolean hasOverlappingRendering() {
9230 return true;
9231 }
9232
9233 /**
Romain Guy171c5922011-01-06 10:04:23 -08009234 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9235 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009236 *
Romain Guy171c5922011-01-06 10:04:23 -08009237 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9238 * responsible for applying the opacity itself. Otherwise, calling this method is
9239 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009240 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009241 *
Chet Haasea5531132012-02-02 13:41:44 -08009242 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9243 * performance implications. It is generally best to use the alpha property sparingly and
9244 * transiently, as in the case of fading animations.</p>
9245 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009246 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009247 *
Joe Malin32736f02011-01-19 16:14:20 -08009248 * @see #setLayerType(int, android.graphics.Paint)
9249 *
Chet Haase73066682010-11-29 15:55:32 -08009250 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009251 */
9252 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009253 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009254 if (mTransformationInfo.mAlpha != alpha) {
9255 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009256 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009257 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009258 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009259 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009260 invalidate(true);
9261 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009262 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009263 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009264 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009265 mDisplayList.setAlpha(alpha);
9266 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009267 }
Chet Haaseed032702010-10-01 14:05:54 -07009268 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009269 }
9270
9271 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009272 * Faster version of setAlpha() which performs the same steps except there are
9273 * no calls to invalidate(). The caller of this function should perform proper invalidation
9274 * on the parent and this object. The return value indicates whether the subclass handles
9275 * alpha (the return value for onSetAlpha()).
9276 *
9277 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009278 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9279 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009280 */
9281 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009282 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009283 if (mTransformationInfo.mAlpha != alpha) {
9284 mTransformationInfo.mAlpha = alpha;
9285 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9286 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009287 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009288 return true;
9289 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009290 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009291 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009292 mDisplayList.setAlpha(alpha);
9293 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009294 }
Chet Haasea00f3862011-02-22 06:34:40 -08009295 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009296 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009297 }
9298
9299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 * Top position of this view relative to its parent.
9301 *
9302 * @return The top of this view, in pixels.
9303 */
9304 @ViewDebug.CapturedViewProperty
9305 public final int getTop() {
9306 return mTop;
9307 }
9308
9309 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009310 * Sets the top position of this view relative to its parent. This method is meant to be called
9311 * by the layout system and should not generally be called otherwise, because the property
9312 * may be changed at any time by the layout.
9313 *
9314 * @param top The top of this view, in pixels.
9315 */
9316 public final void setTop(int top) {
9317 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009318 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009319 final boolean matrixIsIdentity = mTransformationInfo == null
9320 || mTransformationInfo.mMatrixIsIdentity;
9321 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009322 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009323 int minTop;
9324 int yLoc;
9325 if (top < mTop) {
9326 minTop = top;
9327 yLoc = top - mTop;
9328 } else {
9329 minTop = mTop;
9330 yLoc = 0;
9331 }
Chet Haasee9140a72011-02-16 16:23:29 -08009332 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009333 }
9334 } else {
9335 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009336 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009337 }
9338
Chet Haaseed032702010-10-01 14:05:54 -07009339 int width = mRight - mLeft;
9340 int oldHeight = mBottom - mTop;
9341
Chet Haase21cd1382010-09-01 17:42:29 -07009342 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009343 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009344 mDisplayList.setTop(mTop);
9345 }
Chet Haase21cd1382010-09-01 17:42:29 -07009346
Chet Haaseed032702010-10-01 14:05:54 -07009347 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9348
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009349 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009350 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009351 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009352 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009353 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009354 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009355 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009356 }
Chet Haase55dbb652010-12-21 20:15:08 -08009357 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009358 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009359 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009360 // View was rejected last time it was drawn by its parent; this may have changed
9361 invalidateParentIfNeeded();
9362 }
Chet Haase21cd1382010-09-01 17:42:29 -07009363 }
9364 }
9365
9366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009367 * Bottom position of this view relative to its parent.
9368 *
9369 * @return The bottom of this view, in pixels.
9370 */
9371 @ViewDebug.CapturedViewProperty
9372 public final int getBottom() {
9373 return mBottom;
9374 }
9375
9376 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009377 * True if this view has changed since the last time being drawn.
9378 *
9379 * @return The dirty state of this view.
9380 */
9381 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009382 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009383 }
9384
9385 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009386 * Sets the bottom position of this view relative to its parent. This method is meant to be
9387 * called by the layout system and should not generally be called otherwise, because the
9388 * property may be changed at any time by the layout.
9389 *
9390 * @param bottom The bottom of this view, in pixels.
9391 */
9392 public final void setBottom(int bottom) {
9393 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009394 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009395 final boolean matrixIsIdentity = mTransformationInfo == null
9396 || mTransformationInfo.mMatrixIsIdentity;
9397 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009398 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009399 int maxBottom;
9400 if (bottom < mBottom) {
9401 maxBottom = mBottom;
9402 } else {
9403 maxBottom = bottom;
9404 }
Chet Haasee9140a72011-02-16 16:23:29 -08009405 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009406 }
9407 } else {
9408 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009409 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009410 }
9411
Chet Haaseed032702010-10-01 14:05:54 -07009412 int width = mRight - mLeft;
9413 int oldHeight = mBottom - mTop;
9414
Chet Haase21cd1382010-09-01 17:42:29 -07009415 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009416 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009417 mDisplayList.setBottom(mBottom);
9418 }
Chet Haase21cd1382010-09-01 17:42:29 -07009419
Chet Haaseed032702010-10-01 14:05:54 -07009420 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9421
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009422 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009423 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009424 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009425 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009426 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009427 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009428 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009429 }
Chet Haase55dbb652010-12-21 20:15:08 -08009430 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009431 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009433 // View was rejected last time it was drawn by its parent; this may have changed
9434 invalidateParentIfNeeded();
9435 }
Chet Haase21cd1382010-09-01 17:42:29 -07009436 }
9437 }
9438
9439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 * Left position of this view relative to its parent.
9441 *
9442 * @return The left edge of this view, in pixels.
9443 */
9444 @ViewDebug.CapturedViewProperty
9445 public final int getLeft() {
9446 return mLeft;
9447 }
9448
9449 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009450 * Sets the left position of this view relative to its parent. This method is meant to be called
9451 * by the layout system and should not generally be called otherwise, because the property
9452 * may be changed at any time by the layout.
9453 *
9454 * @param left The bottom of this view, in pixels.
9455 */
9456 public final void setLeft(int left) {
9457 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009458 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009459 final boolean matrixIsIdentity = mTransformationInfo == null
9460 || mTransformationInfo.mMatrixIsIdentity;
9461 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009462 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009463 int minLeft;
9464 int xLoc;
9465 if (left < mLeft) {
9466 minLeft = left;
9467 xLoc = left - mLeft;
9468 } else {
9469 minLeft = mLeft;
9470 xLoc = 0;
9471 }
Chet Haasee9140a72011-02-16 16:23:29 -08009472 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009473 }
9474 } else {
9475 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009476 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009477 }
9478
Chet Haaseed032702010-10-01 14:05:54 -07009479 int oldWidth = mRight - mLeft;
9480 int height = mBottom - mTop;
9481
Chet Haase21cd1382010-09-01 17:42:29 -07009482 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009483 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009484 mDisplayList.setLeft(left);
9485 }
Chet Haase21cd1382010-09-01 17:42:29 -07009486
Chet Haaseed032702010-10-01 14:05:54 -07009487 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9488
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009489 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009490 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009491 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009492 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009493 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009494 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009495 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009496 }
Chet Haase55dbb652010-12-21 20:15:08 -08009497 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009498 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009499 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009500 // View was rejected last time it was drawn by its parent; this may have changed
9501 invalidateParentIfNeeded();
9502 }
Chet Haase21cd1382010-09-01 17:42:29 -07009503 }
9504 }
9505
9506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 * Right position of this view relative to its parent.
9508 *
9509 * @return The right edge of this view, in pixels.
9510 */
9511 @ViewDebug.CapturedViewProperty
9512 public final int getRight() {
9513 return mRight;
9514 }
9515
9516 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009517 * Sets the right position of this view relative to its parent. This method is meant to be called
9518 * by the layout system and should not generally be called otherwise, because the property
9519 * may be changed at any time by the layout.
9520 *
9521 * @param right The bottom of this view, in pixels.
9522 */
9523 public final void setRight(int right) {
9524 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009525 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009526 final boolean matrixIsIdentity = mTransformationInfo == null
9527 || mTransformationInfo.mMatrixIsIdentity;
9528 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009529 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009530 int maxRight;
9531 if (right < mRight) {
9532 maxRight = mRight;
9533 } else {
9534 maxRight = right;
9535 }
Chet Haasee9140a72011-02-16 16:23:29 -08009536 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009537 }
9538 } else {
9539 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009540 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009541 }
9542
Chet Haaseed032702010-10-01 14:05:54 -07009543 int oldWidth = mRight - mLeft;
9544 int height = mBottom - mTop;
9545
Chet Haase21cd1382010-09-01 17:42:29 -07009546 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009547 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009548 mDisplayList.setRight(mRight);
9549 }
Chet Haase21cd1382010-09-01 17:42:29 -07009550
Chet Haaseed032702010-10-01 14:05:54 -07009551 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9552
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009553 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009554 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009555 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009556 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009557 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009558 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009559 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009560 }
Chet Haase55dbb652010-12-21 20:15:08 -08009561 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009562 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009563 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009564 // View was rejected last time it was drawn by its parent; this may have changed
9565 invalidateParentIfNeeded();
9566 }
Chet Haase21cd1382010-09-01 17:42:29 -07009567 }
9568 }
9569
9570 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009571 * The visual x position of this view, in pixels. This is equivalent to the
9572 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009573 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009574 *
Chet Haasedf030d22010-07-30 17:22:38 -07009575 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009576 */
Chet Haasea5531132012-02-02 13:41:44 -08009577 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009578 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009579 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009580 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009581
Chet Haasedf030d22010-07-30 17:22:38 -07009582 /**
9583 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9584 * {@link #setTranslationX(float) translationX} property to be the difference between
9585 * the x value passed in and the current {@link #getLeft() left} property.
9586 *
9587 * @param x The visual x position of this view, in pixels.
9588 */
9589 public void setX(float x) {
9590 setTranslationX(x - mLeft);
9591 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009592
Chet Haasedf030d22010-07-30 17:22:38 -07009593 /**
9594 * The visual y position of this view, in pixels. This is equivalent to the
9595 * {@link #setTranslationY(float) translationY} property plus the current
9596 * {@link #getTop() top} property.
9597 *
9598 * @return The visual y position of this view, in pixels.
9599 */
Chet Haasea5531132012-02-02 13:41:44 -08009600 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009601 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009602 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009603 }
9604
9605 /**
9606 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9607 * {@link #setTranslationY(float) translationY} property to be the difference between
9608 * the y value passed in and the current {@link #getTop() top} property.
9609 *
9610 * @param y The visual y position of this view, in pixels.
9611 */
9612 public void setY(float y) {
9613 setTranslationY(y - mTop);
9614 }
9615
9616
9617 /**
9618 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9619 * This position is post-layout, in addition to wherever the object's
9620 * layout placed it.
9621 *
9622 * @return The horizontal position of this view relative to its left position, in pixels.
9623 */
Chet Haasea5531132012-02-02 13:41:44 -08009624 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009625 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009626 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009627 }
9628
9629 /**
9630 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9631 * This effectively positions the object post-layout, in addition to wherever the object's
9632 * layout placed it.
9633 *
9634 * @param translationX The horizontal position of this view relative to its left position,
9635 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009636 *
9637 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009638 */
9639 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009640 ensureTransformationInfo();
9641 final TransformationInfo info = mTransformationInfo;
9642 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009643 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009644 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009645 info.mTranslationX = translationX;
9646 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009647 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009648 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009649 mDisplayList.setTranslationX(translationX);
9650 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009651 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009652 // View was rejected last time it was drawn by its parent; this may have changed
9653 invalidateParentIfNeeded();
9654 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009655 }
9656 }
9657
9658 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009659 * The horizontal location of this view relative to its {@link #getTop() top} position.
9660 * This position is post-layout, in addition to wherever the object's
9661 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009662 *
Chet Haasedf030d22010-07-30 17:22:38 -07009663 * @return The vertical position of this view relative to its top position,
9664 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009665 */
Chet Haasea5531132012-02-02 13:41:44 -08009666 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009667 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009668 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009669 }
9670
9671 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009672 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9673 * This effectively positions the object post-layout, in addition to wherever the object's
9674 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009675 *
Chet Haasedf030d22010-07-30 17:22:38 -07009676 * @param translationY The vertical position of this view relative to its top position,
9677 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009678 *
9679 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009680 */
Chet Haasedf030d22010-07-30 17:22:38 -07009681 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009682 ensureTransformationInfo();
9683 final TransformationInfo info = mTransformationInfo;
9684 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009685 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009686 info.mTranslationY = translationY;
9687 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009688 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009689 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009690 mDisplayList.setTranslationY(translationY);
9691 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009692 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009693 // View was rejected last time it was drawn by its parent; this may have changed
9694 invalidateParentIfNeeded();
9695 }
Chet Haasedf030d22010-07-30 17:22:38 -07009696 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009697 }
9698
9699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 * Hit rectangle in parent's coordinates
9701 *
9702 * @param outRect The hit rectangle of the view.
9703 */
9704 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009705 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009706 final TransformationInfo info = mTransformationInfo;
9707 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009708 outRect.set(mLeft, mTop, mRight, mBottom);
9709 } else {
9710 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009711 tmpRect.set(-info.mPivotX, -info.mPivotY,
9712 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9713 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009714 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9715 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009716 }
9717 }
9718
9719 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009720 * Determines whether the given point, in local coordinates is inside the view.
9721 */
9722 /*package*/ final boolean pointInView(float localX, float localY) {
9723 return localX >= 0 && localX < (mRight - mLeft)
9724 && localY >= 0 && localY < (mBottom - mTop);
9725 }
9726
9727 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009728 * Utility method to determine whether the given point, in local coordinates,
9729 * is inside the view, where the area of the view is expanded by the slop factor.
9730 * This method is called while processing touch-move events to determine if the event
9731 * is still within the view.
9732 */
9733 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009734 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009735 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736 }
9737
9738 /**
9739 * When a view has focus and the user navigates away from it, the next view is searched for
9740 * starting from the rectangle filled in by this method.
9741 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009742 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009743 * of the view. However, if your view maintains some idea of internal selection,
9744 * such as a cursor, or a selected row or column, you should override this method and
9745 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009746 *
9747 * @param r The rectangle to fill in, in this view's coordinates.
9748 */
9749 public void getFocusedRect(Rect r) {
9750 getDrawingRect(r);
9751 }
9752
9753 /**
9754 * If some part of this view is not clipped by any of its parents, then
9755 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009756 * coordinates (without taking possible View rotations into account), offset
9757 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9758 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009759 *
9760 * @param r If true is returned, r holds the global coordinates of the
9761 * visible portion of this view.
9762 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9763 * between this view and its root. globalOffet may be null.
9764 * @return true if r is non-empty (i.e. part of the view is visible at the
9765 * root level.
9766 */
9767 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9768 int width = mRight - mLeft;
9769 int height = mBottom - mTop;
9770 if (width > 0 && height > 0) {
9771 r.set(0, 0, width, height);
9772 if (globalOffset != null) {
9773 globalOffset.set(-mScrollX, -mScrollY);
9774 }
9775 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9776 }
9777 return false;
9778 }
9779
9780 public final boolean getGlobalVisibleRect(Rect r) {
9781 return getGlobalVisibleRect(r, null);
9782 }
9783
9784 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009785 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 if (getGlobalVisibleRect(r, offset)) {
9787 r.offset(-offset.x, -offset.y); // make r local
9788 return true;
9789 }
9790 return false;
9791 }
9792
9793 /**
9794 * Offset this view's vertical location by the specified number of pixels.
9795 *
9796 * @param offset the number of pixels to offset the view by
9797 */
9798 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009799 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009800 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009801 final boolean matrixIsIdentity = mTransformationInfo == null
9802 || mTransformationInfo.mMatrixIsIdentity;
9803 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009804 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009805 invalidateViewProperty(false, false);
9806 } else {
9807 final ViewParent p = mParent;
9808 if (p != null && mAttachInfo != null) {
9809 final Rect r = mAttachInfo.mTmpInvalRect;
9810 int minTop;
9811 int maxBottom;
9812 int yLoc;
9813 if (offset < 0) {
9814 minTop = mTop + offset;
9815 maxBottom = mBottom;
9816 yLoc = offset;
9817 } else {
9818 minTop = mTop;
9819 maxBottom = mBottom + offset;
9820 yLoc = 0;
9821 }
9822 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9823 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009824 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009825 }
9826 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009827 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009828 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009829
Chet Haasec3aa3612010-06-17 08:50:37 -07009830 mTop += offset;
9831 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009832 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009833 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009834 invalidateViewProperty(false, false);
9835 } else {
9836 if (!matrixIsIdentity) {
9837 invalidateViewProperty(false, true);
9838 }
9839 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009840 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 }
9843
9844 /**
9845 * Offset this view's horizontal location by the specified amount of pixels.
9846 *
9847 * @param offset the numer of pixels to offset the view by
9848 */
9849 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009850 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009851 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009852 final boolean matrixIsIdentity = mTransformationInfo == null
9853 || mTransformationInfo.mMatrixIsIdentity;
9854 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009855 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009856 invalidateViewProperty(false, false);
9857 } else {
9858 final ViewParent p = mParent;
9859 if (p != null && mAttachInfo != null) {
9860 final Rect r = mAttachInfo.mTmpInvalRect;
9861 int minLeft;
9862 int maxRight;
9863 if (offset < 0) {
9864 minLeft = mLeft + offset;
9865 maxRight = mRight;
9866 } else {
9867 minLeft = mLeft;
9868 maxRight = mRight + offset;
9869 }
9870 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9871 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009872 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009873 }
9874 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009875 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009876 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009877
Chet Haasec3aa3612010-06-17 08:50:37 -07009878 mLeft += offset;
9879 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009880 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009881 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009882 invalidateViewProperty(false, false);
9883 } else {
9884 if (!matrixIsIdentity) {
9885 invalidateViewProperty(false, true);
9886 }
9887 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009888 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 }
9891
9892 /**
9893 * Get the LayoutParams associated with this view. All views should have
9894 * layout parameters. These supply parameters to the <i>parent</i> of this
9895 * view specifying how it should be arranged. There are many subclasses of
9896 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9897 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009898 *
9899 * This method may return null if this View is not attached to a parent
9900 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9901 * was not invoked successfully. When a View is attached to a parent
9902 * ViewGroup, this method must not return null.
9903 *
9904 * @return The LayoutParams associated with this view, or null if no
9905 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009906 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009907 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 public ViewGroup.LayoutParams getLayoutParams() {
9909 return mLayoutParams;
9910 }
9911
9912 /**
9913 * Set the layout parameters associated with this view. These supply
9914 * parameters to the <i>parent</i> of this view specifying how it should be
9915 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9916 * correspond to the different subclasses of ViewGroup that are responsible
9917 * for arranging their children.
9918 *
Romain Guy01c174b2011-02-22 11:51:06 -08009919 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 */
9921 public void setLayoutParams(ViewGroup.LayoutParams params) {
9922 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009923 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 }
9925 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009926 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009927 if (mParent instanceof ViewGroup) {
9928 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9929 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 requestLayout();
9931 }
9932
9933 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009934 * Resolve the layout parameters depending on the resolved layout direction
9935 */
9936 private void resolveLayoutParams() {
9937 if (mLayoutParams != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07009938 mLayoutParams.onResolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009939 }
9940 }
9941
9942 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 * Set the scrolled position of your view. This will cause a call to
9944 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9945 * invalidated.
9946 * @param x the x position to scroll to
9947 * @param y the y position to scroll to
9948 */
9949 public void scrollTo(int x, int y) {
9950 if (mScrollX != x || mScrollY != y) {
9951 int oldX = mScrollX;
9952 int oldY = mScrollY;
9953 mScrollX = x;
9954 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009955 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009957 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009958 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 }
9961 }
9962
9963 /**
9964 * Move the scrolled position of your view. This will cause a call to
9965 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9966 * invalidated.
9967 * @param x the amount of pixels to scroll by horizontally
9968 * @param y the amount of pixels to scroll by vertically
9969 */
9970 public void scrollBy(int x, int y) {
9971 scrollTo(mScrollX + x, mScrollY + y);
9972 }
9973
9974 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009975 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9976 * animation to fade the scrollbars out after a default delay. If a subclass
9977 * provides animated scrolling, the start delay should equal the duration
9978 * of the scrolling animation.</p>
9979 *
9980 * <p>The animation starts only if at least one of the scrollbars is
9981 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9982 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9983 * this method returns true, and false otherwise. If the animation is
9984 * started, this method calls {@link #invalidate()}; in that case the
9985 * caller should not call {@link #invalidate()}.</p>
9986 *
9987 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009988 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009989 *
9990 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9991 * and {@link #scrollTo(int, int)}.</p>
9992 *
9993 * @return true if the animation is played, false otherwise
9994 *
9995 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009996 * @see #scrollBy(int, int)
9997 * @see #scrollTo(int, int)
9998 * @see #isHorizontalScrollBarEnabled()
9999 * @see #isVerticalScrollBarEnabled()
10000 * @see #setHorizontalScrollBarEnabled(boolean)
10001 * @see #setVerticalScrollBarEnabled(boolean)
10002 */
10003 protected boolean awakenScrollBars() {
10004 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010005 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010006 }
10007
10008 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010009 * Trigger the scrollbars to draw.
10010 * This method differs from awakenScrollBars() only in its default duration.
10011 * initialAwakenScrollBars() will show the scroll bars for longer than
10012 * usual to give the user more of a chance to notice them.
10013 *
10014 * @return true if the animation is played, false otherwise.
10015 */
10016 private boolean initialAwakenScrollBars() {
10017 return mScrollCache != null &&
10018 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10019 }
10020
10021 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010022 * <p>
10023 * Trigger the scrollbars to draw. When invoked this method starts an
10024 * animation to fade the scrollbars out after a fixed delay. If a subclass
10025 * provides animated scrolling, the start delay should equal the duration of
10026 * the scrolling animation.
10027 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010028 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010029 * <p>
10030 * The animation starts only if at least one of the scrollbars is enabled,
10031 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10032 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10033 * this method returns true, and false otherwise. If the animation is
10034 * started, this method calls {@link #invalidate()}; in that case the caller
10035 * should not call {@link #invalidate()}.
10036 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010037 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010038 * <p>
10039 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010040 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010041 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010042 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010043 * @param startDelay the delay, in milliseconds, after which the animation
10044 * should start; when the delay is 0, the animation starts
10045 * immediately
10046 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010047 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010048 * @see #scrollBy(int, int)
10049 * @see #scrollTo(int, int)
10050 * @see #isHorizontalScrollBarEnabled()
10051 * @see #isVerticalScrollBarEnabled()
10052 * @see #setHorizontalScrollBarEnabled(boolean)
10053 * @see #setVerticalScrollBarEnabled(boolean)
10054 */
10055 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010056 return awakenScrollBars(startDelay, true);
10057 }
Joe Malin32736f02011-01-19 16:14:20 -080010058
Mike Cleron290947b2009-09-29 18:34:32 -070010059 /**
10060 * <p>
10061 * Trigger the scrollbars to draw. When invoked this method starts an
10062 * animation to fade the scrollbars out after a fixed delay. If a subclass
10063 * provides animated scrolling, the start delay should equal the duration of
10064 * the scrolling animation.
10065 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010066 *
Mike Cleron290947b2009-09-29 18:34:32 -070010067 * <p>
10068 * The animation starts only if at least one of the scrollbars is enabled,
10069 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10070 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10071 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010072 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010073 * is set to true; in that case the caller
10074 * should not call {@link #invalidate()}.
10075 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010076 *
Mike Cleron290947b2009-09-29 18:34:32 -070010077 * <p>
10078 * This method should be invoked everytime a subclass directly updates the
10079 * scroll parameters.
10080 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010081 *
Mike Cleron290947b2009-09-29 18:34:32 -070010082 * @param startDelay the delay, in milliseconds, after which the animation
10083 * should start; when the delay is 0, the animation starts
10084 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010085 *
Mike Cleron290947b2009-09-29 18:34:32 -070010086 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010087 *
Mike Cleron290947b2009-09-29 18:34:32 -070010088 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010089 *
Mike Cleron290947b2009-09-29 18:34:32 -070010090 * @see #scrollBy(int, int)
10091 * @see #scrollTo(int, int)
10092 * @see #isHorizontalScrollBarEnabled()
10093 * @see #isVerticalScrollBarEnabled()
10094 * @see #setHorizontalScrollBarEnabled(boolean)
10095 * @see #setVerticalScrollBarEnabled(boolean)
10096 */
10097 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010098 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010099
Mike Cleronf116bf82009-09-27 19:14:12 -070010100 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10101 return false;
10102 }
10103
10104 if (scrollCache.scrollBar == null) {
10105 scrollCache.scrollBar = new ScrollBarDrawable();
10106 }
10107
10108 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10109
Mike Cleron290947b2009-09-29 18:34:32 -070010110 if (invalidate) {
10111 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010112 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010113 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010114
10115 if (scrollCache.state == ScrollabilityCache.OFF) {
10116 // FIXME: this is copied from WindowManagerService.
10117 // We should get this value from the system when it
10118 // is possible to do so.
10119 final int KEY_REPEAT_FIRST_DELAY = 750;
10120 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10121 }
10122
10123 // Tell mScrollCache when we should start fading. This may
10124 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010125 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010126 scrollCache.fadeStartTime = fadeStartTime;
10127 scrollCache.state = ScrollabilityCache.ON;
10128
10129 // Schedule our fader to run, unscheduling any old ones first
10130 if (mAttachInfo != null) {
10131 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10132 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10133 }
10134
10135 return true;
10136 }
10137
10138 return false;
10139 }
10140
10141 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010142 * Do not invalidate views which are not visible and which are not running an animation. They
10143 * will not get drawn and they should not set dirty flags as if they will be drawn
10144 */
10145 private boolean skipInvalidate() {
10146 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10147 (!(mParent instanceof ViewGroup) ||
10148 !((ViewGroup) mParent).isViewTransitioning(this));
10149 }
10150 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010151 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010152 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10153 * in the future. This must be called from a UI thread. To call from a non-UI
10154 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 *
10156 * WARNING: This method is destructive to dirty.
10157 * @param dirty the rectangle representing the bounds of the dirty region
10158 */
10159 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010160 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010161 return;
10162 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010163 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10164 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10165 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10166 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10167 mPrivateFlags |= PFLAG_INVALIDATED;
10168 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 final ViewParent p = mParent;
10170 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010171 //noinspection PointlessBooleanExpression,ConstantConditions
10172 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10173 if (p != null && ai != null && ai.mHardwareAccelerated) {
10174 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010175 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010176 p.invalidateChild(this, null);
10177 return;
10178 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 if (p != null && ai != null) {
10181 final int scrollX = mScrollX;
10182 final int scrollY = mScrollY;
10183 final Rect r = ai.mTmpInvalRect;
10184 r.set(dirty.left - scrollX, dirty.top - scrollY,
10185 dirty.right - scrollX, dirty.bottom - scrollY);
10186 mParent.invalidateChild(this, r);
10187 }
10188 }
10189 }
10190
10191 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010192 * 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 -080010193 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010194 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10195 * will be called at some point in the future. This must be called from
10196 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010197 * @param l the left position of the dirty region
10198 * @param t the top position of the dirty region
10199 * @param r the right position of the dirty region
10200 * @param b the bottom position of the dirty region
10201 */
10202 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010203 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010204 return;
10205 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010206 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10207 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10208 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10209 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10210 mPrivateFlags |= PFLAG_INVALIDATED;
10211 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212 final ViewParent p = mParent;
10213 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010214 //noinspection PointlessBooleanExpression,ConstantConditions
10215 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10216 if (p != null && ai != null && ai.mHardwareAccelerated) {
10217 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010218 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010219 p.invalidateChild(this, null);
10220 return;
10221 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010223 if (p != null && ai != null && l < r && t < b) {
10224 final int scrollX = mScrollX;
10225 final int scrollY = mScrollY;
10226 final Rect tmpr = ai.mTmpInvalRect;
10227 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10228 p.invalidateChild(this, tmpr);
10229 }
10230 }
10231 }
10232
10233 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010234 * Invalidate the whole view. If the view is visible,
10235 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10236 * the future. This must be called from a UI thread. To call from a non-UI thread,
10237 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 */
10239 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010240 invalidate(true);
10241 }
Joe Malin32736f02011-01-19 16:14:20 -080010242
Chet Haaseed032702010-10-01 14:05:54 -070010243 /**
10244 * This is where the invalidate() work actually happens. A full invalidate()
10245 * causes the drawing cache to be invalidated, but this function can be called with
10246 * invalidateCache set to false to skip that invalidation step for cases that do not
10247 * need it (for example, a component that remains at the same dimensions with the same
10248 * content).
10249 *
10250 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10251 * well. This is usually true for a full invalidate, but may be set to false if the
10252 * View's contents or dimensions have not changed.
10253 */
Romain Guy849d0a32011-02-01 17:20:48 -080010254 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010255 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010256 return;
10257 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010258 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10259 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10260 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010261 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010262 mPrivateFlags &= ~PFLAG_DRAWN;
10263 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010264 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010265 mPrivateFlags |= PFLAG_INVALIDATED;
10266 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010268 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010269 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010270 //noinspection PointlessBooleanExpression,ConstantConditions
10271 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10272 if (p != null && ai != null && ai.mHardwareAccelerated) {
10273 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010274 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010275 p.invalidateChild(this, null);
10276 return;
10277 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010278 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 if (p != null && ai != null) {
10281 final Rect r = ai.mTmpInvalRect;
10282 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10283 // Don't call invalidate -- we don't want to internally scroll
10284 // our own bounds
10285 p.invalidateChild(this, r);
10286 }
10287 }
10288 }
10289
10290 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010291 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10292 * set any flags or handle all of the cases handled by the default invalidation methods.
10293 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10294 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10295 * walk up the hierarchy, transforming the dirty rect as necessary.
10296 *
10297 * The method also handles normal invalidation logic if display list properties are not
10298 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10299 * backup approach, to handle these cases used in the various property-setting methods.
10300 *
10301 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10302 * are not being used in this view
10303 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10304 * list properties are not being used in this view
10305 */
10306 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010307 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010308 if (invalidateParent) {
10309 invalidateParentCaches();
10310 }
10311 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010312 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010313 }
10314 invalidate(false);
10315 } else {
10316 final AttachInfo ai = mAttachInfo;
10317 final ViewParent p = mParent;
10318 if (p != null && ai != null) {
10319 final Rect r = ai.mTmpInvalRect;
10320 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10321 if (mParent instanceof ViewGroup) {
10322 ((ViewGroup) mParent).invalidateChildFast(this, r);
10323 } else {
10324 mParent.invalidateChild(this, r);
10325 }
10326 }
10327 }
10328 }
10329
10330 /**
10331 * Utility method to transform a given Rect by the current matrix of this view.
10332 */
10333 void transformRect(final Rect rect) {
10334 if (!getMatrix().isIdentity()) {
10335 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10336 boundingRect.set(rect);
10337 getMatrix().mapRect(boundingRect);
10338 rect.set((int) (boundingRect.left - 0.5f),
10339 (int) (boundingRect.top - 0.5f),
10340 (int) (boundingRect.right + 0.5f),
10341 (int) (boundingRect.bottom + 0.5f));
10342 }
10343 }
10344
10345 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010346 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010347 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10348 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010349 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10350 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010351 *
10352 * @hide
10353 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010354 protected void invalidateParentCaches() {
10355 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010356 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010357 }
10358 }
Joe Malin32736f02011-01-19 16:14:20 -080010359
Romain Guy0fd89bf2011-01-26 15:41:30 -080010360 /**
10361 * Used to indicate that the parent of this view should be invalidated. This functionality
10362 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10363 * which is necessary when various parent-managed properties of the view change, such as
10364 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10365 * an invalidation event to the parent.
10366 *
10367 * @hide
10368 */
10369 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010370 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010371 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010372 }
10373 }
10374
10375 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010376 * Indicates whether this View is opaque. An opaque View guarantees that it will
10377 * draw all the pixels overlapping its bounds using a fully opaque color.
10378 *
10379 * Subclasses of View should override this method whenever possible to indicate
10380 * whether an instance is opaque. Opaque Views are treated in a special way by
10381 * the View hierarchy, possibly allowing it to perform optimizations during
10382 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010383 *
Romain Guy24443ea2009-05-11 11:56:30 -070010384 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010385 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010386 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010387 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010388 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010389 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010390 }
10391
Adam Powell20232d02010-12-08 21:08:53 -080010392 /**
10393 * @hide
10394 */
10395 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010396 // Opaque if:
10397 // - Has a background
10398 // - Background is opaque
10399 // - Doesn't have scrollbars or scrollbars are inside overlay
10400
Philip Milne6c8ea062012-04-03 17:38:43 -070010401 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010402 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010403 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010404 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010405 }
10406
10407 final int flags = mViewFlags;
10408 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10409 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010410 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010411 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010412 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010413 }
10414 }
10415
10416 /**
10417 * @hide
10418 */
10419 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010420 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010421 }
10422
10423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010424 * @return A handler associated with the thread running the View. This
10425 * handler can be used to pump events in the UI events queue.
10426 */
10427 public Handler getHandler() {
10428 if (mAttachInfo != null) {
10429 return mAttachInfo.mHandler;
10430 }
10431 return null;
10432 }
10433
10434 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010435 * Gets the view root associated with the View.
10436 * @return The view root, or null if none.
10437 * @hide
10438 */
10439 public ViewRootImpl getViewRootImpl() {
10440 if (mAttachInfo != null) {
10441 return mAttachInfo.mViewRootImpl;
10442 }
10443 return null;
10444 }
10445
10446 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010447 * <p>Causes the Runnable to be added to the message queue.
10448 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010449 *
Romain Guye63a4f32011-08-11 11:33:31 -070010450 * <p>This method can be invoked from outside of the UI thread
10451 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 *
10453 * @param action The Runnable that will be executed.
10454 *
10455 * @return Returns true if the Runnable was successfully placed in to the
10456 * message queue. Returns false on failure, usually because the
10457 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010458 *
10459 * @see #postDelayed
10460 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461 */
10462 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010463 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010464 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010465 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010467 // Assume that post will succeed later
10468 ViewRootImpl.getRunQueue().post(action);
10469 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 }
10471
10472 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010473 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010475 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010476 *
Romain Guye63a4f32011-08-11 11:33:31 -070010477 * <p>This method can be invoked from outside of the UI thread
10478 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 *
10480 * @param action The Runnable that will be executed.
10481 * @param delayMillis The delay (in milliseconds) until the Runnable
10482 * will be executed.
10483 *
10484 * @return true if the Runnable was successfully placed in to the
10485 * message queue. Returns false on failure, usually because the
10486 * looper processing the message queue is exiting. Note that a
10487 * result of true does not mean the Runnable will be processed --
10488 * if the looper is quit before the delivery time of the message
10489 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010490 *
10491 * @see #post
10492 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493 */
10494 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010495 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010496 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010497 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010499 // Assume that post will succeed later
10500 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10501 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 }
10503
10504 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010505 * <p>Causes the Runnable to execute on the next animation time step.
10506 * The runnable will be run on the user interface thread.</p>
10507 *
10508 * <p>This method can be invoked from outside of the UI thread
10509 * only when this View is attached to a window.</p>
10510 *
10511 * @param action The Runnable that will be executed.
10512 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010513 * @see #postOnAnimationDelayed
10514 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010515 */
10516 public void postOnAnimation(Runnable action) {
10517 final AttachInfo attachInfo = mAttachInfo;
10518 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010519 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10520 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010521 } else {
10522 // Assume that post will succeed later
10523 ViewRootImpl.getRunQueue().post(action);
10524 }
10525 }
10526
10527 /**
10528 * <p>Causes the Runnable to execute on the next animation time step,
10529 * after the specified amount of time elapses.
10530 * The runnable will be run on the user interface thread.</p>
10531 *
10532 * <p>This method can be invoked from outside of the UI thread
10533 * only when this View is attached to a window.</p>
10534 *
10535 * @param action The Runnable that will be executed.
10536 * @param delayMillis The delay (in milliseconds) until the Runnable
10537 * will be executed.
10538 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010539 * @see #postOnAnimation
10540 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010541 */
10542 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10543 final AttachInfo attachInfo = mAttachInfo;
10544 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010545 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10546 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010547 } else {
10548 // Assume that post will succeed later
10549 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10550 }
10551 }
10552
10553 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010554 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010555 *
Romain Guye63a4f32011-08-11 11:33:31 -070010556 * <p>This method can be invoked from outside of the UI thread
10557 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 *
10559 * @param action The Runnable to remove from the message handling queue
10560 *
10561 * @return true if this view could ask the Handler to remove the Runnable,
10562 * false otherwise. When the returned value is true, the Runnable
10563 * may or may not have been actually removed from the message queue
10564 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010565 *
10566 * @see #post
10567 * @see #postDelayed
10568 * @see #postOnAnimation
10569 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 */
10571 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010572 if (action != null) {
10573 final AttachInfo attachInfo = mAttachInfo;
10574 if (attachInfo != null) {
10575 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010576 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10577 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010578 } else {
10579 // Assume that post will succeed later
10580 ViewRootImpl.getRunQueue().removeCallbacks(action);
10581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 return true;
10584 }
10585
10586 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010587 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10588 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010589 *
Romain Guye63a4f32011-08-11 11:33:31 -070010590 * <p>This method can be invoked from outside of the UI thread
10591 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010592 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010594 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 */
10596 public void postInvalidate() {
10597 postInvalidateDelayed(0);
10598 }
10599
10600 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010601 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10602 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010603 *
Romain Guye63a4f32011-08-11 11:33:31 -070010604 * <p>This method can be invoked from outside of the UI thread
10605 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606 *
10607 * @param left The left coordinate of the rectangle to invalidate.
10608 * @param top The top coordinate of the rectangle to invalidate.
10609 * @param right The right coordinate of the rectangle to invalidate.
10610 * @param bottom The bottom coordinate of the rectangle to invalidate.
10611 *
10612 * @see #invalidate(int, int, int, int)
10613 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010614 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010615 */
10616 public void postInvalidate(int left, int top, int right, int bottom) {
10617 postInvalidateDelayed(0, left, top, right, bottom);
10618 }
10619
10620 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010621 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10622 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010623 *
Romain Guye63a4f32011-08-11 11:33:31 -070010624 * <p>This method can be invoked from outside of the UI thread
10625 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 *
10627 * @param delayMilliseconds the duration in milliseconds to delay the
10628 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010629 *
10630 * @see #invalidate()
10631 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 */
10633 public void postInvalidateDelayed(long delayMilliseconds) {
10634 // We try only with the AttachInfo because there's no point in invalidating
10635 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010636 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010637 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010638 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
10640 }
10641
10642 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010643 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10644 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010645 *
Romain Guye63a4f32011-08-11 11:33:31 -070010646 * <p>This method can be invoked from outside of the UI thread
10647 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 *
10649 * @param delayMilliseconds the duration in milliseconds to delay the
10650 * invalidation by
10651 * @param left The left coordinate of the rectangle to invalidate.
10652 * @param top The top coordinate of the rectangle to invalidate.
10653 * @param right The right coordinate of the rectangle to invalidate.
10654 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010655 *
10656 * @see #invalidate(int, int, int, int)
10657 * @see #invalidate(Rect)
10658 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 */
10660 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10661 int right, int bottom) {
10662
10663 // We try only with the AttachInfo because there's no point in invalidating
10664 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010665 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010666 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10668 info.target = this;
10669 info.left = left;
10670 info.top = top;
10671 info.right = right;
10672 info.bottom = bottom;
10673
Jeff Browna175a5b2012-02-15 19:18:31 -080010674 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 }
10676 }
10677
10678 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010679 * <p>Cause an invalidate to happen on the next animation time step, typically the
10680 * next display frame.</p>
10681 *
10682 * <p>This method can be invoked from outside of the UI thread
10683 * only when this View is attached to a window.</p>
10684 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010685 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010686 */
10687 public void postInvalidateOnAnimation() {
10688 // We try only with the AttachInfo because there's no point in invalidating
10689 // if we are not attached to our window
10690 final AttachInfo attachInfo = mAttachInfo;
10691 if (attachInfo != null) {
10692 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10693 }
10694 }
10695
10696 /**
10697 * <p>Cause an invalidate of the specified area to happen on the next animation
10698 * time step, typically the next display frame.</p>
10699 *
10700 * <p>This method can be invoked from outside of the UI thread
10701 * only when this View is attached to a window.</p>
10702 *
10703 * @param left The left coordinate of the rectangle to invalidate.
10704 * @param top The top coordinate of the rectangle to invalidate.
10705 * @param right The right coordinate of the rectangle to invalidate.
10706 * @param bottom The bottom coordinate of the rectangle to invalidate.
10707 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010708 * @see #invalidate(int, int, int, int)
10709 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010710 */
10711 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10712 // We try only with the AttachInfo because there's no point in invalidating
10713 // if we are not attached to our window
10714 final AttachInfo attachInfo = mAttachInfo;
10715 if (attachInfo != null) {
10716 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10717 info.target = this;
10718 info.left = left;
10719 info.top = top;
10720 info.right = right;
10721 info.bottom = bottom;
10722
10723 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10724 }
10725 }
10726
10727 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010728 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10729 * This event is sent at most once every
10730 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10731 */
10732 private void postSendViewScrolledAccessibilityEventCallback() {
10733 if (mSendViewScrolledAccessibilityEvent == null) {
10734 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10735 }
10736 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10737 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10738 postDelayed(mSendViewScrolledAccessibilityEvent,
10739 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10740 }
10741 }
10742
10743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 * Called by a parent to request that a child update its values for mScrollX
10745 * and mScrollY if necessary. This will typically be done if the child is
10746 * animating a scroll using a {@link android.widget.Scroller Scroller}
10747 * object.
10748 */
10749 public void computeScroll() {
10750 }
10751
10752 /**
10753 * <p>Indicate whether the horizontal edges are faded when the view is
10754 * scrolled horizontally.</p>
10755 *
10756 * @return true if the horizontal edges should are faded on scroll, false
10757 * otherwise
10758 *
10759 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010760 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010761 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 */
10763 public boolean isHorizontalFadingEdgeEnabled() {
10764 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10765 }
10766
10767 /**
10768 * <p>Define whether the horizontal edges should be faded when this view
10769 * is scrolled horizontally.</p>
10770 *
10771 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10772 * be faded when the view is scrolled
10773 * horizontally
10774 *
10775 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010776 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010777 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 */
10779 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10780 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10781 if (horizontalFadingEdgeEnabled) {
10782 initScrollCache();
10783 }
10784
10785 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10786 }
10787 }
10788
10789 /**
10790 * <p>Indicate whether the vertical edges are faded when the view is
10791 * scrolled horizontally.</p>
10792 *
10793 * @return true if the vertical edges should are faded on scroll, false
10794 * otherwise
10795 *
10796 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010797 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010798 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 */
10800 public boolean isVerticalFadingEdgeEnabled() {
10801 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10802 }
10803
10804 /**
10805 * <p>Define whether the vertical edges should be faded when this view
10806 * is scrolled vertically.</p>
10807 *
10808 * @param verticalFadingEdgeEnabled true if the vertical edges should
10809 * be faded when the view is scrolled
10810 * vertically
10811 *
10812 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010813 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010814 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 */
10816 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10817 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10818 if (verticalFadingEdgeEnabled) {
10819 initScrollCache();
10820 }
10821
10822 mViewFlags ^= FADING_EDGE_VERTICAL;
10823 }
10824 }
10825
10826 /**
10827 * Returns the strength, or intensity, of the top faded edge. The strength is
10828 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10829 * returns 0.0 or 1.0 but no value in between.
10830 *
10831 * Subclasses should override this method to provide a smoother fade transition
10832 * when scrolling occurs.
10833 *
10834 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10835 */
10836 protected float getTopFadingEdgeStrength() {
10837 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10838 }
10839
10840 /**
10841 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10842 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10843 * returns 0.0 or 1.0 but no value in between.
10844 *
10845 * Subclasses should override this method to provide a smoother fade transition
10846 * when scrolling occurs.
10847 *
10848 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10849 */
10850 protected float getBottomFadingEdgeStrength() {
10851 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10852 computeVerticalScrollRange() ? 1.0f : 0.0f;
10853 }
10854
10855 /**
10856 * Returns the strength, or intensity, of the left faded edge. The strength is
10857 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10858 * returns 0.0 or 1.0 but no value in between.
10859 *
10860 * Subclasses should override this method to provide a smoother fade transition
10861 * when scrolling occurs.
10862 *
10863 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10864 */
10865 protected float getLeftFadingEdgeStrength() {
10866 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10867 }
10868
10869 /**
10870 * Returns the strength, or intensity, of the right faded edge. The strength is
10871 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10872 * returns 0.0 or 1.0 but no value in between.
10873 *
10874 * Subclasses should override this method to provide a smoother fade transition
10875 * when scrolling occurs.
10876 *
10877 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10878 */
10879 protected float getRightFadingEdgeStrength() {
10880 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10881 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10882 }
10883
10884 /**
10885 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10886 * scrollbar is not drawn by default.</p>
10887 *
10888 * @return true if the horizontal scrollbar should be painted, false
10889 * otherwise
10890 *
10891 * @see #setHorizontalScrollBarEnabled(boolean)
10892 */
10893 public boolean isHorizontalScrollBarEnabled() {
10894 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10895 }
10896
10897 /**
10898 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10899 * scrollbar is not drawn by default.</p>
10900 *
10901 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10902 * be painted
10903 *
10904 * @see #isHorizontalScrollBarEnabled()
10905 */
10906 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10907 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10908 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010909 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010910 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010911 }
10912 }
10913
10914 /**
10915 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10916 * scrollbar is not drawn by default.</p>
10917 *
10918 * @return true if the vertical scrollbar should be painted, false
10919 * otherwise
10920 *
10921 * @see #setVerticalScrollBarEnabled(boolean)
10922 */
10923 public boolean isVerticalScrollBarEnabled() {
10924 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10925 }
10926
10927 /**
10928 * <p>Define whether the vertical scrollbar should be drawn or not. The
10929 * scrollbar is not drawn by default.</p>
10930 *
10931 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10932 * be painted
10933 *
10934 * @see #isVerticalScrollBarEnabled()
10935 */
10936 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10937 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10938 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010939 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010940 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941 }
10942 }
10943
Adam Powell20232d02010-12-08 21:08:53 -080010944 /**
10945 * @hide
10946 */
10947 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010948 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949 }
Joe Malin32736f02011-01-19 16:14:20 -080010950
Mike Cleronfe81d382009-09-28 14:22:16 -070010951 /**
10952 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010953 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010954 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010955 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010956 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010957 */
10958 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10959 initScrollCache();
10960 final ScrollabilityCache scrollabilityCache = mScrollCache;
10961 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010962 if (fadeScrollbars) {
10963 scrollabilityCache.state = ScrollabilityCache.OFF;
10964 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010965 scrollabilityCache.state = ScrollabilityCache.ON;
10966 }
10967 }
Joe Malin32736f02011-01-19 16:14:20 -080010968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 /**
Joe Malin32736f02011-01-19 16:14:20 -080010970 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010971 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010972 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010973 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010974 *
10975 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010976 */
10977 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010978 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010979 }
Joe Malin32736f02011-01-19 16:14:20 -080010980
Mike Cleron52f0a642009-09-28 18:21:37 -070010981 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010982 *
10983 * Returns the delay before scrollbars fade.
10984 *
10985 * @return the delay before scrollbars fade
10986 *
10987 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10988 */
10989 public int getScrollBarDefaultDelayBeforeFade() {
10990 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10991 mScrollCache.scrollBarDefaultDelayBeforeFade;
10992 }
10993
10994 /**
10995 * Define the delay before scrollbars fade.
10996 *
10997 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10998 *
10999 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11000 */
11001 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11002 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11003 }
11004
11005 /**
11006 *
11007 * Returns the scrollbar fade duration.
11008 *
11009 * @return the scrollbar fade duration
11010 *
11011 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11012 */
11013 public int getScrollBarFadeDuration() {
11014 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11015 mScrollCache.scrollBarFadeDuration;
11016 }
11017
11018 /**
11019 * Define the scrollbar fade duration.
11020 *
11021 * @param scrollBarFadeDuration - the scrollbar fade duration
11022 *
11023 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11024 */
11025 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11026 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11027 }
11028
11029 /**
11030 *
11031 * Returns the scrollbar size.
11032 *
11033 * @return the scrollbar size
11034 *
11035 * @attr ref android.R.styleable#View_scrollbarSize
11036 */
11037 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011038 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011039 mScrollCache.scrollBarSize;
11040 }
11041
11042 /**
11043 * Define the scrollbar size.
11044 *
11045 * @param scrollBarSize - the scrollbar size
11046 *
11047 * @attr ref android.R.styleable#View_scrollbarSize
11048 */
11049 public void setScrollBarSize(int scrollBarSize) {
11050 getScrollCache().scrollBarSize = scrollBarSize;
11051 }
11052
11053 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11055 * inset. When inset, they add to the padding of the view. And the scrollbars
11056 * can be drawn inside the padding area or on the edge of the view. For example,
11057 * if a view has a background drawable and you want to draw the scrollbars
11058 * inside the padding specified by the drawable, you can use
11059 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11060 * appear at the edge of the view, ignoring the padding, then you can use
11061 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11062 * @param style the style of the scrollbars. Should be one of
11063 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11064 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11065 * @see #SCROLLBARS_INSIDE_OVERLAY
11066 * @see #SCROLLBARS_INSIDE_INSET
11067 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11068 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011069 *
11070 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011071 */
11072 public void setScrollBarStyle(int style) {
11073 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11074 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011075 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011076 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011077 }
11078 }
11079
11080 /**
11081 * <p>Returns the current scrollbar style.</p>
11082 * @return the current scrollbar style
11083 * @see #SCROLLBARS_INSIDE_OVERLAY
11084 * @see #SCROLLBARS_INSIDE_INSET
11085 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11086 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011087 *
11088 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011090 @ViewDebug.ExportedProperty(mapping = {
11091 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11092 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11093 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11094 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11095 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 public int getScrollBarStyle() {
11097 return mViewFlags & SCROLLBARS_STYLE_MASK;
11098 }
11099
11100 /**
11101 * <p>Compute the horizontal range that the horizontal scrollbar
11102 * represents.</p>
11103 *
11104 * <p>The range is expressed in arbitrary units that must be the same as the
11105 * units used by {@link #computeHorizontalScrollExtent()} and
11106 * {@link #computeHorizontalScrollOffset()}.</p>
11107 *
11108 * <p>The default range is the drawing width of this view.</p>
11109 *
11110 * @return the total horizontal range represented by the horizontal
11111 * scrollbar
11112 *
11113 * @see #computeHorizontalScrollExtent()
11114 * @see #computeHorizontalScrollOffset()
11115 * @see android.widget.ScrollBarDrawable
11116 */
11117 protected int computeHorizontalScrollRange() {
11118 return getWidth();
11119 }
11120
11121 /**
11122 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11123 * within the horizontal range. This value is used to compute the position
11124 * of the thumb within the scrollbar's track.</p>
11125 *
11126 * <p>The range is expressed in arbitrary units that must be the same as the
11127 * units used by {@link #computeHorizontalScrollRange()} and
11128 * {@link #computeHorizontalScrollExtent()}.</p>
11129 *
11130 * <p>The default offset is the scroll offset of this view.</p>
11131 *
11132 * @return the horizontal offset of the scrollbar's thumb
11133 *
11134 * @see #computeHorizontalScrollRange()
11135 * @see #computeHorizontalScrollExtent()
11136 * @see android.widget.ScrollBarDrawable
11137 */
11138 protected int computeHorizontalScrollOffset() {
11139 return mScrollX;
11140 }
11141
11142 /**
11143 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11144 * within the horizontal range. This value is used to compute the length
11145 * of the thumb within the scrollbar's track.</p>
11146 *
11147 * <p>The range is expressed in arbitrary units that must be the same as the
11148 * units used by {@link #computeHorizontalScrollRange()} and
11149 * {@link #computeHorizontalScrollOffset()}.</p>
11150 *
11151 * <p>The default extent is the drawing width of this view.</p>
11152 *
11153 * @return the horizontal extent of the scrollbar's thumb
11154 *
11155 * @see #computeHorizontalScrollRange()
11156 * @see #computeHorizontalScrollOffset()
11157 * @see android.widget.ScrollBarDrawable
11158 */
11159 protected int computeHorizontalScrollExtent() {
11160 return getWidth();
11161 }
11162
11163 /**
11164 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11165 *
11166 * <p>The range is expressed in arbitrary units that must be the same as the
11167 * units used by {@link #computeVerticalScrollExtent()} and
11168 * {@link #computeVerticalScrollOffset()}.</p>
11169 *
11170 * @return the total vertical range represented by the vertical scrollbar
11171 *
11172 * <p>The default range is the drawing height of this view.</p>
11173 *
11174 * @see #computeVerticalScrollExtent()
11175 * @see #computeVerticalScrollOffset()
11176 * @see android.widget.ScrollBarDrawable
11177 */
11178 protected int computeVerticalScrollRange() {
11179 return getHeight();
11180 }
11181
11182 /**
11183 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11184 * within the horizontal range. This value is used to compute the position
11185 * of the thumb within the scrollbar's track.</p>
11186 *
11187 * <p>The range is expressed in arbitrary units that must be the same as the
11188 * units used by {@link #computeVerticalScrollRange()} and
11189 * {@link #computeVerticalScrollExtent()}.</p>
11190 *
11191 * <p>The default offset is the scroll offset of this view.</p>
11192 *
11193 * @return the vertical offset of the scrollbar's thumb
11194 *
11195 * @see #computeVerticalScrollRange()
11196 * @see #computeVerticalScrollExtent()
11197 * @see android.widget.ScrollBarDrawable
11198 */
11199 protected int computeVerticalScrollOffset() {
11200 return mScrollY;
11201 }
11202
11203 /**
11204 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11205 * within the vertical range. This value is used to compute the length
11206 * of the thumb within the scrollbar's track.</p>
11207 *
11208 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011209 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 * {@link #computeVerticalScrollOffset()}.</p>
11211 *
11212 * <p>The default extent is the drawing height of this view.</p>
11213 *
11214 * @return the vertical extent of the scrollbar's thumb
11215 *
11216 * @see #computeVerticalScrollRange()
11217 * @see #computeVerticalScrollOffset()
11218 * @see android.widget.ScrollBarDrawable
11219 */
11220 protected int computeVerticalScrollExtent() {
11221 return getHeight();
11222 }
11223
11224 /**
Adam Powell69159442011-06-13 17:53:06 -070011225 * Check if this view can be scrolled horizontally in a certain direction.
11226 *
11227 * @param direction Negative to check scrolling left, positive to check scrolling right.
11228 * @return true if this view can be scrolled in the specified direction, false otherwise.
11229 */
11230 public boolean canScrollHorizontally(int direction) {
11231 final int offset = computeHorizontalScrollOffset();
11232 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11233 if (range == 0) return false;
11234 if (direction < 0) {
11235 return offset > 0;
11236 } else {
11237 return offset < range - 1;
11238 }
11239 }
11240
11241 /**
11242 * Check if this view can be scrolled vertically in a certain direction.
11243 *
11244 * @param direction Negative to check scrolling up, positive to check scrolling down.
11245 * @return true if this view can be scrolled in the specified direction, false otherwise.
11246 */
11247 public boolean canScrollVertically(int direction) {
11248 final int offset = computeVerticalScrollOffset();
11249 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11250 if (range == 0) return false;
11251 if (direction < 0) {
11252 return offset > 0;
11253 } else {
11254 return offset < range - 1;
11255 }
11256 }
11257
11258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11260 * scrollbars are painted only if they have been awakened first.</p>
11261 *
11262 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011263 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011264 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011266 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 // scrollbars are drawn only when the animation is running
11268 final ScrollabilityCache cache = mScrollCache;
11269 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011270
Mike Cleronf116bf82009-09-27 19:14:12 -070011271 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011272
Mike Cleronf116bf82009-09-27 19:14:12 -070011273 if (state == ScrollabilityCache.OFF) {
11274 return;
11275 }
Joe Malin32736f02011-01-19 16:14:20 -080011276
Mike Cleronf116bf82009-09-27 19:14:12 -070011277 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011278
Mike Cleronf116bf82009-09-27 19:14:12 -070011279 if (state == ScrollabilityCache.FADING) {
11280 // We're fading -- get our fade interpolation
11281 if (cache.interpolatorValues == null) {
11282 cache.interpolatorValues = new float[1];
11283 }
Joe Malin32736f02011-01-19 16:14:20 -080011284
Mike Cleronf116bf82009-09-27 19:14:12 -070011285 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011286
Mike Cleronf116bf82009-09-27 19:14:12 -070011287 // Stops the animation if we're done
11288 if (cache.scrollBarInterpolator.timeToValues(values) ==
11289 Interpolator.Result.FREEZE_END) {
11290 cache.state = ScrollabilityCache.OFF;
11291 } else {
11292 cache.scrollBar.setAlpha(Math.round(values[0]));
11293 }
Joe Malin32736f02011-01-19 16:14:20 -080011294
11295 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011296 // drawing. We only want this when we're fading so that
11297 // we prevent excessive redraws
11298 invalidate = true;
11299 } else {
11300 // We're just on -- but we may have been fading before so
11301 // reset alpha
11302 cache.scrollBar.setAlpha(255);
11303 }
11304
Joe Malin32736f02011-01-19 16:14:20 -080011305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011306 final int viewFlags = mViewFlags;
11307
11308 final boolean drawHorizontalScrollBar =
11309 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11310 final boolean drawVerticalScrollBar =
11311 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11312 && !isVerticalScrollBarHidden();
11313
11314 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11315 final int width = mRight - mLeft;
11316 final int height = mBottom - mTop;
11317
11318 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319
Mike Reede8853fc2009-09-04 14:01:48 -040011320 final int scrollX = mScrollX;
11321 final int scrollY = mScrollY;
11322 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11323
Mike Cleronf116bf82009-09-27 19:14:12 -070011324 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011326 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011327 int size = scrollBar.getSize(false);
11328 if (size <= 0) {
11329 size = cache.scrollBarSize;
11330 }
11331
Mike Cleronf116bf82009-09-27 19:14:12 -070011332 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011333 computeHorizontalScrollOffset(),
11334 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011335 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011336 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011337 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011338 left = scrollX + (mPaddingLeft & inside);
11339 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11340 bottom = top + size;
11341 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11342 if (invalidate) {
11343 invalidate(left, top, right, bottom);
11344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 }
11346
11347 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011348 int size = scrollBar.getSize(true);
11349 if (size <= 0) {
11350 size = cache.scrollBarSize;
11351 }
11352
Mike Reede8853fc2009-09-04 14:01:48 -040011353 scrollBar.setParameters(computeVerticalScrollRange(),
11354 computeVerticalScrollOffset(),
11355 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011356 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11357 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11358 verticalScrollbarPosition = isLayoutRtl() ?
11359 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11360 }
11361 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011362 default:
Adam Powell20232d02010-12-08 21:08:53 -080011363 case SCROLLBAR_POSITION_RIGHT:
11364 left = scrollX + width - size - (mUserPaddingRight & inside);
11365 break;
11366 case SCROLLBAR_POSITION_LEFT:
11367 left = scrollX + (mUserPaddingLeft & inside);
11368 break;
11369 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011370 top = scrollY + (mPaddingTop & inside);
11371 right = left + size;
11372 bottom = scrollY + height - (mUserPaddingBottom & inside);
11373 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11374 if (invalidate) {
11375 invalidate(left, top, right, bottom);
11376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 }
11378 }
11379 }
11380 }
Romain Guy8506ab42009-06-11 17:35:47 -070011381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011382 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011383 * 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 -080011384 * FastScroller is visible.
11385 * @return whether to temporarily hide the vertical scrollbar
11386 * @hide
11387 */
11388 protected boolean isVerticalScrollBarHidden() {
11389 return false;
11390 }
11391
11392 /**
11393 * <p>Draw the horizontal scrollbar if
11394 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11395 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 * @param canvas the canvas on which to draw the scrollbar
11397 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011398 *
11399 * @see #isHorizontalScrollBarEnabled()
11400 * @see #computeHorizontalScrollRange()
11401 * @see #computeHorizontalScrollExtent()
11402 * @see #computeHorizontalScrollOffset()
11403 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011404 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011405 */
Romain Guy8fb95422010-08-17 18:38:51 -070011406 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11407 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011408 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011409 scrollBar.draw(canvas);
11410 }
Mike Reede8853fc2009-09-04 14:01:48 -040011411
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11414 * returns true.</p>
11415 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416 * @param canvas the canvas on which to draw the scrollbar
11417 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011418 *
11419 * @see #isVerticalScrollBarEnabled()
11420 * @see #computeVerticalScrollRange()
11421 * @see #computeVerticalScrollExtent()
11422 * @see #computeVerticalScrollOffset()
11423 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011424 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425 */
Romain Guy8fb95422010-08-17 18:38:51 -070011426 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11427 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011428 scrollBar.setBounds(l, t, r, b);
11429 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011430 }
11431
11432 /**
11433 * Implement this to do your drawing.
11434 *
11435 * @param canvas the canvas on which the background will be drawn
11436 */
11437 protected void onDraw(Canvas canvas) {
11438 }
11439
11440 /*
11441 * Caller is responsible for calling requestLayout if necessary.
11442 * (This allows addViewInLayout to not request a new layout.)
11443 */
11444 void assignParent(ViewParent parent) {
11445 if (mParent == null) {
11446 mParent = parent;
11447 } else if (parent == null) {
11448 mParent = null;
11449 } else {
11450 throw new RuntimeException("view " + this + " being added, but"
11451 + " it already has a parent");
11452 }
11453 }
11454
11455 /**
11456 * This is called when the view is attached to a window. At this point it
11457 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011458 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11459 * however it may be called any time before the first onDraw -- including
11460 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 *
11462 * @see #onDetachedFromWindow()
11463 */
11464 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011465 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 mParent.requestTransparentRegion(this);
11467 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011468
Dianne Hackborn4702a852012-08-17 15:18:29 -070011469 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011470 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011471 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011472 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011473
Chet Haasea9b61ac2010-12-20 07:40:25 -080011474 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011475
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011476 resolveRtlProperties();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011477 // Notify changes
11478 onRtlPropertiesChanged();
Romain Guy2a0f2282012-05-08 14:43:12 -070011479
Svetoslav Ganov42138042012-03-20 11:51:39 -070011480 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011481 if (isFocused()) {
11482 InputMethodManager imm = InputMethodManager.peekInstance();
11483 imm.focusIn(this);
11484 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011485
11486 if (mAttachInfo != null && mDisplayList != null) {
11487 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11488 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011489 }
Cibu Johny86666632010-02-22 13:01:02 -080011490
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011491 /**
11492 * Resolve all RTL related properties
11493 */
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011494 void resolveRtlProperties() {
11495 // Order is important here: LayoutDirection MUST be resolved first...
11496 resolveLayoutDirection();
11497 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011498 resolveTextDirection();
11499 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011500 resolvePadding();
11501 resolveLayoutParams();
Fabrice Di Meglio0af4b8b2012-06-11 18:30:05 -070011502 resolveDrawables();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011503 }
11504
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011505 // Reset resolution of all RTL related properties
11506 void resetRtlProperties() {
11507 resetResolvedLayoutDirection();
11508 resetResolvedTextDirection();
11509 resetResolvedTextAlignment();
11510 resetResolvedPadding();
11511 }
11512
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011513 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011514 * @see #onScreenStateChanged(int)
11515 */
11516 void dispatchScreenStateChanged(int screenState) {
11517 onScreenStateChanged(screenState);
11518 }
11519
11520 /**
11521 * This method is called whenever the state of the screen this view is
11522 * attached to changes. A state change will usually occurs when the screen
11523 * turns on or off (whether it happens automatically or the user does it
11524 * manually.)
11525 *
11526 * @param screenState The new state of the screen. Can be either
11527 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11528 */
11529 public void onScreenStateChanged(int screenState) {
11530 }
11531
11532 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011533 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11534 */
11535 private boolean hasRtlSupport() {
11536 return mContext.getApplicationInfo().hasRtlSupport();
11537 }
11538
11539 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011540 * Called when any RTL property (layout direction or text direction or text alignment) has
11541 * been changed.
11542 *
11543 * Subclasses need to override this method to take care of cached information that depends on the
11544 * resolved layout direction, or to inform child views that inherit their layout direction.
11545 *
11546 * The default implementation does nothing.
11547 */
11548 public void onRtlPropertiesChanged() {
11549 }
11550
11551 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011552 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11553 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011554 *
11555 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011556 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011557 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011558 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011559 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011560
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011561 if (hasRtlSupport()) {
11562 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011563 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11564 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011565 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011566 // We cannot resolve yet. LTR is by default and let the resolution happen again
11567 // later to get the correct resolved value
11568 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011569
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011570 ViewGroup viewGroup = ((ViewGroup) mParent);
11571
11572 // We cannot resolve yet on the parent too. LTR is by default and let the
11573 // resolution happen again later
11574 if (!viewGroup.canResolveLayoutDirection()) return;
11575
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011576 if (viewGroup.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011577 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011578 }
11579 break;
11580 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011581 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011582 break;
11583 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011584 if((LAYOUT_DIRECTION_RTL ==
11585 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011586 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011587 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011588 break;
11589 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011590 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011591 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011592 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011593
11594 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011595 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011596 }
11597
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011598 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011599 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011600 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011601 * @return true if layout direction resolution can be done otherwise return false.
11602 *
11603 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011604 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011605 public boolean canResolveLayoutDirection() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011606 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11607 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011608 case LAYOUT_DIRECTION_INHERIT:
11609 return (mParent != null) && (mParent instanceof ViewGroup);
11610 default:
11611 return true;
11612 }
11613 }
11614
11615 /**
11616 * Reset the resolved layout direction.
11617 *
11618 * @hide
11619 */
11620 public void resetResolvedLayoutDirection() {
11621 // Reset the current resolved bits
11622 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011623 }
11624
11625 /**
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011626 * @hide
11627 */
11628 public boolean isLayoutDirectionInherited() {
11629 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11630 }
11631
11632 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011633 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011634 *
11635 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011636 */
11637 boolean isPaddingResolved() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011638 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) != 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011639 }
11640
11641 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011642 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011643 *
11644 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011645 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011646 public void resolvePadding() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011647 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11648 if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport()) {
11649 // Pre Jelly Bean MR1 case (compatibility mode) OR no RTL support case:
11650 // left / right padding are used if defined. If they are not defined and start / end
11651 // padding are defined (e.g. in Frameworks resources), then we use start / end and
11652 // resolve them as left / right (layout direction is not taken into account).
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011653 if (mUserPaddingLeftInitial == UNDEFINED_PADDING &&
11654 mUserPaddingStart != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011655 mUserPaddingLeft = mUserPaddingStart;
11656 }
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011657 if (mUserPaddingRightInitial == UNDEFINED_PADDING
11658 && mUserPaddingEnd != UNDEFINED_PADDING) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011659 mUserPaddingRight = mUserPaddingEnd;
11660 }
11661
11662 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11663
11664 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11665 mUserPaddingBottom);
11666 } else {
11667 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11668 // If start / end padding are defined, they will be resolved (hence overriding) to
11669 // left / right or right / left depending on the resolved layout direction.
11670 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011671 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011672 // Set user padding to initial values ...
11673 mUserPaddingLeft = (mUserPaddingLeftInitial == UNDEFINED_PADDING) ?
11674 0 : mUserPaddingLeftInitial;
11675 mUserPaddingRight = (mUserPaddingRightInitial == UNDEFINED_PADDING) ?
11676 0 : mUserPaddingRightInitial;
11677 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011678 switch (resolvedLayoutDirection) {
11679 case LAYOUT_DIRECTION_RTL:
11680 if (mUserPaddingStart != UNDEFINED_PADDING) {
11681 mUserPaddingRight = mUserPaddingStart;
11682 }
11683 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11684 mUserPaddingLeft = mUserPaddingEnd;
11685 }
11686 break;
11687 case LAYOUT_DIRECTION_LTR:
11688 default:
11689 if (mUserPaddingStart != UNDEFINED_PADDING) {
11690 mUserPaddingLeft = mUserPaddingStart;
11691 }
11692 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11693 mUserPaddingRight = mUserPaddingEnd;
11694 }
11695 }
11696
11697 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11698
11699 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11700 mUserPaddingBottom);
11701 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011702 }
11703
Dianne Hackborn4702a852012-08-17 15:18:29 -070011704 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011705 }
11706
11707 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011708 * Reset the resolved layout direction.
11709 *
11710 * @hide
11711 */
11712 public void resetResolvedPadding() {
11713 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11714 }
11715
11716 /**
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011717 * Resolve padding depending on the layout direction. Subclasses that care about
11718 * padding resolution should override this method. The default implementation does
11719 * nothing.
11720 *
11721 * @param layoutDirection the direction of the layout
11722 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070011723 * @see #LAYOUT_DIRECTION_LTR
11724 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011725 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011726 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011727 }
11728
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011730 * This is called when the view is detached from a window. At this point it
11731 * no longer has a surface for drawing.
11732 *
11733 * @see #onAttachedToWindow()
11734 */
11735 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011736 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011737
Romain Guya440b002010-02-24 15:57:54 -080011738 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011739 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011740 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011741 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011743 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011744
Romain Guya998dff2012-03-23 18:58:36 -070011745 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011746
11747 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011748 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011749 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011750 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011751 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011752 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011753 // Should never happen
11754 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011755 }
11756
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011757 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011758
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011759 resetRtlProperties();
11760 onRtlPropertiesChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011761 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 }
11763
11764 /**
11765 * @return The number of times this view has been attached to a window
11766 */
11767 protected int getWindowAttachCount() {
11768 return mWindowAttachCount;
11769 }
11770
11771 /**
11772 * Retrieve a unique token identifying the window this view is attached to.
11773 * @return Return the window's token for use in
11774 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11775 */
11776 public IBinder getWindowToken() {
11777 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11778 }
11779
11780 /**
11781 * Retrieve a unique token identifying the top-level "real" window of
11782 * the window that this view is attached to. That is, this is like
11783 * {@link #getWindowToken}, except if the window this view in is a panel
11784 * window (attached to another containing window), then the token of
11785 * the containing window is returned instead.
11786 *
11787 * @return Returns the associated window token, either
11788 * {@link #getWindowToken()} or the containing window's token.
11789 */
11790 public IBinder getApplicationWindowToken() {
11791 AttachInfo ai = mAttachInfo;
11792 if (ai != null) {
11793 IBinder appWindowToken = ai.mPanelParentWindowToken;
11794 if (appWindowToken == null) {
11795 appWindowToken = ai.mWindowToken;
11796 }
11797 return appWindowToken;
11798 }
11799 return null;
11800 }
11801
11802 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011803 * Gets the logical display to which the view's window has been attached.
11804 *
11805 * @return The logical display, or null if the view is not currently attached to a window.
11806 */
11807 public Display getDisplay() {
11808 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11809 }
11810
11811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011812 * Retrieve private session object this view hierarchy is using to
11813 * communicate with the window manager.
11814 * @return the session object to communicate with the window manager
11815 */
11816 /*package*/ IWindowSession getWindowSession() {
11817 return mAttachInfo != null ? mAttachInfo.mSession : null;
11818 }
11819
11820 /**
11821 * @param info the {@link android.view.View.AttachInfo} to associated with
11822 * this view
11823 */
11824 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11825 //System.out.println("Attached! " + this);
11826 mAttachInfo = info;
11827 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011828 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011829 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011830 if (mFloatingTreeObserver != null) {
11831 info.mTreeObserver.merge(mFloatingTreeObserver);
11832 mFloatingTreeObserver = null;
11833 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011834 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011835 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011836 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011837 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011838 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011839 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011840
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011841 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011842 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011843 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011844 if (listeners != null && listeners.size() > 0) {
11845 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11846 // perform the dispatching. The iterator is a safe guard against listeners that
11847 // could mutate the list by calling the various add/remove methods. This prevents
11848 // the array from being modified while we iterate it.
11849 for (OnAttachStateChangeListener listener : listeners) {
11850 listener.onViewAttachedToWindow(this);
11851 }
11852 }
11853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 int vis = info.mWindowVisibility;
11855 if (vis != GONE) {
11856 onWindowVisibilityChanged(vis);
11857 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011858 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011859 // If nobody has evaluated the drawable state yet, then do it now.
11860 refreshDrawableState();
11861 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011862 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011863 }
11864
11865 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 AttachInfo info = mAttachInfo;
11867 if (info != null) {
11868 int vis = info.mWindowVisibility;
11869 if (vis != GONE) {
11870 onWindowVisibilityChanged(GONE);
11871 }
11872 }
11873
11874 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011875
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011876 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011877 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011878 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011879 if (listeners != null && listeners.size() > 0) {
11880 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11881 // perform the dispatching. The iterator is a safe guard against listeners that
11882 // could mutate the list by calling the various add/remove methods. This prevents
11883 // the array from being modified while we iterate it.
11884 for (OnAttachStateChangeListener listener : listeners) {
11885 listener.onViewDetachedFromWindow(this);
11886 }
11887 }
11888
Dianne Hackborn4702a852012-08-17 15:18:29 -070011889 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011890 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011891 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 mAttachInfo = null;
11895 }
11896
11897 /**
11898 * Store this view hierarchy's frozen state into the given container.
11899 *
11900 * @param container The SparseArray in which to save the view's state.
11901 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011902 * @see #restoreHierarchyState(android.util.SparseArray)
11903 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11904 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011905 */
11906 public void saveHierarchyState(SparseArray<Parcelable> container) {
11907 dispatchSaveInstanceState(container);
11908 }
11909
11910 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011911 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11912 * this view and its children. May be overridden to modify how freezing happens to a
11913 * 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 -080011914 *
11915 * @param container The SparseArray in which to save the view's state.
11916 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011917 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11918 * @see #saveHierarchyState(android.util.SparseArray)
11919 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 */
11921 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11922 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011923 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011924 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011925 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 throw new IllegalStateException(
11927 "Derived class did not call super.onSaveInstanceState()");
11928 }
11929 if (state != null) {
11930 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11931 // + ": " + state);
11932 container.put(mID, state);
11933 }
11934 }
11935 }
11936
11937 /**
11938 * Hook allowing a view to generate a representation of its internal state
11939 * that can later be used to create a new instance with that same state.
11940 * This state should only contain information that is not persistent or can
11941 * not be reconstructed later. For example, you will never store your
11942 * current position on screen because that will be computed again when a
11943 * new instance of the view is placed in its view hierarchy.
11944 * <p>
11945 * Some examples of things you may store here: the current cursor position
11946 * in a text view (but usually not the text itself since that is stored in a
11947 * content provider or other persistent storage), the currently selected
11948 * item in a list view.
11949 *
11950 * @return Returns a Parcelable object containing the view's current dynamic
11951 * state, or null if there is nothing interesting to save. The
11952 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011953 * @see #onRestoreInstanceState(android.os.Parcelable)
11954 * @see #saveHierarchyState(android.util.SparseArray)
11955 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011956 * @see #setSaveEnabled(boolean)
11957 */
11958 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011959 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011960 return BaseSavedState.EMPTY_STATE;
11961 }
11962
11963 /**
11964 * Restore this view hierarchy's frozen state from the given container.
11965 *
11966 * @param container The SparseArray which holds previously frozen states.
11967 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011968 * @see #saveHierarchyState(android.util.SparseArray)
11969 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11970 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 */
11972 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11973 dispatchRestoreInstanceState(container);
11974 }
11975
11976 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011977 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11978 * state for this view and its children. May be overridden to modify how restoring
11979 * happens to a view's children; for example, some views may want to not store state
11980 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 *
11982 * @param container The SparseArray which holds previously saved state.
11983 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011984 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11985 * @see #restoreHierarchyState(android.util.SparseArray)
11986 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 */
11988 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11989 if (mID != NO_ID) {
11990 Parcelable state = container.get(mID);
11991 if (state != null) {
11992 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11993 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011994 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011996 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 throw new IllegalStateException(
11998 "Derived class did not call super.onRestoreInstanceState()");
11999 }
12000 }
12001 }
12002 }
12003
12004 /**
12005 * Hook allowing a view to re-apply a representation of its internal state that had previously
12006 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12007 * null state.
12008 *
12009 * @param state The frozen state that had previously been returned by
12010 * {@link #onSaveInstanceState}.
12011 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012012 * @see #onSaveInstanceState()
12013 * @see #restoreHierarchyState(android.util.SparseArray)
12014 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 */
12016 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012017 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012019 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12020 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012021 + "when two views of different type have the same id in the same hierarchy. "
12022 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012023 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 }
12025 }
12026
12027 /**
12028 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12029 *
12030 * @return the drawing start time in milliseconds
12031 */
12032 public long getDrawingTime() {
12033 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12034 }
12035
12036 /**
12037 * <p>Enables or disables the duplication of the parent's state into this view. When
12038 * duplication is enabled, this view gets its drawable state from its parent rather
12039 * than from its own internal properties.</p>
12040 *
12041 * <p>Note: in the current implementation, setting this property to true after the
12042 * view was added to a ViewGroup might have no effect at all. This property should
12043 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12044 *
12045 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12046 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012047 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012048 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12049 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012050 *
12051 * @param enabled True to enable duplication of the parent's drawable state, false
12052 * to disable it.
12053 *
12054 * @see #getDrawableState()
12055 * @see #isDuplicateParentStateEnabled()
12056 */
12057 public void setDuplicateParentStateEnabled(boolean enabled) {
12058 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12059 }
12060
12061 /**
12062 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12063 *
12064 * @return True if this view's drawable state is duplicated from the parent,
12065 * false otherwise
12066 *
12067 * @see #getDrawableState()
12068 * @see #setDuplicateParentStateEnabled(boolean)
12069 */
12070 public boolean isDuplicateParentStateEnabled() {
12071 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12072 }
12073
12074 /**
Romain Guy171c5922011-01-06 10:04:23 -080012075 * <p>Specifies the type of layer backing this view. The layer can be
12076 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12077 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012078 *
Romain Guy171c5922011-01-06 10:04:23 -080012079 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12080 * instance that controls how the layer is composed on screen. The following
12081 * properties of the paint are taken into account when composing the layer:</p>
12082 * <ul>
12083 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12084 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12085 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12086 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012087 *
Romain Guy171c5922011-01-06 10:04:23 -080012088 * <p>If this view has an alpha value set to < 1.0 by calling
12089 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12090 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12091 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012092 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012093 *
Romain Guy171c5922011-01-06 10:04:23 -080012094 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12095 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12096 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012097 *
Chet Haased15ebf22012-09-05 11:40:29 -070012098 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012099 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12100 * {@link #LAYER_TYPE_HARDWARE}
12101 * @param paint The paint used to compose the layer. This argument is optional
12102 * and can be null. It is ignored when the layer type is
12103 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012104 *
12105 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012106 * @see #LAYER_TYPE_NONE
12107 * @see #LAYER_TYPE_SOFTWARE
12108 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012109 * @see #setAlpha(float)
12110 *
Romain Guy171c5922011-01-06 10:04:23 -080012111 * @attr ref android.R.styleable#View_layerType
12112 */
12113 public void setLayerType(int layerType, Paint paint) {
12114 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012115 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012116 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12117 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012118
Romain Guyd6cd5722011-01-17 14:42:41 -080012119 if (layerType == mLayerType) {
12120 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12121 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012122 invalidateParentCaches();
12123 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012124 }
12125 return;
12126 }
Romain Guy171c5922011-01-06 10:04:23 -080012127
12128 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012129 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012130 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012131 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012132 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012133 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012134 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012135 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012136 default:
12137 break;
Romain Guy171c5922011-01-06 10:04:23 -080012138 }
12139
12140 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012141 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12142 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12143 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012144
Romain Guy0fd89bf2011-01-26 15:41:30 -080012145 invalidateParentCaches();
12146 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012147 }
12148
12149 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012150 * Updates the {@link Paint} object used with the current layer (used only if the current
12151 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12152 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12153 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12154 * ensure that the view gets redrawn immediately.
12155 *
12156 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12157 * instance that controls how the layer is composed on screen. The following
12158 * properties of the paint are taken into account when composing the layer:</p>
12159 * <ul>
12160 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12161 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12162 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12163 * </ul>
12164 *
12165 * <p>If this view has an alpha value set to < 1.0 by calling
12166 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12167 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12168 * equivalent to setting a hardware layer on this view and providing a paint with
12169 * the desired alpha value.</p>
12170 *
12171 * @param paint The paint used to compose the layer. This argument is optional
12172 * and can be null. It is ignored when the layer type is
12173 * {@link #LAYER_TYPE_NONE}
12174 *
12175 * @see #setLayerType(int, android.graphics.Paint)
12176 */
12177 public void setLayerPaint(Paint paint) {
12178 int layerType = getLayerType();
12179 if (layerType != LAYER_TYPE_NONE) {
12180 mLayerPaint = paint == null ? new Paint() : paint;
12181 if (layerType == LAYER_TYPE_HARDWARE) {
12182 HardwareLayer layer = getHardwareLayer();
12183 if (layer != null) {
12184 layer.setLayerPaint(paint);
12185 }
12186 invalidateViewProperty(false, false);
12187 } else {
12188 invalidate();
12189 }
12190 }
12191 }
12192
12193 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012194 * Indicates whether this view has a static layer. A view with layer type
12195 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12196 * dynamic.
12197 */
12198 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012199 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012200 }
12201
12202 /**
Romain Guy171c5922011-01-06 10:04:23 -080012203 * Indicates what type of layer is currently associated with this view. By default
12204 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12205 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12206 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012207 *
Romain Guy171c5922011-01-06 10:04:23 -080012208 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12209 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012210 *
12211 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012212 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012213 * @see #LAYER_TYPE_NONE
12214 * @see #LAYER_TYPE_SOFTWARE
12215 * @see #LAYER_TYPE_HARDWARE
12216 */
12217 public int getLayerType() {
12218 return mLayerType;
12219 }
Joe Malin32736f02011-01-19 16:14:20 -080012220
Romain Guy6c319ca2011-01-11 14:29:25 -080012221 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012222 * Forces this view's layer to be created and this view to be rendered
12223 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12224 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012225 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012226 * This method can for instance be used to render a view into its layer before
12227 * starting an animation. If this view is complex, rendering into the layer
12228 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012229 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012230 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012231 *
12232 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012233 */
12234 public void buildLayer() {
12235 if (mLayerType == LAYER_TYPE_NONE) return;
12236
12237 if (mAttachInfo == null) {
12238 throw new IllegalStateException("This view must be attached to a window first");
12239 }
12240
12241 switch (mLayerType) {
12242 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012243 if (mAttachInfo.mHardwareRenderer != null &&
12244 mAttachInfo.mHardwareRenderer.isEnabled() &&
12245 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012246 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012247 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012248 break;
12249 case LAYER_TYPE_SOFTWARE:
12250 buildDrawingCache(true);
12251 break;
12252 }
12253 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012254
Romain Guyf1ae1062011-03-02 18:16:04 -080012255 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012256 * <p>Returns a hardware layer that can be used to draw this view again
12257 * without executing its draw method.</p>
12258 *
12259 * @return A HardwareLayer ready to render, or null if an error occurred.
12260 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012261 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012262 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12263 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012264 return null;
12265 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012266
Romain Guy9c4b79a2011-11-10 19:23:58 -080012267 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012268
12269 final int width = mRight - mLeft;
12270 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012271
Romain Guy6c319ca2011-01-11 14:29:25 -080012272 if (width == 0 || height == 0) {
12273 return null;
12274 }
12275
Dianne Hackborn4702a852012-08-17 15:18:29 -070012276 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012277 if (mHardwareLayer == null) {
12278 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12279 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012280 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012281 } else {
12282 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012283 if (mHardwareLayer.resize(width, height)) {
12284 mLocalDirtyRect.set(0, 0, width, height);
12285 }
Romain Guy846a5332012-07-11 17:44:57 -070012286 }
12287
12288 // This should not be necessary but applications that change
12289 // the parameters of their background drawable without calling
12290 // this.setBackground(Drawable) can leave the view in a bad state
12291 // (for instance isOpaque() returns true, but the background is
12292 // not opaque.)
12293 computeOpaqueFlags();
12294
12295 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012296 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012297 mHardwareLayer.setOpaque(opaque);
12298 mLocalDirtyRect.set(0, 0, width, height);
12299 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012300 }
12301
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012302 // The layer is not valid if the underlying GPU resources cannot be allocated
12303 if (!mHardwareLayer.isValid()) {
12304 return null;
12305 }
Chet Haased15ebf22012-09-05 11:40:29 -070012306 mHardwareLayer.setLayerPaint(mLayerPaint);
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012307
Chet Haasea1cff502012-02-21 13:43:44 -080012308 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080012309 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012310 }
12311
12312 return mHardwareLayer;
12313 }
Romain Guy171c5922011-01-06 10:04:23 -080012314
Romain Guy589b0bb2011-10-10 13:57:47 -070012315 /**
12316 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012317 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012318 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012319 *
12320 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012321 * @see #LAYER_TYPE_HARDWARE
12322 */
Romain Guya998dff2012-03-23 18:58:36 -070012323 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012324 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012325 AttachInfo info = mAttachInfo;
12326 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012327 info.mHardwareRenderer.isEnabled() &&
12328 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012329 mHardwareLayer.destroy();
12330 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012331
Romain Guy9c4b79a2011-11-10 19:23:58 -080012332 invalidate(true);
12333 invalidateParentCaches();
12334 }
Romain Guy65b345f2011-07-27 18:51:50 -070012335 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012336 }
Romain Guy65b345f2011-07-27 18:51:50 -070012337 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012338 }
12339
Romain Guy171c5922011-01-06 10:04:23 -080012340 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012341 * Destroys all hardware rendering resources. This method is invoked
12342 * when the system needs to reclaim resources. Upon execution of this
12343 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012344 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012345 * Note: you <strong>must</strong> call
12346 * <code>super.destroyHardwareResources()</code> when overriding
12347 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012348 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012349 * @hide
12350 */
12351 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012352 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012353 }
12354
12355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012356 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12357 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12358 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12359 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12360 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12361 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012362 *
Romain Guy171c5922011-01-06 10:04:23 -080012363 * <p>Enabling the drawing cache is similar to
12364 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012365 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12366 * drawing cache has no effect on rendering because the system uses a different mechanism
12367 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12368 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12369 * for information on how to enable software and hardware layers.</p>
12370 *
12371 * <p>This API can be used to manually generate
12372 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12373 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012374 *
12375 * @param enabled true to enable the drawing cache, false otherwise
12376 *
12377 * @see #isDrawingCacheEnabled()
12378 * @see #getDrawingCache()
12379 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012380 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 */
12382 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012383 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012384 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12385 }
12386
12387 /**
12388 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12389 *
12390 * @return true if the drawing cache is enabled
12391 *
12392 * @see #setDrawingCacheEnabled(boolean)
12393 * @see #getDrawingCache()
12394 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012395 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012396 public boolean isDrawingCacheEnabled() {
12397 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12398 }
12399
12400 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012401 * Debugging utility which recursively outputs the dirty state of a view and its
12402 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012403 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012404 * @hide
12405 */
Romain Guy676b1732011-02-14 14:45:33 -080012406 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012407 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012408 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12409 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12410 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12411 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012412 if (clear) {
12413 mPrivateFlags &= clearMask;
12414 }
12415 if (this instanceof ViewGroup) {
12416 ViewGroup parent = (ViewGroup) this;
12417 final int count = parent.getChildCount();
12418 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012419 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012420 child.outputDirtyFlags(indent + " ", clear, clearMask);
12421 }
12422 }
12423 }
12424
12425 /**
12426 * This method is used by ViewGroup to cause its children to restore or recreate their
12427 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12428 * to recreate its own display list, which would happen if it went through the normal
12429 * draw/dispatchDraw mechanisms.
12430 *
12431 * @hide
12432 */
12433 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012434
12435 /**
12436 * A view that is not attached or hardware accelerated cannot create a display list.
12437 * This method checks these conditions and returns the appropriate result.
12438 *
12439 * @return true if view has the ability to create a display list, false otherwise.
12440 *
12441 * @hide
12442 */
12443 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012444 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012445 }
Joe Malin32736f02011-01-19 16:14:20 -080012446
Chet Haasedaf98e92011-01-10 14:10:36 -080012447 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012448 * @return The HardwareRenderer associated with that view or null if hardware rendering
12449 * is not supported or this this has not been attached to a window.
12450 *
12451 * @hide
12452 */
12453 public HardwareRenderer getHardwareRenderer() {
12454 if (mAttachInfo != null) {
12455 return mAttachInfo.mHardwareRenderer;
12456 }
12457 return null;
12458 }
12459
12460 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012461 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12462 * Otherwise, the same display list will be returned (after having been rendered into
12463 * along the way, depending on the invalidation state of the view).
12464 *
12465 * @param displayList The previous version of this displayList, could be null.
12466 * @param isLayer Whether the requester of the display list is a layer. If so,
12467 * the view will avoid creating a layer inside the resulting display list.
12468 * @return A new or reused DisplayList object.
12469 */
12470 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12471 if (!canHaveDisplayList()) {
12472 return null;
12473 }
12474
Dianne Hackborn4702a852012-08-17 15:18:29 -070012475 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012476 displayList == null || !displayList.isValid() ||
12477 (!isLayer && mRecreateDisplayList))) {
12478 // Don't need to recreate the display list, just need to tell our
12479 // children to restore/recreate theirs
12480 if (displayList != null && displayList.isValid() &&
12481 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012482 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12483 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012484 dispatchGetDisplayList();
12485
12486 return displayList;
12487 }
12488
12489 if (!isLayer) {
12490 // If we got here, we're recreating it. Mark it as such to ensure that
12491 // we copy in child display lists into ours in drawChild()
12492 mRecreateDisplayList = true;
12493 }
12494 if (displayList == null) {
12495 final String name = getClass().getSimpleName();
12496 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12497 // If we're creating a new display list, make sure our parent gets invalidated
12498 // since they will need to recreate their display list to account for this
12499 // new child display list.
12500 invalidateParentCaches();
12501 }
12502
12503 boolean caching = false;
12504 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012505 int width = mRight - mLeft;
12506 int height = mBottom - mTop;
12507
12508 try {
12509 canvas.setViewport(width, height);
12510 // The dirty rect should always be null for a display list
12511 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012512 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012513 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012514 if (layerType == LAYER_TYPE_HARDWARE) {
12515 final HardwareLayer layer = getHardwareLayer();
12516 if (layer != null && layer.isValid()) {
12517 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12518 } else {
12519 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12520 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12521 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12522 }
12523 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012524 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012525 buildDrawingCache(true);
12526 Bitmap cache = getDrawingCache(true);
12527 if (cache != null) {
12528 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12529 caching = true;
12530 }
Chet Haasea1cff502012-02-21 13:43:44 -080012531 }
Chet Haasea1cff502012-02-21 13:43:44 -080012532 } else {
12533
12534 computeScroll();
12535
Chet Haasea1cff502012-02-21 13:43:44 -080012536 canvas.translate(-mScrollX, -mScrollY);
12537 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012538 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12539 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012540 }
12541
12542 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012543 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012544 dispatchDraw(canvas);
12545 } else {
12546 draw(canvas);
12547 }
12548 }
12549 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012550 canvas.onPostDraw();
12551
12552 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012553 displayList.setCaching(caching);
12554 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012555 displayList.setLeftTopRightBottom(0, 0, width, height);
12556 } else {
12557 setDisplayListProperties(displayList);
12558 }
12559 }
12560 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012561 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12562 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012563 }
12564
12565 return displayList;
12566 }
12567
12568 /**
12569 * Get the DisplayList for the HardwareLayer
12570 *
12571 * @param layer The HardwareLayer whose DisplayList we want
12572 * @return A DisplayList fopr the specified HardwareLayer
12573 */
12574 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12575 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12576 layer.setDisplayList(displayList);
12577 return displayList;
12578 }
12579
12580
12581 /**
Romain Guyb051e892010-09-28 19:09:36 -070012582 * <p>Returns a display list that can be used to draw this view again
12583 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012584 *
Romain Guyb051e892010-09-28 19:09:36 -070012585 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012586 *
12587 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012588 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012589 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012590 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012591 return mDisplayList;
12592 }
12593
Romain Guy38c2ece2012-05-24 14:20:56 -070012594 private void clearDisplayList() {
12595 if (mDisplayList != null) {
12596 mDisplayList.invalidate();
12597 mDisplayList.clear();
12598 }
12599 }
12600
Romain Guyb051e892010-09-28 19:09:36 -070012601 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012602 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012603 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012604 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012605 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012606 * @see #getDrawingCache(boolean)
12607 */
12608 public Bitmap getDrawingCache() {
12609 return getDrawingCache(false);
12610 }
12611
12612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012613 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12614 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12615 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12616 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12617 * request the drawing cache by calling this method and draw it on screen if the
12618 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012619 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012620 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12621 * this method will create a bitmap of the same size as this view. Because this bitmap
12622 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12623 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12624 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12625 * size than the view. This implies that your application must be able to handle this
12626 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012627 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012628 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12629 * the current density of the screen when the application is in compatibility
12630 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012631 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012632 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012633 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012634 * @see #setDrawingCacheEnabled(boolean)
12635 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012636 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012637 * @see #destroyDrawingCache()
12638 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012639 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12641 return null;
12642 }
12643 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012644 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012645 }
Romain Guy02890fd2010-08-06 17:58:44 -070012646 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012647 }
12648
12649 /**
12650 * <p>Frees the resources used by the drawing cache. If you call
12651 * {@link #buildDrawingCache()} manually without calling
12652 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12653 * should cleanup the cache with this method afterwards.</p>
12654 *
12655 * @see #setDrawingCacheEnabled(boolean)
12656 * @see #buildDrawingCache()
12657 * @see #getDrawingCache()
12658 */
12659 public void destroyDrawingCache() {
12660 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012661 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012662 mDrawingCache = null;
12663 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012664 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012665 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012666 mUnscaledDrawingCache = null;
12667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012668 }
12669
12670 /**
12671 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012672 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 * view will always be drawn on top of a solid color.
12674 *
12675 * @param color The background color to use for the drawing cache's bitmap
12676 *
12677 * @see #setDrawingCacheEnabled(boolean)
12678 * @see #buildDrawingCache()
12679 * @see #getDrawingCache()
12680 */
12681 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012682 if (color != mDrawingCacheBackgroundColor) {
12683 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012684 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012686 }
12687
12688 /**
12689 * @see #setDrawingCacheBackgroundColor(int)
12690 *
12691 * @return The background color to used for the drawing cache's bitmap
12692 */
12693 public int getDrawingCacheBackgroundColor() {
12694 return mDrawingCacheBackgroundColor;
12695 }
12696
12697 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012698 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012699 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012700 * @see #buildDrawingCache(boolean)
12701 */
12702 public void buildDrawingCache() {
12703 buildDrawingCache(false);
12704 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012705
Romain Guyfbd8f692009-06-26 14:51:58 -070012706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12708 *
12709 * <p>If you call {@link #buildDrawingCache()} manually without calling
12710 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12711 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012712 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012713 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12714 * this method will create a bitmap of the same size as this view. Because this bitmap
12715 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12716 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12717 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12718 * size than the view. This implies that your application must be able to handle this
12719 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012720 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012721 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12722 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012723 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012724 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012725 *
12726 * @see #getDrawingCache()
12727 * @see #destroyDrawingCache()
12728 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012729 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012730 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012731 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012732 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733
Romain Guy8506ab42009-06-11 17:35:47 -070012734 int width = mRight - mLeft;
12735 int height = mBottom - mTop;
12736
12737 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012738 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012739
Romain Guye1123222009-06-29 14:24:56 -070012740 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012741 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12742 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012744
12745 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012746 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012747 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012748
Chris Craik10e9d1d2012-09-06 14:42:44 -070012749 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12750 final int drawingCacheSize =
12751 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12752 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012753 if (width > 0 && height > 0) {
12754 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12755 + projectedBitmapSize + " bytes, only "
12756 + drawingCacheSize + " available");
12757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012759 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 return;
12761 }
12762
12763 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012764 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012765
12766 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012767 Bitmap.Config quality;
12768 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012769 // Never pick ARGB_4444 because it looks awful
12770 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12772 case DRAWING_CACHE_QUALITY_AUTO:
12773 quality = Bitmap.Config.ARGB_8888;
12774 break;
12775 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012776 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 break;
12778 case DRAWING_CACHE_QUALITY_HIGH:
12779 quality = Bitmap.Config.ARGB_8888;
12780 break;
12781 default:
12782 quality = Bitmap.Config.ARGB_8888;
12783 break;
12784 }
12785 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012786 // Optimization for translucent windows
12787 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012788 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012789 }
12790
12791 // Try to cleanup memory
12792 if (bitmap != null) bitmap.recycle();
12793
12794 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012795 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12796 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012797 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012798 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012799 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012800 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012801 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012802 }
Adam Powell26153a32010-11-08 15:22:27 -080012803 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012804 } catch (OutOfMemoryError e) {
12805 // If there is not enough memory to create the bitmap cache, just
12806 // ignore the issue as bitmap caches are not required to draw the
12807 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012808 if (autoScale) {
12809 mDrawingCache = null;
12810 } else {
12811 mUnscaledDrawingCache = null;
12812 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012813 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012814 return;
12815 }
12816
12817 clear = drawingCacheBackgroundColor != 0;
12818 }
12819
12820 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821 if (attachInfo != null) {
12822 canvas = attachInfo.mCanvas;
12823 if (canvas == null) {
12824 canvas = new Canvas();
12825 }
12826 canvas.setBitmap(bitmap);
12827 // Temporarily clobber the cached Canvas in case one of our children
12828 // is also using a drawing cache. Without this, the children would
12829 // steal the canvas by attaching their own bitmap to it and bad, bad
12830 // thing would happen (invisible views, corrupted drawings, etc.)
12831 attachInfo.mCanvas = null;
12832 } else {
12833 // This case should hopefully never or seldom happen
12834 canvas = new Canvas(bitmap);
12835 }
12836
12837 if (clear) {
12838 bitmap.eraseColor(drawingCacheBackgroundColor);
12839 }
12840
12841 computeScroll();
12842 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012843
Romain Guye1123222009-06-29 14:24:56 -070012844 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012845 final float scale = attachInfo.mApplicationScale;
12846 canvas.scale(scale, scale);
12847 }
Joe Malin32736f02011-01-19 16:14:20 -080012848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 canvas.translate(-mScrollX, -mScrollY);
12850
Dianne Hackborn4702a852012-08-17 15:18:29 -070012851 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012852 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12853 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012854 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012856
12857 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012858 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12859 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012860 dispatchDraw(canvas);
12861 } else {
12862 draw(canvas);
12863 }
12864
12865 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012866 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012867
12868 if (attachInfo != null) {
12869 // Restore the cached Canvas for our siblings
12870 attachInfo.mCanvas = canvas;
12871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 }
12873 }
12874
12875 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012876 * Create a snapshot of the view into a bitmap. We should probably make
12877 * some form of this public, but should think about the API.
12878 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012879 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012880 int width = mRight - mLeft;
12881 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012882
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012883 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012884 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012885 width = (int) ((width * scale) + 0.5f);
12886 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012887
Dianne Hackborndde331c2012-08-03 14:01:57 -070012888 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12889 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012890 if (bitmap == null) {
12891 throw new OutOfMemoryError();
12892 }
12893
Romain Guyc529d8d2011-09-06 15:01:39 -070012894 Resources resources = getResources();
12895 if (resources != null) {
12896 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12897 }
Joe Malin32736f02011-01-19 16:14:20 -080012898
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012899 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012900 if (attachInfo != null) {
12901 canvas = attachInfo.mCanvas;
12902 if (canvas == null) {
12903 canvas = new Canvas();
12904 }
12905 canvas.setBitmap(bitmap);
12906 // Temporarily clobber the cached Canvas in case one of our children
12907 // is also using a drawing cache. Without this, the children would
12908 // steal the canvas by attaching their own bitmap to it and bad, bad
12909 // things would happen (invisible views, corrupted drawings, etc.)
12910 attachInfo.mCanvas = null;
12911 } else {
12912 // This case should hopefully never or seldom happen
12913 canvas = new Canvas(bitmap);
12914 }
12915
Romain Guy5bcdff42009-05-14 21:27:18 -070012916 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012917 bitmap.eraseColor(backgroundColor);
12918 }
12919
12920 computeScroll();
12921 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012922 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012923 canvas.translate(-mScrollX, -mScrollY);
12924
Romain Guy5bcdff42009-05-14 21:27:18 -070012925 // Temporarily remove the dirty mask
12926 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012927 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012928
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012929 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012930 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012931 dispatchDraw(canvas);
12932 } else {
12933 draw(canvas);
12934 }
12935
Romain Guy5bcdff42009-05-14 21:27:18 -070012936 mPrivateFlags = flags;
12937
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012938 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012939 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012940
12941 if (attachInfo != null) {
12942 // Restore the cached Canvas for our siblings
12943 attachInfo.mCanvas = canvas;
12944 }
Romain Guy8506ab42009-06-11 17:35:47 -070012945
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012946 return bitmap;
12947 }
12948
12949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012950 * Indicates whether this View is currently in edit mode. A View is usually
12951 * in edit mode when displayed within a developer tool. For instance, if
12952 * this View is being drawn by a visual user interface builder, this method
12953 * should return true.
12954 *
12955 * Subclasses should check the return value of this method to provide
12956 * different behaviors if their normal behavior might interfere with the
12957 * host environment. For instance: the class spawns a thread in its
12958 * constructor, the drawing code relies on device-specific features, etc.
12959 *
12960 * This method is usually checked in the drawing code of custom widgets.
12961 *
12962 * @return True if this View is in edit mode, false otherwise.
12963 */
12964 public boolean isInEditMode() {
12965 return false;
12966 }
12967
12968 /**
12969 * If the View draws content inside its padding and enables fading edges,
12970 * it needs to support padding offsets. Padding offsets are added to the
12971 * fading edges to extend the length of the fade so that it covers pixels
12972 * drawn inside the padding.
12973 *
12974 * Subclasses of this class should override this method if they need
12975 * to draw content inside the padding.
12976 *
12977 * @return True if padding offset must be applied, false otherwise.
12978 *
12979 * @see #getLeftPaddingOffset()
12980 * @see #getRightPaddingOffset()
12981 * @see #getTopPaddingOffset()
12982 * @see #getBottomPaddingOffset()
12983 *
12984 * @since CURRENT
12985 */
12986 protected boolean isPaddingOffsetRequired() {
12987 return false;
12988 }
12989
12990 /**
12991 * Amount by which to extend the left fading region. Called only when
12992 * {@link #isPaddingOffsetRequired()} returns true.
12993 *
12994 * @return The left padding offset in pixels.
12995 *
12996 * @see #isPaddingOffsetRequired()
12997 *
12998 * @since CURRENT
12999 */
13000 protected int getLeftPaddingOffset() {
13001 return 0;
13002 }
13003
13004 /**
13005 * Amount by which to extend the right fading region. Called only when
13006 * {@link #isPaddingOffsetRequired()} returns true.
13007 *
13008 * @return The right padding offset in pixels.
13009 *
13010 * @see #isPaddingOffsetRequired()
13011 *
13012 * @since CURRENT
13013 */
13014 protected int getRightPaddingOffset() {
13015 return 0;
13016 }
13017
13018 /**
13019 * Amount by which to extend the top fading region. Called only when
13020 * {@link #isPaddingOffsetRequired()} returns true.
13021 *
13022 * @return The top padding offset in pixels.
13023 *
13024 * @see #isPaddingOffsetRequired()
13025 *
13026 * @since CURRENT
13027 */
13028 protected int getTopPaddingOffset() {
13029 return 0;
13030 }
13031
13032 /**
13033 * Amount by which to extend the bottom fading region. Called only when
13034 * {@link #isPaddingOffsetRequired()} returns true.
13035 *
13036 * @return The bottom padding offset in pixels.
13037 *
13038 * @see #isPaddingOffsetRequired()
13039 *
13040 * @since CURRENT
13041 */
13042 protected int getBottomPaddingOffset() {
13043 return 0;
13044 }
13045
13046 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013047 * @hide
13048 * @param offsetRequired
13049 */
13050 protected int getFadeTop(boolean offsetRequired) {
13051 int top = mPaddingTop;
13052 if (offsetRequired) top += getTopPaddingOffset();
13053 return top;
13054 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013055
Romain Guyf2fc4602011-07-19 15:20:03 -070013056 /**
13057 * @hide
13058 * @param offsetRequired
13059 */
13060 protected int getFadeHeight(boolean offsetRequired) {
13061 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013062 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013063 return mBottom - mTop - mPaddingBottom - padding;
13064 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013065
Romain Guyf2fc4602011-07-19 15:20:03 -070013066 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013067 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013068 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013069 *
Romain Guy2bffd262010-09-12 17:40:02 -070013070 * <p>Even if this method returns true, it does not mean that every call
13071 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13072 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013073 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013074 * window is hardware accelerated,
13075 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13076 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013077 *
Romain Guy2bffd262010-09-12 17:40:02 -070013078 * @return True if the view is attached to a window and the window is
13079 * hardware accelerated; false in any other case.
13080 */
13081 public boolean isHardwareAccelerated() {
13082 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13083 }
Joe Malin32736f02011-01-19 16:14:20 -080013084
Romain Guy2bffd262010-09-12 17:40:02 -070013085 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013086 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13087 * case of an active Animation being run on the view.
13088 */
13089 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13090 Animation a, boolean scalingRequired) {
13091 Transformation invalidationTransform;
13092 final int flags = parent.mGroupFlags;
13093 final boolean initialized = a.isInitialized();
13094 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013095 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013096 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013097 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013098 onAnimationStart();
13099 }
13100
13101 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13102 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13103 if (parent.mInvalidationTransformation == null) {
13104 parent.mInvalidationTransformation = new Transformation();
13105 }
13106 invalidationTransform = parent.mInvalidationTransformation;
13107 a.getTransformation(drawingTime, invalidationTransform, 1f);
13108 } else {
13109 invalidationTransform = parent.mChildTransformation;
13110 }
Romain Guy393a52c2012-05-22 20:21:08 -070013111
Chet Haasebcca79a2012-02-14 08:45:14 -080013112 if (more) {
13113 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013114 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13115 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13116 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13117 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013118 // The child need to draw an animation, potentially offscreen, so
13119 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013120 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013121 parent.invalidate(mLeft, mTop, mRight, mBottom);
13122 }
13123 } else {
13124 if (parent.mInvalidateRegion == null) {
13125 parent.mInvalidateRegion = new RectF();
13126 }
13127 final RectF region = parent.mInvalidateRegion;
13128 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13129 invalidationTransform);
13130
13131 // The child need to draw an animation, potentially offscreen, so
13132 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013133 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013134
13135 final int left = mLeft + (int) region.left;
13136 final int top = mTop + (int) region.top;
13137 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13138 top + (int) (region.height() + .5f));
13139 }
13140 }
13141 return more;
13142 }
13143
Chet Haasea1cff502012-02-21 13:43:44 -080013144 /**
13145 * This method is called by getDisplayList() when a display list is created or re-rendered.
13146 * It sets or resets the current value of all properties on that display list (resetting is
13147 * necessary when a display list is being re-created, because we need to make sure that
13148 * previously-set transform values
13149 */
13150 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013151 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013152 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013153 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013154 if (mParent instanceof ViewGroup) {
13155 displayList.setClipChildren(
13156 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13157 }
Chet Haase9420abd2012-03-29 16:28:32 -070013158 float alpha = 1;
13159 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13160 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13161 ViewGroup parentVG = (ViewGroup) mParent;
13162 final boolean hasTransform =
13163 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13164 if (hasTransform) {
13165 Transformation transform = parentVG.mChildTransformation;
13166 final int transformType = parentVG.mChildTransformation.getTransformationType();
13167 if (transformType != Transformation.TYPE_IDENTITY) {
13168 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13169 alpha = transform.getAlpha();
13170 }
13171 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13172 displayList.setStaticMatrix(transform.getMatrix());
13173 }
13174 }
13175 }
Chet Haasea1cff502012-02-21 13:43:44 -080013176 }
13177 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013178 alpha *= mTransformationInfo.mAlpha;
13179 if (alpha < 1) {
13180 final int multipliedAlpha = (int) (255 * alpha);
13181 if (onSetAlpha(multipliedAlpha)) {
13182 alpha = 1;
13183 }
13184 }
13185 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013186 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13187 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13188 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13189 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013190 if (mTransformationInfo.mCamera == null) {
13191 mTransformationInfo.mCamera = new Camera();
13192 mTransformationInfo.matrix3D = new Matrix();
13193 }
13194 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013195 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013196 displayList.setPivotX(getPivotX());
13197 displayList.setPivotY(getPivotY());
13198 }
Chet Haase9420abd2012-03-29 16:28:32 -070013199 } else if (alpha < 1) {
13200 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013201 }
13202 }
13203 }
13204
Chet Haasebcca79a2012-02-14 08:45:14 -080013205 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013206 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13207 * This draw() method is an implementation detail and is not intended to be overridden or
13208 * to be called from anywhere else other than ViewGroup.drawChild().
13209 */
13210 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013211 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013212 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013213 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013214 final int flags = parent.mGroupFlags;
13215
Chet Haasea1cff502012-02-21 13:43:44 -080013216 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013217 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013218 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013219 }
13220
13221 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013222 boolean concatMatrix = false;
13223
13224 boolean scalingRequired = false;
13225 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013226 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013227
13228 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013229 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13230 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013231 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013232 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013233 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13234 } else {
13235 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13236 }
13237
Chet Haasebcca79a2012-02-14 08:45:14 -080013238 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013239 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013240 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013241 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013242 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013243 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013244 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013245 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013246 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013247 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013248 mDisplayList != null) {
13249 // No longer animating: clear out old animation matrix
13250 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013251 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013252 }
13253 if (!useDisplayListProperties &&
13254 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13255 final boolean hasTransform =
13256 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13257 if (hasTransform) {
13258 final int transformType = parent.mChildTransformation.getTransformationType();
13259 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13260 parent.mChildTransformation : null;
13261 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13262 }
Chet Haase64a48c12012-02-13 16:33:29 -080013263 }
13264 }
13265
13266 concatMatrix |= !childHasIdentityMatrix;
13267
13268 // Sets the flag as early as possible to allow draw() implementations
13269 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013270 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013271
Romain Guyfbb43212012-08-30 15:19:27 -070013272 if (!concatMatrix &&
13273 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13274 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013275 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013276 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13277 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013278 return more;
13279 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013280 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013281
13282 if (hardwareAccelerated) {
13283 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13284 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013285 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13286 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013287 }
13288
Chet Haase64a48c12012-02-13 16:33:29 -080013289 DisplayList displayList = null;
13290 Bitmap cache = null;
13291 boolean hasDisplayList = false;
13292 if (caching) {
13293 if (!hardwareAccelerated) {
13294 if (layerType != LAYER_TYPE_NONE) {
13295 layerType = LAYER_TYPE_SOFTWARE;
13296 buildDrawingCache(true);
13297 }
13298 cache = getDrawingCache(true);
13299 } else {
13300 switch (layerType) {
13301 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013302 if (useDisplayListProperties) {
13303 hasDisplayList = canHaveDisplayList();
13304 } else {
13305 buildDrawingCache(true);
13306 cache = getDrawingCache(true);
13307 }
Chet Haase64a48c12012-02-13 16:33:29 -080013308 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013309 case LAYER_TYPE_HARDWARE:
13310 if (useDisplayListProperties) {
13311 hasDisplayList = canHaveDisplayList();
13312 }
13313 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013314 case LAYER_TYPE_NONE:
13315 // Delay getting the display list until animation-driven alpha values are
13316 // set up and possibly passed on to the view
13317 hasDisplayList = canHaveDisplayList();
13318 break;
13319 }
13320 }
13321 }
Chet Haasea1cff502012-02-21 13:43:44 -080013322 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013323 if (useDisplayListProperties) {
13324 displayList = getDisplayList();
13325 if (!displayList.isValid()) {
13326 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13327 // to getDisplayList(), the display list will be marked invalid and we should not
13328 // try to use it again.
13329 displayList = null;
13330 hasDisplayList = false;
13331 useDisplayListProperties = false;
13332 }
13333 }
Chet Haase64a48c12012-02-13 16:33:29 -080013334
Chet Haase526057b2012-07-12 17:50:41 -070013335 int sx = 0;
13336 int sy = 0;
13337 if (!hasDisplayList) {
13338 computeScroll();
13339 sx = mScrollX;
13340 sy = mScrollY;
13341 }
13342
Chet Haase64a48c12012-02-13 16:33:29 -080013343 final boolean hasNoCache = cache == null || hasDisplayList;
13344 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13345 layerType != LAYER_TYPE_HARDWARE;
13346
Chet Haasea1cff502012-02-21 13:43:44 -080013347 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013348 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013349 restoreTo = canvas.save();
13350 }
Chet Haase64a48c12012-02-13 16:33:29 -080013351 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013352 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013353 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013354 if (!useDisplayListProperties) {
13355 canvas.translate(mLeft, mTop);
13356 }
Chet Haase64a48c12012-02-13 16:33:29 -080013357 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013358 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013359 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013360 restoreTo = canvas.save();
13361 }
Chet Haase64a48c12012-02-13 16:33:29 -080013362 // mAttachInfo cannot be null, otherwise scalingRequired == false
13363 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13364 canvas.scale(scale, scale);
13365 }
13366 }
13367
Chet Haasea1cff502012-02-21 13:43:44 -080013368 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013369 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013370 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013371 if (transformToApply != null || !childHasIdentityMatrix) {
13372 int transX = 0;
13373 int transY = 0;
13374
13375 if (offsetForScroll) {
13376 transX = -sx;
13377 transY = -sy;
13378 }
13379
13380 if (transformToApply != null) {
13381 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013382 if (useDisplayListProperties) {
13383 displayList.setAnimationMatrix(transformToApply.getMatrix());
13384 } else {
13385 // Undo the scroll translation, apply the transformation matrix,
13386 // then redo the scroll translate to get the correct result.
13387 canvas.translate(-transX, -transY);
13388 canvas.concat(transformToApply.getMatrix());
13389 canvas.translate(transX, transY);
13390 }
Chet Haasea1cff502012-02-21 13:43:44 -080013391 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013392 }
13393
13394 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013395 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013396 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013397 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013398 }
13399 }
13400
Chet Haasea1cff502012-02-21 13:43:44 -080013401 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013402 canvas.translate(-transX, -transY);
13403 canvas.concat(getMatrix());
13404 canvas.translate(transX, transY);
13405 }
13406 }
13407
Chet Haase21433372012-06-05 07:54:09 -070013408 // Deal with alpha if it is or used to be <1
13409 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013410 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013411 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013412 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013413 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013414 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013415 }
Chet Haasea1cff502012-02-21 13:43:44 -080013416 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013417 if (hasNoCache) {
13418 final int multipliedAlpha = (int) (255 * alpha);
13419 if (!onSetAlpha(multipliedAlpha)) {
13420 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013421 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013422 layerType != LAYER_TYPE_NONE) {
13423 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13424 }
Chet Haase9420abd2012-03-29 16:28:32 -070013425 if (useDisplayListProperties) {
13426 displayList.setAlpha(alpha * getAlpha());
13427 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013428 final int scrollX = hasDisplayList ? 0 : sx;
13429 final int scrollY = hasDisplayList ? 0 : sy;
13430 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13431 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013432 }
13433 } else {
13434 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013435 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013436 }
13437 }
13438 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013439 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013440 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013441 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013442 }
13443
Chet Haasea1cff502012-02-21 13:43:44 -080013444 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13445 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013446 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013447 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013448 } else {
13449 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013450 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013451 } else {
13452 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13453 }
13454 }
13455 }
13456
Chet Haase9420abd2012-03-29 16:28:32 -070013457 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013458 displayList = getDisplayList();
13459 if (!displayList.isValid()) {
13460 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13461 // to getDisplayList(), the display list will be marked invalid and we should not
13462 // try to use it again.
13463 displayList = null;
13464 hasDisplayList = false;
13465 }
13466 }
13467
13468 if (hasNoCache) {
13469 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013470 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013471 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013472 if (layer != null && layer.isValid()) {
13473 mLayerPaint.setAlpha((int) (alpha * 255));
13474 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13475 layerRendered = true;
13476 } else {
13477 final int scrollX = hasDisplayList ? 0 : sx;
13478 final int scrollY = hasDisplayList ? 0 : sy;
13479 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013480 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013481 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13482 }
13483 }
13484
13485 if (!layerRendered) {
13486 if (!hasDisplayList) {
13487 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013488 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13489 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013490 dispatchDraw(canvas);
13491 } else {
13492 draw(canvas);
13493 }
13494 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013495 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013496 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013497 }
13498 }
13499 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013500 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013501 Paint cachePaint;
13502
13503 if (layerType == LAYER_TYPE_NONE) {
13504 cachePaint = parent.mCachePaint;
13505 if (cachePaint == null) {
13506 cachePaint = new Paint();
13507 cachePaint.setDither(false);
13508 parent.mCachePaint = cachePaint;
13509 }
Chet Haase9420abd2012-03-29 16:28:32 -070013510 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013511 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013512 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13513 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013514 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013515 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013516 }
13517 } else {
13518 cachePaint = mLayerPaint;
13519 cachePaint.setAlpha((int) (alpha * 255));
13520 }
13521 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13522 }
13523
Chet Haasea1cff502012-02-21 13:43:44 -080013524 if (restoreTo >= 0) {
13525 canvas.restoreToCount(restoreTo);
13526 }
Chet Haase64a48c12012-02-13 16:33:29 -080013527
13528 if (a != null && !more) {
13529 if (!hardwareAccelerated && !a.getFillAfter()) {
13530 onSetAlpha(255);
13531 }
13532 parent.finishAnimatingView(this, a);
13533 }
13534
13535 if (more && hardwareAccelerated) {
13536 // invalidation is the trigger to recreate display lists, so if we're using
13537 // display lists to render, force an invalidate to allow the animation to
13538 // continue drawing another frame
13539 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013540 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013541 // alpha animations should cause the child to recreate its display list
13542 invalidate(true);
13543 }
13544 }
13545
13546 mRecreateDisplayList = false;
13547
13548 return more;
13549 }
13550
13551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013552 * Manually render this view (and all of its children) to the given Canvas.
13553 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013554 * called. When implementing a view, implement
13555 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13556 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013557 *
13558 * @param canvas The Canvas to which the View is rendered.
13559 */
13560 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013561 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013562 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013563 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013564 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013566 /*
13567 * Draw traversal performs several drawing steps which must be executed
13568 * in the appropriate order:
13569 *
13570 * 1. Draw the background
13571 * 2. If necessary, save the canvas' layers to prepare for fading
13572 * 3. Draw view's content
13573 * 4. Draw children
13574 * 5. If necessary, draw the fading edges and restore layers
13575 * 6. Draw decorations (scrollbars for instance)
13576 */
13577
13578 // Step 1, draw the background, if needed
13579 int saveCount;
13580
Romain Guy24443ea2009-05-11 11:56:30 -070013581 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013582 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013583 if (background != null) {
13584 final int scrollX = mScrollX;
13585 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586
Romain Guy24443ea2009-05-11 11:56:30 -070013587 if (mBackgroundSizeChanged) {
13588 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13589 mBackgroundSizeChanged = false;
13590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013591
Romain Guy24443ea2009-05-11 11:56:30 -070013592 if ((scrollX | scrollY) == 0) {
13593 background.draw(canvas);
13594 } else {
13595 canvas.translate(scrollX, scrollY);
13596 background.draw(canvas);
13597 canvas.translate(-scrollX, -scrollY);
13598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013599 }
13600 }
13601
13602 // skip step 2 & 5 if possible (common case)
13603 final int viewFlags = mViewFlags;
13604 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13605 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13606 if (!verticalEdges && !horizontalEdges) {
13607 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013608 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013609
13610 // Step 4, draw the children
13611 dispatchDraw(canvas);
13612
13613 // Step 6, draw decorations (scrollbars)
13614 onDrawScrollBars(canvas);
13615
13616 // we're done...
13617 return;
13618 }
13619
13620 /*
13621 * Here we do the full fledged routine...
13622 * (this is an uncommon case where speed matters less,
13623 * this is why we repeat some of the tests that have been
13624 * done above)
13625 */
13626
13627 boolean drawTop = false;
13628 boolean drawBottom = false;
13629 boolean drawLeft = false;
13630 boolean drawRight = false;
13631
13632 float topFadeStrength = 0.0f;
13633 float bottomFadeStrength = 0.0f;
13634 float leftFadeStrength = 0.0f;
13635 float rightFadeStrength = 0.0f;
13636
13637 // Step 2, save the canvas' layers
13638 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013639
13640 final boolean offsetRequired = isPaddingOffsetRequired();
13641 if (offsetRequired) {
13642 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 }
13644
13645 int left = mScrollX + paddingLeft;
13646 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013647 int top = mScrollY + getFadeTop(offsetRequired);
13648 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013649
13650 if (offsetRequired) {
13651 right += getRightPaddingOffset();
13652 bottom += getBottomPaddingOffset();
13653 }
13654
13655 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013656 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013657 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013658
13659 // clip the fade length if top and bottom fades overlap
13660 // overlapping fades produce odd-looking artifacts
13661 if (verticalEdges && (top + length > bottom - length)) {
13662 length = (bottom - top) / 2;
13663 }
13664
13665 // also clip horizontal fades if necessary
13666 if (horizontalEdges && (left + length > right - length)) {
13667 length = (right - left) / 2;
13668 }
13669
13670 if (verticalEdges) {
13671 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013672 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013673 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013674 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013675 }
13676
13677 if (horizontalEdges) {
13678 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013679 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013680 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013681 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013682 }
13683
13684 saveCount = canvas.getSaveCount();
13685
13686 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013687 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13689
13690 if (drawTop) {
13691 canvas.saveLayer(left, top, right, top + length, null, flags);
13692 }
13693
13694 if (drawBottom) {
13695 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13696 }
13697
13698 if (drawLeft) {
13699 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13700 }
13701
13702 if (drawRight) {
13703 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13704 }
13705 } else {
13706 scrollabilityCache.setFadeColor(solidColor);
13707 }
13708
13709 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013710 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013711
13712 // Step 4, draw the children
13713 dispatchDraw(canvas);
13714
13715 // Step 5, draw the fade effect and restore layers
13716 final Paint p = scrollabilityCache.paint;
13717 final Matrix matrix = scrollabilityCache.matrix;
13718 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013719
13720 if (drawTop) {
13721 matrix.setScale(1, fadeHeight * topFadeStrength);
13722 matrix.postTranslate(left, top);
13723 fade.setLocalMatrix(matrix);
13724 canvas.drawRect(left, top, right, top + length, p);
13725 }
13726
13727 if (drawBottom) {
13728 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13729 matrix.postRotate(180);
13730 matrix.postTranslate(left, bottom);
13731 fade.setLocalMatrix(matrix);
13732 canvas.drawRect(left, bottom - length, right, bottom, p);
13733 }
13734
13735 if (drawLeft) {
13736 matrix.setScale(1, fadeHeight * leftFadeStrength);
13737 matrix.postRotate(-90);
13738 matrix.postTranslate(left, top);
13739 fade.setLocalMatrix(matrix);
13740 canvas.drawRect(left, top, left + length, bottom, p);
13741 }
13742
13743 if (drawRight) {
13744 matrix.setScale(1, fadeHeight * rightFadeStrength);
13745 matrix.postRotate(90);
13746 matrix.postTranslate(right, top);
13747 fade.setLocalMatrix(matrix);
13748 canvas.drawRect(right - length, top, right, bottom, p);
13749 }
13750
13751 canvas.restoreToCount(saveCount);
13752
13753 // Step 6, draw decorations (scrollbars)
13754 onDrawScrollBars(canvas);
13755 }
13756
13757 /**
13758 * Override this if your view is known to always be drawn on top of a solid color background,
13759 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13760 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13761 * should be set to 0xFF.
13762 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013763 * @see #setVerticalFadingEdgeEnabled(boolean)
13764 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 *
13766 * @return The known solid color background for this view, or 0 if the color may vary
13767 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013768 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769 public int getSolidColor() {
13770 return 0;
13771 }
13772
13773 /**
13774 * Build a human readable string representation of the specified view flags.
13775 *
13776 * @param flags the view flags to convert to a string
13777 * @return a String representing the supplied flags
13778 */
13779 private static String printFlags(int flags) {
13780 String output = "";
13781 int numFlags = 0;
13782 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13783 output += "TAKES_FOCUS";
13784 numFlags++;
13785 }
13786
13787 switch (flags & VISIBILITY_MASK) {
13788 case INVISIBLE:
13789 if (numFlags > 0) {
13790 output += " ";
13791 }
13792 output += "INVISIBLE";
13793 // USELESS HERE numFlags++;
13794 break;
13795 case GONE:
13796 if (numFlags > 0) {
13797 output += " ";
13798 }
13799 output += "GONE";
13800 // USELESS HERE numFlags++;
13801 break;
13802 default:
13803 break;
13804 }
13805 return output;
13806 }
13807
13808 /**
13809 * Build a human readable string representation of the specified private
13810 * view flags.
13811 *
13812 * @param privateFlags the private view flags to convert to a string
13813 * @return a String representing the supplied flags
13814 */
13815 private static String printPrivateFlags(int privateFlags) {
13816 String output = "";
13817 int numFlags = 0;
13818
Dianne Hackborn4702a852012-08-17 15:18:29 -070013819 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013820 output += "WANTS_FOCUS";
13821 numFlags++;
13822 }
13823
Dianne Hackborn4702a852012-08-17 15:18:29 -070013824 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013825 if (numFlags > 0) {
13826 output += " ";
13827 }
13828 output += "FOCUSED";
13829 numFlags++;
13830 }
13831
Dianne Hackborn4702a852012-08-17 15:18:29 -070013832 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013833 if (numFlags > 0) {
13834 output += " ";
13835 }
13836 output += "SELECTED";
13837 numFlags++;
13838 }
13839
Dianne Hackborn4702a852012-08-17 15:18:29 -070013840 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013841 if (numFlags > 0) {
13842 output += " ";
13843 }
13844 output += "IS_ROOT_NAMESPACE";
13845 numFlags++;
13846 }
13847
Dianne Hackborn4702a852012-08-17 15:18:29 -070013848 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013849 if (numFlags > 0) {
13850 output += " ";
13851 }
13852 output += "HAS_BOUNDS";
13853 numFlags++;
13854 }
13855
Dianne Hackborn4702a852012-08-17 15:18:29 -070013856 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 if (numFlags > 0) {
13858 output += " ";
13859 }
13860 output += "DRAWN";
13861 // USELESS HERE numFlags++;
13862 }
13863 return output;
13864 }
13865
13866 /**
13867 * <p>Indicates whether or not this view's layout will be requested during
13868 * the next hierarchy layout pass.</p>
13869 *
13870 * @return true if the layout will be forced during next layout pass
13871 */
13872 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013873 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013874 }
13875
13876 /**
13877 * Assign a size and position to a view and all of its
13878 * descendants
13879 *
13880 * <p>This is the second phase of the layout mechanism.
13881 * (The first is measuring). In this phase, each parent calls
13882 * layout on all of its children to position them.
13883 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013884 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013885 *
Chet Haase9c087442011-01-12 16:20:16 -080013886 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013887 * Derived classes with children should override
13888 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013889 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 *
13891 * @param l Left position, relative to parent
13892 * @param t Top position, relative to parent
13893 * @param r Right position, relative to parent
13894 * @param b Bottom position, relative to parent
13895 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013896 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013897 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013898 int oldL = mLeft;
13899 int oldT = mTop;
13900 int oldB = mBottom;
13901 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013902 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013903 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013904 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013905 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013906
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013907 ListenerInfo li = mListenerInfo;
13908 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013909 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013910 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013911 int numListeners = listenersCopy.size();
13912 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013913 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013914 }
13915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013916 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013917 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918 }
13919
13920 /**
13921 * Called from layout when this view should
13922 * assign a size and position to each of its children.
13923 *
13924 * Derived classes with children should override
13925 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013926 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013927 * @param changed This is a new size or position for this view
13928 * @param left Left position, relative to parent
13929 * @param top Top position, relative to parent
13930 * @param right Right position, relative to parent
13931 * @param bottom Bottom position, relative to parent
13932 */
13933 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13934 }
13935
13936 /**
13937 * Assign a size and position to this view.
13938 *
13939 * This is called from layout.
13940 *
13941 * @param left Left position, relative to parent
13942 * @param top Top position, relative to parent
13943 * @param right Right position, relative to parent
13944 * @param bottom Bottom position, relative to parent
13945 * @return true if the new size and position are different than the
13946 * previous ones
13947 * {@hide}
13948 */
13949 protected boolean setFrame(int left, int top, int right, int bottom) {
13950 boolean changed = false;
13951
13952 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013953 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013954 + right + "," + bottom + ")");
13955 }
13956
13957 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13958 changed = true;
13959
13960 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070013961 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013963 int oldWidth = mRight - mLeft;
13964 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013965 int newWidth = right - left;
13966 int newHeight = bottom - top;
13967 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13968
13969 // Invalidate our old position
13970 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013971
13972 mLeft = left;
13973 mTop = top;
13974 mRight = right;
13975 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013976 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013977 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979
Dianne Hackborn4702a852012-08-17 15:18:29 -070013980 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013982
Chet Haase75755e22011-07-18 17:48:25 -070013983 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013984 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013985 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013986 if (mTransformationInfo != null) {
13987 mTransformationInfo.mMatrixDirty = true;
13988 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013990 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13991 }
13992
13993 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13994 // If we are visible, force the DRAWN bit to on so that
13995 // this invalidate will go through (at least to our parent).
13996 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013997 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013998 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070013999 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014000 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014001 // parent display list may need to be recreated based on a change in the bounds
14002 // of any child
14003 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 }
14005
14006 // Reset drawn bit to original value (invalidate turns it off)
14007 mPrivateFlags |= drawn;
14008
14009 mBackgroundSizeChanged = true;
14010 }
14011 return changed;
14012 }
14013
14014 /**
14015 * Finalize inflating a view from XML. This is called as the last phase
14016 * of inflation, after all child views have been added.
14017 *
14018 * <p>Even if the subclass overrides onFinishInflate, they should always be
14019 * sure to call the super method, so that we get called.
14020 */
14021 protected void onFinishInflate() {
14022 }
14023
14024 /**
14025 * Returns the resources associated with this view.
14026 *
14027 * @return Resources object.
14028 */
14029 public Resources getResources() {
14030 return mResources;
14031 }
14032
14033 /**
14034 * Invalidates the specified Drawable.
14035 *
14036 * @param drawable the drawable to invalidate
14037 */
14038 public void invalidateDrawable(Drawable drawable) {
14039 if (verifyDrawable(drawable)) {
14040 final Rect dirty = drawable.getBounds();
14041 final int scrollX = mScrollX;
14042 final int scrollY = mScrollY;
14043
14044 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14045 dirty.right + scrollX, dirty.bottom + scrollY);
14046 }
14047 }
14048
14049 /**
14050 * Schedules an action on a drawable to occur at a specified time.
14051 *
14052 * @param who the recipient of the action
14053 * @param what the action to run on the drawable
14054 * @param when the time at which the action must occur. Uses the
14055 * {@link SystemClock#uptimeMillis} timebase.
14056 */
14057 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014058 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014059 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014060 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014061 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14062 Choreographer.CALLBACK_ANIMATION, what, who,
14063 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014064 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014065 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 }
14068 }
14069
14070 /**
14071 * Cancels a scheduled action on a drawable.
14072 *
14073 * @param who the recipient of the action
14074 * @param what the action to cancel
14075 */
14076 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014077 if (verifyDrawable(who) && what != null) {
14078 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014079 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14080 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014081 } else {
14082 ViewRootImpl.getRunQueue().removeCallbacks(what);
14083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014084 }
14085 }
14086
14087 /**
14088 * Unschedule any events associated with the given Drawable. This can be
14089 * used when selecting a new Drawable into a view, so that the previous
14090 * one is completely unscheduled.
14091 *
14092 * @param who The Drawable to unschedule.
14093 *
14094 * @see #drawableStateChanged
14095 */
14096 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014097 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014098 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14099 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100 }
14101 }
14102
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014103 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014104 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14105 * that the View directionality can and will be resolved before its Drawables.
14106 *
14107 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014108 *
14109 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014110 */
14111 public void resolveDrawables() {
14112 if (mBackground != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014113 mBackground.setLayoutDirection(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014114 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014115 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014116 }
14117
14118 /**
14119 * Called when layout direction has been resolved.
14120 *
14121 * The default implementation does nothing.
14122 *
14123 * @param layoutDirection The resolved layout direction.
14124 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014125 * @see #LAYOUT_DIRECTION_LTR
14126 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014127 *
14128 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014129 */
14130 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014131 }
14132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014133 /**
14134 * If your view subclass is displaying its own Drawable objects, it should
14135 * override this function and return true for any Drawable it is
14136 * displaying. This allows animations for those drawables to be
14137 * scheduled.
14138 *
14139 * <p>Be sure to call through to the super class when overriding this
14140 * function.
14141 *
14142 * @param who The Drawable to verify. Return true if it is one you are
14143 * displaying, else return the result of calling through to the
14144 * super class.
14145 *
14146 * @return boolean If true than the Drawable is being displayed in the
14147 * view; else false and it is not allowed to animate.
14148 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014149 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14150 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014151 */
14152 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014153 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 }
14155
14156 /**
14157 * This function is called whenever the state of the view changes in such
14158 * a way that it impacts the state of drawables being shown.
14159 *
14160 * <p>Be sure to call through to the superclass when overriding this
14161 * function.
14162 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014163 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164 */
14165 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014166 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014167 if (d != null && d.isStateful()) {
14168 d.setState(getDrawableState());
14169 }
14170 }
14171
14172 /**
14173 * Call this to force a view to update its drawable state. This will cause
14174 * drawableStateChanged to be called on this view. Views that are interested
14175 * in the new state should call getDrawableState.
14176 *
14177 * @see #drawableStateChanged
14178 * @see #getDrawableState
14179 */
14180 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014181 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014182 drawableStateChanged();
14183
14184 ViewParent parent = mParent;
14185 if (parent != null) {
14186 parent.childDrawableStateChanged(this);
14187 }
14188 }
14189
14190 /**
14191 * Return an array of resource IDs of the drawable states representing the
14192 * current state of the view.
14193 *
14194 * @return The current drawable state
14195 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014196 * @see Drawable#setState(int[])
14197 * @see #drawableStateChanged()
14198 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014199 */
14200 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014201 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014202 return mDrawableState;
14203 } else {
14204 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014205 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014206 return mDrawableState;
14207 }
14208 }
14209
14210 /**
14211 * Generate the new {@link android.graphics.drawable.Drawable} state for
14212 * this view. This is called by the view
14213 * system when the cached Drawable state is determined to be invalid. To
14214 * retrieve the current state, you should use {@link #getDrawableState}.
14215 *
14216 * @param extraSpace if non-zero, this is the number of extra entries you
14217 * would like in the returned array in which you can place your own
14218 * states.
14219 *
14220 * @return Returns an array holding the current {@link Drawable} state of
14221 * the view.
14222 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014223 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014224 */
14225 protected int[] onCreateDrawableState(int extraSpace) {
14226 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14227 mParent instanceof View) {
14228 return ((View) mParent).onCreateDrawableState(extraSpace);
14229 }
14230
14231 int[] drawableState;
14232
14233 int privateFlags = mPrivateFlags;
14234
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014235 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014236 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014237 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14238 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014239 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014240 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014241 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014242 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14243 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014244 // This is set if HW acceleration is requested, even if the current
14245 // process doesn't allow it. This is just to allow app preview
14246 // windows to better match their app.
14247 viewStateIndex |= VIEW_STATE_ACCELERATED;
14248 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014249 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250
Christopher Tate3d4bf172011-03-28 16:16:46 -070014251 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014252 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14253 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014255 drawableState = VIEW_STATE_SETS[viewStateIndex];
14256
14257 //noinspection ConstantIfStatement
14258 if (false) {
14259 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14260 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014261 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014262 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14263 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014264 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014265 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 + ": " + Arrays.toString(drawableState));
14267 }
14268
14269 if (extraSpace == 0) {
14270 return drawableState;
14271 }
14272
14273 final int[] fullState;
14274 if (drawableState != null) {
14275 fullState = new int[drawableState.length + extraSpace];
14276 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14277 } else {
14278 fullState = new int[extraSpace];
14279 }
14280
14281 return fullState;
14282 }
14283
14284 /**
14285 * Merge your own state values in <var>additionalState</var> into the base
14286 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014287 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014288 *
14289 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014290 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014291 * own additional state values.
14292 *
14293 * @param additionalState The additional state values you would like
14294 * added to <var>baseState</var>; this array is not modified.
14295 *
14296 * @return As a convenience, the <var>baseState</var> array you originally
14297 * passed into the function is returned.
14298 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014299 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014300 */
14301 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14302 final int N = baseState.length;
14303 int i = N - 1;
14304 while (i >= 0 && baseState[i] == 0) {
14305 i--;
14306 }
14307 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14308 return baseState;
14309 }
14310
14311 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014312 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14313 * on all Drawable objects associated with this view.
14314 */
14315 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014316 if (mBackground != null) {
14317 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014318 }
14319 }
14320
14321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014322 * Sets the background color for this view.
14323 * @param color the color of the background
14324 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014325 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014327 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014328 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014329 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014330 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014331 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014333 }
14334
14335 /**
14336 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014337 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014338 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014340 * @attr ref android.R.styleable#View_background
14341 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014342 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014343 public void setBackgroundResource(int resid) {
14344 if (resid != 0 && resid == mBackgroundResource) {
14345 return;
14346 }
14347
14348 Drawable d= null;
14349 if (resid != 0) {
14350 d = mResources.getDrawable(resid);
14351 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014352 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014353
14354 mBackgroundResource = resid;
14355 }
14356
14357 /**
14358 * Set the background to a given Drawable, or remove the background. If the
14359 * background has padding, this View's padding is set to the background's
14360 * padding. However, when a background is removed, this View's padding isn't
14361 * touched. If setting the padding is desired, please use
14362 * {@link #setPadding(int, int, int, int)}.
14363 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014364 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014365 * background
14366 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014367 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014368 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014369 setBackgroundDrawable(background);
14370 }
14371
14372 /**
14373 * @deprecated use {@link #setBackground(Drawable)} instead
14374 */
14375 @Deprecated
14376 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014377 computeOpaqueFlags();
14378
Philip Milne6c8ea062012-04-03 17:38:43 -070014379 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014380 return;
14381 }
14382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014383 boolean requestLayout = false;
14384
14385 mBackgroundResource = 0;
14386
14387 /*
14388 * Regardless of whether we're setting a new background or not, we want
14389 * to clear the previous drawable.
14390 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014391 if (mBackground != null) {
14392 mBackground.setCallback(null);
14393 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014394 }
14395
Philip Milne6c8ea062012-04-03 17:38:43 -070014396 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014397 Rect padding = sThreadLocal.get();
14398 if (padding == null) {
14399 padding = new Rect();
14400 sThreadLocal.set(padding);
14401 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014402 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014403 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014404 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014405 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014406 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014407 mUserPaddingLeftInitial = padding.right;
14408 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014409 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014410 break;
14411 case LAYOUT_DIRECTION_LTR:
14412 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014413 mUserPaddingLeftInitial = padding.left;
14414 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014415 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 }
14418
14419 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14420 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014421 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14422 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014423 requestLayout = true;
14424 }
14425
Philip Milne6c8ea062012-04-03 17:38:43 -070014426 background.setCallback(this);
14427 if (background.isStateful()) {
14428 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014429 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014430 background.setVisible(getVisibility() == VISIBLE, false);
14431 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432
Dianne Hackborn4702a852012-08-17 15:18:29 -070014433 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14434 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14435 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014436 requestLayout = true;
14437 }
14438 } else {
14439 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014440 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014441
Dianne Hackborn4702a852012-08-17 15:18:29 -070014442 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014443 /*
14444 * This view ONLY drew the background before and we're removing
14445 * the background, so now it won't draw anything
14446 * (hence we SKIP_DRAW)
14447 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014448 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14449 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014450 }
14451
14452 /*
14453 * When the background is set, we try to apply its padding to this
14454 * View. When the background is removed, we don't touch this View's
14455 * padding. This is noted in the Javadocs. Hence, we don't need to
14456 * requestLayout(), the invalidate() below is sufficient.
14457 */
14458
14459 // The old background's minimum size could have affected this
14460 // View's layout, so let's requestLayout
14461 requestLayout = true;
14462 }
14463
Romain Guy8f1344f52009-05-15 16:03:59 -070014464 computeOpaqueFlags();
14465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014466 if (requestLayout) {
14467 requestLayout();
14468 }
14469
14470 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014471 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014472 }
14473
14474 /**
14475 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014476 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014477 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014478 *
14479 * @see #setBackground(Drawable)
14480 *
14481 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014482 */
14483 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014484 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014485 }
14486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014487 /**
14488 * Sets the padding. The view may add on the space required to display
14489 * the scrollbars, depending on the style and visibility of the scrollbars.
14490 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14491 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14492 * from the values set in this call.
14493 *
14494 * @attr ref android.R.styleable#View_padding
14495 * @attr ref android.R.styleable#View_paddingBottom
14496 * @attr ref android.R.styleable#View_paddingLeft
14497 * @attr ref android.R.styleable#View_paddingRight
14498 * @attr ref android.R.styleable#View_paddingTop
14499 * @param left the left padding in pixels
14500 * @param top the top padding in pixels
14501 * @param right the right padding in pixels
14502 * @param bottom the bottom padding in pixels
14503 */
14504 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014505 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014506
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014507 mUserPaddingStart = UNDEFINED_PADDING;
14508 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014509
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014510 mUserPaddingLeftInitial = left;
14511 mUserPaddingRightInitial = right;
14512
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014513 internalSetPadding(left, top, right, bottom);
14514 }
14515
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014516 /**
14517 * @hide
14518 */
14519 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014520 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014521 mUserPaddingRight = right;
14522 mUserPaddingBottom = bottom;
14523
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014524 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014525 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014526
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014527 // Common case is there are no scroll bars.
14528 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014529 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014530 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014531 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014532 switch (mVerticalScrollbarPosition) {
14533 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014534 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014535 left += offset;
14536 } else {
14537 right += offset;
14538 }
14539 break;
Adam Powell20232d02010-12-08 21:08:53 -080014540 case SCROLLBAR_POSITION_RIGHT:
14541 right += offset;
14542 break;
14543 case SCROLLBAR_POSITION_LEFT:
14544 left += offset;
14545 break;
14546 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014547 }
Adam Powell20232d02010-12-08 21:08:53 -080014548 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014549 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14550 ? 0 : getHorizontalScrollbarHeight();
14551 }
14552 }
Romain Guy8506ab42009-06-11 17:35:47 -070014553
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014554 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014555 changed = true;
14556 mPaddingLeft = left;
14557 }
14558 if (mPaddingTop != top) {
14559 changed = true;
14560 mPaddingTop = top;
14561 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014562 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014564 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014565 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014566 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014567 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014568 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 }
14570
14571 if (changed) {
14572 requestLayout();
14573 }
14574 }
14575
14576 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014577 * Sets the relative padding. The view may add on the space required to display
14578 * the scrollbars, depending on the style and visibility of the scrollbars.
14579 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14580 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14581 * from the values set in this call.
14582 *
14583 * @attr ref android.R.styleable#View_padding
14584 * @attr ref android.R.styleable#View_paddingBottom
14585 * @attr ref android.R.styleable#View_paddingStart
14586 * @attr ref android.R.styleable#View_paddingEnd
14587 * @attr ref android.R.styleable#View_paddingTop
14588 * @param start the start padding in pixels
14589 * @param top the top padding in pixels
14590 * @param end the end padding in pixels
14591 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014592 */
14593 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014594 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014595
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014596 mUserPaddingStart = start;
14597 mUserPaddingEnd = end;
14598
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014599 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014600 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014601 mUserPaddingLeftInitial = end;
14602 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014603 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014604 break;
14605 case LAYOUT_DIRECTION_LTR:
14606 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014607 mUserPaddingLeftInitial = start;
14608 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014609 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014610 }
14611 }
14612
14613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014614 * Returns the top padding of this view.
14615 *
14616 * @return the top padding in pixels
14617 */
14618 public int getPaddingTop() {
14619 return mPaddingTop;
14620 }
14621
14622 /**
14623 * Returns the bottom padding of this view. If there are inset and enabled
14624 * scrollbars, this value may include the space required to display the
14625 * scrollbars as well.
14626 *
14627 * @return the bottom padding in pixels
14628 */
14629 public int getPaddingBottom() {
14630 return mPaddingBottom;
14631 }
14632
14633 /**
14634 * Returns the left padding of this view. If there are inset and enabled
14635 * scrollbars, this value may include the space required to display the
14636 * scrollbars as well.
14637 *
14638 * @return the left padding in pixels
14639 */
14640 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014641 if (!isPaddingResolved()) {
14642 resolvePadding();
14643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014644 return mPaddingLeft;
14645 }
14646
14647 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014648 * Returns the start padding of this view depending on its resolved layout direction.
14649 * If there are inset and enabled scrollbars, this value may include the space
14650 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014651 *
14652 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014653 */
14654 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014655 if (!isPaddingResolved()) {
14656 resolvePadding();
14657 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014658 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014659 mPaddingRight : mPaddingLeft;
14660 }
14661
14662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014663 * Returns the right padding of this view. If there are inset and enabled
14664 * scrollbars, this value may include the space required to display the
14665 * scrollbars as well.
14666 *
14667 * @return the right padding in pixels
14668 */
14669 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014670 if (!isPaddingResolved()) {
14671 resolvePadding();
14672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014673 return mPaddingRight;
14674 }
14675
14676 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014677 * Returns the end padding of this view depending on its resolved layout direction.
14678 * If there are inset and enabled scrollbars, this value may include the space
14679 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014680 *
14681 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014682 */
14683 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014684 if (!isPaddingResolved()) {
14685 resolvePadding();
14686 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014687 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014688 mPaddingLeft : mPaddingRight;
14689 }
14690
14691 /**
14692 * Return if the padding as been set thru relative values
14693 * {@link #setPaddingRelative(int, int, int, int)} or thru
14694 * @attr ref android.R.styleable#View_paddingStart or
14695 * @attr ref android.R.styleable#View_paddingEnd
14696 *
14697 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014698 */
14699 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014700 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014701 }
14702
14703 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014704 * @hide
14705 */
Philip Milne7a23b492012-04-24 22:12:36 -070014706 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014707 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014708 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014709 }
14710 return mLayoutInsets;
14711 }
14712
14713 /**
14714 * @hide
14715 */
14716 public void setLayoutInsets(Insets layoutInsets) {
14717 mLayoutInsets = layoutInsets;
14718 }
14719
14720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014721 * Changes the selection state of this view. A view can be selected or not.
14722 * Note that selection is not the same as focus. Views are typically
14723 * selected in the context of an AdapterView like ListView or GridView;
14724 * the selected view is the view that is highlighted.
14725 *
14726 * @param selected true if the view must be selected, false otherwise
14727 */
14728 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014729 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14730 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014731 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014732 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014733 refreshDrawableState();
14734 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014735 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14736 notifyAccessibilityStateChanged();
14737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 }
14739 }
14740
14741 /**
14742 * Dispatch setSelected to all of this View's children.
14743 *
14744 * @see #setSelected(boolean)
14745 *
14746 * @param selected The new selected state
14747 */
14748 protected void dispatchSetSelected(boolean selected) {
14749 }
14750
14751 /**
14752 * Indicates the selection state of this view.
14753 *
14754 * @return true if the view is selected, false otherwise
14755 */
14756 @ViewDebug.ExportedProperty
14757 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014758 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014759 }
14760
14761 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014762 * Changes the activated state of this view. A view can be activated or not.
14763 * Note that activation is not the same as selection. Selection is
14764 * a transient property, representing the view (hierarchy) the user is
14765 * currently interacting with. Activation is a longer-term state that the
14766 * user can move views in and out of. For example, in a list view with
14767 * single or multiple selection enabled, the views in the current selection
14768 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14769 * here.) The activated state is propagated down to children of the view it
14770 * is set on.
14771 *
14772 * @param activated true if the view must be activated, false otherwise
14773 */
14774 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014775 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14776 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014777 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014778 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014779 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014780 }
14781 }
14782
14783 /**
14784 * Dispatch setActivated to all of this View's children.
14785 *
14786 * @see #setActivated(boolean)
14787 *
14788 * @param activated The new activated state
14789 */
14790 protected void dispatchSetActivated(boolean activated) {
14791 }
14792
14793 /**
14794 * Indicates the activation state of this view.
14795 *
14796 * @return true if the view is activated, false otherwise
14797 */
14798 @ViewDebug.ExportedProperty
14799 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014800 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014801 }
14802
14803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014804 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14805 * observer can be used to get notifications when global events, like
14806 * layout, happen.
14807 *
14808 * The returned ViewTreeObserver observer is not guaranteed to remain
14809 * valid for the lifetime of this View. If the caller of this method keeps
14810 * a long-lived reference to ViewTreeObserver, it should always check for
14811 * the return value of {@link ViewTreeObserver#isAlive()}.
14812 *
14813 * @return The ViewTreeObserver for this view's hierarchy.
14814 */
14815 public ViewTreeObserver getViewTreeObserver() {
14816 if (mAttachInfo != null) {
14817 return mAttachInfo.mTreeObserver;
14818 }
14819 if (mFloatingTreeObserver == null) {
14820 mFloatingTreeObserver = new ViewTreeObserver();
14821 }
14822 return mFloatingTreeObserver;
14823 }
14824
14825 /**
14826 * <p>Finds the topmost view in the current view hierarchy.</p>
14827 *
14828 * @return the topmost view containing this view
14829 */
14830 public View getRootView() {
14831 if (mAttachInfo != null) {
14832 final View v = mAttachInfo.mRootView;
14833 if (v != null) {
14834 return v;
14835 }
14836 }
Romain Guy8506ab42009-06-11 17:35:47 -070014837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014838 View parent = this;
14839
14840 while (parent.mParent != null && parent.mParent instanceof View) {
14841 parent = (View) parent.mParent;
14842 }
14843
14844 return parent;
14845 }
14846
14847 /**
14848 * <p>Computes the coordinates of this view on the screen. The argument
14849 * must be an array of two integers. After the method returns, the array
14850 * contains the x and y location in that order.</p>
14851 *
14852 * @param location an array of two integers in which to hold the coordinates
14853 */
14854 public void getLocationOnScreen(int[] location) {
14855 getLocationInWindow(location);
14856
14857 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014858 if (info != null) {
14859 location[0] += info.mWindowLeft;
14860 location[1] += info.mWindowTop;
14861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014862 }
14863
14864 /**
14865 * <p>Computes the coordinates of this view in its window. The argument
14866 * must be an array of two integers. After the method returns, the array
14867 * contains the x and y location in that order.</p>
14868 *
14869 * @param location an array of two integers in which to hold the coordinates
14870 */
14871 public void getLocationInWindow(int[] location) {
14872 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014873 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014874 }
14875
Gilles Debunne6583ce52011-12-06 18:09:02 -080014876 if (mAttachInfo == null) {
14877 // When the view is not attached to a window, this method does not make sense
14878 location[0] = location[1] = 0;
14879 return;
14880 }
14881
Gilles Debunnecea45132011-11-24 02:19:27 +010014882 float[] position = mAttachInfo.mTmpTransformLocation;
14883 position[0] = position[1] = 0.0f;
14884
14885 if (!hasIdentityMatrix()) {
14886 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014888
Gilles Debunnecea45132011-11-24 02:19:27 +010014889 position[0] += mLeft;
14890 position[1] += mTop;
14891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014892 ViewParent viewParent = mParent;
14893 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014894 final View view = (View) viewParent;
14895
14896 position[0] -= view.mScrollX;
14897 position[1] -= view.mScrollY;
14898
14899 if (!view.hasIdentityMatrix()) {
14900 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014901 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014902
14903 position[0] += view.mLeft;
14904 position[1] += view.mTop;
14905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014906 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014907 }
Romain Guy8506ab42009-06-11 17:35:47 -070014908
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014909 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014911 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14912 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014913 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014914
14915 location[0] = (int) (position[0] + 0.5f);
14916 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014917 }
14918
14919 /**
14920 * {@hide}
14921 * @param id the id of the view to be found
14922 * @return the view of the specified id, null if cannot be found
14923 */
14924 protected View findViewTraversal(int id) {
14925 if (id == mID) {
14926 return this;
14927 }
14928 return null;
14929 }
14930
14931 /**
14932 * {@hide}
14933 * @param tag the tag of the view to be found
14934 * @return the view of specified tag, null if cannot be found
14935 */
14936 protected View findViewWithTagTraversal(Object tag) {
14937 if (tag != null && tag.equals(mTag)) {
14938 return this;
14939 }
14940 return null;
14941 }
14942
14943 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014944 * {@hide}
14945 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014946 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014947 * @return The first view that matches the predicate or null.
14948 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014949 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014950 if (predicate.apply(this)) {
14951 return this;
14952 }
14953 return null;
14954 }
14955
14956 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 * Look for a child view with the given id. If this view has the given
14958 * id, return this view.
14959 *
14960 * @param id The id to search for.
14961 * @return The view that has the given id in the hierarchy or null
14962 */
14963 public final View findViewById(int id) {
14964 if (id < 0) {
14965 return null;
14966 }
14967 return findViewTraversal(id);
14968 }
14969
14970 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014971 * Finds a view by its unuque and stable accessibility id.
14972 *
14973 * @param accessibilityId The searched accessibility id.
14974 * @return The found view.
14975 */
14976 final View findViewByAccessibilityId(int accessibilityId) {
14977 if (accessibilityId < 0) {
14978 return null;
14979 }
14980 return findViewByAccessibilityIdTraversal(accessibilityId);
14981 }
14982
14983 /**
14984 * Performs the traversal to find a view by its unuque and stable accessibility id.
14985 *
14986 * <strong>Note:</strong>This method does not stop at the root namespace
14987 * boundary since the user can touch the screen at an arbitrary location
14988 * potentially crossing the root namespace bounday which will send an
14989 * accessibility event to accessibility services and they should be able
14990 * to obtain the event source. Also accessibility ids are guaranteed to be
14991 * unique in the window.
14992 *
14993 * @param accessibilityId The accessibility id.
14994 * @return The found view.
14995 */
14996 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14997 if (getAccessibilityViewId() == accessibilityId) {
14998 return this;
14999 }
15000 return null;
15001 }
15002
15003 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015004 * Look for a child view with the given tag. If this view has the given
15005 * tag, return this view.
15006 *
15007 * @param tag The tag to search for, using "tag.equals(getTag())".
15008 * @return The View that has the given tag in the hierarchy or null
15009 */
15010 public final View findViewWithTag(Object tag) {
15011 if (tag == null) {
15012 return null;
15013 }
15014 return findViewWithTagTraversal(tag);
15015 }
15016
15017 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015018 * {@hide}
15019 * Look for a child view that matches the specified predicate.
15020 * If this view matches the predicate, return this view.
15021 *
15022 * @param predicate The predicate to evaluate.
15023 * @return The first view that matches the predicate or null.
15024 */
15025 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015026 return findViewByPredicateTraversal(predicate, null);
15027 }
15028
15029 /**
15030 * {@hide}
15031 * Look for a child view that matches the specified predicate,
15032 * starting with the specified view and its descendents and then
15033 * recusively searching the ancestors and siblings of that view
15034 * until this view is reached.
15035 *
15036 * This method is useful in cases where the predicate does not match
15037 * a single unique view (perhaps multiple views use the same id)
15038 * and we are trying to find the view that is "closest" in scope to the
15039 * starting view.
15040 *
15041 * @param start The view to start from.
15042 * @param predicate The predicate to evaluate.
15043 * @return The first view that matches the predicate or null.
15044 */
15045 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15046 View childToSkip = null;
15047 for (;;) {
15048 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15049 if (view != null || start == this) {
15050 return view;
15051 }
15052
15053 ViewParent parent = start.getParent();
15054 if (parent == null || !(parent instanceof View)) {
15055 return null;
15056 }
15057
15058 childToSkip = start;
15059 start = (View) parent;
15060 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015061 }
15062
15063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015064 * Sets the identifier for this view. The identifier does not have to be
15065 * unique in this view's hierarchy. The identifier should be a positive
15066 * number.
15067 *
15068 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015069 * @see #getId()
15070 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015071 *
15072 * @param id a number used to identify the view
15073 *
15074 * @attr ref android.R.styleable#View_id
15075 */
15076 public void setId(int id) {
15077 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015078 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15079 mID = generateViewId();
15080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015081 }
15082
15083 /**
15084 * {@hide}
15085 *
15086 * @param isRoot true if the view belongs to the root namespace, false
15087 * otherwise
15088 */
15089 public void setIsRootNamespace(boolean isRoot) {
15090 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015091 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015092 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015093 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015094 }
15095 }
15096
15097 /**
15098 * {@hide}
15099 *
15100 * @return true if the view belongs to the root namespace, false otherwise
15101 */
15102 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015103 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015104 }
15105
15106 /**
15107 * Returns this view's identifier.
15108 *
15109 * @return a positive integer used to identify the view or {@link #NO_ID}
15110 * if the view has no ID
15111 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015112 * @see #setId(int)
15113 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015114 * @attr ref android.R.styleable#View_id
15115 */
15116 @ViewDebug.CapturedViewProperty
15117 public int getId() {
15118 return mID;
15119 }
15120
15121 /**
15122 * Returns this view's tag.
15123 *
15124 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015125 *
15126 * @see #setTag(Object)
15127 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015128 */
15129 @ViewDebug.ExportedProperty
15130 public Object getTag() {
15131 return mTag;
15132 }
15133
15134 /**
15135 * Sets the tag associated with this view. A tag can be used to mark
15136 * a view in its hierarchy and does not have to be unique within the
15137 * hierarchy. Tags can also be used to store data within a view without
15138 * resorting to another data structure.
15139 *
15140 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015141 *
15142 * @see #getTag()
15143 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144 */
15145 public void setTag(final Object tag) {
15146 mTag = tag;
15147 }
15148
15149 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015150 * Returns the tag associated with this view and the specified key.
15151 *
15152 * @param key The key identifying the tag
15153 *
15154 * @return the Object stored in this view as a tag
15155 *
15156 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015157 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015158 */
15159 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015160 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015161 return null;
15162 }
15163
15164 /**
15165 * Sets a tag associated with this view and a key. A tag can be used
15166 * to mark a view in its hierarchy and does not have to be unique within
15167 * the hierarchy. Tags can also be used to store data within a view
15168 * without resorting to another data structure.
15169 *
15170 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015171 * application to ensure it is unique (see the <a
15172 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15173 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015174 * the Android framework or not associated with any package will cause
15175 * an {@link IllegalArgumentException} to be thrown.
15176 *
15177 * @param key The key identifying the tag
15178 * @param tag An Object to tag the view with
15179 *
15180 * @throws IllegalArgumentException If they specified key is not valid
15181 *
15182 * @see #setTag(Object)
15183 * @see #getTag(int)
15184 */
15185 public void setTag(int key, final Object tag) {
15186 // If the package id is 0x00 or 0x01, it's either an undefined package
15187 // or a framework id
15188 if ((key >>> 24) < 2) {
15189 throw new IllegalArgumentException("The key must be an application-specific "
15190 + "resource id.");
15191 }
15192
Adam Powell2b2f6d62011-09-23 11:15:39 -070015193 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015194 }
15195
15196 /**
15197 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15198 * framework id.
15199 *
15200 * @hide
15201 */
15202 public void setTagInternal(int key, Object tag) {
15203 if ((key >>> 24) != 0x1) {
15204 throw new IllegalArgumentException("The key must be a framework-specific "
15205 + "resource id.");
15206 }
15207
Adam Powell2b2f6d62011-09-23 11:15:39 -070015208 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015209 }
15210
Adam Powell2b2f6d62011-09-23 11:15:39 -070015211 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015212 if (mKeyedTags == null) {
15213 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015214 }
15215
Adam Powell7db82ac2011-09-22 19:44:04 -070015216 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015217 }
15218
15219 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015220 * Prints information about this view in the log output, with the tag
15221 * {@link #VIEW_LOG_TAG}.
15222 *
15223 * @hide
15224 */
15225 public void debug() {
15226 debug(0);
15227 }
15228
15229 /**
15230 * Prints information about this view in the log output, with the tag
15231 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15232 * indentation defined by the <code>depth</code>.
15233 *
15234 * @param depth the indentation level
15235 *
15236 * @hide
15237 */
15238 protected void debug(int depth) {
15239 String output = debugIndent(depth - 1);
15240
15241 output += "+ " + this;
15242 int id = getId();
15243 if (id != -1) {
15244 output += " (id=" + id + ")";
15245 }
15246 Object tag = getTag();
15247 if (tag != null) {
15248 output += " (tag=" + tag + ")";
15249 }
15250 Log.d(VIEW_LOG_TAG, output);
15251
Dianne Hackborn4702a852012-08-17 15:18:29 -070015252 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015253 output = debugIndent(depth) + " FOCUSED";
15254 Log.d(VIEW_LOG_TAG, output);
15255 }
15256
15257 output = debugIndent(depth);
15258 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15259 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15260 + "} ";
15261 Log.d(VIEW_LOG_TAG, output);
15262
15263 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15264 || mPaddingBottom != 0) {
15265 output = debugIndent(depth);
15266 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15267 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15268 Log.d(VIEW_LOG_TAG, output);
15269 }
15270
15271 output = debugIndent(depth);
15272 output += "mMeasureWidth=" + mMeasuredWidth +
15273 " mMeasureHeight=" + mMeasuredHeight;
15274 Log.d(VIEW_LOG_TAG, output);
15275
15276 output = debugIndent(depth);
15277 if (mLayoutParams == null) {
15278 output += "BAD! no layout params";
15279 } else {
15280 output = mLayoutParams.debug(output);
15281 }
15282 Log.d(VIEW_LOG_TAG, output);
15283
15284 output = debugIndent(depth);
15285 output += "flags={";
15286 output += View.printFlags(mViewFlags);
15287 output += "}";
15288 Log.d(VIEW_LOG_TAG, output);
15289
15290 output = debugIndent(depth);
15291 output += "privateFlags={";
15292 output += View.printPrivateFlags(mPrivateFlags);
15293 output += "}";
15294 Log.d(VIEW_LOG_TAG, output);
15295 }
15296
15297 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015298 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015299 *
15300 * @param depth the indentation level
15301 * @return a String containing (depth * 2 + 3) * 2 white spaces
15302 *
15303 * @hide
15304 */
15305 protected static String debugIndent(int depth) {
15306 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15307 for (int i = 0; i < (depth * 2) + 3; i++) {
15308 spaces.append(' ').append(' ');
15309 }
15310 return spaces.toString();
15311 }
15312
15313 /**
15314 * <p>Return the offset of the widget's text baseline from the widget's top
15315 * boundary. If this widget does not support baseline alignment, this
15316 * method returns -1. </p>
15317 *
15318 * @return the offset of the baseline within the widget's bounds or -1
15319 * if baseline alignment is not supported
15320 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015321 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015322 public int getBaseline() {
15323 return -1;
15324 }
15325
15326 /**
15327 * Call this when something has changed which has invalidated the
15328 * layout of this view. This will schedule a layout pass of the view
15329 * tree.
15330 */
15331 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015332 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15333 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015334
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015335 if (mParent != null && !mParent.isLayoutRequested()) {
15336 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015337 }
15338 }
15339
15340 /**
15341 * Forces this view to be laid out during the next layout pass.
15342 * This method does not call requestLayout() or forceLayout()
15343 * on the parent.
15344 */
15345 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015346 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15347 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015348 }
15349
15350 /**
15351 * <p>
15352 * This is called to find out how big a view should be. The parent
15353 * supplies constraint information in the width and height parameters.
15354 * </p>
15355 *
15356 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015357 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015358 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015359 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015360 * </p>
15361 *
15362 *
15363 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15364 * parent
15365 * @param heightMeasureSpec Vertical space requirements as imposed by the
15366 * parent
15367 *
15368 * @see #onMeasure(int, int)
15369 */
15370 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015371 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015372 widthMeasureSpec != mOldWidthMeasureSpec ||
15373 heightMeasureSpec != mOldHeightMeasureSpec) {
15374
15375 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015376 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015377
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070015378 if (!isPaddingResolved()) {
Adam Powell0090f202012-08-07 17:15:30 -070015379 resolvePadding();
15380 }
15381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015382 // measure ourselves, this should set the measured dimension flag back
15383 onMeasure(widthMeasureSpec, heightMeasureSpec);
15384
15385 // flag not set, setMeasuredDimension() was not invoked, we raise
15386 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015387 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015388 throw new IllegalStateException("onMeasure() did not set the"
15389 + " measured dimension by calling"
15390 + " setMeasuredDimension()");
15391 }
15392
Dianne Hackborn4702a852012-08-17 15:18:29 -070015393 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015394 }
15395
15396 mOldWidthMeasureSpec = widthMeasureSpec;
15397 mOldHeightMeasureSpec = heightMeasureSpec;
15398 }
15399
15400 /**
15401 * <p>
15402 * Measure the view and its content to determine the measured width and the
15403 * measured height. This method is invoked by {@link #measure(int, int)} and
15404 * should be overriden by subclasses to provide accurate and efficient
15405 * measurement of their contents.
15406 * </p>
15407 *
15408 * <p>
15409 * <strong>CONTRACT:</strong> When overriding this method, you
15410 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15411 * measured width and height of this view. Failure to do so will trigger an
15412 * <code>IllegalStateException</code>, thrown by
15413 * {@link #measure(int, int)}. Calling the superclass'
15414 * {@link #onMeasure(int, int)} is a valid use.
15415 * </p>
15416 *
15417 * <p>
15418 * The base class implementation of measure defaults to the background size,
15419 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15420 * override {@link #onMeasure(int, int)} to provide better measurements of
15421 * their content.
15422 * </p>
15423 *
15424 * <p>
15425 * If this method is overridden, it is the subclass's responsibility to make
15426 * sure the measured height and width are at least the view's minimum height
15427 * and width ({@link #getSuggestedMinimumHeight()} and
15428 * {@link #getSuggestedMinimumWidth()}).
15429 * </p>
15430 *
15431 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15432 * The requirements are encoded with
15433 * {@link android.view.View.MeasureSpec}.
15434 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15435 * The requirements are encoded with
15436 * {@link android.view.View.MeasureSpec}.
15437 *
15438 * @see #getMeasuredWidth()
15439 * @see #getMeasuredHeight()
15440 * @see #setMeasuredDimension(int, int)
15441 * @see #getSuggestedMinimumHeight()
15442 * @see #getSuggestedMinimumWidth()
15443 * @see android.view.View.MeasureSpec#getMode(int)
15444 * @see android.view.View.MeasureSpec#getSize(int)
15445 */
15446 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15447 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15448 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15449 }
15450
15451 /**
15452 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15453 * measured width and measured height. Failing to do so will trigger an
15454 * exception at measurement time.</p>
15455 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015456 * @param measuredWidth The measured width of this view. May be a complex
15457 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15458 * {@link #MEASURED_STATE_TOO_SMALL}.
15459 * @param measuredHeight The measured height of this view. May be a complex
15460 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15461 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015462 */
15463 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15464 mMeasuredWidth = measuredWidth;
15465 mMeasuredHeight = measuredHeight;
15466
Dianne Hackborn4702a852012-08-17 15:18:29 -070015467 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015468 }
15469
15470 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015471 * Merge two states as returned by {@link #getMeasuredState()}.
15472 * @param curState The current state as returned from a view or the result
15473 * of combining multiple views.
15474 * @param newState The new view state to combine.
15475 * @return Returns a new integer reflecting the combination of the two
15476 * states.
15477 */
15478 public static int combineMeasuredStates(int curState, int newState) {
15479 return curState | newState;
15480 }
15481
15482 /**
15483 * Version of {@link #resolveSizeAndState(int, int, int)}
15484 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15485 */
15486 public static int resolveSize(int size, int measureSpec) {
15487 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15488 }
15489
15490 /**
15491 * Utility to reconcile a desired size and state, with constraints imposed
15492 * by a MeasureSpec. Will take the desired size, unless a different size
15493 * is imposed by the constraints. The returned value is a compound integer,
15494 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15495 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15496 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015497 *
15498 * @param size How big the view wants to be
15499 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015500 * @return Size information bit mask as defined by
15501 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015502 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015503 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015504 int result = size;
15505 int specMode = MeasureSpec.getMode(measureSpec);
15506 int specSize = MeasureSpec.getSize(measureSpec);
15507 switch (specMode) {
15508 case MeasureSpec.UNSPECIFIED:
15509 result = size;
15510 break;
15511 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015512 if (specSize < size) {
15513 result = specSize | MEASURED_STATE_TOO_SMALL;
15514 } else {
15515 result = size;
15516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015517 break;
15518 case MeasureSpec.EXACTLY:
15519 result = specSize;
15520 break;
15521 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015522 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015523 }
15524
15525 /**
15526 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015527 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015528 * by the MeasureSpec.
15529 *
15530 * @param size Default size for this view
15531 * @param measureSpec Constraints imposed by the parent
15532 * @return The size this view should be.
15533 */
15534 public static int getDefaultSize(int size, int measureSpec) {
15535 int result = size;
15536 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015537 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015538
15539 switch (specMode) {
15540 case MeasureSpec.UNSPECIFIED:
15541 result = size;
15542 break;
15543 case MeasureSpec.AT_MOST:
15544 case MeasureSpec.EXACTLY:
15545 result = specSize;
15546 break;
15547 }
15548 return result;
15549 }
15550
15551 /**
15552 * Returns the suggested minimum height that the view should use. This
15553 * returns the maximum of the view's minimum height
15554 * and the background's minimum height
15555 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15556 * <p>
15557 * When being used in {@link #onMeasure(int, int)}, the caller should still
15558 * ensure the returned height is within the requirements of the parent.
15559 *
15560 * @return The suggested minimum height of the view.
15561 */
15562 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015563 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015565 }
15566
15567 /**
15568 * Returns the suggested minimum width that the view should use. This
15569 * returns the maximum of the view's minimum width)
15570 * and the background's minimum width
15571 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15572 * <p>
15573 * When being used in {@link #onMeasure(int, int)}, the caller should still
15574 * ensure the returned width is within the requirements of the parent.
15575 *
15576 * @return The suggested minimum width of the view.
15577 */
15578 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015579 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015581
Philip Milne6c8ea062012-04-03 17:38:43 -070015582 /**
15583 * Returns the minimum height of the view.
15584 *
15585 * @return the minimum height the view will try to be.
15586 *
15587 * @see #setMinimumHeight(int)
15588 *
15589 * @attr ref android.R.styleable#View_minHeight
15590 */
15591 public int getMinimumHeight() {
15592 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015593 }
15594
15595 /**
15596 * Sets the minimum height of the view. It is not guaranteed the view will
15597 * be able to achieve this minimum height (for example, if its parent layout
15598 * constrains it with less available height).
15599 *
15600 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015601 *
15602 * @see #getMinimumHeight()
15603 *
15604 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015605 */
15606 public void setMinimumHeight(int minHeight) {
15607 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015608 requestLayout();
15609 }
15610
15611 /**
15612 * Returns the minimum width of the view.
15613 *
15614 * @return the minimum width the view will try to be.
15615 *
15616 * @see #setMinimumWidth(int)
15617 *
15618 * @attr ref android.R.styleable#View_minWidth
15619 */
15620 public int getMinimumWidth() {
15621 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015622 }
15623
15624 /**
15625 * Sets the minimum width of the view. It is not guaranteed the view will
15626 * be able to achieve this minimum width (for example, if its parent layout
15627 * constrains it with less available width).
15628 *
15629 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015630 *
15631 * @see #getMinimumWidth()
15632 *
15633 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015634 */
15635 public void setMinimumWidth(int minWidth) {
15636 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015637 requestLayout();
15638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015639 }
15640
15641 /**
15642 * Get the animation currently associated with this view.
15643 *
15644 * @return The animation that is currently playing or
15645 * scheduled to play for this view.
15646 */
15647 public Animation getAnimation() {
15648 return mCurrentAnimation;
15649 }
15650
15651 /**
15652 * Start the specified animation now.
15653 *
15654 * @param animation the animation to start now
15655 */
15656 public void startAnimation(Animation animation) {
15657 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15658 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015659 invalidateParentCaches();
15660 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015661 }
15662
15663 /**
15664 * Cancels any animations for this view.
15665 */
15666 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015667 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015668 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015670 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015671 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015672 }
15673
15674 /**
15675 * Sets the next animation to play for this view.
15676 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015677 * {@link #startAnimation(android.view.animation.Animation)} instead.
15678 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015679 * control over the start time and invalidation, but you
15680 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015681 * 2) the view's parent (which controls animations on its children)
15682 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015683 * start.
15684 *
15685 * @param animation The next animation, or null.
15686 */
15687 public void setAnimation(Animation animation) {
15688 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015690 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015691 // If the screen is off assume the animation start time is now instead of
15692 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15693 // would cause the animation to start when the screen turns back on
15694 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15695 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15696 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015698 animation.reset();
15699 }
15700 }
15701
15702 /**
15703 * Invoked by a parent ViewGroup to notify the start of the animation
15704 * currently associated with this view. If you override this method,
15705 * always call super.onAnimationStart();
15706 *
15707 * @see #setAnimation(android.view.animation.Animation)
15708 * @see #getAnimation()
15709 */
15710 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015711 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015712 }
15713
15714 /**
15715 * Invoked by a parent ViewGroup to notify the end of the animation
15716 * currently associated with this view. If you override this method,
15717 * always call super.onAnimationEnd();
15718 *
15719 * @see #setAnimation(android.view.animation.Animation)
15720 * @see #getAnimation()
15721 */
15722 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015723 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015724 }
15725
15726 /**
15727 * Invoked if there is a Transform that involves alpha. Subclass that can
15728 * draw themselves with the specified alpha should return true, and then
15729 * respect that alpha when their onDraw() is called. If this returns false
15730 * then the view may be redirected to draw into an offscreen buffer to
15731 * fulfill the request, which will look fine, but may be slower than if the
15732 * subclass handles it internally. The default implementation returns false.
15733 *
15734 * @param alpha The alpha (0..255) to apply to the view's drawing
15735 * @return true if the view can draw with the specified alpha.
15736 */
15737 protected boolean onSetAlpha(int alpha) {
15738 return false;
15739 }
15740
15741 /**
15742 * This is used by the RootView to perform an optimization when
15743 * the view hierarchy contains one or several SurfaceView.
15744 * SurfaceView is always considered transparent, but its children are not,
15745 * therefore all View objects remove themselves from the global transparent
15746 * region (passed as a parameter to this function).
15747 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015748 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015749 *
15750 * @return Returns true if the effective visibility of the view at this
15751 * point is opaque, regardless of the transparent region; returns false
15752 * if it is possible for underlying windows to be seen behind the view.
15753 *
15754 * {@hide}
15755 */
15756 public boolean gatherTransparentRegion(Region region) {
15757 final AttachInfo attachInfo = mAttachInfo;
15758 if (region != null && attachInfo != null) {
15759 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015760 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015761 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15762 // remove it from the transparent region.
15763 final int[] location = attachInfo.mTransparentLocation;
15764 getLocationInWindow(location);
15765 region.op(location[0], location[1], location[0] + mRight - mLeft,
15766 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015767 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015768 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15769 // exists, so we remove the background drawable's non-transparent
15770 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015771 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015772 }
15773 }
15774 return true;
15775 }
15776
15777 /**
15778 * Play a sound effect for this view.
15779 *
15780 * <p>The framework will play sound effects for some built in actions, such as
15781 * clicking, but you may wish to play these effects in your widget,
15782 * for instance, for internal navigation.
15783 *
15784 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15785 * {@link #isSoundEffectsEnabled()} is true.
15786 *
15787 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15788 */
15789 public void playSoundEffect(int soundConstant) {
15790 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15791 return;
15792 }
15793 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15794 }
15795
15796 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015797 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015798 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015799 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015800 *
15801 * <p>The framework will provide haptic feedback for some built in actions,
15802 * such as long presses, but you may wish to provide feedback for your
15803 * own widget.
15804 *
15805 * <p>The feedback will only be performed if
15806 * {@link #isHapticFeedbackEnabled()} is true.
15807 *
15808 * @param feedbackConstant One of the constants defined in
15809 * {@link HapticFeedbackConstants}
15810 */
15811 public boolean performHapticFeedback(int feedbackConstant) {
15812 return performHapticFeedback(feedbackConstant, 0);
15813 }
15814
15815 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015816 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015817 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015818 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015819 *
15820 * @param feedbackConstant One of the constants defined in
15821 * {@link HapticFeedbackConstants}
15822 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15823 */
15824 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15825 if (mAttachInfo == null) {
15826 return false;
15827 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015828 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015829 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 && !isHapticFeedbackEnabled()) {
15831 return false;
15832 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015833 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15834 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015835 }
15836
15837 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015838 * Request that the visibility of the status bar or other screen/window
15839 * decorations be changed.
15840 *
15841 * <p>This method is used to put the over device UI into temporary modes
15842 * where the user's attention is focused more on the application content,
15843 * by dimming or hiding surrounding system affordances. This is typically
15844 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15845 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15846 * to be placed behind the action bar (and with these flags other system
15847 * affordances) so that smooth transitions between hiding and showing them
15848 * can be done.
15849 *
15850 * <p>Two representative examples of the use of system UI visibility is
15851 * implementing a content browsing application (like a magazine reader)
15852 * and a video playing application.
15853 *
15854 * <p>The first code shows a typical implementation of a View in a content
15855 * browsing application. In this implementation, the application goes
15856 * into a content-oriented mode by hiding the status bar and action bar,
15857 * and putting the navigation elements into lights out mode. The user can
15858 * then interact with content while in this mode. Such an application should
15859 * provide an easy way for the user to toggle out of the mode (such as to
15860 * check information in the status bar or access notifications). In the
15861 * implementation here, this is done simply by tapping on the content.
15862 *
15863 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15864 * content}
15865 *
15866 * <p>This second code sample shows a typical implementation of a View
15867 * in a video playing application. In this situation, while the video is
15868 * playing the application would like to go into a complete full-screen mode,
15869 * to use as much of the display as possible for the video. When in this state
15870 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015871 * touching on the screen to pop the UI out of full screen mode. See
15872 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015873 *
15874 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15875 * content}
15876 *
15877 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15878 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15879 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15880 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015881 */
15882 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015883 if (visibility != mSystemUiVisibility) {
15884 mSystemUiVisibility = visibility;
15885 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15886 mParent.recomputeViewAttributes(this);
15887 }
Joe Onorato664644d2011-01-23 17:53:23 -080015888 }
15889 }
15890
15891 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015892 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15893 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15894 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15895 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15896 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015897 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015898 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015899 return mSystemUiVisibility;
15900 }
15901
Scott Mainec6331b2011-05-24 16:55:56 -070015902 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015903 * Returns the current system UI visibility that is currently set for
15904 * the entire window. This is the combination of the
15905 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15906 * views in the window.
15907 */
15908 public int getWindowSystemUiVisibility() {
15909 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15910 }
15911
15912 /**
15913 * Override to find out when the window's requested system UI visibility
15914 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15915 * This is different from the callbacks recieved through
15916 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15917 * in that this is only telling you about the local request of the window,
15918 * not the actual values applied by the system.
15919 */
15920 public void onWindowSystemUiVisibilityChanged(int visible) {
15921 }
15922
15923 /**
15924 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15925 * the view hierarchy.
15926 */
15927 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15928 onWindowSystemUiVisibilityChanged(visible);
15929 }
15930
15931 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015932 * Set a listener to receive callbacks when the visibility of the system bar changes.
15933 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15934 */
Joe Onorato664644d2011-01-23 17:53:23 -080015935 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015936 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015937 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15938 mParent.recomputeViewAttributes(this);
15939 }
15940 }
15941
15942 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015943 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15944 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015945 */
15946 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015947 ListenerInfo li = mListenerInfo;
15948 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15949 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015950 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015951 }
15952 }
15953
Dianne Hackborncf675782012-05-10 15:07:24 -070015954 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015955 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15956 if (val != mSystemUiVisibility) {
15957 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070015958 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015959 }
Dianne Hackborncf675782012-05-10 15:07:24 -070015960 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015961 }
15962
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015963 /** @hide */
15964 public void setDisabledSystemUiVisibility(int flags) {
15965 if (mAttachInfo != null) {
15966 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15967 mAttachInfo.mDisabledSystemUiVisibility = flags;
15968 if (mParent != null) {
15969 mParent.recomputeViewAttributes(this);
15970 }
15971 }
15972 }
15973 }
15974
Joe Onorato664644d2011-01-23 17:53:23 -080015975 /**
Joe Malin32736f02011-01-19 16:14:20 -080015976 * Creates an image that the system displays during the drag and drop
15977 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15978 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15979 * appearance as the given View. The default also positions the center of the drag shadow
15980 * directly under the touch point. If no View is provided (the constructor with no parameters
15981 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15982 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15983 * default is an invisible drag shadow.
15984 * <p>
15985 * You are not required to use the View you provide to the constructor as the basis of the
15986 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15987 * anything you want as the drag shadow.
15988 * </p>
15989 * <p>
15990 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15991 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15992 * size and position of the drag shadow. It uses this data to construct a
15993 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15994 * so that your application can draw the shadow image in the Canvas.
15995 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015996 *
15997 * <div class="special reference">
15998 * <h3>Developer Guides</h3>
15999 * <p>For a guide to implementing drag and drop features, read the
16000 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16001 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016002 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016003 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016004 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016005
16006 /**
Joe Malin32736f02011-01-19 16:14:20 -080016007 * Constructs a shadow image builder based on a View. By default, the resulting drag
16008 * shadow will have the same appearance and dimensions as the View, with the touch point
16009 * over the center of the View.
16010 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016011 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016012 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016013 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016014 }
16015
Christopher Tate17ed60c2011-01-18 12:50:26 -080016016 /**
16017 * Construct a shadow builder object with no associated View. This
16018 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16019 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16020 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016021 * reference to any View object. If they are not overridden, then the result is an
16022 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016023 */
16024 public DragShadowBuilder() {
16025 mView = new WeakReference<View>(null);
16026 }
16027
16028 /**
16029 * Returns the View object that had been passed to the
16030 * {@link #View.DragShadowBuilder(View)}
16031 * constructor. If that View parameter was {@code null} or if the
16032 * {@link #View.DragShadowBuilder()}
16033 * constructor was used to instantiate the builder object, this method will return
16034 * null.
16035 *
16036 * @return The View object associate with this builder object.
16037 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016038 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016039 final public View getView() {
16040 return mView.get();
16041 }
16042
Christopher Tate2c095f32010-10-04 14:13:40 -070016043 /**
Joe Malin32736f02011-01-19 16:14:20 -080016044 * Provides the metrics for the shadow image. These include the dimensions of
16045 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016046 * be centered under the touch location while dragging.
16047 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016048 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016049 * same as the dimensions of the View itself and centers the shadow under
16050 * the touch point.
16051 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016052 *
Joe Malin32736f02011-01-19 16:14:20 -080016053 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16054 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16055 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16056 * image.
16057 *
16058 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16059 * shadow image that should be underneath the touch point during the drag and drop
16060 * operation. Your application must set {@link android.graphics.Point#x} to the
16061 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016062 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016063 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016064 final View view = mView.get();
16065 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016066 shadowSize.set(view.getWidth(), view.getHeight());
16067 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016068 } else {
16069 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16070 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016071 }
16072
16073 /**
Joe Malin32736f02011-01-19 16:14:20 -080016074 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16075 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016076 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016077 *
Joe Malin32736f02011-01-19 16:14:20 -080016078 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016079 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016080 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016081 final View view = mView.get();
16082 if (view != null) {
16083 view.draw(canvas);
16084 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016085 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016086 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016087 }
16088 }
16089
16090 /**
Joe Malin32736f02011-01-19 16:14:20 -080016091 * Starts a drag and drop operation. When your application calls this method, it passes a
16092 * {@link android.view.View.DragShadowBuilder} object to the system. The
16093 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16094 * to get metrics for the drag shadow, and then calls the object's
16095 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16096 * <p>
16097 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16098 * drag events to all the View objects in your application that are currently visible. It does
16099 * this either by calling the View object's drag listener (an implementation of
16100 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16101 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16102 * Both are passed a {@link android.view.DragEvent} object that has a
16103 * {@link android.view.DragEvent#getAction()} value of
16104 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16105 * </p>
16106 * <p>
16107 * Your application can invoke startDrag() on any attached View object. The View object does not
16108 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16109 * be related to the View the user selected for dragging.
16110 * </p>
16111 * @param data A {@link android.content.ClipData} object pointing to the data to be
16112 * transferred by the drag and drop operation.
16113 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16114 * drag shadow.
16115 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16116 * drop operation. This Object is put into every DragEvent object sent by the system during the
16117 * current drag.
16118 * <p>
16119 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16120 * to the target Views. For example, it can contain flags that differentiate between a
16121 * a copy operation and a move operation.
16122 * </p>
16123 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16124 * so the parameter should be set to 0.
16125 * @return {@code true} if the method completes successfully, or
16126 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16127 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016128 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016129 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016130 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016131 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016132 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016133 }
16134 boolean okay = false;
16135
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016136 Point shadowSize = new Point();
16137 Point shadowTouchPoint = new Point();
16138 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016139
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016140 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16141 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16142 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016143 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016144
Chris Tatea32dcf72010-10-14 12:13:50 -070016145 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016146 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16147 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016148 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016149 Surface surface = new Surface();
16150 try {
16151 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016152 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016153 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016154 + " surface=" + surface);
16155 if (token != null) {
16156 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016157 try {
Chris Tate6b391282010-10-14 15:48:59 -070016158 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016159 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016160 } finally {
16161 surface.unlockCanvasAndPost(canvas);
16162 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016163
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016164 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016165
16166 // Cache the local state object for delivery with DragEvents
16167 root.setLocalDragState(myLocalState);
16168
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016169 // repurpose 'shadowSize' for the last touch point
16170 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016171
Christopher Tatea53146c2010-09-07 11:57:52 -070016172 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016173 shadowSize.x, shadowSize.y,
16174 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016175 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016176
16177 // Off and running! Release our local surface instance; the drag
16178 // shadow surface is now managed by the system process.
16179 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016180 }
16181 } catch (Exception e) {
16182 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16183 surface.destroy();
16184 }
16185
16186 return okay;
16187 }
16188
Christopher Tatea53146c2010-09-07 11:57:52 -070016189 /**
Joe Malin32736f02011-01-19 16:14:20 -080016190 * Handles drag events sent by the system following a call to
16191 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16192 *<p>
16193 * When the system calls this method, it passes a
16194 * {@link android.view.DragEvent} object. A call to
16195 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16196 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16197 * operation.
16198 * @param event The {@link android.view.DragEvent} sent by the system.
16199 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16200 * in DragEvent, indicating the type of drag event represented by this object.
16201 * @return {@code true} if the method was successful, otherwise {@code false}.
16202 * <p>
16203 * The method should return {@code true} in response to an action type of
16204 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16205 * operation.
16206 * </p>
16207 * <p>
16208 * The method should also return {@code true} in response to an action type of
16209 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16210 * {@code false} if it didn't.
16211 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016212 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016213 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016214 return false;
16215 }
16216
16217 /**
Joe Malin32736f02011-01-19 16:14:20 -080016218 * Detects if this View is enabled and has a drag event listener.
16219 * If both are true, then it calls the drag event listener with the
16220 * {@link android.view.DragEvent} it received. If the drag event listener returns
16221 * {@code true}, then dispatchDragEvent() returns {@code true}.
16222 * <p>
16223 * For all other cases, the method calls the
16224 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16225 * method and returns its result.
16226 * </p>
16227 * <p>
16228 * This ensures that a drag event is always consumed, even if the View does not have a drag
16229 * event listener. However, if the View has a listener and the listener returns true, then
16230 * onDragEvent() is not called.
16231 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016232 */
16233 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016234 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016235 ListenerInfo li = mListenerInfo;
16236 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16237 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016238 return true;
16239 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016240 return onDragEvent(event);
16241 }
16242
Christopher Tate3d4bf172011-03-28 16:16:46 -070016243 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016244 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016245 }
16246
Christopher Tatea53146c2010-09-07 11:57:52 -070016247 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016248 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16249 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016250 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016251 */
16252 public void onCloseSystemDialogs(String reason) {
16253 }
Joe Malin32736f02011-01-19 16:14:20 -080016254
Dianne Hackbornffa42482009-09-23 22:20:11 -070016255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016256 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016257 * update a Region being computed for
16258 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016259 * that any non-transparent parts of the Drawable are removed from the
16260 * given transparent region.
16261 *
16262 * @param dr The Drawable whose transparency is to be applied to the region.
16263 * @param region A Region holding the current transparency information,
16264 * where any parts of the region that are set are considered to be
16265 * transparent. On return, this region will be modified to have the
16266 * transparency information reduced by the corresponding parts of the
16267 * Drawable that are not transparent.
16268 * {@hide}
16269 */
16270 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16271 if (DBG) {
16272 Log.i("View", "Getting transparent region for: " + this);
16273 }
16274 final Region r = dr.getTransparentRegion();
16275 final Rect db = dr.getBounds();
16276 final AttachInfo attachInfo = mAttachInfo;
16277 if (r != null && attachInfo != null) {
16278 final int w = getRight()-getLeft();
16279 final int h = getBottom()-getTop();
16280 if (db.left > 0) {
16281 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16282 r.op(0, 0, db.left, h, Region.Op.UNION);
16283 }
16284 if (db.right < w) {
16285 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16286 r.op(db.right, 0, w, h, Region.Op.UNION);
16287 }
16288 if (db.top > 0) {
16289 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16290 r.op(0, 0, w, db.top, Region.Op.UNION);
16291 }
16292 if (db.bottom < h) {
16293 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16294 r.op(0, db.bottom, w, h, Region.Op.UNION);
16295 }
16296 final int[] location = attachInfo.mTransparentLocation;
16297 getLocationInWindow(location);
16298 r.translate(location[0], location[1]);
16299 region.op(r, Region.Op.INTERSECT);
16300 } else {
16301 region.op(db, Region.Op.DIFFERENCE);
16302 }
16303 }
16304
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016305 private void checkForLongClick(int delayOffset) {
16306 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16307 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016308
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016309 if (mPendingCheckForLongPress == null) {
16310 mPendingCheckForLongPress = new CheckForLongPress();
16311 }
16312 mPendingCheckForLongPress.rememberWindowAttachCount();
16313 postDelayed(mPendingCheckForLongPress,
16314 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016316 }
16317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016318 /**
16319 * Inflate a view from an XML resource. This convenience method wraps the {@link
16320 * LayoutInflater} class, which provides a full range of options for view inflation.
16321 *
16322 * @param context The Context object for your activity or application.
16323 * @param resource The resource ID to inflate
16324 * @param root A view group that will be the parent. Used to properly inflate the
16325 * layout_* parameters.
16326 * @see LayoutInflater
16327 */
16328 public static View inflate(Context context, int resource, ViewGroup root) {
16329 LayoutInflater factory = LayoutInflater.from(context);
16330 return factory.inflate(resource, root);
16331 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016333 /**
Adam Powell637d3372010-08-25 14:37:03 -070016334 * Scroll the view with standard behavior for scrolling beyond the normal
16335 * content boundaries. Views that call this method should override
16336 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16337 * results of an over-scroll operation.
16338 *
16339 * Views can use this method to handle any touch or fling-based scrolling.
16340 *
16341 * @param deltaX Change in X in pixels
16342 * @param deltaY Change in Y in pixels
16343 * @param scrollX Current X scroll value in pixels before applying deltaX
16344 * @param scrollY Current Y scroll value in pixels before applying deltaY
16345 * @param scrollRangeX Maximum content scroll range along the X axis
16346 * @param scrollRangeY Maximum content scroll range along the Y axis
16347 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16348 * along the X axis.
16349 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16350 * along the Y axis.
16351 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16352 * @return true if scrolling was clamped to an over-scroll boundary along either
16353 * axis, false otherwise.
16354 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016355 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016356 protected boolean overScrollBy(int deltaX, int deltaY,
16357 int scrollX, int scrollY,
16358 int scrollRangeX, int scrollRangeY,
16359 int maxOverScrollX, int maxOverScrollY,
16360 boolean isTouchEvent) {
16361 final int overScrollMode = mOverScrollMode;
16362 final boolean canScrollHorizontal =
16363 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16364 final boolean canScrollVertical =
16365 computeVerticalScrollRange() > computeVerticalScrollExtent();
16366 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16367 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16368 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16369 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16370
16371 int newScrollX = scrollX + deltaX;
16372 if (!overScrollHorizontal) {
16373 maxOverScrollX = 0;
16374 }
16375
16376 int newScrollY = scrollY + deltaY;
16377 if (!overScrollVertical) {
16378 maxOverScrollY = 0;
16379 }
16380
16381 // Clamp values if at the limits and record
16382 final int left = -maxOverScrollX;
16383 final int right = maxOverScrollX + scrollRangeX;
16384 final int top = -maxOverScrollY;
16385 final int bottom = maxOverScrollY + scrollRangeY;
16386
16387 boolean clampedX = false;
16388 if (newScrollX > right) {
16389 newScrollX = right;
16390 clampedX = true;
16391 } else if (newScrollX < left) {
16392 newScrollX = left;
16393 clampedX = true;
16394 }
16395
16396 boolean clampedY = false;
16397 if (newScrollY > bottom) {
16398 newScrollY = bottom;
16399 clampedY = true;
16400 } else if (newScrollY < top) {
16401 newScrollY = top;
16402 clampedY = true;
16403 }
16404
16405 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16406
16407 return clampedX || clampedY;
16408 }
16409
16410 /**
16411 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16412 * respond to the results of an over-scroll operation.
16413 *
16414 * @param scrollX New X scroll value in pixels
16415 * @param scrollY New Y scroll value in pixels
16416 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16417 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16418 */
16419 protected void onOverScrolled(int scrollX, int scrollY,
16420 boolean clampedX, boolean clampedY) {
16421 // Intentionally empty.
16422 }
16423
16424 /**
16425 * Returns the over-scroll mode for this view. The result will be
16426 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16427 * (allow over-scrolling only if the view content is larger than the container),
16428 * or {@link #OVER_SCROLL_NEVER}.
16429 *
16430 * @return This view's over-scroll mode.
16431 */
16432 public int getOverScrollMode() {
16433 return mOverScrollMode;
16434 }
16435
16436 /**
16437 * Set the over-scroll mode for this view. Valid over-scroll modes are
16438 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16439 * (allow over-scrolling only if the view content is larger than the container),
16440 * or {@link #OVER_SCROLL_NEVER}.
16441 *
16442 * Setting the over-scroll mode of a view will have an effect only if the
16443 * view is capable of scrolling.
16444 *
16445 * @param overScrollMode The new over-scroll mode for this view.
16446 */
16447 public void setOverScrollMode(int overScrollMode) {
16448 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16449 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16450 overScrollMode != OVER_SCROLL_NEVER) {
16451 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16452 }
16453 mOverScrollMode = overScrollMode;
16454 }
16455
16456 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016457 * Gets a scale factor that determines the distance the view should scroll
16458 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16459 * @return The vertical scroll scale factor.
16460 * @hide
16461 */
16462 protected float getVerticalScrollFactor() {
16463 if (mVerticalScrollFactor == 0) {
16464 TypedValue outValue = new TypedValue();
16465 if (!mContext.getTheme().resolveAttribute(
16466 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16467 throw new IllegalStateException(
16468 "Expected theme to define listPreferredItemHeight.");
16469 }
16470 mVerticalScrollFactor = outValue.getDimension(
16471 mContext.getResources().getDisplayMetrics());
16472 }
16473 return mVerticalScrollFactor;
16474 }
16475
16476 /**
16477 * Gets a scale factor that determines the distance the view should scroll
16478 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16479 * @return The horizontal scroll scale factor.
16480 * @hide
16481 */
16482 protected float getHorizontalScrollFactor() {
16483 // TODO: Should use something else.
16484 return getVerticalScrollFactor();
16485 }
16486
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016487 /**
16488 * Return the value specifying the text direction or policy that was set with
16489 * {@link #setTextDirection(int)}.
16490 *
16491 * @return the defined text direction. It can be one of:
16492 *
16493 * {@link #TEXT_DIRECTION_INHERIT},
16494 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16495 * {@link #TEXT_DIRECTION_ANY_RTL},
16496 * {@link #TEXT_DIRECTION_LTR},
16497 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016498 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016499 *
16500 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016501 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016502 @ViewDebug.ExportedProperty(category = "text", mapping = {
16503 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16504 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16505 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16506 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16507 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16508 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16509 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016510 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016511 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016512 }
16513
16514 /**
16515 * Set the text direction.
16516 *
16517 * @param textDirection the direction to set. Should be one of:
16518 *
16519 * {@link #TEXT_DIRECTION_INHERIT},
16520 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16521 * {@link #TEXT_DIRECTION_ANY_RTL},
16522 * {@link #TEXT_DIRECTION_LTR},
16523 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016524 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016525 */
16526 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016527 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016528 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016529 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016530 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016531 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016532 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016533 // Notify change
16534 onRtlPropertiesChanged();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016535 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016536 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016537 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016538 }
16539 }
16540
16541 /**
16542 * Return the resolved text direction.
16543 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016544 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches what has
16545 * been set by {@link #setTextDirection(int)} if it is not TEXT_DIRECTION_INHERIT, otherwise the
16546 * resolution proceeds up the parent chain of the view. If there is no parent, then it will
16547 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016548 *
16549 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016550 *
Doug Feltcb3791202011-07-07 11:57:48 -070016551 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16552 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016553 * {@link #TEXT_DIRECTION_LTR},
16554 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016555 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016556 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016557 public int getTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070016558 // The text direction will be resolved only if needed
Dianne Hackborn4702a852012-08-17 15:18:29 -070016559 if ((mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) != PFLAG2_TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016560 resolveTextDirection();
16561 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016562 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016563 }
16564
16565 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016566 * Resolve the text direction.
16567 *
16568 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016569 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016570 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016571 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016572 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016573
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016574 if (hasRtlSupport()) {
16575 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016576 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016577 switch(textDirection) {
16578 case TEXT_DIRECTION_INHERIT:
16579 if (canResolveTextDirection()) {
16580 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016581
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016582 // Set current resolved direction to the same value as the parent's one
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016583 final int parentResolvedDirection = viewGroup.getTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016584 switch (parentResolvedDirection) {
16585 case TEXT_DIRECTION_FIRST_STRONG:
16586 case TEXT_DIRECTION_ANY_RTL:
16587 case TEXT_DIRECTION_LTR:
16588 case TEXT_DIRECTION_RTL:
16589 case TEXT_DIRECTION_LOCALE:
16590 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016591 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016592 break;
16593 default:
16594 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016595 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016596 }
16597 } else {
16598 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016599 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016600 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016601 break;
16602 case TEXT_DIRECTION_FIRST_STRONG:
16603 case TEXT_DIRECTION_ANY_RTL:
16604 case TEXT_DIRECTION_LTR:
16605 case TEXT_DIRECTION_RTL:
16606 case TEXT_DIRECTION_LOCALE:
16607 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016608 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016609 break;
16610 default:
16611 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016612 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016613 }
16614 } else {
16615 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016616 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016617 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016618
16619 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016620 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016621 }
16622
16623 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016624 * Check if text direction resolution can be done.
16625 *
16626 * @return true if text direction resolution can be done otherwise return false.
16627 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016628 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016629 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016630 case TEXT_DIRECTION_INHERIT:
16631 return (mParent != null) && (mParent instanceof ViewGroup);
16632 default:
16633 return true;
16634 }
16635 }
16636
16637 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016638 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016639 * getTextDirection().
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016640 *
16641 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016642 */
16643 public void resetResolvedTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016644 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016645 }
16646
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016647 /**
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016648 * @hide
16649 */
16650 public boolean isTextDirectionInherited() {
16651 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16652 }
16653
16654 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016655 * Return the value specifying the text alignment or policy that was set with
16656 * {@link #setTextAlignment(int)}.
16657 *
16658 * @return the defined text alignment. It can be one of:
16659 *
16660 * {@link #TEXT_ALIGNMENT_INHERIT},
16661 * {@link #TEXT_ALIGNMENT_GRAVITY},
16662 * {@link #TEXT_ALIGNMENT_CENTER},
16663 * {@link #TEXT_ALIGNMENT_TEXT_START},
16664 * {@link #TEXT_ALIGNMENT_TEXT_END},
16665 * {@link #TEXT_ALIGNMENT_VIEW_START},
16666 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016667 *
16668 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016669 */
16670 @ViewDebug.ExportedProperty(category = "text", mapping = {
16671 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16672 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16673 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16674 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16675 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16676 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16677 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16678 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016679 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016680 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016681 }
16682
16683 /**
16684 * Set the text alignment.
16685 *
16686 * @param textAlignment The text alignment to set. Should be one of
16687 *
16688 * {@link #TEXT_ALIGNMENT_INHERIT},
16689 * {@link #TEXT_ALIGNMENT_GRAVITY},
16690 * {@link #TEXT_ALIGNMENT_CENTER},
16691 * {@link #TEXT_ALIGNMENT_TEXT_START},
16692 * {@link #TEXT_ALIGNMENT_TEXT_END},
16693 * {@link #TEXT_ALIGNMENT_VIEW_START},
16694 * {@link #TEXT_ALIGNMENT_VIEW_END}
16695 *
16696 * @attr ref android.R.styleable#View_textAlignment
16697 */
16698 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016699 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016700 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016701 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016702 resetResolvedTextAlignment();
16703 // Set the new text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016704 mPrivateFlags2 |= ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016705 // Notify change
16706 onRtlPropertiesChanged();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016707 // Refresh
16708 requestLayout();
16709 invalidate(true);
16710 }
16711 }
16712
16713 /**
16714 * Return the resolved text alignment.
16715 *
16716 * The resolved text alignment. This needs resolution if the value is
16717 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16718 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16719 *
16720 * @return the resolved text alignment. Returns one of:
16721 *
16722 * {@link #TEXT_ALIGNMENT_GRAVITY},
16723 * {@link #TEXT_ALIGNMENT_CENTER},
16724 * {@link #TEXT_ALIGNMENT_TEXT_START},
16725 * {@link #TEXT_ALIGNMENT_TEXT_END},
16726 * {@link #TEXT_ALIGNMENT_VIEW_START},
16727 * {@link #TEXT_ALIGNMENT_VIEW_END}
16728 */
16729 @ViewDebug.ExportedProperty(category = "text", mapping = {
16730 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16731 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16732 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16733 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16734 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16735 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16736 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16737 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016738 public int getTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016739 // If text alignment is not resolved, then resolve it
Dianne Hackborn4702a852012-08-17 15:18:29 -070016740 if ((mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) != PFLAG2_TEXT_ALIGNMENT_RESOLVED) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016741 resolveTextAlignment();
16742 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070016743 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >> PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016744 }
16745
16746 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016747 * Resolve the text alignment.
16748 *
16749 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016750 */
16751 public void resolveTextAlignment() {
16752 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016753 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016754
16755 if (hasRtlSupport()) {
16756 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016757 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016758 switch (textAlignment) {
16759 case TEXT_ALIGNMENT_INHERIT:
16760 // Check if we can resolve the text alignment
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016761 if (canResolveTextAlignment() && mParent instanceof View) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016762 View view = (View) mParent;
16763
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016764 final int parentResolvedTextAlignment = view.getTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016765 switch (parentResolvedTextAlignment) {
16766 case TEXT_ALIGNMENT_GRAVITY:
16767 case TEXT_ALIGNMENT_TEXT_START:
16768 case TEXT_ALIGNMENT_TEXT_END:
16769 case TEXT_ALIGNMENT_CENTER:
16770 case TEXT_ALIGNMENT_VIEW_START:
16771 case TEXT_ALIGNMENT_VIEW_END:
16772 // Resolved text alignment is the same as the parent resolved
16773 // text alignment
16774 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016775 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016776 break;
16777 default:
16778 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016779 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016780 }
16781 }
16782 else {
16783 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016784 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016785 }
16786 break;
16787 case TEXT_ALIGNMENT_GRAVITY:
16788 case TEXT_ALIGNMENT_TEXT_START:
16789 case TEXT_ALIGNMENT_TEXT_END:
16790 case TEXT_ALIGNMENT_CENTER:
16791 case TEXT_ALIGNMENT_VIEW_START:
16792 case TEXT_ALIGNMENT_VIEW_END:
16793 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016794 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016795 break;
16796 default:
16797 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016798 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016799 }
16800 } else {
16801 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016802 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016803 }
16804
16805 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016806 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016807 }
16808
16809 /**
16810 * Check if text alignment resolution can be done.
16811 *
16812 * @return true if text alignment resolution can be done otherwise return false.
16813 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016814 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016815 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016816 case TEXT_DIRECTION_INHERIT:
16817 return (mParent != null);
16818 default:
16819 return true;
16820 }
16821 }
16822
16823 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016824 * Reset resolved text alignment.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016825 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016826 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016827 */
16828 public void resetResolvedTextAlignment() {
16829 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016830 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016831 }
16832
Adam Powella9108a22012-07-18 11:18:09 -070016833 /**
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016834 * @hide
16835 */
16836 public boolean isTextAlignmentInherited() {
16837 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16838 }
16839
16840 /**
Adam Powella9108a22012-07-18 11:18:09 -070016841 * Generate a value suitable for use in {@link #setId(int)}.
16842 * This value will not collide with ID values generated at build time by aapt for R.id.
16843 *
16844 * @return a generated ID value
16845 */
16846 public static int generateViewId() {
16847 for (;;) {
16848 final int result = sNextGeneratedId.get();
16849 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16850 int newValue = result + 1;
16851 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16852 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16853 return result;
16854 }
16855 }
16856 }
16857
Chet Haaseb39f0512011-05-24 14:36:40 -070016858 //
16859 // Properties
16860 //
16861 /**
16862 * A Property wrapper around the <code>alpha</code> functionality handled by the
16863 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16864 */
Chet Haased47f1532011-12-16 11:18:52 -080016865 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016866 @Override
16867 public void setValue(View object, float value) {
16868 object.setAlpha(value);
16869 }
16870
16871 @Override
16872 public Float get(View object) {
16873 return object.getAlpha();
16874 }
16875 };
16876
16877 /**
16878 * A Property wrapper around the <code>translationX</code> functionality handled by the
16879 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16880 */
Chet Haased47f1532011-12-16 11:18:52 -080016881 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016882 @Override
16883 public void setValue(View object, float value) {
16884 object.setTranslationX(value);
16885 }
16886
16887 @Override
16888 public Float get(View object) {
16889 return object.getTranslationX();
16890 }
16891 };
16892
16893 /**
16894 * A Property wrapper around the <code>translationY</code> functionality handled by the
16895 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16896 */
Chet Haased47f1532011-12-16 11:18:52 -080016897 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016898 @Override
16899 public void setValue(View object, float value) {
16900 object.setTranslationY(value);
16901 }
16902
16903 @Override
16904 public Float get(View object) {
16905 return object.getTranslationY();
16906 }
16907 };
16908
16909 /**
16910 * A Property wrapper around the <code>x</code> functionality handled by the
16911 * {@link View#setX(float)} and {@link View#getX()} methods.
16912 */
Chet Haased47f1532011-12-16 11:18:52 -080016913 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016914 @Override
16915 public void setValue(View object, float value) {
16916 object.setX(value);
16917 }
16918
16919 @Override
16920 public Float get(View object) {
16921 return object.getX();
16922 }
16923 };
16924
16925 /**
16926 * A Property wrapper around the <code>y</code> functionality handled by the
16927 * {@link View#setY(float)} and {@link View#getY()} methods.
16928 */
Chet Haased47f1532011-12-16 11:18:52 -080016929 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016930 @Override
16931 public void setValue(View object, float value) {
16932 object.setY(value);
16933 }
16934
16935 @Override
16936 public Float get(View object) {
16937 return object.getY();
16938 }
16939 };
16940
16941 /**
16942 * A Property wrapper around the <code>rotation</code> functionality handled by the
16943 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16944 */
Chet Haased47f1532011-12-16 11:18:52 -080016945 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016946 @Override
16947 public void setValue(View object, float value) {
16948 object.setRotation(value);
16949 }
16950
16951 @Override
16952 public Float get(View object) {
16953 return object.getRotation();
16954 }
16955 };
16956
16957 /**
16958 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16959 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16960 */
Chet Haased47f1532011-12-16 11:18:52 -080016961 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016962 @Override
16963 public void setValue(View object, float value) {
16964 object.setRotationX(value);
16965 }
16966
16967 @Override
16968 public Float get(View object) {
16969 return object.getRotationX();
16970 }
16971 };
16972
16973 /**
16974 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16975 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16976 */
Chet Haased47f1532011-12-16 11:18:52 -080016977 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016978 @Override
16979 public void setValue(View object, float value) {
16980 object.setRotationY(value);
16981 }
16982
16983 @Override
16984 public Float get(View object) {
16985 return object.getRotationY();
16986 }
16987 };
16988
16989 /**
16990 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16991 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16992 */
Chet Haased47f1532011-12-16 11:18:52 -080016993 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016994 @Override
16995 public void setValue(View object, float value) {
16996 object.setScaleX(value);
16997 }
16998
16999 @Override
17000 public Float get(View object) {
17001 return object.getScaleX();
17002 }
17003 };
17004
17005 /**
17006 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17007 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17008 */
Chet Haased47f1532011-12-16 11:18:52 -080017009 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017010 @Override
17011 public void setValue(View object, float value) {
17012 object.setScaleY(value);
17013 }
17014
17015 @Override
17016 public Float get(View object) {
17017 return object.getScaleY();
17018 }
17019 };
17020
Jeff Brown33bbfd22011-02-24 20:55:35 -080017021 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017022 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17023 * Each MeasureSpec represents a requirement for either the width or the height.
17024 * A MeasureSpec is comprised of a size and a mode. There are three possible
17025 * modes:
17026 * <dl>
17027 * <dt>UNSPECIFIED</dt>
17028 * <dd>
17029 * The parent has not imposed any constraint on the child. It can be whatever size
17030 * it wants.
17031 * </dd>
17032 *
17033 * <dt>EXACTLY</dt>
17034 * <dd>
17035 * The parent has determined an exact size for the child. The child is going to be
17036 * given those bounds regardless of how big it wants to be.
17037 * </dd>
17038 *
17039 * <dt>AT_MOST</dt>
17040 * <dd>
17041 * The child can be as large as it wants up to the specified size.
17042 * </dd>
17043 * </dl>
17044 *
17045 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17046 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17047 */
17048 public static class MeasureSpec {
17049 private static final int MODE_SHIFT = 30;
17050 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17051
17052 /**
17053 * Measure specification mode: The parent has not imposed any constraint
17054 * on the child. It can be whatever size it wants.
17055 */
17056 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17057
17058 /**
17059 * Measure specification mode: The parent has determined an exact size
17060 * for the child. The child is going to be given those bounds regardless
17061 * of how big it wants to be.
17062 */
17063 public static final int EXACTLY = 1 << MODE_SHIFT;
17064
17065 /**
17066 * Measure specification mode: The child can be as large as it wants up
17067 * to the specified size.
17068 */
17069 public static final int AT_MOST = 2 << MODE_SHIFT;
17070
17071 /**
17072 * Creates a measure specification based on the supplied size and mode.
17073 *
17074 * The mode must always be one of the following:
17075 * <ul>
17076 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17077 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17078 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17079 * </ul>
17080 *
17081 * @param size the size of the measure specification
17082 * @param mode the mode of the measure specification
17083 * @return the measure specification based on size and mode
17084 */
17085 public static int makeMeasureSpec(int size, int mode) {
17086 return size + mode;
17087 }
17088
17089 /**
17090 * Extracts the mode from the supplied measure specification.
17091 *
17092 * @param measureSpec the measure specification to extract the mode from
17093 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17094 * {@link android.view.View.MeasureSpec#AT_MOST} or
17095 * {@link android.view.View.MeasureSpec#EXACTLY}
17096 */
17097 public static int getMode(int measureSpec) {
17098 return (measureSpec & MODE_MASK);
17099 }
17100
17101 /**
17102 * Extracts the size from the supplied measure specification.
17103 *
17104 * @param measureSpec the measure specification to extract the size from
17105 * @return the size in pixels defined in the supplied measure specification
17106 */
17107 public static int getSize(int measureSpec) {
17108 return (measureSpec & ~MODE_MASK);
17109 }
17110
17111 /**
17112 * Returns a String representation of the specified measure
17113 * specification.
17114 *
17115 * @param measureSpec the measure specification to convert to a String
17116 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17117 */
17118 public static String toString(int measureSpec) {
17119 int mode = getMode(measureSpec);
17120 int size = getSize(measureSpec);
17121
17122 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17123
17124 if (mode == UNSPECIFIED)
17125 sb.append("UNSPECIFIED ");
17126 else if (mode == EXACTLY)
17127 sb.append("EXACTLY ");
17128 else if (mode == AT_MOST)
17129 sb.append("AT_MOST ");
17130 else
17131 sb.append(mode).append(" ");
17132
17133 sb.append(size);
17134 return sb.toString();
17135 }
17136 }
17137
17138 class CheckForLongPress implements Runnable {
17139
17140 private int mOriginalWindowAttachCount;
17141
17142 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017143 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017144 && mOriginalWindowAttachCount == mWindowAttachCount) {
17145 if (performLongClick()) {
17146 mHasPerformedLongPress = true;
17147 }
17148 }
17149 }
17150
17151 public void rememberWindowAttachCount() {
17152 mOriginalWindowAttachCount = mWindowAttachCount;
17153 }
17154 }
Joe Malin32736f02011-01-19 16:14:20 -080017155
Adam Powelle14579b2009-12-16 18:39:52 -080017156 private final class CheckForTap implements Runnable {
17157 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017158 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017159 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017160 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017161 }
17162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017163
Adam Powella35d7682010-03-12 14:48:13 -080017164 private final class PerformClick implements Runnable {
17165 public void run() {
17166 performClick();
17167 }
17168 }
17169
Dianne Hackborn63042d62011-01-26 18:56:29 -080017170 /** @hide */
17171 public void hackTurnOffWindowResizeAnim(boolean off) {
17172 mAttachInfo.mTurnOffWindowResizeAnim = off;
17173 }
Joe Malin32736f02011-01-19 16:14:20 -080017174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017175 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017176 * This method returns a ViewPropertyAnimator object, which can be used to animate
17177 * specific properties on this View.
17178 *
17179 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17180 */
17181 public ViewPropertyAnimator animate() {
17182 if (mAnimator == null) {
17183 mAnimator = new ViewPropertyAnimator(this);
17184 }
17185 return mAnimator;
17186 }
17187
17188 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017189 * Interface definition for a callback to be invoked when a hardware key event is
17190 * dispatched to this view. The callback will be invoked before the key event is
17191 * given to the view. This is only useful for hardware keyboards; a software input
17192 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017193 */
17194 public interface OnKeyListener {
17195 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017196 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017197 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017198 * <p>Key presses in software keyboards will generally NOT trigger this method,
17199 * although some may elect to do so in some situations. Do not assume a
17200 * software input method has to be key-based; even if it is, it may use key presses
17201 * in a different way than you expect, so there is no way to reliably catch soft
17202 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017203 *
17204 * @param v The view the key has been dispatched to.
17205 * @param keyCode The code for the physical key that was pressed
17206 * @param event The KeyEvent object containing full information about
17207 * the event.
17208 * @return True if the listener has consumed the event, false otherwise.
17209 */
17210 boolean onKey(View v, int keyCode, KeyEvent event);
17211 }
17212
17213 /**
17214 * Interface definition for a callback to be invoked when a touch event is
17215 * dispatched to this view. The callback will be invoked before the touch
17216 * event is given to the view.
17217 */
17218 public interface OnTouchListener {
17219 /**
17220 * Called when a touch event is dispatched to a view. This allows listeners to
17221 * get a chance to respond before the target view.
17222 *
17223 * @param v The view the touch event has been dispatched to.
17224 * @param event The MotionEvent object containing full information about
17225 * the event.
17226 * @return True if the listener has consumed the event, false otherwise.
17227 */
17228 boolean onTouch(View v, MotionEvent event);
17229 }
17230
17231 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017232 * Interface definition for a callback to be invoked when a hover event is
17233 * dispatched to this view. The callback will be invoked before the hover
17234 * event is given to the view.
17235 */
17236 public interface OnHoverListener {
17237 /**
17238 * Called when a hover event is dispatched to a view. This allows listeners to
17239 * get a chance to respond before the target view.
17240 *
17241 * @param v The view the hover event has been dispatched to.
17242 * @param event The MotionEvent object containing full information about
17243 * the event.
17244 * @return True if the listener has consumed the event, false otherwise.
17245 */
17246 boolean onHover(View v, MotionEvent event);
17247 }
17248
17249 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017250 * Interface definition for a callback to be invoked when a generic motion event is
17251 * dispatched to this view. The callback will be invoked before the generic motion
17252 * event is given to the view.
17253 */
17254 public interface OnGenericMotionListener {
17255 /**
17256 * Called when a generic motion event is dispatched to a view. This allows listeners to
17257 * get a chance to respond before the target view.
17258 *
17259 * @param v The view the generic motion event has been dispatched to.
17260 * @param event The MotionEvent object containing full information about
17261 * the event.
17262 * @return True if the listener has consumed the event, false otherwise.
17263 */
17264 boolean onGenericMotion(View v, MotionEvent event);
17265 }
17266
17267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017268 * Interface definition for a callback to be invoked when a view has been clicked and held.
17269 */
17270 public interface OnLongClickListener {
17271 /**
17272 * Called when a view has been clicked and held.
17273 *
17274 * @param v The view that was clicked and held.
17275 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017276 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017277 */
17278 boolean onLongClick(View v);
17279 }
17280
17281 /**
Chris Tate32affef2010-10-18 15:29:21 -070017282 * Interface definition for a callback to be invoked when a drag is being dispatched
17283 * to this view. The callback will be invoked before the hosting view's own
17284 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17285 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017286 *
17287 * <div class="special reference">
17288 * <h3>Developer Guides</h3>
17289 * <p>For a guide to implementing drag and drop features, read the
17290 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17291 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017292 */
17293 public interface OnDragListener {
17294 /**
17295 * Called when a drag event is dispatched to a view. This allows listeners
17296 * to get a chance to override base View behavior.
17297 *
Joe Malin32736f02011-01-19 16:14:20 -080017298 * @param v The View that received the drag event.
17299 * @param event The {@link android.view.DragEvent} object for the drag event.
17300 * @return {@code true} if the drag event was handled successfully, or {@code false}
17301 * if the drag event was not handled. Note that {@code false} will trigger the View
17302 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017303 */
17304 boolean onDrag(View v, DragEvent event);
17305 }
17306
17307 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017308 * Interface definition for a callback to be invoked when the focus state of
17309 * a view changed.
17310 */
17311 public interface OnFocusChangeListener {
17312 /**
17313 * Called when the focus state of a view has changed.
17314 *
17315 * @param v The view whose state has changed.
17316 * @param hasFocus The new focus state of v.
17317 */
17318 void onFocusChange(View v, boolean hasFocus);
17319 }
17320
17321 /**
17322 * Interface definition for a callback to be invoked when a view is clicked.
17323 */
17324 public interface OnClickListener {
17325 /**
17326 * Called when a view has been clicked.
17327 *
17328 * @param v The view that was clicked.
17329 */
17330 void onClick(View v);
17331 }
17332
17333 /**
17334 * Interface definition for a callback to be invoked when the context menu
17335 * for this view is being built.
17336 */
17337 public interface OnCreateContextMenuListener {
17338 /**
17339 * Called when the context menu for this view is being built. It is not
17340 * safe to hold onto the menu after this method returns.
17341 *
17342 * @param menu The context menu that is being built
17343 * @param v The view for which the context menu is being built
17344 * @param menuInfo Extra information about the item for which the
17345 * context menu should be shown. This information will vary
17346 * depending on the class of v.
17347 */
17348 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17349 }
17350
Joe Onorato664644d2011-01-23 17:53:23 -080017351 /**
17352 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017353 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017354 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017355 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017356 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017357 */
17358 public interface OnSystemUiVisibilityChangeListener {
17359 /**
17360 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017361 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017362 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017363 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17364 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17365 * This tells you the <strong>global</strong> state of these UI visibility
17366 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017367 */
17368 public void onSystemUiVisibilityChange(int visibility);
17369 }
17370
Adam Powell4afd62b2011-02-18 15:02:18 -080017371 /**
17372 * Interface definition for a callback to be invoked when this view is attached
17373 * or detached from its window.
17374 */
17375 public interface OnAttachStateChangeListener {
17376 /**
17377 * Called when the view is attached to a window.
17378 * @param v The view that was attached
17379 */
17380 public void onViewAttachedToWindow(View v);
17381 /**
17382 * Called when the view is detached from a window.
17383 * @param v The view that was detached
17384 */
17385 public void onViewDetachedFromWindow(View v);
17386 }
17387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017388 private final class UnsetPressedState implements Runnable {
17389 public void run() {
17390 setPressed(false);
17391 }
17392 }
17393
17394 /**
17395 * Base class for derived classes that want to save and restore their own
17396 * state in {@link android.view.View#onSaveInstanceState()}.
17397 */
17398 public static class BaseSavedState extends AbsSavedState {
17399 /**
17400 * Constructor used when reading from a parcel. Reads the state of the superclass.
17401 *
17402 * @param source
17403 */
17404 public BaseSavedState(Parcel source) {
17405 super(source);
17406 }
17407
17408 /**
17409 * Constructor called by derived classes when creating their SavedState objects
17410 *
17411 * @param superState The state of the superclass of this view
17412 */
17413 public BaseSavedState(Parcelable superState) {
17414 super(superState);
17415 }
17416
17417 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17418 new Parcelable.Creator<BaseSavedState>() {
17419 public BaseSavedState createFromParcel(Parcel in) {
17420 return new BaseSavedState(in);
17421 }
17422
17423 public BaseSavedState[] newArray(int size) {
17424 return new BaseSavedState[size];
17425 }
17426 };
17427 }
17428
17429 /**
17430 * A set of information given to a view when it is attached to its parent
17431 * window.
17432 */
17433 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017434 interface Callbacks {
17435 void playSoundEffect(int effectId);
17436 boolean performHapticFeedback(int effectId, boolean always);
17437 }
17438
17439 /**
17440 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17441 * to a Handler. This class contains the target (View) to invalidate and
17442 * the coordinates of the dirty rectangle.
17443 *
17444 * For performance purposes, this class also implements a pool of up to
17445 * POOL_LIMIT objects that get reused. This reduces memory allocations
17446 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017447 */
Romain Guyd928d682009-03-31 17:52:16 -070017448 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017449 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017450 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17451 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017452 public InvalidateInfo newInstance() {
17453 return new InvalidateInfo();
17454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017455
Romain Guyd928d682009-03-31 17:52:16 -070017456 public void onAcquired(InvalidateInfo element) {
17457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017458
Romain Guyd928d682009-03-31 17:52:16 -070017459 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017460 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017461 }
17462 }, POOL_LIMIT)
17463 );
17464
17465 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017466 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017467
17468 View target;
17469
17470 int left;
17471 int top;
17472 int right;
17473 int bottom;
17474
Romain Guyd928d682009-03-31 17:52:16 -070017475 public void setNextPoolable(InvalidateInfo element) {
17476 mNext = element;
17477 }
17478
17479 public InvalidateInfo getNextPoolable() {
17480 return mNext;
17481 }
17482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017483 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017484 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017485 }
17486
17487 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017488 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017489 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017490
17491 public boolean isPooled() {
17492 return mIsPooled;
17493 }
17494
17495 public void setPooled(boolean isPooled) {
17496 mIsPooled = isPooled;
17497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017498 }
17499
17500 final IWindowSession mSession;
17501
17502 final IWindow mWindow;
17503
17504 final IBinder mWindowToken;
17505
Jeff Brown98365d72012-08-19 20:30:52 -070017506 final Display mDisplay;
17507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017508 final Callbacks mRootCallbacks;
17509
Romain Guy59a12ca2011-06-09 17:48:21 -070017510 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017512 /**
17513 * The top view of the hierarchy.
17514 */
17515 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017517 IBinder mPanelParentWindowToken;
17518 Surface mSurface;
17519
Romain Guyb051e892010-09-28 19:09:36 -070017520 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017521 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017522 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017523
Romain Guy7e4e5612012-03-05 14:37:29 -080017524 boolean mScreenOn;
17525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017526 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017527 * Scale factor used by the compatibility mode
17528 */
17529 float mApplicationScale;
17530
17531 /**
17532 * Indicates whether the application is in compatibility mode
17533 */
17534 boolean mScalingRequired;
17535
17536 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017537 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017538 */
17539 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017540
Dianne Hackborn63042d62011-01-26 18:56:29 -080017541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017542 * Left position of this view's window
17543 */
17544 int mWindowLeft;
17545
17546 /**
17547 * Top position of this view's window
17548 */
17549 int mWindowTop;
17550
17551 /**
Adam Powell26153a32010-11-08 15:22:27 -080017552 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017553 */
Adam Powell26153a32010-11-08 15:22:27 -080017554 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017555
17556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017557 * For windows that are full-screen but using insets to layout inside
17558 * of the screen decorations, these are the current insets for the
17559 * content of the window.
17560 */
17561 final Rect mContentInsets = new Rect();
17562
17563 /**
17564 * For windows that are full-screen but using insets to layout inside
17565 * of the screen decorations, these are the current insets for the
17566 * actual visible parts of the window.
17567 */
17568 final Rect mVisibleInsets = new Rect();
17569
17570 /**
17571 * The internal insets given by this window. This value is
17572 * supplied by the client (through
17573 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17574 * be given to the window manager when changed to be used in laying
17575 * out windows behind it.
17576 */
17577 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17578 = new ViewTreeObserver.InternalInsetsInfo();
17579
17580 /**
17581 * All views in the window's hierarchy that serve as scroll containers,
17582 * used to determine if the window can be resized or must be panned
17583 * to adjust for a soft input area.
17584 */
17585 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17586
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017587 final KeyEvent.DispatcherState mKeyDispatchState
17588 = new KeyEvent.DispatcherState();
17589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017590 /**
17591 * Indicates whether the view's window currently has the focus.
17592 */
17593 boolean mHasWindowFocus;
17594
17595 /**
17596 * The current visibility of the window.
17597 */
17598 int mWindowVisibility;
17599
17600 /**
17601 * Indicates the time at which drawing started to occur.
17602 */
17603 long mDrawingTime;
17604
17605 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017606 * Indicates whether or not ignoring the DIRTY_MASK flags.
17607 */
17608 boolean mIgnoreDirtyState;
17609
17610 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017611 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17612 * to avoid clearing that flag prematurely.
17613 */
17614 boolean mSetIgnoreDirtyState = false;
17615
17616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017617 * Indicates whether the view's window is currently in touch mode.
17618 */
17619 boolean mInTouchMode;
17620
17621 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017622 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017623 * the next time it performs a traversal
17624 */
17625 boolean mRecomputeGlobalAttributes;
17626
17627 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017628 * Always report new attributes at next traversal.
17629 */
17630 boolean mForceReportNewAttributes;
17631
17632 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017633 * Set during a traveral if any views want to keep the screen on.
17634 */
17635 boolean mKeepScreenOn;
17636
17637 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017638 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17639 */
17640 int mSystemUiVisibility;
17641
17642 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017643 * Hack to force certain system UI visibility flags to be cleared.
17644 */
17645 int mDisabledSystemUiVisibility;
17646
17647 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017648 * Last global system UI visibility reported by the window manager.
17649 */
17650 int mGlobalSystemUiVisibility;
17651
17652 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017653 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17654 * attached.
17655 */
17656 boolean mHasSystemUiListeners;
17657
17658 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017659 * Set if the visibility of any views has changed.
17660 */
17661 boolean mViewVisibilityChanged;
17662
17663 /**
17664 * Set to true if a view has been scrolled.
17665 */
17666 boolean mViewScrollChanged;
17667
17668 /**
17669 * Global to the view hierarchy used as a temporary for dealing with
17670 * x/y points in the transparent region computations.
17671 */
17672 final int[] mTransparentLocation = new int[2];
17673
17674 /**
17675 * Global to the view hierarchy used as a temporary for dealing with
17676 * x/y points in the ViewGroup.invalidateChild implementation.
17677 */
17678 final int[] mInvalidateChildLocation = new int[2];
17679
Chet Haasec3aa3612010-06-17 08:50:37 -070017680
17681 /**
17682 * Global to the view hierarchy used as a temporary for dealing with
17683 * x/y location when view is transformed.
17684 */
17685 final float[] mTmpTransformLocation = new float[2];
17686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017687 /**
17688 * The view tree observer used to dispatch global events like
17689 * layout, pre-draw, touch mode change, etc.
17690 */
17691 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17692
17693 /**
17694 * A Canvas used by the view hierarchy to perform bitmap caching.
17695 */
17696 Canvas mCanvas;
17697
17698 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017699 * The view root impl.
17700 */
17701 final ViewRootImpl mViewRootImpl;
17702
17703 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017704 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017705 * handler can be used to pump events in the UI events queue.
17706 */
17707 final Handler mHandler;
17708
17709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017710 * Temporary for use in computing invalidate rectangles while
17711 * calling up the hierarchy.
17712 */
17713 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017714
17715 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017716 * Temporary for use in computing hit areas with transformed views
17717 */
17718 final RectF mTmpTransformRect = new RectF();
17719
17720 /**
Chet Haase599913d2012-07-23 16:22:05 -070017721 * Temporary for use in transforming invalidation rect
17722 */
17723 final Matrix mTmpMatrix = new Matrix();
17724
17725 /**
17726 * Temporary for use in transforming invalidation rect
17727 */
17728 final Transformation mTmpTransformation = new Transformation();
17729
17730 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017731 * Temporary list for use in collecting focusable descendents of a view.
17732 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017733 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017735 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017736 * The id of the window for accessibility purposes.
17737 */
17738 int mAccessibilityWindowId = View.NO_ID;
17739
17740 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017741 * Whether to ingore not exposed for accessibility Views when
17742 * reporting the view tree to accessibility services.
17743 */
17744 boolean mIncludeNotImportantViews;
17745
17746 /**
17747 * The drawable for highlighting accessibility focus.
17748 */
17749 Drawable mAccessibilityFocusDrawable;
17750
17751 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017752 * Show where the margins, bounds and layout bounds are for each view.
17753 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017754 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017755
17756 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017757 * Point used to compute visible regions.
17758 */
17759 final Point mPoint = new Point();
17760
17761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017762 * Creates a new set of attachment information with the specified
17763 * events handler and thread.
17764 *
17765 * @param handler the events handler the view must use
17766 */
Jeff Brown98365d72012-08-19 20:30:52 -070017767 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017768 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017769 mSession = session;
17770 mWindow = window;
17771 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017772 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017773 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017774 mHandler = handler;
17775 mRootCallbacks = effectPlayer;
17776 }
17777 }
17778
17779 /**
17780 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17781 * is supported. This avoids keeping too many unused fields in most
17782 * instances of View.</p>
17783 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017784 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017785
Mike Cleronf116bf82009-09-27 19:14:12 -070017786 /**
17787 * Scrollbars are not visible
17788 */
17789 public static final int OFF = 0;
17790
17791 /**
17792 * Scrollbars are visible
17793 */
17794 public static final int ON = 1;
17795
17796 /**
17797 * Scrollbars are fading away
17798 */
17799 public static final int FADING = 2;
17800
17801 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017803 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017804 public int scrollBarDefaultDelayBeforeFade;
17805 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017806
17807 public int scrollBarSize;
17808 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017809 public float[] interpolatorValues;
17810 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017811
17812 public final Paint paint;
17813 public final Matrix matrix;
17814 public Shader shader;
17815
Mike Cleronf116bf82009-09-27 19:14:12 -070017816 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17817
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017818 private static final float[] OPAQUE = { 255 };
17819 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017820
Mike Cleronf116bf82009-09-27 19:14:12 -070017821 /**
17822 * When fading should start. This time moves into the future every time
17823 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17824 */
17825 public long fadeStartTime;
17826
17827
17828 /**
17829 * The current state of the scrollbars: ON, OFF, or FADING
17830 */
17831 public int state = OFF;
17832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017833 private int mLastColor;
17834
Mike Cleronf116bf82009-09-27 19:14:12 -070017835 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017836 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17837 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017838 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17839 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017840
17841 paint = new Paint();
17842 matrix = new Matrix();
17843 // use use a height of 1, and then wack the matrix each time we
17844 // actually use it.
17845 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017846 paint.setShader(shader);
17847 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017848
Mike Cleronf116bf82009-09-27 19:14:12 -070017849 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017850 }
Romain Guy8506ab42009-06-11 17:35:47 -070017851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017852 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017853 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017854 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017855
Romain Guyd679b572012-08-29 21:49:00 -070017856 if (color != 0) {
17857 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17858 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17859 paint.setShader(shader);
17860 // Restore the default transfer mode (src_over)
17861 paint.setXfermode(null);
17862 } else {
17863 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17864 paint.setShader(shader);
17865 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017867 }
17868 }
Joe Malin32736f02011-01-19 16:14:20 -080017869
Mike Cleronf116bf82009-09-27 19:14:12 -070017870 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017871 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017872 if (now >= fadeStartTime) {
17873
17874 // the animation fades the scrollbars out by changing
17875 // the opacity (alpha) from fully opaque to fully
17876 // transparent
17877 int nextFrame = (int) now;
17878 int framesCount = 0;
17879
17880 Interpolator interpolator = scrollBarInterpolator;
17881
17882 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017883 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017884
17885 // End transparent
17886 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017887 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017888
17889 state = FADING;
17890
17891 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017892 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017893 }
17894 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017895 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017896
Svetoslav Ganova0156172011-06-26 17:55:44 -070017897 /**
17898 * Resuable callback for sending
17899 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17900 */
17901 private class SendViewScrolledAccessibilityEvent implements Runnable {
17902 public volatile boolean mIsPending;
17903
17904 public void run() {
17905 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17906 mIsPending = false;
17907 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017908 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017909
17910 /**
17911 * <p>
17912 * This class represents a delegate that can be registered in a {@link View}
17913 * to enhance accessibility support via composition rather via inheritance.
17914 * It is specifically targeted to widget developers that extend basic View
17915 * classes i.e. classes in package android.view, that would like their
17916 * applications to be backwards compatible.
17917 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017918 * <div class="special reference">
17919 * <h3>Developer Guides</h3>
17920 * <p>For more information about making applications accessible, read the
17921 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17922 * developer guide.</p>
17923 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017924 * <p>
17925 * A scenario in which a developer would like to use an accessibility delegate
17926 * is overriding a method introduced in a later API version then the minimal API
17927 * version supported by the application. For example, the method
17928 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17929 * in API version 4 when the accessibility APIs were first introduced. If a
17930 * developer would like his application to run on API version 4 devices (assuming
17931 * all other APIs used by the application are version 4 or lower) and take advantage
17932 * of this method, instead of overriding the method which would break the application's
17933 * backwards compatibility, he can override the corresponding method in this
17934 * delegate and register the delegate in the target View if the API version of
17935 * the system is high enough i.e. the API version is same or higher to the API
17936 * version that introduced
17937 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17938 * </p>
17939 * <p>
17940 * Here is an example implementation:
17941 * </p>
17942 * <code><pre><p>
17943 * if (Build.VERSION.SDK_INT >= 14) {
17944 * // If the API version is equal of higher than the version in
17945 * // which onInitializeAccessibilityNodeInfo was introduced we
17946 * // register a delegate with a customized implementation.
17947 * View view = findViewById(R.id.view_id);
17948 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17949 * public void onInitializeAccessibilityNodeInfo(View host,
17950 * AccessibilityNodeInfo info) {
17951 * // Let the default implementation populate the info.
17952 * super.onInitializeAccessibilityNodeInfo(host, info);
17953 * // Set some other information.
17954 * info.setEnabled(host.isEnabled());
17955 * }
17956 * });
17957 * }
17958 * </code></pre></p>
17959 * <p>
17960 * This delegate contains methods that correspond to the accessibility methods
17961 * in View. If a delegate has been specified the implementation in View hands
17962 * off handling to the corresponding method in this delegate. The default
17963 * implementation the delegate methods behaves exactly as the corresponding
17964 * method in View for the case of no accessibility delegate been set. Hence,
17965 * to customize the behavior of a View method, clients can override only the
17966 * corresponding delegate method without altering the behavior of the rest
17967 * accessibility related methods of the host view.
17968 * </p>
17969 */
17970 public static class AccessibilityDelegate {
17971
17972 /**
17973 * Sends an accessibility event of the given type. If accessibility is not
17974 * enabled this method has no effect.
17975 * <p>
17976 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17977 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17978 * been set.
17979 * </p>
17980 *
17981 * @param host The View hosting the delegate.
17982 * @param eventType The type of the event to send.
17983 *
17984 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17985 */
17986 public void sendAccessibilityEvent(View host, int eventType) {
17987 host.sendAccessibilityEventInternal(eventType);
17988 }
17989
17990 /**
alanv8eeefef2012-05-07 16:57:53 -070017991 * Performs the specified accessibility action on the view. For
17992 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
17993 * <p>
17994 * The default implementation behaves as
17995 * {@link View#performAccessibilityAction(int, Bundle)
17996 * View#performAccessibilityAction(int, Bundle)} for the case of
17997 * no accessibility delegate been set.
17998 * </p>
17999 *
18000 * @param action The action to perform.
18001 * @return Whether the action was performed.
18002 *
18003 * @see View#performAccessibilityAction(int, Bundle)
18004 * View#performAccessibilityAction(int, Bundle)
18005 */
18006 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18007 return host.performAccessibilityActionInternal(action, args);
18008 }
18009
18010 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018011 * Sends an accessibility event. This method behaves exactly as
18012 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18013 * empty {@link AccessibilityEvent} and does not perform a check whether
18014 * accessibility is enabled.
18015 * <p>
18016 * The default implementation behaves as
18017 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18018 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18019 * the case of no accessibility delegate been set.
18020 * </p>
18021 *
18022 * @param host The View hosting the delegate.
18023 * @param event The event to send.
18024 *
18025 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18026 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18027 */
18028 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18029 host.sendAccessibilityEventUncheckedInternal(event);
18030 }
18031
18032 /**
18033 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18034 * to its children for adding their text content to the event.
18035 * <p>
18036 * The default implementation behaves as
18037 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18038 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18039 * the case of no accessibility delegate been set.
18040 * </p>
18041 *
18042 * @param host The View hosting the delegate.
18043 * @param event The event.
18044 * @return True if the event population was completed.
18045 *
18046 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18047 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18048 */
18049 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18050 return host.dispatchPopulateAccessibilityEventInternal(event);
18051 }
18052
18053 /**
18054 * Gives a chance to the host View to populate the accessibility event with its
18055 * text content.
18056 * <p>
18057 * The default implementation behaves as
18058 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18059 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18060 * the case of no accessibility delegate been set.
18061 * </p>
18062 *
18063 * @param host The View hosting the delegate.
18064 * @param event The accessibility event which to populate.
18065 *
18066 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18067 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18068 */
18069 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18070 host.onPopulateAccessibilityEventInternal(event);
18071 }
18072
18073 /**
18074 * Initializes an {@link AccessibilityEvent} with information about the
18075 * the host View which is the event source.
18076 * <p>
18077 * The default implementation behaves as
18078 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18079 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18080 * the case of no accessibility delegate been set.
18081 * </p>
18082 *
18083 * @param host The View hosting the delegate.
18084 * @param event The event to initialize.
18085 *
18086 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18087 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18088 */
18089 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18090 host.onInitializeAccessibilityEventInternal(event);
18091 }
18092
18093 /**
18094 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18095 * <p>
18096 * The default implementation behaves as
18097 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18098 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18099 * the case of no accessibility delegate been set.
18100 * </p>
18101 *
18102 * @param host The View hosting the delegate.
18103 * @param info The instance to initialize.
18104 *
18105 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18106 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18107 */
18108 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18109 host.onInitializeAccessibilityNodeInfoInternal(info);
18110 }
18111
18112 /**
18113 * Called when a child of the host View has requested sending an
18114 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18115 * to augment the event.
18116 * <p>
18117 * The default implementation behaves as
18118 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18119 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18120 * the case of no accessibility delegate been set.
18121 * </p>
18122 *
18123 * @param host The View hosting the delegate.
18124 * @param child The child which requests sending the event.
18125 * @param event The event to be sent.
18126 * @return True if the event should be sent
18127 *
18128 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18129 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18130 */
18131 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18132 AccessibilityEvent event) {
18133 return host.onRequestSendAccessibilityEventInternal(child, event);
18134 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018135
18136 /**
18137 * Gets the provider for managing a virtual view hierarchy rooted at this View
18138 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18139 * that explore the window content.
18140 * <p>
18141 * The default implementation behaves as
18142 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18143 * the case of no accessibility delegate been set.
18144 * </p>
18145 *
18146 * @return The provider.
18147 *
18148 * @see AccessibilityNodeProvider
18149 */
18150 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18151 return null;
18152 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018153 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018154
18155 private class MatchIdPredicate implements Predicate<View> {
18156 public int mId;
18157
18158 @Override
18159 public boolean apply(View view) {
18160 return (view.mID == mId);
18161 }
18162 }
18163
18164 private class MatchLabelForPredicate implements Predicate<View> {
18165 private int mLabeledId;
18166
18167 @Override
18168 public boolean apply(View view) {
18169 return (view.mLabelForId == mLabeledId);
18170 }
18171 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018172
18173 /**
18174 * Dump all private flags in readable format, useful for documentation and
18175 * sanity checking.
18176 */
18177 private static void dumpFlags() {
18178 final HashMap<String, String> found = Maps.newHashMap();
18179 try {
18180 for (Field field : View.class.getDeclaredFields()) {
18181 final int modifiers = field.getModifiers();
18182 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18183 if (field.getType().equals(int.class)) {
18184 final int value = field.getInt(null);
18185 dumpFlag(found, field.getName(), value);
18186 } else if (field.getType().equals(int[].class)) {
18187 final int[] values = (int[]) field.get(null);
18188 for (int i = 0; i < values.length; i++) {
18189 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18190 }
18191 }
18192 }
18193 }
18194 } catch (IllegalAccessException e) {
18195 throw new RuntimeException(e);
18196 }
18197
18198 final ArrayList<String> keys = Lists.newArrayList();
18199 keys.addAll(found.keySet());
18200 Collections.sort(keys);
18201 for (String key : keys) {
18202 Log.d(VIEW_LOG_TAG, found.get(key));
18203 }
18204 }
18205
18206 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18207 // Sort flags by prefix, then by bits, always keeping unique keys
18208 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18209 final int prefix = name.indexOf('_');
18210 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18211 final String output = bits + " " + name;
18212 found.put(key, output);
18213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018214}