blob: 111f95903ba318ab97f03611b0f810ad742aefde [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
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002144 /**
2145 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2146 */
2147 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2148
2149 /**
2150 * Group of bits indicating that RTL properties resolution is done.
2151 */
2152 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002153 PFLAG2_TEXT_DIRECTION_RESOLVED |
2154 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2155 PFLAG2_PADDING_RESOLVED |
2156 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002157
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002158 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002159
Christopher Tate3d4bf172011-03-28 16:16:46 -07002160 /* End of masks for mPrivateFlags2 */
2161
Chet Haase21433372012-06-05 07:54:09 -07002162 /* Masks for mPrivateFlags3 */
2163
2164 /**
2165 * Flag indicating that view has a transform animation set on it. This is used to track whether
2166 * an animation is cleared between successive frames, in order to tell the associated
2167 * DisplayList to clear its animation matrix.
2168 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002169 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002170
2171 /**
2172 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2173 * animation is cleared between successive frames, in order to tell the associated
2174 * DisplayList to restore its alpha value.
2175 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002176 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002177
2178
2179 /* End of masks for mPrivateFlags3 */
2180
Dianne Hackborn4702a852012-08-17 15:18:29 -07002181 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002182
Chet Haasedaf98e92011-01-10 14:10:36 -08002183 /**
Adam Powell637d3372010-08-25 14:37:03 -07002184 * Always allow a user to over-scroll this view, provided it is a
2185 * view that can scroll.
2186 *
2187 * @see #getOverScrollMode()
2188 * @see #setOverScrollMode(int)
2189 */
2190 public static final int OVER_SCROLL_ALWAYS = 0;
2191
2192 /**
2193 * Allow a user to over-scroll this view only if the content is large
2194 * enough to meaningfully scroll, provided it is a view that can scroll.
2195 *
2196 * @see #getOverScrollMode()
2197 * @see #setOverScrollMode(int)
2198 */
2199 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2200
2201 /**
2202 * Never allow a user to over-scroll this view.
2203 *
2204 * @see #getOverScrollMode()
2205 * @see #setOverScrollMode(int)
2206 */
2207 public static final int OVER_SCROLL_NEVER = 2;
2208
2209 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002210 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2211 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002212 *
Joe Malin32736f02011-01-19 16:14:20 -08002213 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002214 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002215 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002216
2217 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002218 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2219 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002220 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002221 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002222 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002223 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002224 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002225 *
Joe Malin32736f02011-01-19 16:14:20 -08002226 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002227 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002228 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2229
2230 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002231 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2232 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002233 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002234 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002235 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2236 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2237 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002238 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002239 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2240 * window flags) for displaying content using every last pixel on the display.
2241 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002242 * <p>There is a limitation: because navigation controls are so important, the least user
2243 * interaction will cause them to reappear immediately. When this happens, both
2244 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2245 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002246 *
2247 * @see #setSystemUiVisibility(int)
2248 */
2249 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2250
2251 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002252 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2253 * into the normal fullscreen mode so that its content can take over the screen
2254 * while still allowing the user to interact with the application.
2255 *
2256 * <p>This has the same visual effect as
2257 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2258 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2259 * meaning that non-critical screen decorations (such as the status bar) will be
2260 * hidden while the user is in the View's window, focusing the experience on
2261 * that content. Unlike the window flag, if you are using ActionBar in
2262 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2263 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2264 * hide the action bar.
2265 *
2266 * <p>This approach to going fullscreen is best used over the window flag when
2267 * it is a transient state -- that is, the application does this at certain
2268 * points in its user interaction where it wants to allow the user to focus
2269 * on content, but not as a continuous state. For situations where the application
2270 * would like to simply stay full screen the entire time (such as a game that
2271 * wants to take over the screen), the
2272 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2273 * is usually a better approach. The state set here will be removed by the system
2274 * in various situations (such as the user moving to another application) like
2275 * the other system UI states.
2276 *
2277 * <p>When using this flag, the application should provide some easy facility
2278 * for the user to go out of it. A common example would be in an e-book
2279 * reader, where tapping on the screen brings back whatever screen and UI
2280 * decorations that had been hidden while the user was immersed in reading
2281 * the book.
2282 *
2283 * @see #setSystemUiVisibility(int)
2284 */
2285 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2286
2287 /**
2288 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2289 * flags, we would like a stable view of the content insets given to
2290 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2291 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002292 * as a continuous state. In the stock Android UI this is the space for
2293 * the system bar, nav bar, and status bar, but not more transient elements
2294 * such as an input method.
2295 *
2296 * The stable layout your UI sees is based on the system UI modes you can
2297 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2298 * then you will get a stable layout for changes of the
2299 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2300 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2301 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2302 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2303 * with a stable layout. (Note that you should avoid using
2304 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2305 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002306 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002307 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2308 * then a hidden status bar will be considered a "stable" state for purposes
2309 * here. This allows your UI to continually hide the status bar, while still
2310 * using the system UI flags to hide the action bar while still retaining
2311 * a stable layout. Note that changing the window fullscreen flag will never
2312 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002313 *
2314 * <p>If you are using ActionBar in
2315 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2316 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2317 * insets it adds to those given to the application.
2318 */
2319 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2320
2321 /**
2322 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2323 * to be layed out as if it has requested
2324 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2325 * allows it to avoid artifacts when switching in and out of that mode, at
2326 * the expense that some of its user interface may be covered by screen
2327 * decorations when they are shown. You can perform layout of your inner
2328 * UI elements to account for the navagation system UI through the
2329 * {@link #fitSystemWindows(Rect)} method.
2330 */
2331 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2332
2333 /**
2334 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2335 * to be layed out as if it has requested
2336 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2337 * allows it to avoid artifacts when switching in and out of that mode, at
2338 * the expense that some of its user interface may be covered by screen
2339 * decorations when they are shown. You can perform layout of your inner
2340 * UI elements to account for non-fullscreen system UI through the
2341 * {@link #fitSystemWindows(Rect)} method.
2342 */
2343 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2344
2345 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002346 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2347 */
2348 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2349
2350 /**
2351 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2352 */
2353 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002354
2355 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356 * @hide
2357 *
2358 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2359 * out of the public fields to keep the undefined bits out of the developer's way.
2360 *
2361 * Flag to make the status bar not expandable. Unless you also
2362 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2363 */
2364 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2365
2366 /**
2367 * @hide
2368 *
2369 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2370 * out of the public fields to keep the undefined bits out of the developer's way.
2371 *
2372 * Flag to hide notification icons and scrolling ticker text.
2373 */
2374 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2375
2376 /**
2377 * @hide
2378 *
2379 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2380 * out of the public fields to keep the undefined bits out of the developer's way.
2381 *
2382 * Flag to disable incoming notification alerts. This will not block
2383 * icons, but it will block sound, vibrating and other visual or aural notifications.
2384 */
2385 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2386
2387 /**
2388 * @hide
2389 *
2390 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2391 * out of the public fields to keep the undefined bits out of the developer's way.
2392 *
2393 * Flag to hide only the scrolling ticker. Note that
2394 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2395 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2396 */
2397 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2398
2399 /**
2400 * @hide
2401 *
2402 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2403 * out of the public fields to keep the undefined bits out of the developer's way.
2404 *
2405 * Flag to hide the center system info area.
2406 */
2407 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2408
2409 /**
2410 * @hide
2411 *
2412 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2413 * out of the public fields to keep the undefined bits out of the developer's way.
2414 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002415 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002416 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2417 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002418 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002419
2420 /**
2421 * @hide
2422 *
2423 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2424 * out of the public fields to keep the undefined bits out of the developer's way.
2425 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002426 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002427 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2428 */
2429 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2430
2431 /**
2432 * @hide
2433 *
2434 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2435 * out of the public fields to keep the undefined bits out of the developer's way.
2436 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002437 * Flag to hide only the clock. You might use this if your activity has
2438 * its own clock making the status bar's clock redundant.
2439 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002440 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2441
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002442 /**
2443 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002444 *
2445 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2446 * out of the public fields to keep the undefined bits out of the developer's way.
2447 *
2448 * Flag to hide only the recent apps button. Don't use this
2449 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2450 */
2451 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2452
2453 /**
2454 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002455 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002456 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002457
2458 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002459 * These are the system UI flags that can be cleared by events outside
2460 * of an application. Currently this is just the ability to tap on the
2461 * screen while hiding the navigation bar to have it return.
2462 * @hide
2463 */
2464 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002465 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2466 | SYSTEM_UI_FLAG_FULLSCREEN;
2467
2468 /**
2469 * Flags that can impact the layout in relation to system UI.
2470 */
2471 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2472 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2473 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002474
2475 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002476 * Find views that render the specified text.
2477 *
2478 * @see #findViewsWithText(ArrayList, CharSequence, int)
2479 */
2480 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2481
2482 /**
2483 * Find find views that contain the specified content description.
2484 *
2485 * @see #findViewsWithText(ArrayList, CharSequence, int)
2486 */
2487 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2488
2489 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002490 * Find views that contain {@link AccessibilityNodeProvider}. Such
2491 * a View is a root of virtual view hierarchy and may contain the searched
2492 * text. If this flag is set Views with providers are automatically
2493 * added and it is a responsibility of the client to call the APIs of
2494 * the provider to determine whether the virtual tree rooted at this View
2495 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2496 * represeting the virtual views with this text.
2497 *
2498 * @see #findViewsWithText(ArrayList, CharSequence, int)
2499 *
2500 * @hide
2501 */
2502 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2503
2504 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002505 * The undefined cursor position.
2506 */
2507 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2508
2509 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002510 * Indicates that the screen has changed state and is now off.
2511 *
2512 * @see #onScreenStateChanged(int)
2513 */
2514 public static final int SCREEN_STATE_OFF = 0x0;
2515
2516 /**
2517 * Indicates that the screen has changed state and is now on.
2518 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002519 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002520 */
2521 public static final int SCREEN_STATE_ON = 0x1;
2522
2523 /**
Adam Powell637d3372010-08-25 14:37:03 -07002524 * Controls the over-scroll mode for this view.
2525 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2526 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2527 * and {@link #OVER_SCROLL_NEVER}.
2528 */
2529 private int mOverScrollMode;
2530
2531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 * The parent this view is attached to.
2533 * {@hide}
2534 *
2535 * @see #getParent()
2536 */
2537 protected ViewParent mParent;
2538
2539 /**
2540 * {@hide}
2541 */
2542 AttachInfo mAttachInfo;
2543
2544 /**
2545 * {@hide}
2546 */
Romain Guy809a7f62009-05-14 15:44:42 -07002547 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002548 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002549 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002550 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002551 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002552 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002553 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002554 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2555 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2556 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2557 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002558 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002560 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002561 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562
2563 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002564 * This view's request for the visibility of the status bar.
2565 * @hide
2566 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002567 @ViewDebug.ExportedProperty(flagMapping = {
2568 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2569 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2570 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2571 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2572 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2573 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2574 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2575 equals = SYSTEM_UI_FLAG_VISIBLE,
2576 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2577 })
Joe Onorato664644d2011-01-23 17:53:23 -08002578 int mSystemUiVisibility;
2579
2580 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002581 * Reference count for transient state.
2582 * @see #setHasTransientState(boolean)
2583 */
2584 int mTransientStateCount = 0;
2585
2586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 * Count of how many windows this view has been attached to.
2588 */
2589 int mWindowAttachCount;
2590
2591 /**
2592 * The layout parameters associated with this view and used by the parent
2593 * {@link android.view.ViewGroup} to determine how this view should be
2594 * laid out.
2595 * {@hide}
2596 */
2597 protected ViewGroup.LayoutParams mLayoutParams;
2598
2599 /**
2600 * The view flags hold various views states.
2601 * {@hide}
2602 */
2603 @ViewDebug.ExportedProperty
2604 int mViewFlags;
2605
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002606 static class TransformationInfo {
2607 /**
2608 * The transform matrix for the View. This transform is calculated internally
2609 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2610 * is used by default. Do *not* use this variable directly; instead call
2611 * getMatrix(), which will automatically recalculate the matrix if necessary
2612 * to get the correct matrix based on the latest rotation and scale properties.
2613 */
2614 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002615
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002616 /**
2617 * The transform matrix for the View. This transform is calculated internally
2618 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2619 * is used by default. Do *not* use this variable directly; instead call
2620 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2621 * to get the correct matrix based on the latest rotation and scale properties.
2622 */
2623 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002624
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002625 /**
2626 * An internal 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.
2629 */
2630 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002631
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002632 /**
2633 * An internal variable that tracks whether we need to recalculate the
2634 * transform matrix, based on whether the rotation or scaleX/Y properties
2635 * have changed since the matrix was last calculated.
2636 */
2637 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002638
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002639 /**
2640 * A variable that tracks whether we need to recalculate the
2641 * transform matrix, based on whether the rotation or scaleX/Y properties
2642 * have changed since the matrix was last calculated. This variable
2643 * is only valid after a call to updateMatrix() or to a function that
2644 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2645 */
2646 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002647
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002648 /**
2649 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2650 */
2651 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002652
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002653 /**
2654 * This matrix is used when computing the matrix for 3D rotations.
2655 */
2656 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002658 /**
2659 * These prev values are used to recalculate a centered pivot point when necessary. The
2660 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2661 * set), so thes values are only used then as well.
2662 */
2663 private int mPrevWidth = -1;
2664 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002665
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002666 /**
2667 * The degrees rotation around the vertical axis through the pivot point.
2668 */
2669 @ViewDebug.ExportedProperty
2670 float mRotationY = 0f;
2671
2672 /**
2673 * The degrees rotation around the horizontal axis through the pivot point.
2674 */
2675 @ViewDebug.ExportedProperty
2676 float mRotationX = 0f;
2677
2678 /**
2679 * The degrees rotation around the pivot point.
2680 */
2681 @ViewDebug.ExportedProperty
2682 float mRotation = 0f;
2683
2684 /**
2685 * The amount of translation of the object away from its left property (post-layout).
2686 */
2687 @ViewDebug.ExportedProperty
2688 float mTranslationX = 0f;
2689
2690 /**
2691 * The amount of translation of the object away from its top property (post-layout).
2692 */
2693 @ViewDebug.ExportedProperty
2694 float mTranslationY = 0f;
2695
2696 /**
2697 * The amount of scale in the x direction around the pivot point. A
2698 * value of 1 means no scaling is applied.
2699 */
2700 @ViewDebug.ExportedProperty
2701 float mScaleX = 1f;
2702
2703 /**
2704 * The amount of scale in the y direction around the pivot point. A
2705 * value of 1 means no scaling is applied.
2706 */
2707 @ViewDebug.ExportedProperty
2708 float mScaleY = 1f;
2709
2710 /**
Chet Haasea33de552012-02-03 16:28:24 -08002711 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002712 */
2713 @ViewDebug.ExportedProperty
2714 float mPivotX = 0f;
2715
2716 /**
Chet Haasea33de552012-02-03 16:28:24 -08002717 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002718 */
2719 @ViewDebug.ExportedProperty
2720 float mPivotY = 0f;
2721
2722 /**
2723 * The opacity of the View. This is a value from 0 to 1, where 0 means
2724 * completely transparent and 1 means completely opaque.
2725 */
2726 @ViewDebug.ExportedProperty
2727 float mAlpha = 1f;
2728 }
2729
2730 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002731
Joe Malin32736f02011-01-19 16:14:20 -08002732 private boolean mLastIsOpaque;
2733
Chet Haasefd2b0022010-08-06 13:08:56 -07002734 /**
2735 * Convenience value to check for float values that are close enough to zero to be considered
2736 * zero.
2737 */
Romain Guy2542d192010-08-18 11:47:12 -07002738 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002739
2740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 * The distance in pixels from the left edge of this view's parent
2742 * to the left 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 mLeft;
2747 /**
2748 * The distance in pixels from the left edge of this view's parent
2749 * to the right 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 mRight;
2754 /**
2755 * The distance in pixels from the top edge of this view's parent
2756 * to the top edge of this view.
2757 * {@hide}
2758 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002759 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 protected int mTop;
2761 /**
2762 * The distance in pixels from the top edge of this view's parent
2763 * to the bottom edge of this view.
2764 * {@hide}
2765 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002766 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 protected int mBottom;
2768
2769 /**
2770 * The offset, in pixels, by which the content of this view is scrolled
2771 * horizontally.
2772 * {@hide}
2773 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002774 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 protected int mScrollX;
2776 /**
2777 * The offset, in pixels, by which the content of this view is scrolled
2778 * vertically.
2779 * {@hide}
2780 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002781 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 protected int mScrollY;
2783
2784 /**
2785 * The left padding in pixels, that is the distance in pixels between the
2786 * left edge of this view and the left edge of its content.
2787 * {@hide}
2788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002790 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 /**
2792 * The right padding in pixels, that is the distance in pixels between the
2793 * right edge of this view and the right edge of its content.
2794 * {@hide}
2795 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002796 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002797 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * The top padding in pixels, that is the distance in pixels between the
2800 * top edge of this view and the top edge of its content.
2801 * {@hide}
2802 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002803 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 protected int mPaddingTop;
2805 /**
2806 * The bottom padding in pixels, that is the distance in pixels between the
2807 * bottom edge of this view and the bottom edge of its content.
2808 * {@hide}
2809 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002810 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 protected int mPaddingBottom;
2812
2813 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002814 * The layout insets in pixels, that is the distance in pixels between the
2815 * visible edges of this view its bounds.
2816 */
2817 private Insets mLayoutInsets;
2818
2819 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002820 * Briefly describes the view and is primarily used for accessibility support.
2821 */
2822 private CharSequence mContentDescription;
2823
2824 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002825 * Specifies the id of a view for which this view serves as a label for
2826 * accessibility purposes.
2827 */
2828 private int mLabelForId = View.NO_ID;
2829
2830 /**
2831 * Predicate for matching labeled view id with its label for
2832 * accessibility purposes.
2833 */
2834 private MatchLabelForPredicate mMatchLabelForPredicate;
2835
2836 /**
2837 * Predicate for matching a view by its id.
2838 */
2839 private MatchIdPredicate mMatchIdPredicate;
2840
2841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002843 *
2844 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002846 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002847 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848
2849 /**
2850 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002851 *
2852 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002854 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002855 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002857 /**
Adam Powell20232d02010-12-08 21:08:53 -08002858 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002859 *
2860 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002861 */
2862 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002863 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002864
2865 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002866 * Cache the paddingStart set by the user to append to the scrollbar's size.
2867 *
2868 */
2869 @ViewDebug.ExportedProperty(category = "padding")
2870 int mUserPaddingStart;
2871
2872 /**
2873 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2874 *
2875 */
2876 @ViewDebug.ExportedProperty(category = "padding")
2877 int mUserPaddingEnd;
2878
2879 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002880 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002881 *
2882 * @hide
2883 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002884 int mUserPaddingLeftInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002885
2886 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002887 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002888 *
2889 * @hide
2890 */
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002891 int mUserPaddingRightInitial = UNDEFINED_PADDING;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002892
2893 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002894 * Default undefined padding
2895 */
2896 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2897
2898 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002899 * @hide
2900 */
2901 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2902 /**
2903 * @hide
2904 */
2905 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906
Philip Milne6c8ea062012-04-03 17:38:43 -07002907 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908
2909 private int mBackgroundResource;
2910 private boolean mBackgroundSizeChanged;
2911
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002912 static class ListenerInfo {
2913 /**
2914 * Listener used to dispatch focus change events.
2915 * This field should be made private, so it is hidden from the SDK.
2916 * {@hide}
2917 */
2918 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002920 /**
2921 * Listeners for layout change events.
2922 */
2923 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002924
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002925 /**
2926 * Listeners for attach events.
2927 */
2928 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002929
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002930 /**
2931 * Listener used to dispatch click events.
2932 * This field should be made private, so it is hidden from the SDK.
2933 * {@hide}
2934 */
2935 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002937 /**
2938 * Listener used to dispatch long click events.
2939 * This field should be made private, so it is hidden from the SDK.
2940 * {@hide}
2941 */
2942 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002944 /**
2945 * Listener used to build the context menu.
2946 * This field should be made private, so it is hidden from the SDK.
2947 * {@hide}
2948 */
2949 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002951 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002953 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002955 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002956
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002957 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002958
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002959 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002960
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002961 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2962 }
2963
2964 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 /**
2967 * The application environment this view lives in.
2968 * This field should be made private, so it is hidden from the SDK.
2969 * {@hide}
2970 */
2971 protected Context mContext;
2972
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002973 private final Resources mResources;
2974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 private ScrollabilityCache mScrollCache;
2976
2977 private int[] mDrawableState = null;
2978
Romain Guy0211a0a2011-02-14 16:34:59 -08002979 /**
2980 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002981 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002982 * @hide
2983 */
2984 public boolean mCachingFailed;
2985
Romain Guy02890fd2010-08-06 17:58:44 -07002986 private Bitmap mDrawingCache;
2987 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002988 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002989 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990
2991 /**
2992 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2993 * the user may specify which view to go to next.
2994 */
2995 private int mNextFocusLeftId = View.NO_ID;
2996
2997 /**
2998 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2999 * the user may specify which view to go to next.
3000 */
3001 private int mNextFocusRightId = View.NO_ID;
3002
3003 /**
3004 * When this view has focus and the next focus is {@link #FOCUS_UP},
3005 * the user may specify which view to go to next.
3006 */
3007 private int mNextFocusUpId = View.NO_ID;
3008
3009 /**
3010 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3011 * the user may specify which view to go to next.
3012 */
3013 private int mNextFocusDownId = View.NO_ID;
3014
Jeff Brown4e6319b2010-12-13 10:36:51 -08003015 /**
3016 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3017 * the user may specify which view to go to next.
3018 */
3019 int mNextFocusForwardId = View.NO_ID;
3020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003022 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003023 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003024 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 private UnsetPressedState mUnsetPressedState;
3027
3028 /**
3029 * Whether the long press's action has been invoked. The tap's action is invoked on the
3030 * up event while a long press is invoked as soon as the long press duration is reached, so
3031 * a long press could be performed before the tap is checked, in which case the tap's action
3032 * should not be invoked.
3033 */
3034 private boolean mHasPerformedLongPress;
3035
3036 /**
3037 * The minimum height of the view. We'll try our best to have the height
3038 * of this view to at least this amount.
3039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003040 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 private int mMinHeight;
3042
3043 /**
3044 * The minimum width of the view. We'll try our best to have the width
3045 * of this view to at least this amount.
3046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003047 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 private int mMinWidth;
3049
3050 /**
3051 * The delegate to handle touch events that are physically in this view
3052 * but should be handled by another view.
3053 */
3054 private TouchDelegate mTouchDelegate = null;
3055
3056 /**
3057 * Solid color to use as a background when creating the drawing cache. Enables
3058 * the cache to use 16 bit bitmaps instead of 32 bit.
3059 */
3060 private int mDrawingCacheBackgroundColor = 0;
3061
3062 /**
3063 * Special tree observer used when mAttachInfo is null.
3064 */
3065 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003066
Adam Powelle14579b2009-12-16 18:39:52 -08003067 /**
3068 * Cache the touch slop from the context that created the view.
3069 */
3070 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003073 * Object that handles automatic animation of view properties.
3074 */
3075 private ViewPropertyAnimator mAnimator = null;
3076
3077 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003078 * Flag indicating that a drag can cross window boundaries. When
3079 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3080 * with this flag set, all visible applications will be able to participate
3081 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003082 *
3083 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003084 */
3085 public static final int DRAG_FLAG_GLOBAL = 1;
3086
3087 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003088 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3089 */
3090 private float mVerticalScrollFactor;
3091
3092 /**
Adam Powell20232d02010-12-08 21:08:53 -08003093 * Position of the vertical scroll bar.
3094 */
3095 private int mVerticalScrollbarPosition;
3096
3097 /**
3098 * Position the scroll bar at the default position as determined by the system.
3099 */
3100 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3101
3102 /**
3103 * Position the scroll bar along the left edge.
3104 */
3105 public static final int SCROLLBAR_POSITION_LEFT = 1;
3106
3107 /**
3108 * Position the scroll bar along the right edge.
3109 */
3110 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3111
3112 /**
Romain Guy171c5922011-01-06 10:04:23 -08003113 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003114 *
3115 * @see #getLayerType()
3116 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003117 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003118 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003119 */
3120 public static final int LAYER_TYPE_NONE = 0;
3121
3122 /**
3123 * <p>Indicates that the view has a software layer. A software layer is backed
3124 * by a bitmap and causes the view to be rendered using Android's software
3125 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003126 *
Romain Guy171c5922011-01-06 10:04:23 -08003127 * <p>Software layers have various usages:</p>
3128 * <p>When the application is not using hardware acceleration, a software layer
3129 * is useful to apply a specific color filter and/or blending mode and/or
3130 * translucency to a view and all its children.</p>
3131 * <p>When the application is using hardware acceleration, a software layer
3132 * is useful to render drawing primitives not supported by the hardware
3133 * accelerated pipeline. It can also be used to cache a complex view tree
3134 * into a texture and reduce the complexity of drawing operations. For instance,
3135 * when animating a complex view tree with a translation, a software layer can
3136 * be used to render the view tree only once.</p>
3137 * <p>Software layers should be avoided when the affected view tree updates
3138 * often. Every update will require to re-render the software layer, which can
3139 * potentially be slow (particularly when hardware acceleration is turned on
3140 * since the layer will have to be uploaded into a hardware texture after every
3141 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003142 *
3143 * @see #getLayerType()
3144 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003145 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003146 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003147 */
3148 public static final int LAYER_TYPE_SOFTWARE = 1;
3149
3150 /**
3151 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3152 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3153 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3154 * rendering pipeline, but only if hardware acceleration is turned on for the
3155 * view hierarchy. When hardware acceleration is turned off, hardware layers
3156 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003157 *
Romain Guy171c5922011-01-06 10:04:23 -08003158 * <p>A hardware layer is useful to apply a specific color filter and/or
3159 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003160 * <p>A hardware layer can be used to cache a complex view tree into a
3161 * texture and reduce the complexity of drawing operations. For instance,
3162 * when animating a complex view tree with a translation, a hardware layer can
3163 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003164 * <p>A hardware layer can also be used to increase the rendering quality when
3165 * rotation transformations are applied on a view. It can also be used to
3166 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003167 *
3168 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003169 * @see #setLayerType(int, android.graphics.Paint)
3170 * @see #LAYER_TYPE_NONE
3171 * @see #LAYER_TYPE_SOFTWARE
3172 */
3173 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003174
Romain Guy3aaff3a2011-01-12 14:18:47 -08003175 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3176 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3177 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3178 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3179 })
Romain Guy171c5922011-01-06 10:04:23 -08003180 int mLayerType = LAYER_TYPE_NONE;
3181 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003182 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003183
3184 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003185 * Set to true when the view is sending hover accessibility events because it
3186 * is the innermost hovered view.
3187 */
3188 private boolean mSendingHoverAccessibilityEvents;
3189
Dianne Hackborn4702a852012-08-17 15:18:29 -07003190 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003191 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003192 */
3193 AccessibilityDelegate mAccessibilityDelegate;
3194
3195 /**
3196 * Consistency verifier for debugging purposes.
3197 * @hide
3198 */
3199 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3200 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3201 new InputEventConsistencyVerifier(this, 0) : null;
3202
Adam Powella9108a22012-07-18 11:18:09 -07003203 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3204
Jeff Brown87b7f802011-06-21 18:35:45 -07003205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 * Simple constructor to use when creating a view from code.
3207 *
3208 * @param context The Context the view is running in, through which it can
3209 * access the current theme, resources, etc.
3210 */
3211 public View(Context context) {
3212 mContext = context;
3213 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003214 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003215 // Set layout and text direction defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003216 mPrivateFlags2 =
3217 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003218 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003219 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003220 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003221 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003222 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003223 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003224 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003225 mUserPaddingStart = UNDEFINED_PADDING;
3226 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 /**
3230 * Constructor that is called when inflating a view from XML. This is called
3231 * when a view is being constructed from an XML file, supplying attributes
3232 * that were specified in the XML file. This version uses a default style of
3233 * 0, so the only attribute values applied are those in the Context's Theme
3234 * and the given AttributeSet.
3235 *
3236 * <p>
3237 * The method onFinishInflate() will be called after all children have been
3238 * added.
3239 *
3240 * @param context The Context the view is running in, through which it can
3241 * access the current theme, resources, etc.
3242 * @param attrs The attributes of the XML tag that is inflating the view.
3243 * @see #View(Context, AttributeSet, int)
3244 */
3245 public View(Context context, AttributeSet attrs) {
3246 this(context, attrs, 0);
3247 }
3248
3249 /**
3250 * Perform inflation from XML and apply a class-specific base style. This
3251 * constructor of View allows subclasses to use their own base style when
3252 * they are inflating. For example, a Button class's constructor would call
3253 * this version of the super class constructor and supply
3254 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3255 * the theme's button style to modify all of the base view attributes (in
3256 * particular its background) as well as the Button class's attributes.
3257 *
3258 * @param context The Context the view is running in, through which it can
3259 * access the current theme, resources, etc.
3260 * @param attrs The attributes of the XML tag that is inflating the view.
3261 * @param defStyle The default style to apply to this view. If 0, no style
3262 * will be applied (beyond what is included in the theme). This may
3263 * either be an attribute resource, whose value will be retrieved
3264 * from the current theme, or an explicit style resource.
3265 * @see #View(Context, AttributeSet)
3266 */
3267 public View(Context context, AttributeSet attrs, int defStyle) {
3268 this(context);
3269
3270 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3271 defStyle, 0);
3272
3273 Drawable background = null;
3274
3275 int leftPadding = -1;
3276 int topPadding = -1;
3277 int rightPadding = -1;
3278 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003279 int startPadding = UNDEFINED_PADDING;
3280 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281
3282 int padding = -1;
3283
3284 int viewFlagValues = 0;
3285 int viewFlagMasks = 0;
3286
3287 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 int x = 0;
3290 int y = 0;
3291
Chet Haase73066682010-11-29 15:55:32 -08003292 float tx = 0;
3293 float ty = 0;
3294 float rotation = 0;
3295 float rotationX = 0;
3296 float rotationY = 0;
3297 float sx = 1f;
3298 float sy = 1f;
3299 boolean transformSet = false;
3300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003302 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003303 boolean initializeScrollbars = false;
3304
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003305 boolean leftPaddingDefined = false;
3306 boolean rightPaddingDefined = false;
3307 boolean startPaddingDefined = false;
3308 boolean endPaddingDefined = false;
3309
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003310 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 final int N = a.getIndexCount();
3313 for (int i = 0; i < N; i++) {
3314 int attr = a.getIndex(i);
3315 switch (attr) {
3316 case com.android.internal.R.styleable.View_background:
3317 background = a.getDrawable(attr);
3318 break;
3319 case com.android.internal.R.styleable.View_padding:
3320 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003321 mUserPaddingLeftInitial = padding;
3322 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003323 leftPaddingDefined = true;
3324 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 break;
3326 case com.android.internal.R.styleable.View_paddingLeft:
3327 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003328 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003329 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 break;
3331 case com.android.internal.R.styleable.View_paddingTop:
3332 topPadding = a.getDimensionPixelSize(attr, -1);
3333 break;
3334 case com.android.internal.R.styleable.View_paddingRight:
3335 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003336 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003337 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 break;
3339 case com.android.internal.R.styleable.View_paddingBottom:
3340 bottomPadding = a.getDimensionPixelSize(attr, -1);
3341 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003342 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003343 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003344 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003345 break;
3346 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003347 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003348 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003349 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 case com.android.internal.R.styleable.View_scrollX:
3351 x = a.getDimensionPixelOffset(attr, 0);
3352 break;
3353 case com.android.internal.R.styleable.View_scrollY:
3354 y = a.getDimensionPixelOffset(attr, 0);
3355 break;
Chet Haase73066682010-11-29 15:55:32 -08003356 case com.android.internal.R.styleable.View_alpha:
3357 setAlpha(a.getFloat(attr, 1f));
3358 break;
3359 case com.android.internal.R.styleable.View_transformPivotX:
3360 setPivotX(a.getDimensionPixelOffset(attr, 0));
3361 break;
3362 case com.android.internal.R.styleable.View_transformPivotY:
3363 setPivotY(a.getDimensionPixelOffset(attr, 0));
3364 break;
3365 case com.android.internal.R.styleable.View_translationX:
3366 tx = a.getDimensionPixelOffset(attr, 0);
3367 transformSet = true;
3368 break;
3369 case com.android.internal.R.styleable.View_translationY:
3370 ty = a.getDimensionPixelOffset(attr, 0);
3371 transformSet = true;
3372 break;
3373 case com.android.internal.R.styleable.View_rotation:
3374 rotation = a.getFloat(attr, 0);
3375 transformSet = true;
3376 break;
3377 case com.android.internal.R.styleable.View_rotationX:
3378 rotationX = a.getFloat(attr, 0);
3379 transformSet = true;
3380 break;
3381 case com.android.internal.R.styleable.View_rotationY:
3382 rotationY = a.getFloat(attr, 0);
3383 transformSet = true;
3384 break;
3385 case com.android.internal.R.styleable.View_scaleX:
3386 sx = a.getFloat(attr, 1f);
3387 transformSet = true;
3388 break;
3389 case com.android.internal.R.styleable.View_scaleY:
3390 sy = a.getFloat(attr, 1f);
3391 transformSet = true;
3392 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 case com.android.internal.R.styleable.View_id:
3394 mID = a.getResourceId(attr, NO_ID);
3395 break;
3396 case com.android.internal.R.styleable.View_tag:
3397 mTag = a.getText(attr);
3398 break;
3399 case com.android.internal.R.styleable.View_fitsSystemWindows:
3400 if (a.getBoolean(attr, false)) {
3401 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3402 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3403 }
3404 break;
3405 case com.android.internal.R.styleable.View_focusable:
3406 if (a.getBoolean(attr, false)) {
3407 viewFlagValues |= FOCUSABLE;
3408 viewFlagMasks |= FOCUSABLE_MASK;
3409 }
3410 break;
3411 case com.android.internal.R.styleable.View_focusableInTouchMode:
3412 if (a.getBoolean(attr, false)) {
3413 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3414 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3415 }
3416 break;
3417 case com.android.internal.R.styleable.View_clickable:
3418 if (a.getBoolean(attr, false)) {
3419 viewFlagValues |= CLICKABLE;
3420 viewFlagMasks |= CLICKABLE;
3421 }
3422 break;
3423 case com.android.internal.R.styleable.View_longClickable:
3424 if (a.getBoolean(attr, false)) {
3425 viewFlagValues |= LONG_CLICKABLE;
3426 viewFlagMasks |= LONG_CLICKABLE;
3427 }
3428 break;
3429 case com.android.internal.R.styleable.View_saveEnabled:
3430 if (!a.getBoolean(attr, true)) {
3431 viewFlagValues |= SAVE_DISABLED;
3432 viewFlagMasks |= SAVE_DISABLED_MASK;
3433 }
3434 break;
3435 case com.android.internal.R.styleable.View_duplicateParentState:
3436 if (a.getBoolean(attr, false)) {
3437 viewFlagValues |= DUPLICATE_PARENT_STATE;
3438 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3439 }
3440 break;
3441 case com.android.internal.R.styleable.View_visibility:
3442 final int visibility = a.getInt(attr, 0);
3443 if (visibility != 0) {
3444 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3445 viewFlagMasks |= VISIBILITY_MASK;
3446 }
3447 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003448 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003449 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003450 mPrivateFlags2 &=
3451 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003452 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003453 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003454 final int value = (layoutDirection != -1) ?
3455 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003456 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003457 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 case com.android.internal.R.styleable.View_drawingCacheQuality:
3459 final int cacheQuality = a.getInt(attr, 0);
3460 if (cacheQuality != 0) {
3461 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3462 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3463 }
3464 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003465 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003466 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003467 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003468 case com.android.internal.R.styleable.View_labelFor:
3469 setLabelFor(a.getResourceId(attr, NO_ID));
3470 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3472 if (!a.getBoolean(attr, true)) {
3473 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3474 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3475 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003476 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3478 if (!a.getBoolean(attr, true)) {
3479 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3480 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3481 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003482 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 case R.styleable.View_scrollbars:
3484 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3485 if (scrollbars != SCROLLBARS_NONE) {
3486 viewFlagValues |= scrollbars;
3487 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003488 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 }
3490 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003491 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003493 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003494 // Ignore the attribute starting with ICS
3495 break;
3496 }
3497 // With builds < ICS, fall through and apply fading edges
3498 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3500 if (fadingEdge != FADING_EDGE_NONE) {
3501 viewFlagValues |= fadingEdge;
3502 viewFlagMasks |= FADING_EDGE_MASK;
3503 initializeFadingEdge(a);
3504 }
3505 break;
3506 case R.styleable.View_scrollbarStyle:
3507 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3508 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3509 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3510 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3511 }
3512 break;
3513 case R.styleable.View_isScrollContainer:
3514 setScrollContainer = true;
3515 if (a.getBoolean(attr, false)) {
3516 setScrollContainer(true);
3517 }
3518 break;
3519 case com.android.internal.R.styleable.View_keepScreenOn:
3520 if (a.getBoolean(attr, false)) {
3521 viewFlagValues |= KEEP_SCREEN_ON;
3522 viewFlagMasks |= KEEP_SCREEN_ON;
3523 }
3524 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003525 case R.styleable.View_filterTouchesWhenObscured:
3526 if (a.getBoolean(attr, false)) {
3527 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3528 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3529 }
3530 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 case R.styleable.View_nextFocusLeft:
3532 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3533 break;
3534 case R.styleable.View_nextFocusRight:
3535 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3536 break;
3537 case R.styleable.View_nextFocusUp:
3538 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3539 break;
3540 case R.styleable.View_nextFocusDown:
3541 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3542 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003543 case R.styleable.View_nextFocusForward:
3544 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3545 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 case R.styleable.View_minWidth:
3547 mMinWidth = a.getDimensionPixelSize(attr, 0);
3548 break;
3549 case R.styleable.View_minHeight:
3550 mMinHeight = a.getDimensionPixelSize(attr, 0);
3551 break;
Romain Guy9a817362009-05-01 10:57:14 -07003552 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003553 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003554 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003555 + "be used within a restricted context");
3556 }
3557
Romain Guy9a817362009-05-01 10:57:14 -07003558 final String handlerName = a.getString(attr);
3559 if (handlerName != null) {
3560 setOnClickListener(new OnClickListener() {
3561 private Method mHandler;
3562
3563 public void onClick(View v) {
3564 if (mHandler == null) {
3565 try {
3566 mHandler = getContext().getClass().getMethod(handlerName,
3567 View.class);
3568 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003569 int id = getId();
3570 String idText = id == NO_ID ? "" : " with id '"
3571 + getContext().getResources().getResourceEntryName(
3572 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003573 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003574 handlerName + "(View) in the activity "
3575 + getContext().getClass() + " for onClick handler"
3576 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003577 }
3578 }
3579
3580 try {
3581 mHandler.invoke(getContext(), View.this);
3582 } catch (IllegalAccessException e) {
3583 throw new IllegalStateException("Could not execute non "
3584 + "public method of the activity", e);
3585 } catch (InvocationTargetException e) {
3586 throw new IllegalStateException("Could not execute "
3587 + "method of the activity", e);
3588 }
3589 }
3590 });
3591 }
3592 break;
Adam Powell637d3372010-08-25 14:37:03 -07003593 case R.styleable.View_overScrollMode:
3594 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3595 break;
Adam Powell20232d02010-12-08 21:08:53 -08003596 case R.styleable.View_verticalScrollbarPosition:
3597 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3598 break;
Romain Guy171c5922011-01-06 10:04:23 -08003599 case R.styleable.View_layerType:
3600 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3601 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003602 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003603 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003604 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003605 // Set the text direction flags depending on the value of the attribute
3606 final int textDirection = a.getInt(attr, -1);
3607 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003608 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003609 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003610 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003611 case R.styleable.View_textAlignment:
3612 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003613 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003614 // Set the text alignment flag depending on the value of the attribute
3615 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003616 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003617 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003618 case R.styleable.View_importantForAccessibility:
3619 setImportantForAccessibility(a.getInt(attr,
3620 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003621 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 }
3623 }
3624
Adam Powell637d3372010-08-25 14:37:03 -07003625 setOverScrollMode(overScrollMode);
3626
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003627 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3628 // the resolved layout direction). Those cached values will be used later during padding
3629 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003630 mUserPaddingStart = startPadding;
3631 mUserPaddingEnd = endPadding;
3632
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003633 if (background != null) {
3634 setBackground(background);
3635 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 if (padding >= 0) {
3638 leftPadding = padding;
3639 topPadding = padding;
3640 rightPadding = padding;
3641 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003642 mUserPaddingLeftInitial = padding;
3643 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 }
3645
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003646 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3647 // left / right padding are used if defined (meaning here nothing to do). If they are not
3648 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3649 // start / end and resolve them as left / right (layout direction is not taken into account).
3650 if (isRtlCompatibilityMode()) {
3651 if (!leftPaddingDefined && startPaddingDefined) {
3652 leftPadding = startPadding;
3653 }
3654 if (!rightPaddingDefined && endPaddingDefined) {
3655 rightPadding = endPadding;
3656 }
3657 }
3658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 // If the user specified the padding (either with android:padding or
3660 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3661 // use the default padding or the padding from the background drawable
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003662 // (stored at this point in mPadding*). Padding resolution will happen later if
3663 // RTL is supported.
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003664 mUserPaddingLeftInitial = leftPadding >= 0 ? leftPadding : mPaddingLeft;
3665 mUserPaddingRightInitial = rightPadding >= 0 ? rightPadding : mPaddingRight;
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003666 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003667 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003669 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3671
3672 if (viewFlagMasks != 0) {
3673 setFlags(viewFlagValues, viewFlagMasks);
3674 }
3675
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003676 if (initializeScrollbars) {
3677 initializeScrollbars(a);
3678 }
3679
3680 a.recycle();
3681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 // Needs to be called after mViewFlags is set
3683 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3684 recomputePadding();
3685 }
3686
3687 if (x != 0 || y != 0) {
3688 scrollTo(x, y);
3689 }
3690
Chet Haase73066682010-11-29 15:55:32 -08003691 if (transformSet) {
3692 setTranslationX(tx);
3693 setTranslationY(ty);
3694 setRotation(rotation);
3695 setRotationX(rotationX);
3696 setRotationY(rotationY);
3697 setScaleX(sx);
3698 setScaleY(sy);
3699 }
3700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3702 setScrollContainer(true);
3703 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003704
3705 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 }
3707
3708 /**
3709 * Non-public constructor for use in testing
3710 */
3711 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003712 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 }
3714
Dianne Hackborn4702a852012-08-17 15:18:29 -07003715 public String toString() {
3716 StringBuilder out = new StringBuilder(128);
3717 out.append(getClass().getName());
3718 out.append('{');
3719 out.append(Integer.toHexString(System.identityHashCode(this)));
3720 out.append(' ');
3721 switch (mViewFlags&VISIBILITY_MASK) {
3722 case VISIBLE: out.append('V'); break;
3723 case INVISIBLE: out.append('I'); break;
3724 case GONE: out.append('G'); break;
3725 default: out.append('.'); break;
3726 }
3727 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3728 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3729 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3730 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3731 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3732 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3733 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3734 out.append(' ');
3735 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3736 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3737 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3738 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3739 out.append('p');
3740 } else {
3741 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3742 }
3743 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3744 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3745 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3746 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3747 out.append(' ');
3748 out.append(mLeft);
3749 out.append(',');
3750 out.append(mTop);
3751 out.append('-');
3752 out.append(mRight);
3753 out.append(',');
3754 out.append(mBottom);
3755 final int id = getId();
3756 if (id != NO_ID) {
3757 out.append(" #");
3758 out.append(Integer.toHexString(id));
3759 final Resources r = mResources;
3760 if (id != 0 && r != null) {
3761 try {
3762 String pkgname;
3763 switch (id&0xff000000) {
3764 case 0x7f000000:
3765 pkgname="app";
3766 break;
3767 case 0x01000000:
3768 pkgname="android";
3769 break;
3770 default:
3771 pkgname = r.getResourcePackageName(id);
3772 break;
3773 }
3774 String typename = r.getResourceTypeName(id);
3775 String entryname = r.getResourceEntryName(id);
3776 out.append(" ");
3777 out.append(pkgname);
3778 out.append(":");
3779 out.append(typename);
3780 out.append("/");
3781 out.append(entryname);
3782 } catch (Resources.NotFoundException e) {
3783 }
3784 }
3785 }
3786 out.append("}");
3787 return out.toString();
3788 }
3789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 /**
3791 * <p>
3792 * Initializes the fading edges from a given set of styled attributes. This
3793 * method should be called by subclasses that need fading edges and when an
3794 * instance of these subclasses is created programmatically rather than
3795 * being inflated from XML. This method is automatically called when the XML
3796 * is inflated.
3797 * </p>
3798 *
3799 * @param a the styled attributes set to initialize the fading edges from
3800 */
3801 protected void initializeFadingEdge(TypedArray a) {
3802 initScrollCache();
3803
3804 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3805 R.styleable.View_fadingEdgeLength,
3806 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3807 }
3808
3809 /**
3810 * Returns the size of the vertical faded edges used to indicate that more
3811 * content in this view is visible.
3812 *
3813 * @return The size in pixels of the vertical faded edge or 0 if vertical
3814 * faded edges are not enabled for this view.
3815 * @attr ref android.R.styleable#View_fadingEdgeLength
3816 */
3817 public int getVerticalFadingEdgeLength() {
3818 if (isVerticalFadingEdgeEnabled()) {
3819 ScrollabilityCache cache = mScrollCache;
3820 if (cache != null) {
3821 return cache.fadingEdgeLength;
3822 }
3823 }
3824 return 0;
3825 }
3826
3827 /**
3828 * Set the size of the faded edge used to indicate that more content in this
3829 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003830 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3831 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3832 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 *
3834 * @param length The size in pixels of the faded edge used to indicate that more
3835 * content in this view is visible.
3836 */
3837 public void setFadingEdgeLength(int length) {
3838 initScrollCache();
3839 mScrollCache.fadingEdgeLength = length;
3840 }
3841
3842 /**
3843 * Returns the size of the horizontal faded edges used to indicate that more
3844 * content in this view is visible.
3845 *
3846 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3847 * faded edges are not enabled for this view.
3848 * @attr ref android.R.styleable#View_fadingEdgeLength
3849 */
3850 public int getHorizontalFadingEdgeLength() {
3851 if (isHorizontalFadingEdgeEnabled()) {
3852 ScrollabilityCache cache = mScrollCache;
3853 if (cache != null) {
3854 return cache.fadingEdgeLength;
3855 }
3856 }
3857 return 0;
3858 }
3859
3860 /**
3861 * Returns the width of the vertical scrollbar.
3862 *
3863 * @return The width in pixels of the vertical scrollbar or 0 if there
3864 * is no vertical scrollbar.
3865 */
3866 public int getVerticalScrollbarWidth() {
3867 ScrollabilityCache cache = mScrollCache;
3868 if (cache != null) {
3869 ScrollBarDrawable scrollBar = cache.scrollBar;
3870 if (scrollBar != null) {
3871 int size = scrollBar.getSize(true);
3872 if (size <= 0) {
3873 size = cache.scrollBarSize;
3874 }
3875 return size;
3876 }
3877 return 0;
3878 }
3879 return 0;
3880 }
3881
3882 /**
3883 * Returns the height of the horizontal scrollbar.
3884 *
3885 * @return The height in pixels of the horizontal scrollbar or 0 if
3886 * there is no horizontal scrollbar.
3887 */
3888 protected int getHorizontalScrollbarHeight() {
3889 ScrollabilityCache cache = mScrollCache;
3890 if (cache != null) {
3891 ScrollBarDrawable scrollBar = cache.scrollBar;
3892 if (scrollBar != null) {
3893 int size = scrollBar.getSize(false);
3894 if (size <= 0) {
3895 size = cache.scrollBarSize;
3896 }
3897 return size;
3898 }
3899 return 0;
3900 }
3901 return 0;
3902 }
3903
3904 /**
3905 * <p>
3906 * Initializes the scrollbars from a given set of styled attributes. This
3907 * method should be called by subclasses that need scrollbars and when an
3908 * instance of these subclasses is created programmatically rather than
3909 * being inflated from XML. This method is automatically called when the XML
3910 * is inflated.
3911 * </p>
3912 *
3913 * @param a the styled attributes set to initialize the scrollbars from
3914 */
3915 protected void initializeScrollbars(TypedArray a) {
3916 initScrollCache();
3917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003918 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003919
Mike Cleronf116bf82009-09-27 19:14:12 -07003920 if (scrollabilityCache.scrollBar == null) {
3921 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3922 }
Joe Malin32736f02011-01-19 16:14:20 -08003923
Romain Guy8bda2482010-03-02 11:42:11 -08003924 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925
Mike Cleronf116bf82009-09-27 19:14:12 -07003926 if (!fadeScrollbars) {
3927 scrollabilityCache.state = ScrollabilityCache.ON;
3928 }
3929 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003930
3931
Mike Cleronf116bf82009-09-27 19:14:12 -07003932 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3933 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3934 .getScrollBarFadeDuration());
3935 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3936 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3937 ViewConfiguration.getScrollDefaultDelay());
3938
Joe Malin32736f02011-01-19 16:14:20 -08003939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3941 com.android.internal.R.styleable.View_scrollbarSize,
3942 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3943
3944 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3945 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3946
3947 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3948 if (thumb != null) {
3949 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3950 }
3951
3952 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3953 false);
3954 if (alwaysDraw) {
3955 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3956 }
3957
3958 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3959 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3960
3961 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3962 if (thumb != null) {
3963 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3964 }
3965
3966 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3967 false);
3968 if (alwaysDraw) {
3969 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3970 }
3971
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003972 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003973 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003974 if (track != null) {
3975 track.setLayoutDirection(layoutDirection);
3976 }
3977 if (thumb != null) {
3978 thumb.setLayoutDirection(layoutDirection);
3979 }
3980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003982 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 }
3984
3985 /**
3986 * <p>
3987 * Initalizes the scrollability cache if necessary.
3988 * </p>
3989 */
3990 private void initScrollCache() {
3991 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003992 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 }
3994 }
3995
Philip Milne6c8ea062012-04-03 17:38:43 -07003996 private ScrollabilityCache getScrollCache() {
3997 initScrollCache();
3998 return mScrollCache;
3999 }
4000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 /**
Adam Powell20232d02010-12-08 21:08:53 -08004002 * Set the position of the vertical scroll bar. Should be one of
4003 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4004 * {@link #SCROLLBAR_POSITION_RIGHT}.
4005 *
4006 * @param position Where the vertical scroll bar should be positioned.
4007 */
4008 public void setVerticalScrollbarPosition(int position) {
4009 if (mVerticalScrollbarPosition != position) {
4010 mVerticalScrollbarPosition = position;
4011 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004012 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004013 }
4014 }
4015
4016 /**
4017 * @return The position where the vertical scroll bar will show, if applicable.
4018 * @see #setVerticalScrollbarPosition(int)
4019 */
4020 public int getVerticalScrollbarPosition() {
4021 return mVerticalScrollbarPosition;
4022 }
4023
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004024 ListenerInfo getListenerInfo() {
4025 if (mListenerInfo != null) {
4026 return mListenerInfo;
4027 }
4028 mListenerInfo = new ListenerInfo();
4029 return mListenerInfo;
4030 }
4031
Adam Powell20232d02010-12-08 21:08:53 -08004032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 * Register a callback to be invoked when focus of this view changed.
4034 *
4035 * @param l The callback that will run.
4036 */
4037 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004038 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 }
4040
4041 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004042 * Add a listener that will be called when the bounds of the view change due to
4043 * layout processing.
4044 *
4045 * @param listener The listener that will be called when layout bounds change.
4046 */
4047 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004048 ListenerInfo li = getListenerInfo();
4049 if (li.mOnLayoutChangeListeners == null) {
4050 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004051 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004052 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4053 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004054 }
Chet Haase21cd1382010-09-01 17:42:29 -07004055 }
4056
4057 /**
4058 * Remove a listener for layout changes.
4059 *
4060 * @param listener The listener for layout bounds change.
4061 */
4062 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004063 ListenerInfo li = mListenerInfo;
4064 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004065 return;
4066 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004067 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004068 }
4069
4070 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004071 * Add a listener for attach state changes.
4072 *
4073 * This listener will be called whenever this view is attached or detached
4074 * from a window. Remove the listener using
4075 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4076 *
4077 * @param listener Listener to attach
4078 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4079 */
4080 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004081 ListenerInfo li = getListenerInfo();
4082 if (li.mOnAttachStateChangeListeners == null) {
4083 li.mOnAttachStateChangeListeners
4084 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004085 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004086 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004087 }
4088
4089 /**
4090 * Remove a listener for attach state changes. The listener will receive no further
4091 * notification of window attach/detach events.
4092 *
4093 * @param listener Listener to remove
4094 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4095 */
4096 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 ListenerInfo li = mListenerInfo;
4098 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004099 return;
4100 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004101 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004102 }
4103
4104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 * Returns the focus-change callback registered for this view.
4106 *
4107 * @return The callback, or null if one is not registered.
4108 */
4109 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004110 ListenerInfo li = mListenerInfo;
4111 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113
4114 /**
4115 * Register a callback to be invoked when this view is clicked. If this view is not
4116 * clickable, it becomes clickable.
4117 *
4118 * @param l The callback that will run
4119 *
4120 * @see #setClickable(boolean)
4121 */
4122 public void setOnClickListener(OnClickListener l) {
4123 if (!isClickable()) {
4124 setClickable(true);
4125 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004126 getListenerInfo().mOnClickListener = l;
4127 }
4128
4129 /**
4130 * Return whether this view has an attached OnClickListener. Returns
4131 * true if there is a listener, false if there is none.
4132 */
4133 public boolean hasOnClickListeners() {
4134 ListenerInfo li = mListenerInfo;
4135 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 }
4137
4138 /**
4139 * Register a callback to be invoked when this view is clicked and held. If this view is not
4140 * long clickable, it becomes long clickable.
4141 *
4142 * @param l The callback that will run
4143 *
4144 * @see #setLongClickable(boolean)
4145 */
4146 public void setOnLongClickListener(OnLongClickListener l) {
4147 if (!isLongClickable()) {
4148 setLongClickable(true);
4149 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004150 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 }
4152
4153 /**
4154 * Register a callback to be invoked when the context menu for this view is
4155 * being built. If this view is not long clickable, it becomes long clickable.
4156 *
4157 * @param l The callback that will run
4158 *
4159 */
4160 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4161 if (!isLongClickable()) {
4162 setLongClickable(true);
4163 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004164 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 }
4166
4167 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004168 * Call this view's OnClickListener, if it is defined. Performs all normal
4169 * actions associated with clicking: reporting accessibility event, playing
4170 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 *
4172 * @return True there was an assigned OnClickListener that was called, false
4173 * otherwise is returned.
4174 */
4175 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004176 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4177
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004178 ListenerInfo li = mListenerInfo;
4179 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004181 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 return true;
4183 }
4184
4185 return false;
4186 }
4187
4188 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004189 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4190 * this only calls the listener, and does not do any associated clicking
4191 * actions like reporting an accessibility event.
4192 *
4193 * @return True there was an assigned OnClickListener that was called, false
4194 * otherwise is returned.
4195 */
4196 public boolean callOnClick() {
4197 ListenerInfo li = mListenerInfo;
4198 if (li != null && li.mOnClickListener != null) {
4199 li.mOnClickListener.onClick(this);
4200 return true;
4201 }
4202 return false;
4203 }
4204
4205 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004206 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4207 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004209 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 */
4211 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004212 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004215 ListenerInfo li = mListenerInfo;
4216 if (li != null && li.mOnLongClickListener != null) {
4217 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 }
4219 if (!handled) {
4220 handled = showContextMenu();
4221 }
4222 if (handled) {
4223 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4224 }
4225 return handled;
4226 }
4227
4228 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004229 * Performs button-related actions during a touch down event.
4230 *
4231 * @param event The event.
4232 * @return True if the down was consumed.
4233 *
4234 * @hide
4235 */
4236 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4237 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4238 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4239 return true;
4240 }
4241 }
4242 return false;
4243 }
4244
4245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 * Bring up the context menu for this view.
4247 *
4248 * @return Whether a context menu was displayed.
4249 */
4250 public boolean showContextMenu() {
4251 return getParent().showContextMenuForChild(this);
4252 }
4253
4254 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004255 * Bring up the context menu for this view, referring to the item under the specified point.
4256 *
4257 * @param x The referenced x coordinate.
4258 * @param y The referenced y coordinate.
4259 * @param metaState The keyboard modifiers that were pressed.
4260 * @return Whether a context menu was displayed.
4261 *
4262 * @hide
4263 */
4264 public boolean showContextMenu(float x, float y, int metaState) {
4265 return showContextMenu();
4266 }
4267
4268 /**
Adam Powell6e346362010-07-23 10:18:23 -07004269 * Start an action mode.
4270 *
4271 * @param callback Callback that will control the lifecycle of the action mode
4272 * @return The new action mode if it is started, null otherwise
4273 *
4274 * @see ActionMode
4275 */
4276 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004277 ViewParent parent = getParent();
4278 if (parent == null) return null;
4279 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004280 }
4281
4282 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004283 * Register a callback to be invoked when a hardware key is pressed in this view.
4284 * Key presses in software input methods will generally not trigger the methods of
4285 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 * @param l the key listener to attach to this view
4287 */
4288 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004289 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 }
4291
4292 /**
4293 * Register a callback to be invoked when a touch event is sent to this view.
4294 * @param l the touch listener to attach to this view
4295 */
4296 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004297 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 }
4299
4300 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004301 * Register a callback to be invoked when a generic motion event is sent to this view.
4302 * @param l the generic motion listener to attach to this view
4303 */
4304 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004305 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004306 }
4307
4308 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004309 * Register a callback to be invoked when a hover event is sent to this view.
4310 * @param l the hover listener to attach to this view
4311 */
4312 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004313 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004314 }
4315
4316 /**
Joe Malin32736f02011-01-19 16:14:20 -08004317 * Register a drag event listener callback object for this View. The parameter is
4318 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4319 * View, the system calls the
4320 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4321 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004322 */
4323 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004324 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004325 }
4326
4327 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004328 * Give this view focus. This will cause
4329 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 *
4331 * Note: this does not check whether this {@link View} should get focus, it just
4332 * gives it focus no matter what. It should only be called internally by framework
4333 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4334 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004335 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4336 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 * focus moved when requestFocus() is called. It may not always
4338 * apply, in which case use the default View.FOCUS_DOWN.
4339 * @param previouslyFocusedRect The rectangle of the view that had focus
4340 * prior in this View's coordinate system.
4341 */
4342 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4343 if (DBG) {
4344 System.out.println(this + " requestFocus()");
4345 }
4346
Dianne Hackborn4702a852012-08-17 15:18:29 -07004347 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4348 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349
4350 if (mParent != null) {
4351 mParent.requestChildFocus(this, this);
4352 }
4353
4354 onFocusChanged(true, direction, previouslyFocusedRect);
4355 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004356
4357 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4358 notifyAccessibilityStateChanged();
4359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 }
4361 }
4362
4363 /**
4364 * Request that a rectangle of this view be visible on the screen,
4365 * scrolling if necessary just enough.
4366 *
4367 * <p>A View should call this if it maintains some notion of which part
4368 * of its content is interesting. For example, a text editing view
4369 * should call this when its cursor moves.
4370 *
4371 * @param rectangle The rectangle.
4372 * @return Whether any parent scrolled.
4373 */
4374 public boolean requestRectangleOnScreen(Rect rectangle) {
4375 return requestRectangleOnScreen(rectangle, false);
4376 }
4377
4378 /**
4379 * Request that a rectangle of this view be visible on the screen,
4380 * scrolling if necessary just enough.
4381 *
4382 * <p>A View should call this if it maintains some notion of which part
4383 * of its content is interesting. For example, a text editing view
4384 * should call this when its cursor moves.
4385 *
4386 * <p>When <code>immediate</code> is set to true, scrolling will not be
4387 * animated.
4388 *
4389 * @param rectangle The rectangle.
4390 * @param immediate True to forbid animated scrolling, false otherwise
4391 * @return Whether any parent scrolled.
4392 */
4393 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004394 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004395 return false;
4396 }
4397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004399
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004400 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004401 position.set(rectangle);
4402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 ViewParent parent = mParent;
4404 boolean scrolled = false;
4405 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004406 rectangle.set((int) position.left, (int) position.top,
4407 (int) position.right, (int) position.bottom);
4408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 scrolled |= parent.requestChildRectangleOnScreen(child,
4410 rectangle, immediate);
4411
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004412 if (!child.hasIdentityMatrix()) {
4413 child.getMatrix().mapRect(position);
4414 }
4415
4416 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417
4418 if (!(parent instanceof View)) {
4419 break;
4420 }
Romain Guy8506ab42009-06-11 17:35:47 -07004421
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004422 View parentView = (View) parent;
4423
4424 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4425
4426 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 parent = child.getParent();
4428 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 return scrolled;
4431 }
4432
4433 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004434 * Called when this view wants to give up focus. If focus is cleared
4435 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4436 * <p>
4437 * <strong>Note:</strong> When a View clears focus the framework is trying
4438 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004439 * View is the first from the top that can take focus, then all callbacks
4440 * related to clearing focus will be invoked after wich the framework will
4441 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004442 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 */
4444 public void clearFocus() {
4445 if (DBG) {
4446 System.out.println(this + " clearFocus()");
4447 }
4448
Dianne Hackborn4702a852012-08-17 15:18:29 -07004449 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4450 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451
4452 if (mParent != null) {
4453 mParent.clearChildFocus(this);
4454 }
4455
4456 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004459
4460 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004461
4462 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4463 notifyAccessibilityStateChanged();
4464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
4466 }
4467
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004468 void ensureInputFocusOnFirstFocusable() {
4469 View root = getRootView();
4470 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004471 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 }
4473 }
4474
4475 /**
4476 * Called internally by the view system when a new view is getting focus.
4477 * This is what clears the old focus.
4478 */
4479 void unFocus() {
4480 if (DBG) {
4481 System.out.println(this + " unFocus()");
4482 }
4483
Dianne Hackborn4702a852012-08-17 15:18:29 -07004484 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4485 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486
4487 onFocusChanged(false, 0, null);
4488 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004489
4490 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4491 notifyAccessibilityStateChanged();
4492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 }
4494 }
4495
4496 /**
4497 * Returns true if this view has focus iteself, or is the ancestor of the
4498 * view that has focus.
4499 *
4500 * @return True if this view has or contains focus, false otherwise.
4501 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004502 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004504 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 }
4506
4507 /**
4508 * Returns true if this view is focusable or if it contains a reachable View
4509 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4510 * is a View whose parents do not block descendants focus.
4511 *
4512 * Only {@link #VISIBLE} views are considered focusable.
4513 *
4514 * @return True if the view is focusable or if the view contains a focusable
4515 * View, false otherwise.
4516 *
4517 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4518 */
4519 public boolean hasFocusable() {
4520 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4521 }
4522
4523 /**
4524 * Called by the view system when the focus state of this view changes.
4525 * When the focus change event is caused by directional navigation, direction
4526 * and previouslyFocusedRect provide insight into where the focus is coming from.
4527 * When overriding, be sure to call up through to the super class so that
4528 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004529 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004530 * @param gainFocus True if the View has focus; false otherwise.
4531 * @param direction The direction focus has moved when requestFocus()
4532 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004533 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4534 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4535 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4537 * system, of the previously focused view. If applicable, this will be
4538 * passed in as finer grained information about where the focus is coming
4539 * from (in addition to direction). Will be <code>null</code> otherwise.
4540 */
4541 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004542 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004543 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4544 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004545 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004546 }
4547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 InputMethodManager imm = InputMethodManager.peekInstance();
4549 if (!gainFocus) {
4550 if (isPressed()) {
4551 setPressed(false);
4552 }
4553 if (imm != null && mAttachInfo != null
4554 && mAttachInfo.mHasWindowFocus) {
4555 imm.focusOut(this);
4556 }
Romain Guya2431d02009-04-30 16:30:00 -07004557 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558 } else if (imm != null && mAttachInfo != null
4559 && mAttachInfo.mHasWindowFocus) {
4560 imm.focusIn(this);
4561 }
Romain Guy8506ab42009-06-11 17:35:47 -07004562
Romain Guy0fd89bf2011-01-26 15:41:30 -08004563 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004564 ListenerInfo li = mListenerInfo;
4565 if (li != null && li.mOnFocusChangeListener != null) {
4566 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 }
Joe Malin32736f02011-01-19 16:14:20 -08004568
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004569 if (mAttachInfo != null) {
4570 mAttachInfo.mKeyDispatchState.reset(this);
4571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 }
4573
4574 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004575 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004576 * not enabled this method has no effect. The default implementation calls
4577 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4578 * to populate information about the event source (this View), then calls
4579 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4580 * populate the text content of the event source including its descendants,
4581 * and last calls
4582 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4583 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004584 * <p>
4585 * If an {@link AccessibilityDelegate} has been specified via calling
4586 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4587 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4588 * responsible for handling this call.
4589 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004590 *
Scott Mainb303d832011-10-12 16:45:18 -07004591 * @param eventType The type of the event to send, as defined by several types from
4592 * {@link android.view.accessibility.AccessibilityEvent}, such as
4593 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4594 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004595 *
4596 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4597 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4598 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004599 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004600 */
4601 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004602 if (mAccessibilityDelegate != null) {
4603 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4604 } else {
4605 sendAccessibilityEventInternal(eventType);
4606 }
4607 }
4608
4609 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004610 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4611 * {@link AccessibilityEvent} to make an announcement which is related to some
4612 * sort of a context change for which none of the events representing UI transitions
4613 * is a good fit. For example, announcing a new page in a book. If accessibility
4614 * is not enabled this method does nothing.
4615 *
4616 * @param text The announcement text.
4617 */
4618 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004619 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004620 AccessibilityEvent event = AccessibilityEvent.obtain(
4621 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004622 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004623 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004624 event.setContentDescription(null);
4625 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004626 }
4627 }
4628
4629 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004630 * @see #sendAccessibilityEvent(int)
4631 *
4632 * Note: Called from the default {@link AccessibilityDelegate}.
4633 */
4634 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004635 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4636 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4637 }
4638 }
4639
4640 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004641 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4642 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004643 * perform a check whether accessibility is enabled.
4644 * <p>
4645 * If an {@link AccessibilityDelegate} has been specified via calling
4646 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4647 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4648 * is responsible for handling this call.
4649 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004650 *
4651 * @param event The event to send.
4652 *
4653 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004654 */
4655 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004656 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004657 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004658 } else {
4659 sendAccessibilityEventUncheckedInternal(event);
4660 }
4661 }
4662
4663 /**
4664 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4665 *
4666 * Note: Called from the default {@link AccessibilityDelegate}.
4667 */
4668 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004669 if (!isShown()) {
4670 return;
4671 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004672 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004673 // Only a subset of accessibility events populates text content.
4674 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4675 dispatchPopulateAccessibilityEvent(event);
4676 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004677 // In the beginning we called #isShown(), so we know that getParent() is not null.
4678 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004679 }
4680
4681 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004682 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4683 * to its children for adding their text content to the event. Note that the
4684 * event text is populated in a separate dispatch path since we add to the
4685 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004686 * A typical implementation will call
4687 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4688 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4689 * on each child. Override this method if custom population of the event text
4690 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004691 * <p>
4692 * If an {@link AccessibilityDelegate} has been specified via calling
4693 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4694 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4695 * is responsible for handling this call.
4696 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004697 * <p>
4698 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4699 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4700 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004701 *
4702 * @param event The event.
4703 *
4704 * @return True if the event population was completed.
4705 */
4706 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004707 if (mAccessibilityDelegate != null) {
4708 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4709 } else {
4710 return dispatchPopulateAccessibilityEventInternal(event);
4711 }
4712 }
4713
4714 /**
4715 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4716 *
4717 * Note: Called from the default {@link AccessibilityDelegate}.
4718 */
4719 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004720 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004721 return false;
4722 }
4723
4724 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004725 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004726 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004727 * text content. While this method is free to modify event
4728 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004729 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4730 * <p>
4731 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004732 * to the text added by the super implementation:
4733 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004734 * super.onPopulateAccessibilityEvent(event);
4735 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4736 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4737 * mCurrentDate.getTimeInMillis(), flags);
4738 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004739 * }</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#onPopulateAccessibilityEvent(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 Ganov736c2752011-04-22 18:30:36 -07004749 *
4750 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004751 *
4752 * @see #sendAccessibilityEvent(int)
4753 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004754 */
4755 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004756 if (mAccessibilityDelegate != null) {
4757 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4758 } else {
4759 onPopulateAccessibilityEventInternal(event);
4760 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004761 }
4762
4763 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004764 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4765 *
4766 * Note: Called from the default {@link AccessibilityDelegate}.
4767 */
4768 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4769
4770 }
4771
4772 /**
4773 * Initializes an {@link AccessibilityEvent} with information about
4774 * this View which is the event source. In other words, the source of
4775 * an accessibility event is the view whose state change triggered firing
4776 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004777 * <p>
4778 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004779 * to properties set by the super implementation:
4780 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4781 * super.onInitializeAccessibilityEvent(event);
4782 * event.setPassword(true);
4783 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004784 * <p>
4785 * If an {@link AccessibilityDelegate} has been specified via calling
4786 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4787 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4788 * is responsible for handling this call.
4789 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004790 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4791 * information to the event, in case the default implementation has basic information to add.
4792 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004793 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004794 *
4795 * @see #sendAccessibilityEvent(int)
4796 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4797 */
4798 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004799 if (mAccessibilityDelegate != null) {
4800 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4801 } else {
4802 onInitializeAccessibilityEventInternal(event);
4803 }
4804 }
4805
4806 /**
4807 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4808 *
4809 * Note: Called from the default {@link AccessibilityDelegate}.
4810 */
4811 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004812 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004813 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004814 event.setPackageName(getContext().getPackageName());
4815 event.setEnabled(isEnabled());
4816 event.setContentDescription(mContentDescription);
4817
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004818 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004819 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004820 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4821 FOCUSABLES_ALL);
4822 event.setItemCount(focusablesTempList.size());
4823 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4824 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004825 }
4826 }
4827
4828 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004829 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4830 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4831 * This method is responsible for obtaining an accessibility node info from a
4832 * pool of reusable instances and calling
4833 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4834 * initialize the former.
4835 * <p>
4836 * Note: The client is responsible for recycling the obtained instance by calling
4837 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4838 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004839 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004840 * @return A populated {@link AccessibilityNodeInfo}.
4841 *
4842 * @see AccessibilityNodeInfo
4843 */
4844 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004845 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4846 if (provider != null) {
4847 return provider.createAccessibilityNodeInfo(View.NO_ID);
4848 } else {
4849 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4850 onInitializeAccessibilityNodeInfo(info);
4851 return info;
4852 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004853 }
4854
4855 /**
4856 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4857 * The base implementation sets:
4858 * <ul>
4859 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004860 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4861 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004862 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4863 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4864 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4865 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4866 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4867 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4868 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4869 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4870 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4871 * </ul>
4872 * <p>
4873 * Subclasses should override this method, call the super implementation,
4874 * and set additional attributes.
4875 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004876 * <p>
4877 * If an {@link AccessibilityDelegate} has been specified via calling
4878 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4879 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4880 * is responsible for handling this call.
4881 * </p>
4882 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004883 * @param info The instance to initialize.
4884 */
4885 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004886 if (mAccessibilityDelegate != null) {
4887 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4888 } else {
4889 onInitializeAccessibilityNodeInfoInternal(info);
4890 }
4891 }
4892
4893 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004894 * Gets the location of this view in screen coordintates.
4895 *
4896 * @param outRect The output location
4897 */
4898 private void getBoundsOnScreen(Rect outRect) {
4899 if (mAttachInfo == null) {
4900 return;
4901 }
4902
4903 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004904 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004905
4906 if (!hasIdentityMatrix()) {
4907 getMatrix().mapRect(position);
4908 }
4909
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004910 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004911
4912 ViewParent parent = mParent;
4913 while (parent instanceof View) {
4914 View parentView = (View) parent;
4915
4916 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4917
4918 if (!parentView.hasIdentityMatrix()) {
4919 parentView.getMatrix().mapRect(position);
4920 }
4921
4922 position.offset(parentView.mLeft, parentView.mTop);
4923
4924 parent = parentView.mParent;
4925 }
4926
4927 if (parent instanceof ViewRootImpl) {
4928 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4929 position.offset(0, -viewRootImpl.mCurScrollY);
4930 }
4931
4932 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4933
4934 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4935 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4936 }
4937
4938 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004939 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4940 *
4941 * Note: Called from the default {@link AccessibilityDelegate}.
4942 */
4943 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004944 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004945
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004946 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004947 info.setBoundsInParent(bounds);
4948
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004949 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004950 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004951
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004952 ViewParent parent = getParentForAccessibility();
4953 if (parent instanceof View) {
4954 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004955 }
4956
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004957 if (mID != View.NO_ID) {
4958 View rootView = getRootView();
4959 if (rootView == null) {
4960 rootView = this;
4961 }
4962 View label = rootView.findLabelForView(this, mID);
4963 if (label != null) {
4964 info.setLabeledBy(label);
4965 }
4966 }
4967
4968 if (mLabelForId != View.NO_ID) {
4969 View rootView = getRootView();
4970 if (rootView == null) {
4971 rootView = this;
4972 }
4973 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4974 if (labeled != null) {
4975 info.setLabelFor(labeled);
4976 }
4977 }
4978
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004979 info.setVisibleToUser(isVisibleToUser());
4980
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004981 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004982 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004983 info.setContentDescription(getContentDescription());
4984
4985 info.setEnabled(isEnabled());
4986 info.setClickable(isClickable());
4987 info.setFocusable(isFocusable());
4988 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004989 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004990 info.setSelected(isSelected());
4991 info.setLongClickable(isLongClickable());
4992
4993 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004994 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004995 // we should report as selectable view in an AdapterView.
4996 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4997 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4998
4999 if (isFocusable()) {
5000 if (isFocused()) {
5001 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5002 } else {
5003 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5004 }
5005 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005006
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005007 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005008 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005009 } else {
5010 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5011 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005012
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005013 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005014 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5015 }
5016
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005017 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005018 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5019 }
5020
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005021 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005022 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5023 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5024 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005025 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5026 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005027 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005028 }
5029
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005030 private View findLabelForView(View view, int labeledId) {
5031 if (mMatchLabelForPredicate == null) {
5032 mMatchLabelForPredicate = new MatchLabelForPredicate();
5033 }
5034 mMatchLabelForPredicate.mLabeledId = labeledId;
5035 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5036 }
5037
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005038 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005039 * Computes whether this view is visible to the user. Such a view is
5040 * attached, visible, all its predecessors are visible, it is not clipped
5041 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005042 *
5043 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005044 *
5045 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005046 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005047 protected boolean isVisibleToUser() {
5048 return isVisibleToUser(null);
5049 }
5050
5051 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005052 * Computes whether the given portion of this view is visible to the user.
5053 * Such a view is attached, visible, all its predecessors are visible,
5054 * has an alpha greater than zero, and the specified portion is not
5055 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005056 *
5057 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5058 * <code>null</code>, and the entire view will be tested in this case.
5059 * When <code>true</code> is returned by the function, the actual visible
5060 * region will be stored in this parameter; that is, if boundInView is fully
5061 * contained within the view, no modification will be made, otherwise regions
5062 * outside of the visible area of the view will be clipped.
5063 *
5064 * @return Whether the specified portion of the view is visible on the screen.
5065 *
5066 * @hide
5067 */
5068 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005069 if (mAttachInfo != null) {
5070 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5071 Point offset = mAttachInfo.mPoint;
5072 // The first two checks are made also made by isShown() which
5073 // however traverses the tree up to the parent to catch that.
5074 // Therefore, we do some fail fast check to minimize the up
5075 // tree traversal.
5076 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
5077 && getAlpha() > 0
5078 && isShown()
5079 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005080 if (isVisible && boundInView != null) {
5081 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07005082 // isVisible is always true here, use a simple assignment
5083 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005084 }
5085 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07005086 }
5087
5088 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005089 }
5090
5091 /**
alanvb72fe7a2012-08-27 16:44:25 -07005092 * Returns the delegate for implementing accessibility support via
5093 * composition. For more details see {@link AccessibilityDelegate}.
5094 *
5095 * @return The delegate, or null if none set.
5096 *
5097 * @hide
5098 */
5099 public AccessibilityDelegate getAccessibilityDelegate() {
5100 return mAccessibilityDelegate;
5101 }
5102
5103 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005104 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005105 * opposed to inheritance. The delegate's primary use is for implementing
5106 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5107 *
5108 * @param delegate The delegate instance.
5109 *
5110 * @see AccessibilityDelegate
5111 */
5112 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5113 mAccessibilityDelegate = delegate;
5114 }
5115
5116 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005117 * Gets the provider for managing a virtual view hierarchy rooted at this View
5118 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5119 * that explore the window content.
5120 * <p>
5121 * If this method returns an instance, this instance is responsible for managing
5122 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5123 * View including the one representing the View itself. Similarly the returned
5124 * instance is responsible for performing accessibility actions on any virtual
5125 * view or the root view itself.
5126 * </p>
5127 * <p>
5128 * If an {@link AccessibilityDelegate} has been specified via calling
5129 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5130 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5131 * is responsible for handling this call.
5132 * </p>
5133 *
5134 * @return The provider.
5135 *
5136 * @see AccessibilityNodeProvider
5137 */
5138 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5139 if (mAccessibilityDelegate != null) {
5140 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5141 } else {
5142 return null;
5143 }
5144 }
5145
5146 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005147 * Gets the unique identifier of this view on the screen for accessibility purposes.
5148 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5149 *
5150 * @return The view accessibility id.
5151 *
5152 * @hide
5153 */
5154 public int getAccessibilityViewId() {
5155 if (mAccessibilityViewId == NO_ID) {
5156 mAccessibilityViewId = sNextAccessibilityViewId++;
5157 }
5158 return mAccessibilityViewId;
5159 }
5160
5161 /**
5162 * Gets the unique identifier of the window in which this View reseides.
5163 *
5164 * @return The window accessibility id.
5165 *
5166 * @hide
5167 */
5168 public int getAccessibilityWindowId() {
5169 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5170 }
5171
5172 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005173 * Gets the {@link View} description. It briefly describes the view and is
5174 * primarily used for accessibility support. Set this property to enable
5175 * better accessibility support for your application. This is especially
5176 * true for views that do not have textual representation (For example,
5177 * ImageButton).
5178 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005179 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005180 *
5181 * @attr ref android.R.styleable#View_contentDescription
5182 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005183 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005184 public CharSequence getContentDescription() {
5185 return mContentDescription;
5186 }
5187
5188 /**
5189 * Sets the {@link View} description. It briefly describes the view and is
5190 * primarily used for accessibility support. Set this property to enable
5191 * better accessibility support for your application. This is especially
5192 * true for views that do not have textual representation (For example,
5193 * ImageButton).
5194 *
5195 * @param contentDescription The content description.
5196 *
5197 * @attr ref android.R.styleable#View_contentDescription
5198 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005199 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005200 public void setContentDescription(CharSequence contentDescription) {
5201 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005202 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5203 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5204 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5205 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005206 }
5207
5208 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005209 * Gets the id of a view for which this view serves as a label for
5210 * accessibility purposes.
5211 *
5212 * @return The labeled view id.
5213 */
5214 @ViewDebug.ExportedProperty(category = "accessibility")
5215 public int getLabelFor() {
5216 return mLabelForId;
5217 }
5218
5219 /**
5220 * Sets the id of a view for which this view serves as a label for
5221 * accessibility purposes.
5222 *
5223 * @param id The labeled view id.
5224 */
5225 @RemotableViewMethod
5226 public void setLabelFor(int id) {
5227 mLabelForId = id;
5228 if (mLabelForId != View.NO_ID
5229 && mID == View.NO_ID) {
5230 mID = generateViewId();
5231 }
5232 }
5233
5234 /**
Romain Guya2431d02009-04-30 16:30:00 -07005235 * Invoked whenever this view loses focus, either by losing window focus or by losing
5236 * focus within its window. This method can be used to clear any state tied to the
5237 * focus. For instance, if a button is held pressed with the trackball and the window
5238 * loses focus, this method can be used to cancel the press.
5239 *
5240 * Subclasses of View overriding this method should always call super.onFocusLost().
5241 *
5242 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005243 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005244 *
5245 * @hide pending API council approval
5246 */
5247 protected void onFocusLost() {
5248 resetPressedState();
5249 }
5250
5251 private void resetPressedState() {
5252 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5253 return;
5254 }
5255
5256 if (isPressed()) {
5257 setPressed(false);
5258
5259 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005260 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005261 }
5262 }
5263 }
5264
5265 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 * Returns true if this view has focus
5267 *
5268 * @return True if this view has focus, false otherwise.
5269 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005270 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005272 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005273 }
5274
5275 /**
5276 * Find the view in the hierarchy rooted at this view that currently has
5277 * focus.
5278 *
5279 * @return The view that currently has focus, or null if no focused view can
5280 * be found.
5281 */
5282 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005283 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 }
5285
5286 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005287 * Indicates whether this view is one of the set of scrollable containers in
5288 * its window.
5289 *
5290 * @return whether this view is one of the set of scrollable containers in
5291 * its window
5292 *
5293 * @attr ref android.R.styleable#View_isScrollContainer
5294 */
5295 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005296 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005297 }
5298
5299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 * Change whether this view is one of the set of scrollable containers in
5301 * its window. This will be used to determine whether the window can
5302 * resize or must pan when a soft input area is open -- scrollable
5303 * containers allow the window to use resize mode since the container
5304 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005305 *
5306 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 */
5308 public void setScrollContainer(boolean isScrollContainer) {
5309 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005310 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005312 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005314 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005315 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005316 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 mAttachInfo.mScrollContainers.remove(this);
5318 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005319 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 }
5321 }
5322
5323 /**
5324 * Returns the quality of the drawing cache.
5325 *
5326 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5327 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5328 *
5329 * @see #setDrawingCacheQuality(int)
5330 * @see #setDrawingCacheEnabled(boolean)
5331 * @see #isDrawingCacheEnabled()
5332 *
5333 * @attr ref android.R.styleable#View_drawingCacheQuality
5334 */
5335 public int getDrawingCacheQuality() {
5336 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5337 }
5338
5339 /**
5340 * Set the drawing cache quality of this view. This value is used only when the
5341 * drawing cache is enabled
5342 *
5343 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5344 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5345 *
5346 * @see #getDrawingCacheQuality()
5347 * @see #setDrawingCacheEnabled(boolean)
5348 * @see #isDrawingCacheEnabled()
5349 *
5350 * @attr ref android.R.styleable#View_drawingCacheQuality
5351 */
5352 public void setDrawingCacheQuality(int quality) {
5353 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5354 }
5355
5356 /**
5357 * Returns whether the screen should remain on, corresponding to the current
5358 * value of {@link #KEEP_SCREEN_ON}.
5359 *
5360 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5361 *
5362 * @see #setKeepScreenOn(boolean)
5363 *
5364 * @attr ref android.R.styleable#View_keepScreenOn
5365 */
5366 public boolean getKeepScreenOn() {
5367 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5368 }
5369
5370 /**
5371 * Controls whether the screen should remain on, modifying the
5372 * value of {@link #KEEP_SCREEN_ON}.
5373 *
5374 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5375 *
5376 * @see #getKeepScreenOn()
5377 *
5378 * @attr ref android.R.styleable#View_keepScreenOn
5379 */
5380 public void setKeepScreenOn(boolean keepScreenOn) {
5381 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5382 }
5383
5384 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005385 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5386 * @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 -08005387 *
5388 * @attr ref android.R.styleable#View_nextFocusLeft
5389 */
5390 public int getNextFocusLeftId() {
5391 return mNextFocusLeftId;
5392 }
5393
5394 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005395 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5396 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5397 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005398 *
5399 * @attr ref android.R.styleable#View_nextFocusLeft
5400 */
5401 public void setNextFocusLeftId(int nextFocusLeftId) {
5402 mNextFocusLeftId = nextFocusLeftId;
5403 }
5404
5405 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005406 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5407 * @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 -08005408 *
5409 * @attr ref android.R.styleable#View_nextFocusRight
5410 */
5411 public int getNextFocusRightId() {
5412 return mNextFocusRightId;
5413 }
5414
5415 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005416 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5417 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5418 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 *
5420 * @attr ref android.R.styleable#View_nextFocusRight
5421 */
5422 public void setNextFocusRightId(int nextFocusRightId) {
5423 mNextFocusRightId = nextFocusRightId;
5424 }
5425
5426 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005427 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5428 * @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 -08005429 *
5430 * @attr ref android.R.styleable#View_nextFocusUp
5431 */
5432 public int getNextFocusUpId() {
5433 return mNextFocusUpId;
5434 }
5435
5436 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005437 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5438 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5439 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 *
5441 * @attr ref android.R.styleable#View_nextFocusUp
5442 */
5443 public void setNextFocusUpId(int nextFocusUpId) {
5444 mNextFocusUpId = nextFocusUpId;
5445 }
5446
5447 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005448 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5449 * @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 -08005450 *
5451 * @attr ref android.R.styleable#View_nextFocusDown
5452 */
5453 public int getNextFocusDownId() {
5454 return mNextFocusDownId;
5455 }
5456
5457 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005458 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5459 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5460 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 *
5462 * @attr ref android.R.styleable#View_nextFocusDown
5463 */
5464 public void setNextFocusDownId(int nextFocusDownId) {
5465 mNextFocusDownId = nextFocusDownId;
5466 }
5467
5468 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005469 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5470 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5471 *
5472 * @attr ref android.R.styleable#View_nextFocusForward
5473 */
5474 public int getNextFocusForwardId() {
5475 return mNextFocusForwardId;
5476 }
5477
5478 /**
5479 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5480 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5481 * decide automatically.
5482 *
5483 * @attr ref android.R.styleable#View_nextFocusForward
5484 */
5485 public void setNextFocusForwardId(int nextFocusForwardId) {
5486 mNextFocusForwardId = nextFocusForwardId;
5487 }
5488
5489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 * Returns the visibility of this view and all of its ancestors
5491 *
5492 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5493 */
5494 public boolean isShown() {
5495 View current = this;
5496 //noinspection ConstantConditions
5497 do {
5498 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5499 return false;
5500 }
5501 ViewParent parent = current.mParent;
5502 if (parent == null) {
5503 return false; // We are not attached to the view root
5504 }
5505 if (!(parent instanceof View)) {
5506 return true;
5507 }
5508 current = (View) parent;
5509 } while (current != null);
5510
5511 return false;
5512 }
5513
5514 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005515 * Called by the view hierarchy when the content insets for a window have
5516 * changed, to allow it to adjust its content to fit within those windows.
5517 * The content insets tell you the space that the status bar, input method,
5518 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005520 * <p>You do not normally need to deal with this function, since the default
5521 * window decoration given to applications takes care of applying it to the
5522 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5523 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5524 * and your content can be placed under those system elements. You can then
5525 * use this method within your view hierarchy if you have parts of your UI
5526 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005528 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005529 * inset's to the view's padding, consuming that content (modifying the
5530 * insets to be 0), and returning true. This behavior is off by default, but can
5531 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5532 *
5533 * <p>This function's traversal down the hierarchy is depth-first. The same content
5534 * insets object is propagated down the hierarchy, so any changes made to it will
5535 * be seen by all following views (including potentially ones above in
5536 * the hierarchy since this is a depth-first traversal). The first view
5537 * that returns true will abort the entire traversal.
5538 *
5539 * <p>The default implementation works well for a situation where it is
5540 * used with a container that covers the entire window, allowing it to
5541 * apply the appropriate insets to its content on all edges. If you need
5542 * a more complicated layout (such as two different views fitting system
5543 * windows, one on the top of the window, and one on the bottom),
5544 * you can override the method and handle the insets however you would like.
5545 * Note that the insets provided by the framework are always relative to the
5546 * far edges of the window, not accounting for the location of the called view
5547 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005548 * where the layout will place the view, as it is done before layout happens.)
5549 *
5550 * <p>Note: unlike many View methods, there is no dispatch phase to this
5551 * call. If you are overriding it in a ViewGroup and want to allow the
5552 * call to continue to your children, you must be sure to call the super
5553 * implementation.
5554 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005555 * <p>Here is a sample layout that makes use of fitting system windows
5556 * to have controls for a video view placed inside of the window decorations
5557 * that it hides and shows. This can be used with code like the second
5558 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5559 *
5560 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5561 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005562 * @param insets Current content insets of the window. Prior to
5563 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5564 * the insets or else you and Android will be unhappy.
5565 *
5566 * @return Return true if this view applied the insets and it should not
5567 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005568 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005569 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005570 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 */
5572 protected boolean fitSystemWindows(Rect insets) {
5573 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005574 mUserPaddingStart = UNDEFINED_PADDING;
5575 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005576 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5577 || mAttachInfo == null
5578 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5579 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5580 return true;
5581 } else {
5582 internalSetPadding(0, 0, 0, 0);
5583 return false;
5584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 }
5586 return false;
5587 }
5588
5589 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005590 * Sets whether or not this view should account for system screen decorations
5591 * such as the status bar and inset its content; that is, controlling whether
5592 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5593 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005594 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005595 * <p>Note that if you are providing your own implementation of
5596 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5597 * flag to true -- your implementation will be overriding the default
5598 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005599 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005600 * @param fitSystemWindows If true, then the default implementation of
5601 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005602 *
5603 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005604 * @see #getFitsSystemWindows()
5605 * @see #fitSystemWindows(Rect)
5606 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005607 */
5608 public void setFitsSystemWindows(boolean fitSystemWindows) {
5609 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5610 }
5611
5612 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005613 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005614 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5615 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005616 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005617 * @return Returns true if the default implementation of
5618 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005619 *
5620 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005621 * @see #setFitsSystemWindows()
5622 * @see #fitSystemWindows(Rect)
5623 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005624 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005625 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005626 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5627 }
5628
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005629 /** @hide */
5630 public boolean fitsSystemWindows() {
5631 return getFitsSystemWindows();
5632 }
5633
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005634 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005635 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5636 */
5637 public void requestFitSystemWindows() {
5638 if (mParent != null) {
5639 mParent.requestFitSystemWindows();
5640 }
5641 }
5642
5643 /**
5644 * For use by PhoneWindow to make its own system window fitting optional.
5645 * @hide
5646 */
5647 public void makeOptionalFitsSystemWindows() {
5648 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5649 }
5650
5651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 * Returns the visibility status for this view.
5653 *
5654 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5655 * @attr ref android.R.styleable#View_visibility
5656 */
5657 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005658 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5659 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5660 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 })
5662 public int getVisibility() {
5663 return mViewFlags & VISIBILITY_MASK;
5664 }
5665
5666 /**
5667 * Set the enabled state of this view.
5668 *
5669 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5670 * @attr ref android.R.styleable#View_visibility
5671 */
5672 @RemotableViewMethod
5673 public void setVisibility(int visibility) {
5674 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005675 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 }
5677
5678 /**
5679 * Returns the enabled status for this view. The interpretation of the
5680 * enabled state varies by subclass.
5681 *
5682 * @return True if this view is enabled, false otherwise.
5683 */
5684 @ViewDebug.ExportedProperty
5685 public boolean isEnabled() {
5686 return (mViewFlags & ENABLED_MASK) == ENABLED;
5687 }
5688
5689 /**
5690 * Set the enabled state of this view. The interpretation of the enabled
5691 * state varies by subclass.
5692 *
5693 * @param enabled True if this view is enabled, false otherwise.
5694 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005695 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005697 if (enabled == isEnabled()) return;
5698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005699 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5700
5701 /*
5702 * The View most likely has to change its appearance, so refresh
5703 * the drawable state.
5704 */
5705 refreshDrawableState();
5706
5707 // Invalidate too, since the default behavior for views is to be
5708 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005709 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 }
5711
5712 /**
5713 * Set whether this view can receive the focus.
5714 *
5715 * Setting this to false will also ensure that this view is not focusable
5716 * in touch mode.
5717 *
5718 * @param focusable If true, this view can receive the focus.
5719 *
5720 * @see #setFocusableInTouchMode(boolean)
5721 * @attr ref android.R.styleable#View_focusable
5722 */
5723 public void setFocusable(boolean focusable) {
5724 if (!focusable) {
5725 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5726 }
5727 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5728 }
5729
5730 /**
5731 * Set whether this view can receive focus while in touch mode.
5732 *
5733 * Setting this to true will also ensure that this view is focusable.
5734 *
5735 * @param focusableInTouchMode If true, this view can receive the focus while
5736 * in touch mode.
5737 *
5738 * @see #setFocusable(boolean)
5739 * @attr ref android.R.styleable#View_focusableInTouchMode
5740 */
5741 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5742 // Focusable in touch mode should always be set before the focusable flag
5743 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5744 // which, in touch mode, will not successfully request focus on this view
5745 // because the focusable in touch mode flag is not set
5746 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5747 if (focusableInTouchMode) {
5748 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5749 }
5750 }
5751
5752 /**
5753 * Set whether this view should have sound effects enabled for events such as
5754 * clicking and touching.
5755 *
5756 * <p>You may wish to disable sound effects for a view if you already play sounds,
5757 * for instance, a dial key that plays dtmf tones.
5758 *
5759 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5760 * @see #isSoundEffectsEnabled()
5761 * @see #playSoundEffect(int)
5762 * @attr ref android.R.styleable#View_soundEffectsEnabled
5763 */
5764 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5765 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5766 }
5767
5768 /**
5769 * @return whether this view should have sound effects enabled for events such as
5770 * clicking and touching.
5771 *
5772 * @see #setSoundEffectsEnabled(boolean)
5773 * @see #playSoundEffect(int)
5774 * @attr ref android.R.styleable#View_soundEffectsEnabled
5775 */
5776 @ViewDebug.ExportedProperty
5777 public boolean isSoundEffectsEnabled() {
5778 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5779 }
5780
5781 /**
5782 * Set whether this view should have haptic feedback for events such as
5783 * long presses.
5784 *
5785 * <p>You may wish to disable haptic feedback if your view already controls
5786 * its own haptic feedback.
5787 *
5788 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5789 * @see #isHapticFeedbackEnabled()
5790 * @see #performHapticFeedback(int)
5791 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5792 */
5793 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5794 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5795 }
5796
5797 /**
5798 * @return whether this view should have haptic feedback enabled for events
5799 * long presses.
5800 *
5801 * @see #setHapticFeedbackEnabled(boolean)
5802 * @see #performHapticFeedback(int)
5803 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5804 */
5805 @ViewDebug.ExportedProperty
5806 public boolean isHapticFeedbackEnabled() {
5807 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5808 }
5809
5810 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005811 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005812 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005813 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5814 * {@link #LAYOUT_DIRECTION_RTL},
5815 * {@link #LAYOUT_DIRECTION_INHERIT} or
5816 * {@link #LAYOUT_DIRECTION_LOCALE}.
5817 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005818 *
5819 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005820 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005821 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005822 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5823 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5824 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5825 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005826 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005827 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005828 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005829 }
5830
5831 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005832 * Set the layout direction for this view. This will propagate a reset of layout direction
5833 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005834 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005835 * @param layoutDirection the layout direction to set. Should be one of:
5836 *
5837 * {@link #LAYOUT_DIRECTION_LTR},
5838 * {@link #LAYOUT_DIRECTION_RTL},
5839 * {@link #LAYOUT_DIRECTION_INHERIT},
5840 * {@link #LAYOUT_DIRECTION_LOCALE}.
5841 *
5842 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5843 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5844 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005845 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005846 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005847 */
5848 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005849 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005850 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005851 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005852 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005853 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005854 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005855 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005856 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005857 // We need to resolve all RTL properties as they all depend on layout direction
5858 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005859 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005860 }
5861
5862 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005863 * Returns the resolved layout direction for this view.
5864 *
5865 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005866 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005867 *
5868 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5869 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005870 */
5871 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005872 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5873 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005874 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005875 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005876 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5877 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005878 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005879 return LAYOUT_DIRECTION_LTR;
5880 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005881 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5882 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005883 }
5884
5885 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005886 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5887 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005888 *
5889 * @return true if the layout is right-to-left.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005890 */
5891 @ViewDebug.ExportedProperty(category = "layout")
5892 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005893 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005894 }
5895
5896 /**
Adam Powell539ee872012-02-03 19:00:49 -08005897 * Indicates whether the view is currently tracking transient state that the
5898 * app should not need to concern itself with saving and restoring, but that
5899 * the framework should take special note to preserve when possible.
5900 *
Adam Powell785c4472012-05-02 21:25:39 -07005901 * <p>A view with transient state cannot be trivially rebound from an external
5902 * data source, such as an adapter binding item views in a list. This may be
5903 * because the view is performing an animation, tracking user selection
5904 * of content, or similar.</p>
5905 *
Adam Powell539ee872012-02-03 19:00:49 -08005906 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005907 */
5908 @ViewDebug.ExportedProperty(category = "layout")
5909 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005910 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005911 }
5912
5913 /**
5914 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005915 * framework should attempt to preserve when possible. This flag is reference counted,
5916 * so every call to setHasTransientState(true) should be paired with a later call
5917 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005918 *
Adam Powell785c4472012-05-02 21:25:39 -07005919 * <p>A view with transient state cannot be trivially rebound from an external
5920 * data source, such as an adapter binding item views in a list. This may be
5921 * because the view is performing an animation, tracking user selection
5922 * of content, or similar.</p>
5923 *
Adam Powell539ee872012-02-03 19:00:49 -08005924 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005925 */
5926 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005927 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5928 mTransientStateCount - 1;
5929 if (mTransientStateCount < 0) {
5930 mTransientStateCount = 0;
5931 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5932 "unmatched pair of setHasTransientState calls");
5933 }
5934 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005935 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005936 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005937 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5938 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005939 if (mParent != null) {
5940 try {
5941 mParent.childHasTransientStateChanged(this, hasTransientState);
5942 } catch (AbstractMethodError e) {
5943 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5944 " does not fully implement ViewParent", e);
5945 }
Adam Powell539ee872012-02-03 19:00:49 -08005946 }
5947 }
5948 }
5949
5950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 * If this view doesn't do any drawing on its own, set this flag to
5952 * allow further optimizations. By default, this flag is not set on
5953 * View, but could be set on some View subclasses such as ViewGroup.
5954 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005955 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5956 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 *
5958 * @param willNotDraw whether or not this View draw on its own
5959 */
5960 public void setWillNotDraw(boolean willNotDraw) {
5961 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5962 }
5963
5964 /**
5965 * Returns whether or not this View draws on its own.
5966 *
5967 * @return true if this view has nothing to draw, false otherwise
5968 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005969 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 public boolean willNotDraw() {
5971 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5972 }
5973
5974 /**
5975 * When a View's drawing cache is enabled, drawing is redirected to an
5976 * offscreen bitmap. Some views, like an ImageView, must be able to
5977 * bypass this mechanism if they already draw a single bitmap, to avoid
5978 * unnecessary usage of the memory.
5979 *
5980 * @param willNotCacheDrawing true if this view does not cache its
5981 * drawing, false otherwise
5982 */
5983 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5984 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5985 }
5986
5987 /**
5988 * Returns whether or not this View can cache its drawing or not.
5989 *
5990 * @return true if this view does not cache its drawing, false otherwise
5991 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005992 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 public boolean willNotCacheDrawing() {
5994 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5995 }
5996
5997 /**
5998 * Indicates whether this view reacts to click events or not.
5999 *
6000 * @return true if the view is clickable, false otherwise
6001 *
6002 * @see #setClickable(boolean)
6003 * @attr ref android.R.styleable#View_clickable
6004 */
6005 @ViewDebug.ExportedProperty
6006 public boolean isClickable() {
6007 return (mViewFlags & CLICKABLE) == CLICKABLE;
6008 }
6009
6010 /**
6011 * Enables or disables click events for this view. When a view
6012 * is clickable it will change its state to "pressed" on every click.
6013 * Subclasses should set the view clickable to visually react to
6014 * user's clicks.
6015 *
6016 * @param clickable true to make the view clickable, false otherwise
6017 *
6018 * @see #isClickable()
6019 * @attr ref android.R.styleable#View_clickable
6020 */
6021 public void setClickable(boolean clickable) {
6022 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6023 }
6024
6025 /**
6026 * Indicates whether this view reacts to long click events or not.
6027 *
6028 * @return true if the view is long clickable, false otherwise
6029 *
6030 * @see #setLongClickable(boolean)
6031 * @attr ref android.R.styleable#View_longClickable
6032 */
6033 public boolean isLongClickable() {
6034 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6035 }
6036
6037 /**
6038 * Enables or disables long click events for this view. When a view is long
6039 * clickable it reacts to the user holding down the button for a longer
6040 * duration than a tap. This event can either launch the listener or a
6041 * context menu.
6042 *
6043 * @param longClickable true to make the view long clickable, false otherwise
6044 * @see #isLongClickable()
6045 * @attr ref android.R.styleable#View_longClickable
6046 */
6047 public void setLongClickable(boolean longClickable) {
6048 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6049 }
6050
6051 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006052 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 *
6054 * @see #isClickable()
6055 * @see #setClickable(boolean)
6056 *
6057 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6058 * the View's internal state from a previously set "pressed" state.
6059 */
6060 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006061 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006064 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006066 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006068
6069 if (needsRefresh) {
6070 refreshDrawableState();
6071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 dispatchSetPressed(pressed);
6073 }
6074
6075 /**
6076 * Dispatch setPressed to all of this View's children.
6077 *
6078 * @see #setPressed(boolean)
6079 *
6080 * @param pressed The new pressed state
6081 */
6082 protected void dispatchSetPressed(boolean pressed) {
6083 }
6084
6085 /**
6086 * Indicates whether the view is currently in pressed state. Unless
6087 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6088 * the pressed state.
6089 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006090 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 * @see #isClickable()
6092 * @see #setClickable(boolean)
6093 *
6094 * @return true if the view is currently pressed, false otherwise
6095 */
6096 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006097 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 }
6099
6100 /**
6101 * Indicates whether this view will save its state (that is,
6102 * whether its {@link #onSaveInstanceState} method will be called).
6103 *
6104 * @return Returns true if the view state saving is enabled, else false.
6105 *
6106 * @see #setSaveEnabled(boolean)
6107 * @attr ref android.R.styleable#View_saveEnabled
6108 */
6109 public boolean isSaveEnabled() {
6110 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6111 }
6112
6113 /**
6114 * Controls whether the saving of this view's state is
6115 * enabled (that is, whether its {@link #onSaveInstanceState} method
6116 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006117 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 * for its state to be saved. This flag can only disable the
6119 * saving of this view; any child views may still have their state saved.
6120 *
6121 * @param enabled Set to false to <em>disable</em> state saving, or true
6122 * (the default) to allow it.
6123 *
6124 * @see #isSaveEnabled()
6125 * @see #setId(int)
6126 * @see #onSaveInstanceState()
6127 * @attr ref android.R.styleable#View_saveEnabled
6128 */
6129 public void setSaveEnabled(boolean enabled) {
6130 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6131 }
6132
Jeff Brown85a31762010-09-01 17:01:00 -07006133 /**
6134 * Gets whether the framework should discard touches when the view's
6135 * window is obscured by another visible window.
6136 * Refer to the {@link View} security documentation for more details.
6137 *
6138 * @return True if touch filtering is enabled.
6139 *
6140 * @see #setFilterTouchesWhenObscured(boolean)
6141 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6142 */
6143 @ViewDebug.ExportedProperty
6144 public boolean getFilterTouchesWhenObscured() {
6145 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6146 }
6147
6148 /**
6149 * Sets whether the framework should discard touches when the view's
6150 * window is obscured by another visible window.
6151 * Refer to the {@link View} security documentation for more details.
6152 *
6153 * @param enabled True if touch filtering should be enabled.
6154 *
6155 * @see #getFilterTouchesWhenObscured
6156 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6157 */
6158 public void setFilterTouchesWhenObscured(boolean enabled) {
6159 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6160 FILTER_TOUCHES_WHEN_OBSCURED);
6161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162
6163 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006164 * Indicates whether the entire hierarchy under this view will save its
6165 * state when a state saving traversal occurs from its parent. The default
6166 * is true; if false, these views will not be saved unless
6167 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6168 *
6169 * @return Returns true if the view state saving from parent is enabled, else false.
6170 *
6171 * @see #setSaveFromParentEnabled(boolean)
6172 */
6173 public boolean isSaveFromParentEnabled() {
6174 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6175 }
6176
6177 /**
6178 * Controls whether the entire hierarchy under this view will save its
6179 * state when a state saving traversal occurs from its parent. The default
6180 * is true; if false, these views will not be saved unless
6181 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6182 *
6183 * @param enabled Set to false to <em>disable</em> state saving, or true
6184 * (the default) to allow it.
6185 *
6186 * @see #isSaveFromParentEnabled()
6187 * @see #setId(int)
6188 * @see #onSaveInstanceState()
6189 */
6190 public void setSaveFromParentEnabled(boolean enabled) {
6191 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6192 }
6193
6194
6195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 * Returns whether this View is able to take focus.
6197 *
6198 * @return True if this view can take focus, or false otherwise.
6199 * @attr ref android.R.styleable#View_focusable
6200 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006201 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006202 public final boolean isFocusable() {
6203 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6204 }
6205
6206 /**
6207 * When a view is focusable, it may not want to take focus when in touch mode.
6208 * For example, a button would like focus when the user is navigating via a D-pad
6209 * so that the user can click on it, but once the user starts touching the screen,
6210 * the button shouldn't take focus
6211 * @return Whether the view is focusable in touch mode.
6212 * @attr ref android.R.styleable#View_focusableInTouchMode
6213 */
6214 @ViewDebug.ExportedProperty
6215 public final boolean isFocusableInTouchMode() {
6216 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6217 }
6218
6219 /**
6220 * Find the nearest view in the specified direction that can take focus.
6221 * This does not actually give focus to that view.
6222 *
6223 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6224 *
6225 * @return The nearest focusable in the specified direction, or null if none
6226 * can be found.
6227 */
6228 public View focusSearch(int direction) {
6229 if (mParent != null) {
6230 return mParent.focusSearch(this, direction);
6231 } else {
6232 return null;
6233 }
6234 }
6235
6236 /**
6237 * This method is the last chance for the focused view and its ancestors to
6238 * respond to an arrow key. This is called when the focused view did not
6239 * consume the key internally, nor could the view system find a new view in
6240 * the requested direction to give focus to.
6241 *
6242 * @param focused The currently focused view.
6243 * @param direction The direction focus wants to move. One of FOCUS_UP,
6244 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6245 * @return True if the this view consumed this unhandled move.
6246 */
6247 public boolean dispatchUnhandledMove(View focused, int direction) {
6248 return false;
6249 }
6250
6251 /**
6252 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006253 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006255 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6256 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 * @return The user specified next view, or null if there is none.
6258 */
6259 View findUserSetNextFocus(View root, int direction) {
6260 switch (direction) {
6261 case FOCUS_LEFT:
6262 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006263 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006264 case FOCUS_RIGHT:
6265 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006266 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 case FOCUS_UP:
6268 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006269 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 case FOCUS_DOWN:
6271 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006272 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006273 case FOCUS_FORWARD:
6274 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006275 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006276 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006277 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006278 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006279 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006280 @Override
6281 public boolean apply(View t) {
6282 return t.mNextFocusForwardId == id;
6283 }
6284 });
6285 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 }
6287 return null;
6288 }
6289
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006290 private View findViewInsideOutShouldExist(View root, int id) {
6291 if (mMatchIdPredicate == null) {
6292 mMatchIdPredicate = new MatchIdPredicate();
6293 }
6294 mMatchIdPredicate.mId = id;
6295 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006296 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006297 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 }
6299 return result;
6300 }
6301
6302 /**
6303 * Find and return all focusable views that are descendants of this view,
6304 * possibly including this view if it is focusable itself.
6305 *
6306 * @param direction The direction of the focus
6307 * @return A list of focusable views
6308 */
6309 public ArrayList<View> getFocusables(int direction) {
6310 ArrayList<View> result = new ArrayList<View>(24);
6311 addFocusables(result, direction);
6312 return result;
6313 }
6314
6315 /**
6316 * Add any focusable views that are descendants of this view (possibly
6317 * including this view if it is focusable itself) to views. If we are in touch mode,
6318 * only add views that are also focusable in touch mode.
6319 *
6320 * @param views Focusable views found so far
6321 * @param direction The direction of the focus
6322 */
6323 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006324 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326
svetoslavganov75986cf2009-05-14 22:28:01 -07006327 /**
6328 * Adds any focusable views that are descendants of this view (possibly
6329 * including this view if it is focusable itself) to views. This method
6330 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006331 * only views focusable in touch mode if we are in touch mode or
6332 * only views that can take accessibility focus if accessibility is enabeld
6333 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006334 *
6335 * @param views Focusable views found so far or null if all we are interested is
6336 * the number of focusables.
6337 * @param direction The direction of the focus.
6338 * @param focusableMode The type of focusables to be added.
6339 *
6340 * @see #FOCUSABLES_ALL
6341 * @see #FOCUSABLES_TOUCH_MODE
6342 */
6343 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006344 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006345 return;
6346 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006347 if (!isFocusable()) {
6348 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006349 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006350 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6351 && isInTouchMode() && !isFocusableInTouchMode()) {
6352 return;
6353 }
6354 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355 }
6356
6357 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006358 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006359 * The search is performed by either the text that the View renders or the content
6360 * description that describes the view for accessibility purposes and the view does
6361 * not render or both. Clients can specify how the search is to be performed via
6362 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6363 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006364 *
6365 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006366 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006367 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006368 * @see #FIND_VIEWS_WITH_TEXT
6369 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6370 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006371 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006372 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006373 if (getAccessibilityNodeProvider() != null) {
6374 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6375 outViews.add(this);
6376 }
6377 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006378 && (searched != null && searched.length() > 0)
6379 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006380 String searchedLowerCase = searched.toString().toLowerCase();
6381 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6382 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6383 outViews.add(this);
6384 }
6385 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006386 }
6387
6388 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 * Find and return all touchable views that are descendants of this view,
6390 * possibly including this view if it is touchable itself.
6391 *
6392 * @return A list of touchable views
6393 */
6394 public ArrayList<View> getTouchables() {
6395 ArrayList<View> result = new ArrayList<View>();
6396 addTouchables(result);
6397 return result;
6398 }
6399
6400 /**
6401 * Add any touchable views that are descendants of this view (possibly
6402 * including this view if it is touchable itself) to views.
6403 *
6404 * @param views Touchable views found so far
6405 */
6406 public void addTouchables(ArrayList<View> views) {
6407 final int viewFlags = mViewFlags;
6408
6409 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6410 && (viewFlags & ENABLED_MASK) == ENABLED) {
6411 views.add(this);
6412 }
6413 }
6414
6415 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006416 * Returns whether this View is accessibility focused.
6417 *
6418 * @return True if this View is accessibility focused.
6419 */
6420 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006421 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006422 }
6423
6424 /**
6425 * Call this to try to give accessibility focus to this view.
6426 *
6427 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6428 * returns false or the view is no visible or the view already has accessibility
6429 * focus.
6430 *
6431 * See also {@link #focusSearch(int)}, which is what you call to say that you
6432 * have focus, and you want your parent to look for the next one.
6433 *
6434 * @return Whether this view actually took accessibility focus.
6435 *
6436 * @hide
6437 */
6438 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006439 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6440 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006441 return false;
6442 }
6443 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6444 return false;
6445 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006446 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6447 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006448 ViewRootImpl viewRootImpl = getViewRootImpl();
6449 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006450 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006451 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006452 if (mAttachInfo != null) {
6453 Rect rectangle = mAttachInfo.mTmpInvalRect;
6454 getDrawingRect(rectangle);
6455 requestRectangleOnScreen(rectangle);
6456 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006457 invalidate();
6458 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6459 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006460 return true;
6461 }
6462 return false;
6463 }
6464
6465 /**
6466 * Call this to try to clear accessibility focus of this view.
6467 *
6468 * See also {@link #focusSearch(int)}, which is what you call to say that you
6469 * have focus, and you want your parent to look for the next one.
6470 *
6471 * @hide
6472 */
6473 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006474 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6475 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006476 invalidate();
6477 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6478 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006479 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006480 // Clear the global reference of accessibility focus if this
6481 // view or any of its descendants had accessibility focus.
6482 ViewRootImpl viewRootImpl = getViewRootImpl();
6483 if (viewRootImpl != null) {
6484 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6485 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006486 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006487 }
6488 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006489 }
6490
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006491 private void sendAccessibilityHoverEvent(int eventType) {
6492 // Since we are not delivering to a client accessibility events from not
6493 // important views (unless the clinet request that) we need to fire the
6494 // event from the deepest view exposed to the client. As a consequence if
6495 // the user crosses a not exposed view the client will see enter and exit
6496 // of the exposed predecessor followed by and enter and exit of that same
6497 // predecessor when entering and exiting the not exposed descendant. This
6498 // is fine since the client has a clear idea which view is hovered at the
6499 // price of a couple more events being sent. This is a simple and
6500 // working solution.
6501 View source = this;
6502 while (true) {
6503 if (source.includeForAccessibility()) {
6504 source.sendAccessibilityEvent(eventType);
6505 return;
6506 }
6507 ViewParent parent = source.getParent();
6508 if (parent instanceof View) {
6509 source = (View) parent;
6510 } else {
6511 return;
6512 }
6513 }
6514 }
6515
Svetoslav Ganov42138042012-03-20 11:51:39 -07006516 /**
6517 * Clears accessibility focus without calling any callback methods
6518 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6519 * is used for clearing accessibility focus when giving this focus to
6520 * another view.
6521 */
6522 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006523 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6524 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006525 invalidate();
6526 }
6527 }
6528
6529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 * Call this to try to give focus to a specific view or to one of its
6531 * descendants.
6532 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006533 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6534 * false), or if it is focusable and it is not focusable in touch mode
6535 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006537 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 * have focus, and you want your parent to look for the next one.
6539 *
6540 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6541 * {@link #FOCUS_DOWN} and <code>null</code>.
6542 *
6543 * @return Whether this view or one of its descendants actually took focus.
6544 */
6545 public final boolean requestFocus() {
6546 return requestFocus(View.FOCUS_DOWN);
6547 }
6548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 /**
6550 * Call this to try to give focus to a specific view or to one of its
6551 * descendants and give it a hint about what direction focus is heading.
6552 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006553 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6554 * false), or if it is focusable and it is not focusable in touch mode
6555 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006557 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 * have focus, and you want your parent to look for the next one.
6559 *
6560 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6561 * <code>null</code> set for the previously focused rectangle.
6562 *
6563 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6564 * @return Whether this view or one of its descendants actually took focus.
6565 */
6566 public final boolean requestFocus(int direction) {
6567 return requestFocus(direction, null);
6568 }
6569
6570 /**
6571 * Call this to try to give focus to a specific view or to one of its descendants
6572 * and give it hints about the direction and a specific rectangle that the focus
6573 * is coming from. The rectangle can help give larger views a finer grained hint
6574 * about where focus is coming from, and therefore, where to show selection, or
6575 * forward focus change internally.
6576 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006577 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6578 * false), or if it is focusable and it is not focusable in touch mode
6579 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 *
6581 * A View will not take focus if it is not visible.
6582 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006583 * A View will not take focus if one of its parents has
6584 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6585 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006587 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 * have focus, and you want your parent to look for the next one.
6589 *
6590 * You may wish to override this method if your custom {@link View} has an internal
6591 * {@link View} that it wishes to forward the request to.
6592 *
6593 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6594 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6595 * to give a finer grained hint about where focus is coming from. May be null
6596 * if there is no hint.
6597 * @return Whether this view or one of its descendants actually took focus.
6598 */
6599 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006600 return requestFocusNoSearch(direction, previouslyFocusedRect);
6601 }
6602
6603 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 // need to be focusable
6605 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6606 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6607 return false;
6608 }
6609
6610 // need to be focusable in touch mode if in touch mode
6611 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006612 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6613 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 }
6615
6616 // need to not have any parents blocking us
6617 if (hasAncestorThatBlocksDescendantFocus()) {
6618 return false;
6619 }
6620
6621 handleFocusGainInternal(direction, previouslyFocusedRect);
6622 return true;
6623 }
6624
6625 /**
6626 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6627 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6628 * touch mode to request focus when they are touched.
6629 *
6630 * @return Whether this view or one of its descendants actually took focus.
6631 *
6632 * @see #isInTouchMode()
6633 *
6634 */
6635 public final boolean requestFocusFromTouch() {
6636 // Leave touch mode if we need to
6637 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006638 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006639 if (viewRoot != null) {
6640 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 }
6642 }
6643 return requestFocus(View.FOCUS_DOWN);
6644 }
6645
6646 /**
6647 * @return Whether any ancestor of this view blocks descendant focus.
6648 */
6649 private boolean hasAncestorThatBlocksDescendantFocus() {
6650 ViewParent ancestor = mParent;
6651 while (ancestor instanceof ViewGroup) {
6652 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6653 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6654 return true;
6655 } else {
6656 ancestor = vgAncestor.getParent();
6657 }
6658 }
6659 return false;
6660 }
6661
6662 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006663 * Gets the mode for determining whether this View is important for accessibility
6664 * which is if it fires accessibility events and if it is reported to
6665 * accessibility services that query the screen.
6666 *
6667 * @return The mode for determining whether a View is important for accessibility.
6668 *
6669 * @attr ref android.R.styleable#View_importantForAccessibility
6670 *
6671 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6672 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6673 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6674 */
6675 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006676 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6677 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6678 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006679 })
6680 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006681 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6682 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006683 }
6684
6685 /**
6686 * Sets how to determine whether this view is important for accessibility
6687 * which is if it fires accessibility events and if it is reported to
6688 * accessibility services that query the screen.
6689 *
6690 * @param mode How to determine whether this view is important for accessibility.
6691 *
6692 * @attr ref android.R.styleable#View_importantForAccessibility
6693 *
6694 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6695 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6696 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6697 */
6698 public void setImportantForAccessibility(int mode) {
6699 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006700 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6701 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6702 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006703 notifyAccessibilityStateChanged();
6704 }
6705 }
6706
6707 /**
6708 * Gets whether this view should be exposed for accessibility.
6709 *
6710 * @return Whether the view is exposed for accessibility.
6711 *
6712 * @hide
6713 */
6714 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006715 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6716 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006717 switch (mode) {
6718 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6719 return true;
6720 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6721 return false;
6722 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006723 return isActionableForAccessibility() || hasListenersForAccessibility()
6724 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006725 default:
6726 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6727 + mode);
6728 }
6729 }
6730
6731 /**
6732 * Gets the parent for accessibility purposes. Note that the parent for
6733 * accessibility is not necessary the immediate parent. It is the first
6734 * predecessor that is important for accessibility.
6735 *
6736 * @return The parent for accessibility purposes.
6737 */
6738 public ViewParent getParentForAccessibility() {
6739 if (mParent instanceof View) {
6740 View parentView = (View) mParent;
6741 if (parentView.includeForAccessibility()) {
6742 return mParent;
6743 } else {
6744 return mParent.getParentForAccessibility();
6745 }
6746 }
6747 return null;
6748 }
6749
6750 /**
6751 * Adds the children of a given View for accessibility. Since some Views are
6752 * not important for accessibility the children for accessibility are not
6753 * necessarily direct children of the riew, rather they are the first level of
6754 * descendants important for accessibility.
6755 *
6756 * @param children The list of children for accessibility.
6757 */
6758 public void addChildrenForAccessibility(ArrayList<View> children) {
6759 if (includeForAccessibility()) {
6760 children.add(this);
6761 }
6762 }
6763
6764 /**
6765 * Whether to regard this view for accessibility. A view is regarded for
6766 * accessibility if it is important for accessibility or the querying
6767 * accessibility service has explicitly requested that view not
6768 * important for accessibility are regarded.
6769 *
6770 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006771 *
6772 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006773 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006774 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006775 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006776 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006777 }
6778 return false;
6779 }
6780
6781 /**
6782 * Returns whether the View is considered actionable from
6783 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006784 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006785 *
6786 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006787 *
6788 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006789 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006790 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006791 return (isClickable() || isLongClickable() || isFocusable());
6792 }
6793
6794 /**
6795 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006796 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006797 *
6798 * @return True if the view is actionable for accessibility.
6799 */
6800 private boolean hasListenersForAccessibility() {
6801 ListenerInfo info = getListenerInfo();
6802 return mTouchDelegate != null || info.mOnKeyListener != null
6803 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6804 || info.mOnHoverListener != null || info.mOnDragListener != null;
6805 }
6806
6807 /**
6808 * Notifies accessibility services that some view's important for
6809 * accessibility state has changed. Note that such notifications
6810 * are made at most once every
6811 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6812 * to avoid unnecessary load to the system. Also once a view has
6813 * made a notifucation this method is a NOP until the notification has
6814 * been sent to clients.
6815 *
6816 * @hide
6817 *
6818 * TODO: Makse sure this method is called for any view state change
6819 * that is interesting for accessilility purposes.
6820 */
6821 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006822 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6823 return;
6824 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006825 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6826 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006827 if (mParent != null) {
6828 mParent.childAccessibilityStateChanged(this);
6829 }
6830 }
6831 }
6832
6833 /**
6834 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006835 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006836 *
6837 * @hide
6838 */
6839 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006840 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006841 }
6842
6843 /**
6844 * Performs the specified accessibility action on the view. For
6845 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006846 * <p>
6847 * If an {@link AccessibilityDelegate} has been specified via calling
6848 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6849 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6850 * is responsible for handling this call.
6851 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006852 *
6853 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006854 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006855 * @return Whether the action was performed.
6856 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006857 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006858 if (mAccessibilityDelegate != null) {
6859 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6860 } else {
6861 return performAccessibilityActionInternal(action, arguments);
6862 }
6863 }
6864
6865 /**
6866 * @see #performAccessibilityAction(int, Bundle)
6867 *
6868 * Note: Called from the default {@link AccessibilityDelegate}.
6869 */
6870 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006871 switch (action) {
6872 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006873 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006874 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006875 }
6876 } break;
6877 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6878 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006879 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006880 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006881 } break;
6882 case AccessibilityNodeInfo.ACTION_FOCUS: {
6883 if (!hasFocus()) {
6884 // Get out of touch mode since accessibility
6885 // wants to move focus around.
6886 getViewRootImpl().ensureTouchMode(false);
6887 return requestFocus();
6888 }
6889 } break;
6890 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6891 if (hasFocus()) {
6892 clearFocus();
6893 return !isFocused();
6894 }
6895 } break;
6896 case AccessibilityNodeInfo.ACTION_SELECT: {
6897 if (!isSelected()) {
6898 setSelected(true);
6899 return isSelected();
6900 }
6901 } break;
6902 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6903 if (isSelected()) {
6904 setSelected(false);
6905 return !isSelected();
6906 }
6907 } break;
6908 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006909 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006910 return requestAccessibilityFocus();
6911 }
6912 } break;
6913 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6914 if (isAccessibilityFocused()) {
6915 clearAccessibilityFocus();
6916 return true;
6917 }
6918 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006919 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6920 if (arguments != null) {
6921 final int granularity = arguments.getInt(
6922 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6923 return nextAtGranularity(granularity);
6924 }
6925 } break;
6926 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6927 if (arguments != null) {
6928 final int granularity = arguments.getInt(
6929 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6930 return previousAtGranularity(granularity);
6931 }
6932 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006933 }
6934 return false;
6935 }
6936
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006937 private boolean nextAtGranularity(int granularity) {
6938 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006939 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006940 return false;
6941 }
6942 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6943 if (iterator == null) {
6944 return false;
6945 }
6946 final int current = getAccessibilityCursorPosition();
6947 final int[] range = iterator.following(current);
6948 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006949 return false;
6950 }
6951 final int start = range[0];
6952 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006953 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006954 sendViewTextTraversedAtGranularityEvent(
6955 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6956 granularity, start, end);
6957 return true;
6958 }
6959
6960 private boolean previousAtGranularity(int granularity) {
6961 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006962 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006963 return false;
6964 }
6965 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6966 if (iterator == null) {
6967 return false;
6968 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006969 int current = getAccessibilityCursorPosition();
6970 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6971 current = text.length();
6972 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6973 // When traversing by character we always put the cursor after the character
6974 // to ease edit and have to compensate before asking the for previous segment.
6975 current--;
6976 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006977 final int[] range = iterator.preceding(current);
6978 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006979 return false;
6980 }
6981 final int start = range[0];
6982 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006983 // Always put the cursor after the character to ease edit.
6984 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6985 setAccessibilityCursorPosition(end);
6986 } else {
6987 setAccessibilityCursorPosition(start);
6988 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006989 sendViewTextTraversedAtGranularityEvent(
6990 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6991 granularity, start, end);
6992 return true;
6993 }
6994
6995 /**
6996 * Gets the text reported for accessibility purposes.
6997 *
6998 * @return The accessibility text.
6999 *
7000 * @hide
7001 */
7002 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007003 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007004 }
7005
7006 /**
7007 * @hide
7008 */
7009 public int getAccessibilityCursorPosition() {
7010 return mAccessibilityCursorPosition;
7011 }
7012
7013 /**
7014 * @hide
7015 */
7016 public void setAccessibilityCursorPosition(int position) {
7017 mAccessibilityCursorPosition = position;
7018 }
7019
7020 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7021 int fromIndex, int toIndex) {
7022 if (mParent == null) {
7023 return;
7024 }
7025 AccessibilityEvent event = AccessibilityEvent.obtain(
7026 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7027 onInitializeAccessibilityEvent(event);
7028 onPopulateAccessibilityEvent(event);
7029 event.setFromIndex(fromIndex);
7030 event.setToIndex(toIndex);
7031 event.setAction(action);
7032 event.setMovementGranularity(granularity);
7033 mParent.requestSendAccessibilityEvent(this, event);
7034 }
7035
7036 /**
7037 * @hide
7038 */
7039 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7040 switch (granularity) {
7041 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7042 CharSequence text = getIterableTextForAccessibility();
7043 if (text != null && text.length() > 0) {
7044 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007045 CharacterTextSegmentIterator.getInstance(
7046 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007047 iterator.initialize(text.toString());
7048 return iterator;
7049 }
7050 } break;
7051 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7052 CharSequence text = getIterableTextForAccessibility();
7053 if (text != null && text.length() > 0) {
7054 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007055 WordTextSegmentIterator.getInstance(
7056 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007057 iterator.initialize(text.toString());
7058 return iterator;
7059 }
7060 } break;
7061 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7062 CharSequence text = getIterableTextForAccessibility();
7063 if (text != null && text.length() > 0) {
7064 ParagraphTextSegmentIterator iterator =
7065 ParagraphTextSegmentIterator.getInstance();
7066 iterator.initialize(text.toString());
7067 return iterator;
7068 }
7069 } break;
7070 }
7071 return null;
7072 }
7073
Svetoslav Ganov42138042012-03-20 11:51:39 -07007074 /**
Romain Guya440b002010-02-24 15:57:54 -08007075 * @hide
7076 */
7077 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007078 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007079 clearDisplayList();
7080
Romain Guya440b002010-02-24 15:57:54 -08007081 onStartTemporaryDetach();
7082 }
7083
7084 /**
7085 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7087 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007088 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 */
7090 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007091 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007092 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007093 }
7094
7095 /**
7096 * @hide
7097 */
7098 public void dispatchFinishTemporaryDetach() {
7099 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100 }
Romain Guy8506ab42009-06-11 17:35:47 -07007101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 /**
7103 * Called after {@link #onStartTemporaryDetach} when the container is done
7104 * changing the view.
7105 */
7106 public void onFinishTemporaryDetach() {
7107 }
Romain Guy8506ab42009-06-11 17:35:47 -07007108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007110 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7111 * for this view's window. Returns null if the view is not currently attached
7112 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007113 * just use the standard high-level event callbacks like
7114 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007115 */
7116 public KeyEvent.DispatcherState getKeyDispatcherState() {
7117 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7118 }
Joe Malin32736f02011-01-19 16:14:20 -08007119
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 * Dispatch a key event before it is processed by any input method
7122 * associated with the view hierarchy. This can be used to intercept
7123 * key events in special situations before the IME consumes them; a
7124 * typical example would be handling the BACK key to update the application's
7125 * UI instead of allowing the IME to see it and close itself.
7126 *
7127 * @param event The key event to be dispatched.
7128 * @return True if the event was handled, false otherwise.
7129 */
7130 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7131 return onKeyPreIme(event.getKeyCode(), event);
7132 }
7133
7134 /**
7135 * Dispatch a key event to the next view on the focus path. This path runs
7136 * from the top of the view tree down to the currently focused view. If this
7137 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7138 * the next node down the focus path. This method also fires any key
7139 * listeners.
7140 *
7141 * @param event The key event to be dispatched.
7142 * @return True if the event was handled, false otherwise.
7143 */
7144 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007145 if (mInputEventConsistencyVerifier != null) {
7146 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007148
Jeff Brown21bc5c92011-02-28 18:27:14 -08007149 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007150 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007151 ListenerInfo li = mListenerInfo;
7152 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7153 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007154 return true;
7155 }
7156
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007157 if (event.dispatch(this, mAttachInfo != null
7158 ? mAttachInfo.mKeyDispatchState : null, this)) {
7159 return true;
7160 }
7161
7162 if (mInputEventConsistencyVerifier != null) {
7163 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7164 }
7165 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 }
7167
7168 /**
7169 * Dispatches a key shortcut event.
7170 *
7171 * @param event The key event to be dispatched.
7172 * @return True if the event was handled by the view, false otherwise.
7173 */
7174 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7175 return onKeyShortcut(event.getKeyCode(), event);
7176 }
7177
7178 /**
7179 * Pass the touch screen motion event down to the target view, or this
7180 * view if it is the target.
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 dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007186 if (mInputEventConsistencyVerifier != null) {
7187 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7188 }
7189
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007190 if (onFilterTouchEventForSecurity(event)) {
7191 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007192 ListenerInfo li = mListenerInfo;
7193 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7194 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007195 return true;
7196 }
7197
7198 if (onTouchEvent(event)) {
7199 return true;
7200 }
Jeff Brown85a31762010-09-01 17:01:00 -07007201 }
7202
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007203 if (mInputEventConsistencyVerifier != null) {
7204 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007206 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
7208
7209 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007210 * Filter the touch event to apply security policies.
7211 *
7212 * @param event The motion event to be filtered.
7213 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007214 *
Jeff Brown85a31762010-09-01 17:01:00 -07007215 * @see #getFilterTouchesWhenObscured
7216 */
7217 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007218 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007219 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7220 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7221 // Window is obscured, drop this touch.
7222 return false;
7223 }
7224 return true;
7225 }
7226
7227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 * Pass a trackball motion event down to the focused view.
7229 *
7230 * @param event The motion event to be dispatched.
7231 * @return True if the event was handled by the view, false otherwise.
7232 */
7233 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007234 if (mInputEventConsistencyVerifier != null) {
7235 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7236 }
7237
Romain Guy02ccac62011-06-24 13:20:23 -07007238 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 }
7240
7241 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007242 * Dispatch a generic motion event.
7243 * <p>
7244 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7245 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007246 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007247 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007248 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007249 *
7250 * @param event The motion event to be dispatched.
7251 * @return True if the event was handled by the view, false otherwise.
7252 */
7253 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007254 if (mInputEventConsistencyVerifier != null) {
7255 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7256 }
7257
Jeff Browna032cc02011-03-07 16:56:21 -08007258 final int source = event.getSource();
7259 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7260 final int action = event.getAction();
7261 if (action == MotionEvent.ACTION_HOVER_ENTER
7262 || action == MotionEvent.ACTION_HOVER_MOVE
7263 || action == MotionEvent.ACTION_HOVER_EXIT) {
7264 if (dispatchHoverEvent(event)) {
7265 return true;
7266 }
7267 } else if (dispatchGenericPointerEvent(event)) {
7268 return true;
7269 }
7270 } else if (dispatchGenericFocusedEvent(event)) {
7271 return true;
7272 }
7273
Jeff Brown10b62902011-06-20 16:40:37 -07007274 if (dispatchGenericMotionEventInternal(event)) {
7275 return true;
7276 }
7277
7278 if (mInputEventConsistencyVerifier != null) {
7279 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7280 }
7281 return false;
7282 }
7283
7284 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007285 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007286 ListenerInfo li = mListenerInfo;
7287 if (li != null && li.mOnGenericMotionListener != null
7288 && (mViewFlags & ENABLED_MASK) == ENABLED
7289 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007290 return true;
7291 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007292
7293 if (onGenericMotionEvent(event)) {
7294 return true;
7295 }
7296
7297 if (mInputEventConsistencyVerifier != null) {
7298 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7299 }
7300 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007301 }
7302
7303 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007304 * Dispatch a hover event.
7305 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007306 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007307 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007308 * </p>
7309 *
7310 * @param event The motion event to be dispatched.
7311 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007312 */
7313 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007314 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007315 ListenerInfo li = mListenerInfo;
7316 if (li != null && li.mOnHoverListener != null
7317 && (mViewFlags & ENABLED_MASK) == ENABLED
7318 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007319 return true;
7320 }
7321
Jeff Browna032cc02011-03-07 16:56:21 -08007322 return onHoverEvent(event);
7323 }
7324
7325 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007326 * Returns true if the view has a child to which it has recently sent
7327 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7328 * it does not have a hovered child, then it must be the innermost hovered view.
7329 * @hide
7330 */
7331 protected boolean hasHoveredChild() {
7332 return false;
7333 }
7334
7335 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007336 * Dispatch a generic motion event to the view under the first pointer.
7337 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007338 * Do not call this method directly.
7339 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007340 * </p>
7341 *
7342 * @param event The motion event to be dispatched.
7343 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007344 */
7345 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7346 return false;
7347 }
7348
7349 /**
7350 * Dispatch a generic motion event to the currently focused view.
7351 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007352 * Do not call this method directly.
7353 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007354 * </p>
7355 *
7356 * @param event The motion event to be dispatched.
7357 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007358 */
7359 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7360 return false;
7361 }
7362
7363 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007364 * Dispatch a pointer event.
7365 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007366 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7367 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7368 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007369 * and should not be expected to handle other pointing device features.
7370 * </p>
7371 *
7372 * @param event The motion event to be dispatched.
7373 * @return True if the event was handled by the view, false otherwise.
7374 * @hide
7375 */
7376 public final boolean dispatchPointerEvent(MotionEvent event) {
7377 if (event.isTouchEvent()) {
7378 return dispatchTouchEvent(event);
7379 } else {
7380 return dispatchGenericMotionEvent(event);
7381 }
7382 }
7383
7384 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 * Called when the window containing this view gains or loses window focus.
7386 * ViewGroups should override to route to their children.
7387 *
7388 * @param hasFocus True if the window containing this view now has focus,
7389 * false otherwise.
7390 */
7391 public void dispatchWindowFocusChanged(boolean hasFocus) {
7392 onWindowFocusChanged(hasFocus);
7393 }
7394
7395 /**
7396 * Called when the window containing this view gains or loses focus. Note
7397 * that this is separate from view focus: to receive key events, both
7398 * your view and its window must have focus. If a window is displayed
7399 * on top of yours that takes input focus, then your own window will lose
7400 * focus but the view focus will remain unchanged.
7401 *
7402 * @param hasWindowFocus True if the window containing this view now has
7403 * focus, false otherwise.
7404 */
7405 public void onWindowFocusChanged(boolean hasWindowFocus) {
7406 InputMethodManager imm = InputMethodManager.peekInstance();
7407 if (!hasWindowFocus) {
7408 if (isPressed()) {
7409 setPressed(false);
7410 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007411 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 imm.focusOut(this);
7413 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007414 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007415 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007416 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007417 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 imm.focusIn(this);
7419 }
7420 refreshDrawableState();
7421 }
7422
7423 /**
7424 * Returns true if this view is in a window that currently has window focus.
7425 * Note that this is not the same as the view itself having focus.
7426 *
7427 * @return True if this view is in a window that currently has window focus.
7428 */
7429 public boolean hasWindowFocus() {
7430 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7431 }
7432
7433 /**
Adam Powell326d8082009-12-09 15:10:07 -08007434 * Dispatch a view visibility change down the view hierarchy.
7435 * ViewGroups should override to route to their children.
7436 * @param changedView The view whose visibility changed. Could be 'this' or
7437 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007438 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7439 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007440 */
7441 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7442 onVisibilityChanged(changedView, visibility);
7443 }
7444
7445 /**
7446 * Called when the visibility of the view or an ancestor of the view is changed.
7447 * @param changedView The view whose visibility changed. Could be 'this' or
7448 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007449 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7450 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007451 */
7452 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007453 if (visibility == VISIBLE) {
7454 if (mAttachInfo != null) {
7455 initialAwakenScrollBars();
7456 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007457 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007458 }
7459 }
Adam Powell326d8082009-12-09 15:10:07 -08007460 }
7461
7462 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007463 * Dispatch a hint about whether this view is displayed. For instance, when
7464 * a View moves out of the screen, it might receives a display hint indicating
7465 * the view is not displayed. Applications should not <em>rely</em> on this hint
7466 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007467 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007468 * @param hint A hint about whether or not this view is displayed:
7469 * {@link #VISIBLE} or {@link #INVISIBLE}.
7470 */
7471 public void dispatchDisplayHint(int hint) {
7472 onDisplayHint(hint);
7473 }
7474
7475 /**
7476 * Gives this view a hint about whether is displayed or not. For instance, when
7477 * a View moves out of the screen, it might receives a display hint indicating
7478 * the view is not displayed. Applications should not <em>rely</em> on this hint
7479 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007480 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007481 * @param hint A hint about whether or not this view is displayed:
7482 * {@link #VISIBLE} or {@link #INVISIBLE}.
7483 */
7484 protected void onDisplayHint(int hint) {
7485 }
7486
7487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 * Dispatch a window visibility change down the view hierarchy.
7489 * ViewGroups should override to route to their children.
7490 *
7491 * @param visibility The new visibility of the window.
7492 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007493 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 */
7495 public void dispatchWindowVisibilityChanged(int visibility) {
7496 onWindowVisibilityChanged(visibility);
7497 }
7498
7499 /**
7500 * Called when the window containing has change its visibility
7501 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7502 * that this tells you whether or not your window is being made visible
7503 * to the window manager; this does <em>not</em> tell you whether or not
7504 * your window is obscured by other windows on the screen, even if it
7505 * is itself visible.
7506 *
7507 * @param visibility The new visibility of the window.
7508 */
7509 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007510 if (visibility == VISIBLE) {
7511 initialAwakenScrollBars();
7512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 }
7514
7515 /**
7516 * Returns the current visibility of the window this view is attached to
7517 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7518 *
7519 * @return Returns the current visibility of the view's window.
7520 */
7521 public int getWindowVisibility() {
7522 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7523 }
7524
7525 /**
7526 * Retrieve the overall visible display size in which the window this view is
7527 * attached to has been positioned in. This takes into account screen
7528 * decorations above the window, for both cases where the window itself
7529 * is being position inside of them or the window is being placed under
7530 * then and covered insets are used for the window to position its content
7531 * inside. In effect, this tells you the available area where content can
7532 * be placed and remain visible to users.
7533 *
7534 * <p>This function requires an IPC back to the window manager to retrieve
7535 * the requested information, so should not be used in performance critical
7536 * code like drawing.
7537 *
7538 * @param outRect Filled in with the visible display frame. If the view
7539 * is not attached to a window, this is simply the raw display size.
7540 */
7541 public void getWindowVisibleDisplayFrame(Rect outRect) {
7542 if (mAttachInfo != null) {
7543 try {
7544 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7545 } catch (RemoteException e) {
7546 return;
7547 }
7548 // XXX This is really broken, and probably all needs to be done
7549 // in the window manager, and we need to know more about whether
7550 // we want the area behind or in front of the IME.
7551 final Rect insets = mAttachInfo.mVisibleInsets;
7552 outRect.left += insets.left;
7553 outRect.top += insets.top;
7554 outRect.right -= insets.right;
7555 outRect.bottom -= insets.bottom;
7556 return;
7557 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007558 // The view is not attached to a display so we don't have a context.
7559 // Make a best guess about the display size.
7560 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007561 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
7563
7564 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007565 * Dispatch a notification about a resource configuration change down
7566 * the view hierarchy.
7567 * ViewGroups should override to route to their children.
7568 *
7569 * @param newConfig The new resource configuration.
7570 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007571 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007572 */
7573 public void dispatchConfigurationChanged(Configuration newConfig) {
7574 onConfigurationChanged(newConfig);
7575 }
7576
7577 /**
7578 * Called when the current configuration of the resources being used
7579 * by the application have changed. You can use this to decide when
7580 * to reload resources that can changed based on orientation and other
7581 * configuration characterstics. You only need to use this if you are
7582 * not relying on the normal {@link android.app.Activity} mechanism of
7583 * recreating the activity instance upon a configuration change.
7584 *
7585 * @param newConfig The new resource configuration.
7586 */
7587 protected void onConfigurationChanged(Configuration newConfig) {
7588 }
7589
7590 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 * Private function to aggregate all per-view attributes in to the view
7592 * root.
7593 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007594 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7595 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 }
7597
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007598 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7599 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007600 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007601 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007602 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007603 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007604 ListenerInfo li = mListenerInfo;
7605 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007606 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007608 }
7609 }
7610
7611 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007612 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007613 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007614 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7615 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 ai.mRecomputeGlobalAttributes = true;
7617 }
7618 }
7619 }
7620
7621 /**
7622 * Returns whether the device is currently in touch mode. Touch mode is entered
7623 * once the user begins interacting with the device by touch, and affects various
7624 * things like whether focus is always visible to the user.
7625 *
7626 * @return Whether the device is in touch mode.
7627 */
7628 @ViewDebug.ExportedProperty
7629 public boolean isInTouchMode() {
7630 if (mAttachInfo != null) {
7631 return mAttachInfo.mInTouchMode;
7632 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007633 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 }
7635 }
7636
7637 /**
7638 * Returns the context the view is running in, through which it can
7639 * access the current theme, resources, etc.
7640 *
7641 * @return The view's Context.
7642 */
7643 @ViewDebug.CapturedViewProperty
7644 public final Context getContext() {
7645 return mContext;
7646 }
7647
7648 /**
7649 * Handle a key event before it is processed by any input method
7650 * associated with the view hierarchy. This can be used to intercept
7651 * key events in special situations before the IME consumes them; a
7652 * typical example would be handling the BACK key to update the application's
7653 * UI instead of allowing the IME to see it and close itself.
7654 *
7655 * @param keyCode The value in event.getKeyCode().
7656 * @param event Description of the key event.
7657 * @return If you handled the event, return true. If you want to allow the
7658 * event to be handled by the next receiver, return false.
7659 */
7660 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7661 return false;
7662 }
7663
7664 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007665 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7666 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7668 * is released, if the view is enabled and clickable.
7669 *
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.
7673 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 * @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 onKeyDown(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 // Long clickable items don't necessarily have to be clickable
7688 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7689 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7690 (event.getRepeatCount() == 0)) {
7691 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007692 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 return true;
7694 }
7695 break;
7696 }
7697 }
7698 return result;
7699 }
7700
7701 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007702 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7703 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7704 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007705 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7706 * although some may elect to do so in some situations. Do not rely on this to
7707 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007708 */
7709 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7710 return false;
7711 }
7712
7713 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007714 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7715 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7717 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007718 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7719 * although some may elect to do so in some situations. Do not rely on this to
7720 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721 *
7722 * @param keyCode A key code that represents the button pressed, from
7723 * {@link android.view.KeyEvent}.
7724 * @param event The KeyEvent object that defines the button action.
7725 */
7726 public boolean onKeyUp(int keyCode, KeyEvent event) {
7727 boolean result = false;
7728
7729 switch (keyCode) {
7730 case KeyEvent.KEYCODE_DPAD_CENTER:
7731 case KeyEvent.KEYCODE_ENTER: {
7732 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7733 return true;
7734 }
7735 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7736 setPressed(false);
7737
7738 if (!mHasPerformedLongPress) {
7739 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007740 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741
7742 result = performClick();
7743 }
7744 }
7745 break;
7746 }
7747 }
7748 return result;
7749 }
7750
7751 /**
7752 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7753 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7754 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007755 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7756 * although some may elect to do so in some situations. Do not rely on this to
7757 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007758 *
7759 * @param keyCode A key code that represents the button pressed, from
7760 * {@link android.view.KeyEvent}.
7761 * @param repeatCount The number of times the action was made.
7762 * @param event The KeyEvent object that defines the button action.
7763 */
7764 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7765 return false;
7766 }
7767
7768 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007769 * Called on the focused view when a key shortcut event is not handled.
7770 * Override this method to implement local key shortcuts for the View.
7771 * Key shortcuts can also be implemented by setting the
7772 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 *
7774 * @param keyCode The value in event.getKeyCode().
7775 * @param event Description of the key event.
7776 * @return If you handled the event, return true. If you want to allow the
7777 * event to be handled by the next receiver, return false.
7778 */
7779 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7780 return false;
7781 }
7782
7783 /**
7784 * Check whether the called view is a text editor, in which case it
7785 * would make sense to automatically display a soft input window for
7786 * it. Subclasses should override this if they implement
7787 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007788 * a call on that method would return a non-null InputConnection, and
7789 * they are really a first-class editor that the user would normally
7790 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007791 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007792 * <p>The default implementation always returns false. This does
7793 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7794 * will not be called or the user can not otherwise perform edits on your
7795 * view; it is just a hint to the system that this is not the primary
7796 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007797 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 * @return Returns true if this view is a text editor, else false.
7799 */
7800 public boolean onCheckIsTextEditor() {
7801 return false;
7802 }
Romain Guy8506ab42009-06-11 17:35:47 -07007803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007804 /**
7805 * Create a new InputConnection for an InputMethod to interact
7806 * with the view. The default implementation returns null, since it doesn't
7807 * support input methods. You can override this to implement such support.
7808 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007809 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 * <p>When implementing this, you probably also want to implement
7811 * {@link #onCheckIsTextEditor()} to indicate you will return a
7812 * non-null InputConnection.
7813 *
7814 * @param outAttrs Fill in with attribute information about the connection.
7815 */
7816 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7817 return null;
7818 }
7819
7820 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007821 * Called by the {@link android.view.inputmethod.InputMethodManager}
7822 * when a view who is not the current
7823 * input connection target is trying to make a call on the manager. The
7824 * default implementation returns false; you can override this to return
7825 * true for certain views if you are performing InputConnection proxying
7826 * to them.
7827 * @param view The View that is making the InputMethodManager call.
7828 * @return Return true to allow the call, false to reject.
7829 */
7830 public boolean checkInputConnectionProxy(View view) {
7831 return false;
7832 }
Romain Guy8506ab42009-06-11 17:35:47 -07007833
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 * Show the context menu for this view. It is not safe to hold on to the
7836 * menu after returning from this method.
7837 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007838 * You should normally not overload this method. Overload
7839 * {@link #onCreateContextMenu(ContextMenu)} or define an
7840 * {@link OnCreateContextMenuListener} to add items to the context menu.
7841 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 * @param menu The context menu to populate
7843 */
7844 public void createContextMenu(ContextMenu menu) {
7845 ContextMenuInfo menuInfo = getContextMenuInfo();
7846
7847 // Sets the current menu info so all items added to menu will have
7848 // my extra info set.
7849 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7850
7851 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007852 ListenerInfo li = mListenerInfo;
7853 if (li != null && li.mOnCreateContextMenuListener != null) {
7854 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 }
7856
7857 // Clear the extra information so subsequent items that aren't mine don't
7858 // have my extra info.
7859 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7860
7861 if (mParent != null) {
7862 mParent.createContextMenu(menu);
7863 }
7864 }
7865
7866 /**
7867 * Views should implement this if they have extra information to associate
7868 * with the context menu. The return result is supplied as a parameter to
7869 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7870 * callback.
7871 *
7872 * @return Extra information about the item for which the context menu
7873 * should be shown. This information will vary across different
7874 * subclasses of View.
7875 */
7876 protected ContextMenuInfo getContextMenuInfo() {
7877 return null;
7878 }
7879
7880 /**
7881 * Views should implement this if the view itself is going to add items to
7882 * the context menu.
7883 *
7884 * @param menu the context menu to populate
7885 */
7886 protected void onCreateContextMenu(ContextMenu menu) {
7887 }
7888
7889 /**
7890 * Implement this method to handle trackball motion events. The
7891 * <em>relative</em> movement of the trackball since the last event
7892 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7893 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7894 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7895 * they will often be fractional values, representing the more fine-grained
7896 * movement information available from a trackball).
7897 *
7898 * @param event The motion event.
7899 * @return True if the event was handled, false otherwise.
7900 */
7901 public boolean onTrackballEvent(MotionEvent event) {
7902 return false;
7903 }
7904
7905 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007906 * Implement this method to handle generic motion events.
7907 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007908 * Generic motion events describe joystick movements, mouse hovers, track pad
7909 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007910 * {@link MotionEvent#getSource() source} of the motion event specifies
7911 * the class of input that was received. Implementations of this method
7912 * must examine the bits in the source before processing the event.
7913 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007914 * </p><p>
7915 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7916 * are delivered to the view under the pointer. All other generic motion events are
7917 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007918 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007919 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007920 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007921 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7922 * // process the joystick movement...
7923 * return true;
7924 * }
7925 * }
7926 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7927 * switch (event.getAction()) {
7928 * case MotionEvent.ACTION_HOVER_MOVE:
7929 * // process the mouse hover movement...
7930 * return true;
7931 * case MotionEvent.ACTION_SCROLL:
7932 * // process the scroll wheel movement...
7933 * return true;
7934 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007935 * }
7936 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007937 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007938 *
7939 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007940 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007941 */
7942 public boolean onGenericMotionEvent(MotionEvent event) {
7943 return false;
7944 }
7945
7946 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007947 * Implement this method to handle hover events.
7948 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007949 * This method is called whenever a pointer is hovering into, over, or out of the
7950 * bounds of a view and the view is not currently being touched.
7951 * Hover events are represented as pointer events with action
7952 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7953 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7954 * </p>
7955 * <ul>
7956 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7957 * when the pointer enters the bounds of the view.</li>
7958 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7959 * when the pointer has already entered the bounds of the view and has moved.</li>
7960 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7961 * when the pointer has exited the bounds of the view or when the pointer is
7962 * about to go down due to a button click, tap, or similar user action that
7963 * causes the view to be touched.</li>
7964 * </ul>
7965 * <p>
7966 * The view should implement this method to return true to indicate that it is
7967 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007968 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007969 * The default implementation calls {@link #setHovered} to update the hovered state
7970 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007971 * is enabled and is clickable. The default implementation also sends hover
7972 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007973 * </p>
7974 *
7975 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007976 * @return True if the view handled the hover event.
7977 *
7978 * @see #isHovered
7979 * @see #setHovered
7980 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007981 */
7982 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007983 // The root view may receive hover (or touch) events that are outside the bounds of
7984 // the window. This code ensures that we only send accessibility events for
7985 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007986 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007987 if (!mSendingHoverAccessibilityEvents) {
7988 if ((action == MotionEvent.ACTION_HOVER_ENTER
7989 || action == MotionEvent.ACTION_HOVER_MOVE)
7990 && !hasHoveredChild()
7991 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007992 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007993 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007994 }
7995 } else {
7996 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007997 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007998 && !pointInView(event.getX(), event.getY()))) {
7999 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008000 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008001 // If the window does not have input focus we take away accessibility
8002 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008003 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008004 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008005 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008006 }
Jeff Browna1b24182011-07-28 13:38:24 -07008007 }
8008
Jeff Brown87b7f802011-06-21 18:35:45 -07008009 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008010 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008011 case MotionEvent.ACTION_HOVER_ENTER:
8012 setHovered(true);
8013 break;
8014 case MotionEvent.ACTION_HOVER_EXIT:
8015 setHovered(false);
8016 break;
8017 }
Jeff Browna1b24182011-07-28 13:38:24 -07008018
8019 // Dispatch the event to onGenericMotionEvent before returning true.
8020 // This is to provide compatibility with existing applications that
8021 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8022 // break because of the new default handling for hoverable views
8023 // in onHoverEvent.
8024 // Note that onGenericMotionEvent will be called by default when
8025 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8026 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008027 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008028 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008029
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008030 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008031 }
8032
8033 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008034 * Returns true if the view should handle {@link #onHoverEvent}
8035 * by calling {@link #setHovered} to change its hovered state.
8036 *
8037 * @return True if the view is hoverable.
8038 */
8039 private boolean isHoverable() {
8040 final int viewFlags = mViewFlags;
8041 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8042 return false;
8043 }
8044
8045 return (viewFlags & CLICKABLE) == CLICKABLE
8046 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8047 }
8048
8049 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008050 * Returns true if the view is currently hovered.
8051 *
8052 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008053 *
8054 * @see #setHovered
8055 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008056 */
Jeff Brown10b62902011-06-20 16:40:37 -07008057 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008058 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008059 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008060 }
8061
8062 /**
8063 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008064 * <p>
8065 * Calling this method also changes the drawable state of the view. This
8066 * enables the view to react to hover by using different drawable resources
8067 * to change its appearance.
8068 * </p><p>
8069 * The {@link #onHoverChanged} method is called when the hovered state changes.
8070 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008071 *
8072 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008073 *
8074 * @see #isHovered
8075 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008076 */
8077 public void setHovered(boolean hovered) {
8078 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008079 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8080 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008081 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008082 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008083 }
8084 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008085 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8086 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008087 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008088 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008089 }
8090 }
8091 }
8092
8093 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008094 * Implement this method to handle hover state changes.
8095 * <p>
8096 * This method is called whenever the hover state changes as a result of a
8097 * call to {@link #setHovered}.
8098 * </p>
8099 *
8100 * @param hovered The current hover state, as returned by {@link #isHovered}.
8101 *
8102 * @see #isHovered
8103 * @see #setHovered
8104 */
8105 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008106 }
8107
8108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 * Implement this method to handle touch screen motion events.
8110 *
8111 * @param event The motion event.
8112 * @return True if the event was handled, false otherwise.
8113 */
8114 public boolean onTouchEvent(MotionEvent event) {
8115 final int viewFlags = mViewFlags;
8116
8117 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008118 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008119 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 // A disabled view that is clickable still consumes the touch
8122 // events, it just doesn't respond to them.
8123 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8124 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8125 }
8126
8127 if (mTouchDelegate != null) {
8128 if (mTouchDelegate.onTouchEvent(event)) {
8129 return true;
8130 }
8131 }
8132
8133 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8134 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8135 switch (event.getAction()) {
8136 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008137 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8138 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 // take focus if we don't have it already and we should in
8140 // touch mode.
8141 boolean focusTaken = false;
8142 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8143 focusTaken = requestFocus();
8144 }
8145
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008146 if (prepressed) {
8147 // The button is being released before we actually
8148 // showed it as pressed. Make it show the pressed
8149 // state now (before scheduling the click) to ensure
8150 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008151 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008152 }
Joe Malin32736f02011-01-19 16:14:20 -08008153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 if (!mHasPerformedLongPress) {
8155 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008156 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157
8158 // Only perform take click actions if we were in the pressed state
8159 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008160 // Use a Runnable and post this rather than calling
8161 // performClick directly. This lets other visual state
8162 // of the view update before click actions start.
8163 if (mPerformClick == null) {
8164 mPerformClick = new PerformClick();
8165 }
8166 if (!post(mPerformClick)) {
8167 performClick();
8168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 }
8170 }
8171
8172 if (mUnsetPressedState == null) {
8173 mUnsetPressedState = new UnsetPressedState();
8174 }
8175
Adam Powelle14579b2009-12-16 18:39:52 -08008176 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008177 postDelayed(mUnsetPressedState,
8178 ViewConfiguration.getPressedStateDuration());
8179 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 // If the post failed, unpress right now
8181 mUnsetPressedState.run();
8182 }
Adam Powelle14579b2009-12-16 18:39:52 -08008183 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 }
8185 break;
8186
8187 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008188 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008189
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008190 if (performButtonActionOnTouchDown(event)) {
8191 break;
8192 }
8193
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008194 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008195 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008196
8197 // For views inside a scrolling container, delay the pressed feedback for
8198 // a short period in case this is a scroll.
8199 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008200 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008201 if (mPendingCheckForTap == null) {
8202 mPendingCheckForTap = new CheckForTap();
8203 }
8204 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8205 } else {
8206 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008207 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008208 checkForLongClick(0);
8209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 break;
8211
8212 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008213 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008214 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008215 break;
8216
8217 case MotionEvent.ACTION_MOVE:
8218 final int x = (int) event.getX();
8219 final int y = (int) event.getY();
8220
8221 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008222 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008224 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008225 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008226 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008227 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228
Adam Powell4d6f0662012-02-21 15:11:11 -08008229 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008231 }
8232 break;
8233 }
8234 return true;
8235 }
8236
8237 return false;
8238 }
8239
8240 /**
Adam Powell10298662011-08-14 18:26:30 -07008241 * @hide
8242 */
8243 public boolean isInScrollingContainer() {
8244 ViewParent p = getParent();
8245 while (p != null && p instanceof ViewGroup) {
8246 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8247 return true;
8248 }
8249 p = p.getParent();
8250 }
8251 return false;
8252 }
8253
8254 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008255 * Remove the longpress detection timer.
8256 */
8257 private void removeLongPressCallback() {
8258 if (mPendingCheckForLongPress != null) {
8259 removeCallbacks(mPendingCheckForLongPress);
8260 }
8261 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008262
8263 /**
8264 * Remove the pending click action
8265 */
8266 private void removePerformClickCallback() {
8267 if (mPerformClick != null) {
8268 removeCallbacks(mPerformClick);
8269 }
8270 }
8271
Adam Powelle14579b2009-12-16 18:39:52 -08008272 /**
Romain Guya440b002010-02-24 15:57:54 -08008273 * Remove the prepress detection timer.
8274 */
8275 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008276 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008277 setPressed(false);
8278 removeCallbacks(mUnsetPressedState);
8279 }
8280 }
8281
8282 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008283 * Remove the tap detection timer.
8284 */
8285 private void removeTapCallback() {
8286 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008287 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008288 removeCallbacks(mPendingCheckForTap);
8289 }
8290 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008291
8292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008293 * Cancels a pending long press. Your subclass can use this if you
8294 * want the context menu to come up if the user presses and holds
8295 * at the same place, but you don't want it to come up if they press
8296 * and then move around enough to cause scrolling.
8297 */
8298 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008299 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008300
8301 /*
8302 * The prepressed state handled by the tap callback is a display
8303 * construct, but the tap callback will post a long press callback
8304 * less its own timeout. Remove it here.
8305 */
8306 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008307 }
8308
8309 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008310 * Remove the pending callback for sending a
8311 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8312 */
8313 private void removeSendViewScrolledAccessibilityEventCallback() {
8314 if (mSendViewScrolledAccessibilityEvent != null) {
8315 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008316 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008317 }
8318 }
8319
8320 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 * Sets the TouchDelegate for this View.
8322 */
8323 public void setTouchDelegate(TouchDelegate delegate) {
8324 mTouchDelegate = delegate;
8325 }
8326
8327 /**
8328 * Gets the TouchDelegate for this View.
8329 */
8330 public TouchDelegate getTouchDelegate() {
8331 return mTouchDelegate;
8332 }
8333
8334 /**
8335 * Set flags controlling behavior of this view.
8336 *
8337 * @param flags Constant indicating the value which should be set
8338 * @param mask Constant indicating the bit range that should be changed
8339 */
8340 void setFlags(int flags, int mask) {
8341 int old = mViewFlags;
8342 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8343
8344 int changed = mViewFlags ^ old;
8345 if (changed == 0) {
8346 return;
8347 }
8348 int privateFlags = mPrivateFlags;
8349
8350 /* Check if the FOCUSABLE bit has changed */
8351 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008352 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008354 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 /* Give up focus if we are no longer focusable */
8356 clearFocus();
8357 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008358 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008359 /*
8360 * Tell the view system that we are now available to take focus
8361 * if no one else already has it.
8362 */
8363 if (mParent != null) mParent.focusableViewAvailable(this);
8364 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008365 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8366 notifyAccessibilityStateChanged();
8367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008368 }
8369
8370 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8371 if ((changed & VISIBILITY_MASK) != 0) {
8372 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008373 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008374 * it was not visible. Marking it drawn ensures that the invalidation will
8375 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008377 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008378 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379
8380 needGlobalAttributesUpdate(true);
8381
8382 // a view becoming visible is worth notifying the parent
8383 // about in case nothing has focus. even if this specific view
8384 // isn't focusable, it may contain something that is, so let
8385 // the root view try to give this focus if nothing else does.
8386 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8387 mParent.focusableViewAvailable(this);
8388 }
8389 }
8390 }
8391
8392 /* Check if the GONE bit has changed */
8393 if ((changed & GONE) != 0) {
8394 needGlobalAttributesUpdate(false);
8395 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396
Romain Guyecd80ee2009-12-03 17:13:02 -08008397 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8398 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008399 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008400 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008401 if (mParent instanceof View) {
8402 // GONE views noop invalidation, so invalidate the parent
8403 ((View) mParent).invalidate(true);
8404 }
8405 // Mark the view drawn to ensure that it gets invalidated properly the next
8406 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008407 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 }
8409 if (mAttachInfo != null) {
8410 mAttachInfo.mViewVisibilityChanged = true;
8411 }
8412 }
8413
8414 /* Check if the VISIBLE bit has changed */
8415 if ((changed & INVISIBLE) != 0) {
8416 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008417 /*
8418 * If this view is becoming invisible, set the DRAWN flag so that
8419 * the next invalidate() will not be skipped.
8420 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008421 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422
8423 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008424 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 if (getRootView() != this) {
8426 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008427 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 }
8429 }
8430 if (mAttachInfo != null) {
8431 mAttachInfo.mViewVisibilityChanged = true;
8432 }
8433 }
8434
Adam Powell326d8082009-12-09 15:10:07 -08008435 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008436 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008437 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8438 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008439 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008440 } else if (mParent != null) {
8441 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008442 }
Adam Powell326d8082009-12-09 15:10:07 -08008443 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8444 }
8445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8447 destroyDrawingCache();
8448 }
8449
8450 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8451 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008452 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008453 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 }
8455
8456 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8457 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008458 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 }
8460
8461 if ((changed & DRAW_MASK) != 0) {
8462 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008463 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008464 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8465 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008467 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 }
8469 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008470 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 }
8472 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008473 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 }
8475
8476 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008477 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 mParent.recomputeViewAttributes(this);
8479 }
8480 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008481
8482 if (AccessibilityManager.getInstance(mContext).isEnabled()
8483 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8484 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8485 notifyAccessibilityStateChanged();
8486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008487 }
8488
8489 /**
8490 * Change the view's z order in the tree, so it's on top of other sibling
8491 * views
8492 */
8493 public void bringToFront() {
8494 if (mParent != null) {
8495 mParent.bringChildToFront(this);
8496 }
8497 }
8498
8499 /**
8500 * This is called in response to an internal scroll in this view (i.e., the
8501 * view scrolled its own contents). This is typically as a result of
8502 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8503 * called.
8504 *
8505 * @param l Current horizontal scroll origin.
8506 * @param t Current vertical scroll origin.
8507 * @param oldl Previous horizontal scroll origin.
8508 * @param oldt Previous vertical scroll origin.
8509 */
8510 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008511 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8512 postSendViewScrolledAccessibilityEventCallback();
8513 }
8514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008515 mBackgroundSizeChanged = true;
8516
8517 final AttachInfo ai = mAttachInfo;
8518 if (ai != null) {
8519 ai.mViewScrollChanged = true;
8520 }
8521 }
8522
8523 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008524 * Interface definition for a callback to be invoked when the layout bounds of a view
8525 * changes due to layout processing.
8526 */
8527 public interface OnLayoutChangeListener {
8528 /**
8529 * Called when the focus state of a view has changed.
8530 *
8531 * @param v The view whose state has changed.
8532 * @param left The new value of the view's left property.
8533 * @param top The new value of the view's top property.
8534 * @param right The new value of the view's right property.
8535 * @param bottom The new value of the view's bottom property.
8536 * @param oldLeft The previous value of the view's left property.
8537 * @param oldTop The previous value of the view's top property.
8538 * @param oldRight The previous value of the view's right property.
8539 * @param oldBottom The previous value of the view's bottom property.
8540 */
8541 void onLayoutChange(View v, int left, int top, int right, int bottom,
8542 int oldLeft, int oldTop, int oldRight, int oldBottom);
8543 }
8544
8545 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 * This is called during layout when the size of this view has changed. If
8547 * you were just added to the view hierarchy, you're called with the old
8548 * values of 0.
8549 *
8550 * @param w Current width of this view.
8551 * @param h Current height of this view.
8552 * @param oldw Old width of this view.
8553 * @param oldh Old height of this view.
8554 */
8555 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8556 }
8557
8558 /**
8559 * Called by draw to draw the child views. This may be overridden
8560 * by derived classes to gain control just before its children are drawn
8561 * (but after its own view has been drawn).
8562 * @param canvas the canvas on which to draw the view
8563 */
8564 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 }
8567
8568 /**
8569 * Gets the parent of this view. Note that the parent is a
8570 * ViewParent and not necessarily a View.
8571 *
8572 * @return Parent of this view.
8573 */
8574 public final ViewParent getParent() {
8575 return mParent;
8576 }
8577
8578 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008579 * Set the horizontal scrolled position of your view. This will cause a call to
8580 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8581 * invalidated.
8582 * @param value the x position to scroll to
8583 */
8584 public void setScrollX(int value) {
8585 scrollTo(value, mScrollY);
8586 }
8587
8588 /**
8589 * Set the vertical scrolled position of your view. This will cause a call to
8590 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8591 * invalidated.
8592 * @param value the y position to scroll to
8593 */
8594 public void setScrollY(int value) {
8595 scrollTo(mScrollX, value);
8596 }
8597
8598 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008599 * Return the scrolled left position of this view. This is the left edge of
8600 * the displayed part of your view. You do not need to draw any pixels
8601 * farther left, since those are outside of the frame of your view on
8602 * screen.
8603 *
8604 * @return The left edge of the displayed part of your view, in pixels.
8605 */
8606 public final int getScrollX() {
8607 return mScrollX;
8608 }
8609
8610 /**
8611 * Return the scrolled top position of this view. This is the top edge of
8612 * the displayed part of your view. You do not need to draw any pixels above
8613 * it, since those are outside of the frame of your view on screen.
8614 *
8615 * @return The top edge of the displayed part of your view, in pixels.
8616 */
8617 public final int getScrollY() {
8618 return mScrollY;
8619 }
8620
8621 /**
8622 * Return the width of the your view.
8623 *
8624 * @return The width of your view, in pixels.
8625 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008626 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 public final int getWidth() {
8628 return mRight - mLeft;
8629 }
8630
8631 /**
8632 * Return the height of your view.
8633 *
8634 * @return The height of your view, in pixels.
8635 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008636 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 public final int getHeight() {
8638 return mBottom - mTop;
8639 }
8640
8641 /**
8642 * Return the visible drawing bounds of your view. Fills in the output
8643 * rectangle with the values from getScrollX(), getScrollY(),
8644 * getWidth(), and getHeight().
8645 *
8646 * @param outRect The (scrolled) drawing bounds of the view.
8647 */
8648 public void getDrawingRect(Rect outRect) {
8649 outRect.left = mScrollX;
8650 outRect.top = mScrollY;
8651 outRect.right = mScrollX + (mRight - mLeft);
8652 outRect.bottom = mScrollY + (mBottom - mTop);
8653 }
8654
8655 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008656 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8657 * raw width component (that is the result is masked by
8658 * {@link #MEASURED_SIZE_MASK}).
8659 *
8660 * @return The raw measured width of this view.
8661 */
8662 public final int getMeasuredWidth() {
8663 return mMeasuredWidth & MEASURED_SIZE_MASK;
8664 }
8665
8666 /**
8667 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008668 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008669 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008670 * This should be used during measurement and layout calculations only. Use
8671 * {@link #getWidth()} to see how wide a view is after layout.
8672 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008673 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008675 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 return mMeasuredWidth;
8677 }
8678
8679 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008680 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8681 * raw width component (that is the result is masked by
8682 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008684 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 */
8686 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008687 return mMeasuredHeight & MEASURED_SIZE_MASK;
8688 }
8689
8690 /**
8691 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008692 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008693 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8694 * This should be used during measurement and layout calculations only. Use
8695 * {@link #getHeight()} to see how wide a view is after layout.
8696 *
8697 * @return The measured width of this view as a bit mask.
8698 */
8699 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 return mMeasuredHeight;
8701 }
8702
8703 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008704 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8705 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8706 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8707 * and the height component is at the shifted bits
8708 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8709 */
8710 public final int getMeasuredState() {
8711 return (mMeasuredWidth&MEASURED_STATE_MASK)
8712 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8713 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8714 }
8715
8716 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008717 * The transform matrix of this view, which is calculated based on the current
8718 * roation, scale, and pivot properties.
8719 *
8720 * @see #getRotation()
8721 * @see #getScaleX()
8722 * @see #getScaleY()
8723 * @see #getPivotX()
8724 * @see #getPivotY()
8725 * @return The current transform matrix for the view
8726 */
8727 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008728 if (mTransformationInfo != null) {
8729 updateMatrix();
8730 return mTransformationInfo.mMatrix;
8731 }
8732 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008733 }
8734
8735 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008736 * Utility function to determine if the value is far enough away from zero to be
8737 * considered non-zero.
8738 * @param value A floating point value to check for zero-ness
8739 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8740 */
8741 private static boolean nonzero(float value) {
8742 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8743 }
8744
8745 /**
Jeff Brown86671742010-09-30 20:00:15 -07008746 * Returns true if the transform matrix is the identity matrix.
8747 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008748 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008749 * @return True if the transform matrix is the identity matrix, false otherwise.
8750 */
Jeff Brown86671742010-09-30 20:00:15 -07008751 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008752 if (mTransformationInfo != null) {
8753 updateMatrix();
8754 return mTransformationInfo.mMatrixIsIdentity;
8755 }
8756 return true;
8757 }
8758
8759 void ensureTransformationInfo() {
8760 if (mTransformationInfo == null) {
8761 mTransformationInfo = new TransformationInfo();
8762 }
Jeff Brown86671742010-09-30 20:00:15 -07008763 }
8764
8765 /**
8766 * Recomputes the transform matrix if necessary.
8767 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008768 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008769 final TransformationInfo info = mTransformationInfo;
8770 if (info == null) {
8771 return;
8772 }
8773 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008774 // transform-related properties have changed since the last time someone
8775 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008776
8777 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008778 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008779 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8780 info.mPrevWidth = mRight - mLeft;
8781 info.mPrevHeight = mBottom - mTop;
8782 info.mPivotX = info.mPrevWidth / 2f;
8783 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008784 }
8785 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008786 info.mMatrix.reset();
8787 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8788 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8789 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8790 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008791 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008792 if (info.mCamera == null) {
8793 info.mCamera = new Camera();
8794 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008795 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008796 info.mCamera.save();
8797 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8798 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8799 info.mCamera.getMatrix(info.matrix3D);
8800 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8801 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8802 info.mPivotY + info.mTranslationY);
8803 info.mMatrix.postConcat(info.matrix3D);
8804 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008805 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008806 info.mMatrixDirty = false;
8807 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8808 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008809 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008810 }
8811
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008812 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008813 * Utility method to retrieve the inverse of the current mMatrix property.
8814 * We cache the matrix to avoid recalculating it when transform properties
8815 * have not changed.
8816 *
8817 * @return The inverse of the current matrix of this view.
8818 */
Jeff Brown86671742010-09-30 20:00:15 -07008819 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008820 final TransformationInfo info = mTransformationInfo;
8821 if (info != null) {
8822 updateMatrix();
8823 if (info.mInverseMatrixDirty) {
8824 if (info.mInverseMatrix == null) {
8825 info.mInverseMatrix = new Matrix();
8826 }
8827 info.mMatrix.invert(info.mInverseMatrix);
8828 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008829 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008830 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008832 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008833 }
8834
8835 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008836 * Gets the distance along the Z axis from the camera to this view.
8837 *
8838 * @see #setCameraDistance(float)
8839 *
8840 * @return The distance along the Z axis.
8841 */
8842 public float getCameraDistance() {
8843 ensureTransformationInfo();
8844 final float dpi = mResources.getDisplayMetrics().densityDpi;
8845 final TransformationInfo info = mTransformationInfo;
8846 if (info.mCamera == null) {
8847 info.mCamera = new Camera();
8848 info.matrix3D = new Matrix();
8849 }
8850 return -(info.mCamera.getLocationZ() * dpi);
8851 }
8852
8853 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008854 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8855 * views are drawn) from the camera to this view. The camera's distance
8856 * affects 3D transformations, for instance rotations around the X and Y
8857 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008858 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008859 * use a camera distance that's greater than the height (X axis rotation) or
8860 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008861 *
Romain Guya5364ee2011-02-24 14:46:04 -08008862 * <p>The distance of the camera from the view plane can have an affect on the
8863 * perspective distortion of the view when it is rotated around the x or y axis.
8864 * For example, a large distance will result in a large viewing angle, and there
8865 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008866 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008867 * also result in some drawing artifacts if the rotated view ends up partially
8868 * behind the camera (which is why the recommendation is to use a distance at
8869 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008870 *
Romain Guya5364ee2011-02-24 14:46:04 -08008871 * <p>The distance is expressed in "depth pixels." The default distance depends
8872 * on the screen density. For instance, on a medium density display, the
8873 * default distance is 1280. On a high density display, the default distance
8874 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008875 *
Romain Guya5364ee2011-02-24 14:46:04 -08008876 * <p>If you want to specify a distance that leads to visually consistent
8877 * results across various densities, use the following formula:</p>
8878 * <pre>
8879 * float scale = context.getResources().getDisplayMetrics().density;
8880 * view.setCameraDistance(distance * scale);
8881 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008882 *
Romain Guya5364ee2011-02-24 14:46:04 -08008883 * <p>The density scale factor of a high density display is 1.5,
8884 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008885 *
Romain Guya5364ee2011-02-24 14:46:04 -08008886 * @param distance The distance in "depth pixels", if negative the opposite
8887 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008888 *
8889 * @see #setRotationX(float)
8890 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008891 */
8892 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008893 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008894
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008895 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008896 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 final TransformationInfo info = mTransformationInfo;
8898 if (info.mCamera == null) {
8899 info.mCamera = new Camera();
8900 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008901 }
8902
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008903 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8904 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008905
Chet Haase9d1992d2012-03-13 11:03:25 -07008906 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008907 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008908 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008909 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008910 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008911 // View was rejected last time it was drawn by its parent; this may have changed
8912 invalidateParentIfNeeded();
8913 }
Romain Guya5364ee2011-02-24 14:46:04 -08008914 }
8915
8916 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008917 * The degrees that the view is rotated around the pivot point.
8918 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008919 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008920 * @see #getPivotX()
8921 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008922 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008923 * @return The degrees of rotation.
8924 */
Chet Haasea5531132012-02-02 13:41:44 -08008925 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008926 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008927 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008928 }
8929
8930 /**
Chet Haase897247b2010-09-09 14:54:47 -07008931 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8932 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008933 *
8934 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008935 *
8936 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008937 * @see #getPivotX()
8938 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008939 * @see #setRotationX(float)
8940 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008941 *
8942 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008943 */
8944 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008945 ensureTransformationInfo();
8946 final TransformationInfo info = mTransformationInfo;
8947 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008949 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008950 info.mRotation = rotation;
8951 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008952 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008953 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008954 mDisplayList.setRotation(rotation);
8955 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008956 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008957 // View was rejected last time it was drawn by its parent; this may have changed
8958 invalidateParentIfNeeded();
8959 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008960 }
8961 }
8962
8963 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008964 * The degrees that the view is rotated around the vertical axis through the pivot point.
8965 *
8966 * @see #getPivotX()
8967 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008968 * @see #setRotationY(float)
8969 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008970 * @return The degrees of Y rotation.
8971 */
Chet Haasea5531132012-02-02 13:41:44 -08008972 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008973 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008974 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008975 }
8976
8977 /**
Chet Haase897247b2010-09-09 14:54:47 -07008978 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8979 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8980 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008981 *
Romain Guya5364ee2011-02-24 14:46:04 -08008982 * When rotating large views, it is recommended to adjust the camera distance
8983 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008984 *
8985 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008986 *
8987 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008988 * @see #getPivotX()
8989 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008990 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008991 * @see #setRotationX(float)
8992 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008993 *
8994 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008995 */
8996 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008997 ensureTransformationInfo();
8998 final TransformationInfo info = mTransformationInfo;
8999 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009000 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009001 info.mRotationY = rotationY;
9002 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009003 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009004 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009005 mDisplayList.setRotationY(rotationY);
9006 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009007 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009008 // View was rejected last time it was drawn by its parent; this may have changed
9009 invalidateParentIfNeeded();
9010 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009011 }
9012 }
9013
9014 /**
9015 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9016 *
9017 * @see #getPivotX()
9018 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009019 * @see #setRotationX(float)
9020 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009021 * @return The degrees of X rotation.
9022 */
Chet Haasea5531132012-02-02 13:41:44 -08009023 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009024 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009025 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009026 }
9027
9028 /**
Chet Haase897247b2010-09-09 14:54:47 -07009029 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9030 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9031 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009032 *
Romain Guya5364ee2011-02-24 14:46:04 -08009033 * When rotating large views, it is recommended to adjust the camera distance
9034 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009035 *
9036 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009037 *
9038 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009039 * @see #getPivotX()
9040 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009041 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009042 * @see #setRotationY(float)
9043 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009044 *
9045 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009046 */
9047 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009048 ensureTransformationInfo();
9049 final TransformationInfo info = mTransformationInfo;
9050 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009051 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009052 info.mRotationX = rotationX;
9053 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009054 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009055 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009056 mDisplayList.setRotationX(rotationX);
9057 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009058 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009059 // View was rejected last time it was drawn by its parent; this may have changed
9060 invalidateParentIfNeeded();
9061 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009062 }
9063 }
9064
9065 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009066 * The amount that the view is scaled in x around the pivot point, as a proportion of
9067 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9068 *
Joe Onorato93162322010-09-16 15:42:01 -04009069 * <p>By default, this is 1.0f.
9070 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009071 * @see #getPivotX()
9072 * @see #getPivotY()
9073 * @return The scaling factor.
9074 */
Chet Haasea5531132012-02-02 13:41:44 -08009075 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009076 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009078 }
9079
9080 /**
9081 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9082 * the view's unscaled width. A value of 1 means that no scaling is applied.
9083 *
9084 * @param scaleX The scaling factor.
9085 * @see #getPivotX()
9086 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009087 *
9088 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009089 */
9090 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009091 ensureTransformationInfo();
9092 final TransformationInfo info = mTransformationInfo;
9093 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009094 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009095 info.mScaleX = scaleX;
9096 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009097 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009098 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009099 mDisplayList.setScaleX(scaleX);
9100 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009101 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009102 // View was rejected last time it was drawn by its parent; this may have changed
9103 invalidateParentIfNeeded();
9104 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009105 }
9106 }
9107
9108 /**
9109 * The amount that the view is scaled in y around the pivot point, as a proportion of
9110 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9111 *
Joe Onorato93162322010-09-16 15:42:01 -04009112 * <p>By default, this is 1.0f.
9113 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009114 * @see #getPivotX()
9115 * @see #getPivotY()
9116 * @return The scaling factor.
9117 */
Chet Haasea5531132012-02-02 13:41:44 -08009118 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009119 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009120 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009121 }
9122
9123 /**
9124 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9125 * the view's unscaled width. A value of 1 means that no scaling is applied.
9126 *
9127 * @param scaleY The scaling factor.
9128 * @see #getPivotX()
9129 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009130 *
9131 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009132 */
9133 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009134 ensureTransformationInfo();
9135 final TransformationInfo info = mTransformationInfo;
9136 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009137 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009138 info.mScaleY = scaleY;
9139 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009140 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009141 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009142 mDisplayList.setScaleY(scaleY);
9143 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009144 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009145 // View was rejected last time it was drawn by its parent; this may have changed
9146 invalidateParentIfNeeded();
9147 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009148 }
9149 }
9150
9151 /**
9152 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9153 * and {@link #setScaleX(float) scaled}.
9154 *
9155 * @see #getRotation()
9156 * @see #getScaleX()
9157 * @see #getScaleY()
9158 * @see #getPivotY()
9159 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009160 *
9161 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009162 */
Chet Haasea5531132012-02-02 13:41:44 -08009163 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009164 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009165 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009166 }
9167
9168 /**
9169 * Sets the x location of the point around which the view is
9170 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009171 * By default, the pivot point is centered on the object.
9172 * Setting this property disables this behavior and causes the view to use only the
9173 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009174 *
9175 * @param pivotX The x location of the pivot point.
9176 * @see #getRotation()
9177 * @see #getScaleX()
9178 * @see #getScaleY()
9179 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009180 *
9181 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009182 */
9183 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009184 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009185 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009186 final TransformationInfo info = mTransformationInfo;
9187 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009188 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009189 info.mPivotX = pivotX;
9190 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009191 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009192 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009193 mDisplayList.setPivotX(pivotX);
9194 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009195 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009196 // View was rejected last time it was drawn by its parent; this may have changed
9197 invalidateParentIfNeeded();
9198 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 }
9200 }
9201
9202 /**
9203 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9204 * and {@link #setScaleY(float) scaled}.
9205 *
9206 * @see #getRotation()
9207 * @see #getScaleX()
9208 * @see #getScaleY()
9209 * @see #getPivotY()
9210 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009211 *
9212 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009213 */
Chet Haasea5531132012-02-02 13:41:44 -08009214 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009215 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009216 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009217 }
9218
9219 /**
9220 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009221 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9222 * Setting this property disables this behavior and causes the view to use only the
9223 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009224 *
9225 * @param pivotY The y location of the pivot point.
9226 * @see #getRotation()
9227 * @see #getScaleX()
9228 * @see #getScaleY()
9229 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009230 *
9231 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009232 */
9233 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009234 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009235 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009236 final TransformationInfo info = mTransformationInfo;
9237 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009238 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009239 info.mPivotY = pivotY;
9240 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009241 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009242 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009243 mDisplayList.setPivotY(pivotY);
9244 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009245 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009246 // View was rejected last time it was drawn by its parent; this may have changed
9247 invalidateParentIfNeeded();
9248 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009249 }
9250 }
9251
9252 /**
9253 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9254 * completely transparent and 1 means the view is completely opaque.
9255 *
Joe Onorato93162322010-09-16 15:42:01 -04009256 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009257 * @return The opacity of the view.
9258 */
Chet Haasea5531132012-02-02 13:41:44 -08009259 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009260 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009262 }
9263
9264 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009265 * Returns whether this View has content which overlaps. This function, intended to be
9266 * overridden by specific View types, is an optimization when alpha is set on a view. If
9267 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9268 * and then composited it into place, which can be expensive. If the view has no overlapping
9269 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9270 * An example of overlapping rendering is a TextView with a background image, such as a
9271 * Button. An example of non-overlapping rendering is a TextView with no background, or
9272 * an ImageView with only the foreground image. The default implementation returns true;
9273 * subclasses should override if they have cases which can be optimized.
9274 *
9275 * @return true if the content in this view might overlap, false otherwise.
9276 */
9277 public boolean hasOverlappingRendering() {
9278 return true;
9279 }
9280
9281 /**
Romain Guy171c5922011-01-06 10:04:23 -08009282 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9283 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009284 *
Romain Guy171c5922011-01-06 10:04:23 -08009285 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9286 * responsible for applying the opacity itself. Otherwise, calling this method is
9287 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009288 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009289 *
Chet Haasea5531132012-02-02 13:41:44 -08009290 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9291 * performance implications. It is generally best to use the alpha property sparingly and
9292 * transiently, as in the case of fading animations.</p>
9293 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009294 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009295 *
Joe Malin32736f02011-01-19 16:14:20 -08009296 * @see #setLayerType(int, android.graphics.Paint)
9297 *
Chet Haase73066682010-11-29 15:55:32 -08009298 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009299 */
9300 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009301 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009302 if (mTransformationInfo.mAlpha != alpha) {
9303 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009304 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009305 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009306 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009307 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009308 invalidate(true);
9309 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009310 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009311 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009312 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009313 mDisplayList.setAlpha(alpha);
9314 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009315 }
Chet Haaseed032702010-10-01 14:05:54 -07009316 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009317 }
9318
9319 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009320 * Faster version of setAlpha() which performs the same steps except there are
9321 * no calls to invalidate(). The caller of this function should perform proper invalidation
9322 * on the parent and this object. The return value indicates whether the subclass handles
9323 * alpha (the return value for onSetAlpha()).
9324 *
9325 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009326 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9327 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009328 */
9329 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009330 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009331 if (mTransformationInfo.mAlpha != alpha) {
9332 mTransformationInfo.mAlpha = alpha;
9333 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9334 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009335 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009336 return true;
9337 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009338 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009339 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009340 mDisplayList.setAlpha(alpha);
9341 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009342 }
Chet Haasea00f3862011-02-22 06:34:40 -08009343 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009344 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009345 }
9346
9347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009348 * Top position of this view relative to its parent.
9349 *
9350 * @return The top of this view, in pixels.
9351 */
9352 @ViewDebug.CapturedViewProperty
9353 public final int getTop() {
9354 return mTop;
9355 }
9356
9357 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009358 * Sets the top position of this view relative to its parent. This method is meant to be called
9359 * by the layout system and should not generally be called otherwise, because the property
9360 * may be changed at any time by the layout.
9361 *
9362 * @param top The top of this view, in pixels.
9363 */
9364 public final void setTop(int top) {
9365 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009366 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009367 final boolean matrixIsIdentity = mTransformationInfo == null
9368 || mTransformationInfo.mMatrixIsIdentity;
9369 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009370 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009371 int minTop;
9372 int yLoc;
9373 if (top < mTop) {
9374 minTop = top;
9375 yLoc = top - mTop;
9376 } else {
9377 minTop = mTop;
9378 yLoc = 0;
9379 }
Chet Haasee9140a72011-02-16 16:23:29 -08009380 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009381 }
9382 } else {
9383 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009384 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009385 }
9386
Chet Haaseed032702010-10-01 14:05:54 -07009387 int width = mRight - mLeft;
9388 int oldHeight = mBottom - mTop;
9389
Chet Haase21cd1382010-09-01 17:42:29 -07009390 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009391 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009392 mDisplayList.setTop(mTop);
9393 }
Chet Haase21cd1382010-09-01 17:42:29 -07009394
Chet Haaseed032702010-10-01 14:05:54 -07009395 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9396
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009397 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009398 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009399 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009400 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009401 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009402 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009403 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009404 }
Chet Haase55dbb652010-12-21 20:15:08 -08009405 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009406 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009407 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009408 // View was rejected last time it was drawn by its parent; this may have changed
9409 invalidateParentIfNeeded();
9410 }
Chet Haase21cd1382010-09-01 17:42:29 -07009411 }
9412 }
9413
9414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009415 * Bottom position of this view relative to its parent.
9416 *
9417 * @return The bottom of this view, in pixels.
9418 */
9419 @ViewDebug.CapturedViewProperty
9420 public final int getBottom() {
9421 return mBottom;
9422 }
9423
9424 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009425 * True if this view has changed since the last time being drawn.
9426 *
9427 * @return The dirty state of this view.
9428 */
9429 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009430 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009431 }
9432
9433 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009434 * Sets the bottom position of this view relative to its parent. This method is meant to be
9435 * called by the layout system and should not generally be called otherwise, because the
9436 * property may be changed at any time by the layout.
9437 *
9438 * @param bottom The bottom of this view, in pixels.
9439 */
9440 public final void setBottom(int bottom) {
9441 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009442 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009443 final boolean matrixIsIdentity = mTransformationInfo == null
9444 || mTransformationInfo.mMatrixIsIdentity;
9445 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009446 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009447 int maxBottom;
9448 if (bottom < mBottom) {
9449 maxBottom = mBottom;
9450 } else {
9451 maxBottom = bottom;
9452 }
Chet Haasee9140a72011-02-16 16:23:29 -08009453 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009454 }
9455 } else {
9456 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009457 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009458 }
9459
Chet Haaseed032702010-10-01 14:05:54 -07009460 int width = mRight - mLeft;
9461 int oldHeight = mBottom - mTop;
9462
Chet Haase21cd1382010-09-01 17:42:29 -07009463 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009464 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009465 mDisplayList.setBottom(mBottom);
9466 }
Chet Haase21cd1382010-09-01 17:42:29 -07009467
Chet Haaseed032702010-10-01 14:05:54 -07009468 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9469
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009470 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009471 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009472 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009473 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009474 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009475 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009476 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009477 }
Chet Haase55dbb652010-12-21 20:15:08 -08009478 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009479 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009480 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009481 // View was rejected last time it was drawn by its parent; this may have changed
9482 invalidateParentIfNeeded();
9483 }
Chet Haase21cd1382010-09-01 17:42:29 -07009484 }
9485 }
9486
9487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009488 * Left position of this view relative to its parent.
9489 *
9490 * @return The left edge of this view, in pixels.
9491 */
9492 @ViewDebug.CapturedViewProperty
9493 public final int getLeft() {
9494 return mLeft;
9495 }
9496
9497 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009498 * Sets the left position of this view relative to its parent. This method is meant to be called
9499 * by the layout system and should not generally be called otherwise, because the property
9500 * may be changed at any time by the layout.
9501 *
9502 * @param left The bottom of this view, in pixels.
9503 */
9504 public final void setLeft(int left) {
9505 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009506 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009507 final boolean matrixIsIdentity = mTransformationInfo == null
9508 || mTransformationInfo.mMatrixIsIdentity;
9509 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009510 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009511 int minLeft;
9512 int xLoc;
9513 if (left < mLeft) {
9514 minLeft = left;
9515 xLoc = left - mLeft;
9516 } else {
9517 minLeft = mLeft;
9518 xLoc = 0;
9519 }
Chet Haasee9140a72011-02-16 16:23:29 -08009520 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009521 }
9522 } else {
9523 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009524 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009525 }
9526
Chet Haaseed032702010-10-01 14:05:54 -07009527 int oldWidth = mRight - mLeft;
9528 int height = mBottom - mTop;
9529
Chet Haase21cd1382010-09-01 17:42:29 -07009530 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009531 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009532 mDisplayList.setLeft(left);
9533 }
Chet Haase21cd1382010-09-01 17:42:29 -07009534
Chet Haaseed032702010-10-01 14:05:54 -07009535 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9536
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009537 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009538 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009539 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009540 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009541 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009542 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009543 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009544 }
Chet Haase55dbb652010-12-21 20:15:08 -08009545 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009546 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009547 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009548 // View was rejected last time it was drawn by its parent; this may have changed
9549 invalidateParentIfNeeded();
9550 }
Chet Haase21cd1382010-09-01 17:42:29 -07009551 }
9552 }
9553
9554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555 * Right position of this view relative to its parent.
9556 *
9557 * @return The right edge of this view, in pixels.
9558 */
9559 @ViewDebug.CapturedViewProperty
9560 public final int getRight() {
9561 return mRight;
9562 }
9563
9564 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009565 * Sets the right position of this view relative to its parent. This method is meant to be called
9566 * by the layout system and should not generally be called otherwise, because the property
9567 * may be changed at any time by the layout.
9568 *
9569 * @param right The bottom of this view, in pixels.
9570 */
9571 public final void setRight(int right) {
9572 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009573 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009574 final boolean matrixIsIdentity = mTransformationInfo == null
9575 || mTransformationInfo.mMatrixIsIdentity;
9576 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009577 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009578 int maxRight;
9579 if (right < mRight) {
9580 maxRight = mRight;
9581 } else {
9582 maxRight = right;
9583 }
Chet Haasee9140a72011-02-16 16:23:29 -08009584 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009585 }
9586 } else {
9587 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009588 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009589 }
9590
Chet Haaseed032702010-10-01 14:05:54 -07009591 int oldWidth = mRight - mLeft;
9592 int height = mBottom - mTop;
9593
Chet Haase21cd1382010-09-01 17:42:29 -07009594 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009595 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009596 mDisplayList.setRight(mRight);
9597 }
Chet Haase21cd1382010-09-01 17:42:29 -07009598
Chet Haaseed032702010-10-01 14:05:54 -07009599 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9600
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009601 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009602 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009603 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009604 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009605 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009606 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009607 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009608 }
Chet Haase55dbb652010-12-21 20:15:08 -08009609 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009610 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009611 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009612 // View was rejected last time it was drawn by its parent; this may have changed
9613 invalidateParentIfNeeded();
9614 }
Chet Haase21cd1382010-09-01 17:42:29 -07009615 }
9616 }
9617
9618 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009619 * The visual x position of this view, in pixels. This is equivalent to the
9620 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009621 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009622 *
Chet Haasedf030d22010-07-30 17:22:38 -07009623 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009624 */
Chet Haasea5531132012-02-02 13:41:44 -08009625 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009626 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009627 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009628 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009629
Chet Haasedf030d22010-07-30 17:22:38 -07009630 /**
9631 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9632 * {@link #setTranslationX(float) translationX} property to be the difference between
9633 * the x value passed in and the current {@link #getLeft() left} property.
9634 *
9635 * @param x The visual x position of this view, in pixels.
9636 */
9637 public void setX(float x) {
9638 setTranslationX(x - mLeft);
9639 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009640
Chet Haasedf030d22010-07-30 17:22:38 -07009641 /**
9642 * The visual y position of this view, in pixels. This is equivalent to the
9643 * {@link #setTranslationY(float) translationY} property plus the current
9644 * {@link #getTop() top} property.
9645 *
9646 * @return The visual y position of this view, in pixels.
9647 */
Chet Haasea5531132012-02-02 13:41:44 -08009648 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009649 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009650 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009651 }
9652
9653 /**
9654 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9655 * {@link #setTranslationY(float) translationY} property to be the difference between
9656 * the y value passed in and the current {@link #getTop() top} property.
9657 *
9658 * @param y The visual y position of this view, in pixels.
9659 */
9660 public void setY(float y) {
9661 setTranslationY(y - mTop);
9662 }
9663
9664
9665 /**
9666 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9667 * This position is post-layout, in addition to wherever the object's
9668 * layout placed it.
9669 *
9670 * @return The horizontal position of this view relative to its left position, in pixels.
9671 */
Chet Haasea5531132012-02-02 13:41:44 -08009672 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009673 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009674 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009675 }
9676
9677 /**
9678 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9679 * This effectively positions the object post-layout, in addition to wherever the object's
9680 * layout placed it.
9681 *
9682 * @param translationX The horizontal position of this view relative to its left position,
9683 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009684 *
9685 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009686 */
9687 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009688 ensureTransformationInfo();
9689 final TransformationInfo info = mTransformationInfo;
9690 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009691 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009692 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009693 info.mTranslationX = translationX;
9694 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009695 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009696 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009697 mDisplayList.setTranslationX(translationX);
9698 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009699 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009700 // View was rejected last time it was drawn by its parent; this may have changed
9701 invalidateParentIfNeeded();
9702 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009703 }
9704 }
9705
9706 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009707 * The horizontal location of this view relative to its {@link #getTop() top} position.
9708 * This position is post-layout, in addition to wherever the object's
9709 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009710 *
Chet Haasedf030d22010-07-30 17:22:38 -07009711 * @return The vertical position of this view relative to its top position,
9712 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009713 */
Chet Haasea5531132012-02-02 13:41:44 -08009714 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009715 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009716 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009717 }
9718
9719 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009720 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9721 * This effectively positions the object post-layout, in addition to wherever the object's
9722 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009723 *
Chet Haasedf030d22010-07-30 17:22:38 -07009724 * @param translationY The vertical position of this view relative to its top position,
9725 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009726 *
9727 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009728 */
Chet Haasedf030d22010-07-30 17:22:38 -07009729 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009730 ensureTransformationInfo();
9731 final TransformationInfo info = mTransformationInfo;
9732 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009733 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009734 info.mTranslationY = translationY;
9735 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009736 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009737 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009738 mDisplayList.setTranslationY(translationY);
9739 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009740 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009741 // View was rejected last time it was drawn by its parent; this may have changed
9742 invalidateParentIfNeeded();
9743 }
Chet Haasedf030d22010-07-30 17:22:38 -07009744 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009745 }
9746
9747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 * Hit rectangle in parent's coordinates
9749 *
9750 * @param outRect The hit rectangle of the view.
9751 */
9752 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009753 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009754 final TransformationInfo info = mTransformationInfo;
9755 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009756 outRect.set(mLeft, mTop, mRight, mBottom);
9757 } else {
9758 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009759 tmpRect.set(-info.mPivotX, -info.mPivotY,
9760 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9761 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009762 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9763 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009764 }
9765 }
9766
9767 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009768 * Determines whether the given point, in local coordinates is inside the view.
9769 */
9770 /*package*/ final boolean pointInView(float localX, float localY) {
9771 return localX >= 0 && localX < (mRight - mLeft)
9772 && localY >= 0 && localY < (mBottom - mTop);
9773 }
9774
9775 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009776 * Utility method to determine whether the given point, in local coordinates,
9777 * is inside the view, where the area of the view is expanded by the slop factor.
9778 * This method is called while processing touch-move events to determine if the event
9779 * is still within the view.
9780 */
9781 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009782 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009783 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 }
9785
9786 /**
9787 * When a view has focus and the user navigates away from it, the next view is searched for
9788 * starting from the rectangle filled in by this method.
9789 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009790 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009791 * of the view. However, if your view maintains some idea of internal selection,
9792 * such as a cursor, or a selected row or column, you should override this method and
9793 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 *
9795 * @param r The rectangle to fill in, in this view's coordinates.
9796 */
9797 public void getFocusedRect(Rect r) {
9798 getDrawingRect(r);
9799 }
9800
9801 /**
9802 * If some part of this view is not clipped by any of its parents, then
9803 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009804 * coordinates (without taking possible View rotations into account), offset
9805 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9806 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009807 *
9808 * @param r If true is returned, r holds the global coordinates of the
9809 * visible portion of this view.
9810 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9811 * between this view and its root. globalOffet may be null.
9812 * @return true if r is non-empty (i.e. part of the view is visible at the
9813 * root level.
9814 */
9815 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9816 int width = mRight - mLeft;
9817 int height = mBottom - mTop;
9818 if (width > 0 && height > 0) {
9819 r.set(0, 0, width, height);
9820 if (globalOffset != null) {
9821 globalOffset.set(-mScrollX, -mScrollY);
9822 }
9823 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9824 }
9825 return false;
9826 }
9827
9828 public final boolean getGlobalVisibleRect(Rect r) {
9829 return getGlobalVisibleRect(r, null);
9830 }
9831
9832 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009833 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009834 if (getGlobalVisibleRect(r, offset)) {
9835 r.offset(-offset.x, -offset.y); // make r local
9836 return true;
9837 }
9838 return false;
9839 }
9840
9841 /**
9842 * Offset this view's vertical location by the specified number of pixels.
9843 *
9844 * @param offset the number of pixels to offset the view by
9845 */
9846 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009847 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009848 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009849 final boolean matrixIsIdentity = mTransformationInfo == null
9850 || mTransformationInfo.mMatrixIsIdentity;
9851 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009852 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009853 invalidateViewProperty(false, false);
9854 } else {
9855 final ViewParent p = mParent;
9856 if (p != null && mAttachInfo != null) {
9857 final Rect r = mAttachInfo.mTmpInvalRect;
9858 int minTop;
9859 int maxBottom;
9860 int yLoc;
9861 if (offset < 0) {
9862 minTop = mTop + offset;
9863 maxBottom = mBottom;
9864 yLoc = offset;
9865 } else {
9866 minTop = mTop;
9867 maxBottom = mBottom + offset;
9868 yLoc = 0;
9869 }
9870 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
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 mTop += offset;
9879 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009880 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009881 mDisplayList.offsetTopBottom(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 * Offset this view's horizontal location by the specified amount of pixels.
9894 *
9895 * @param offset the numer of pixels to offset the view by
9896 */
9897 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009898 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009899 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009900 final boolean matrixIsIdentity = mTransformationInfo == null
9901 || mTransformationInfo.mMatrixIsIdentity;
9902 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009903 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009904 invalidateViewProperty(false, false);
9905 } else {
9906 final ViewParent p = mParent;
9907 if (p != null && mAttachInfo != null) {
9908 final Rect r = mAttachInfo.mTmpInvalRect;
9909 int minLeft;
9910 int maxRight;
9911 if (offset < 0) {
9912 minLeft = mLeft + offset;
9913 maxRight = mRight;
9914 } else {
9915 minLeft = mLeft;
9916 maxRight = mRight + offset;
9917 }
9918 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9919 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009920 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009921 }
9922 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009923 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009924 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009925
Chet Haasec3aa3612010-06-17 08:50:37 -07009926 mLeft += offset;
9927 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009928 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009929 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009930 invalidateViewProperty(false, false);
9931 } else {
9932 if (!matrixIsIdentity) {
9933 invalidateViewProperty(false, true);
9934 }
9935 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009936 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 }
9939
9940 /**
9941 * Get the LayoutParams associated with this view. All views should have
9942 * layout parameters. These supply parameters to the <i>parent</i> of this
9943 * view specifying how it should be arranged. There are many subclasses of
9944 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9945 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009946 *
9947 * This method may return null if this View is not attached to a parent
9948 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9949 * was not invoked successfully. When a View is attached to a parent
9950 * ViewGroup, this method must not return null.
9951 *
9952 * @return The LayoutParams associated with this view, or null if no
9953 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009955 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 public ViewGroup.LayoutParams getLayoutParams() {
9957 return mLayoutParams;
9958 }
9959
9960 /**
9961 * Set the layout parameters associated with this view. These supply
9962 * parameters to the <i>parent</i> of this view specifying how it should be
9963 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9964 * correspond to the different subclasses of ViewGroup that are responsible
9965 * for arranging their children.
9966 *
Romain Guy01c174b2011-02-22 11:51:06 -08009967 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 */
9969 public void setLayoutParams(ViewGroup.LayoutParams params) {
9970 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009971 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 }
9973 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009974 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009975 if (mParent instanceof ViewGroup) {
9976 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 requestLayout();
9979 }
9980
9981 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009982 * Resolve the layout parameters depending on the resolved layout direction
9983 */
9984 private void resolveLayoutParams() {
9985 if (mLayoutParams != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07009986 mLayoutParams.onResolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009987 }
9988 }
9989
9990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991 * Set the scrolled position of your view. This will cause a call to
9992 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9993 * invalidated.
9994 * @param x the x position to scroll to
9995 * @param y the y position to scroll to
9996 */
9997 public void scrollTo(int x, int y) {
9998 if (mScrollX != x || mScrollY != y) {
9999 int oldX = mScrollX;
10000 int oldY = mScrollY;
10001 mScrollX = x;
10002 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010003 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010005 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010006 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010008 }
10009 }
10010
10011 /**
10012 * Move the scrolled position of your view. This will cause a call to
10013 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10014 * invalidated.
10015 * @param x the amount of pixels to scroll by horizontally
10016 * @param y the amount of pixels to scroll by vertically
10017 */
10018 public void scrollBy(int x, int y) {
10019 scrollTo(mScrollX + x, mScrollY + y);
10020 }
10021
10022 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010023 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10024 * animation to fade the scrollbars out after a default delay. If a subclass
10025 * provides animated scrolling, the start delay should equal the duration
10026 * of the scrolling animation.</p>
10027 *
10028 * <p>The animation starts only if at least one of the scrollbars is
10029 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10030 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10031 * this method returns true, and false otherwise. If the animation is
10032 * started, this method calls {@link #invalidate()}; in that case the
10033 * caller should not call {@link #invalidate()}.</p>
10034 *
10035 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010036 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010037 *
10038 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10039 * and {@link #scrollTo(int, int)}.</p>
10040 *
10041 * @return true if the animation is played, false otherwise
10042 *
10043 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010044 * @see #scrollBy(int, int)
10045 * @see #scrollTo(int, int)
10046 * @see #isHorizontalScrollBarEnabled()
10047 * @see #isVerticalScrollBarEnabled()
10048 * @see #setHorizontalScrollBarEnabled(boolean)
10049 * @see #setVerticalScrollBarEnabled(boolean)
10050 */
10051 protected boolean awakenScrollBars() {
10052 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010053 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010054 }
10055
10056 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010057 * Trigger the scrollbars to draw.
10058 * This method differs from awakenScrollBars() only in its default duration.
10059 * initialAwakenScrollBars() will show the scroll bars for longer than
10060 * usual to give the user more of a chance to notice them.
10061 *
10062 * @return true if the animation is played, false otherwise.
10063 */
10064 private boolean initialAwakenScrollBars() {
10065 return mScrollCache != null &&
10066 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10067 }
10068
10069 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010070 * <p>
10071 * Trigger the scrollbars to draw. When invoked this method starts an
10072 * animation to fade the scrollbars out after a fixed delay. If a subclass
10073 * provides animated scrolling, the start delay should equal the duration of
10074 * the scrolling animation.
10075 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010076 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010077 * <p>
10078 * The animation starts only if at least one of the scrollbars is enabled,
10079 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10080 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10081 * this method returns true, and false otherwise. If the animation is
10082 * started, this method calls {@link #invalidate()}; in that case the caller
10083 * should not call {@link #invalidate()}.
10084 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010085 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010086 * <p>
10087 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010088 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010089 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010090 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010091 * @param startDelay the delay, in milliseconds, after which the animation
10092 * should start; when the delay is 0, the animation starts
10093 * immediately
10094 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010095 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010096 * @see #scrollBy(int, int)
10097 * @see #scrollTo(int, int)
10098 * @see #isHorizontalScrollBarEnabled()
10099 * @see #isVerticalScrollBarEnabled()
10100 * @see #setHorizontalScrollBarEnabled(boolean)
10101 * @see #setVerticalScrollBarEnabled(boolean)
10102 */
10103 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010104 return awakenScrollBars(startDelay, true);
10105 }
Joe Malin32736f02011-01-19 16:14:20 -080010106
Mike Cleron290947b2009-09-29 18:34:32 -070010107 /**
10108 * <p>
10109 * Trigger the scrollbars to draw. When invoked this method starts an
10110 * animation to fade the scrollbars out after a fixed delay. If a subclass
10111 * provides animated scrolling, the start delay should equal the duration of
10112 * the scrolling animation.
10113 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010114 *
Mike Cleron290947b2009-09-29 18:34:32 -070010115 * <p>
10116 * The animation starts only if at least one of the scrollbars is enabled,
10117 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10118 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10119 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010120 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010121 * is set to true; in that case the caller
10122 * should not call {@link #invalidate()}.
10123 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010124 *
Mike Cleron290947b2009-09-29 18:34:32 -070010125 * <p>
10126 * This method should be invoked everytime a subclass directly updates the
10127 * scroll parameters.
10128 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010129 *
Mike Cleron290947b2009-09-29 18:34:32 -070010130 * @param startDelay the delay, in milliseconds, after which the animation
10131 * should start; when the delay is 0, the animation starts
10132 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010133 *
Mike Cleron290947b2009-09-29 18:34:32 -070010134 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010135 *
Mike Cleron290947b2009-09-29 18:34:32 -070010136 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010137 *
Mike Cleron290947b2009-09-29 18:34:32 -070010138 * @see #scrollBy(int, int)
10139 * @see #scrollTo(int, int)
10140 * @see #isHorizontalScrollBarEnabled()
10141 * @see #isVerticalScrollBarEnabled()
10142 * @see #setHorizontalScrollBarEnabled(boolean)
10143 * @see #setVerticalScrollBarEnabled(boolean)
10144 */
10145 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010146 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010147
Mike Cleronf116bf82009-09-27 19:14:12 -070010148 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10149 return false;
10150 }
10151
10152 if (scrollCache.scrollBar == null) {
10153 scrollCache.scrollBar = new ScrollBarDrawable();
10154 }
10155
10156 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10157
Mike Cleron290947b2009-09-29 18:34:32 -070010158 if (invalidate) {
10159 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010160 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010161 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010162
10163 if (scrollCache.state == ScrollabilityCache.OFF) {
10164 // FIXME: this is copied from WindowManagerService.
10165 // We should get this value from the system when it
10166 // is possible to do so.
10167 final int KEY_REPEAT_FIRST_DELAY = 750;
10168 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10169 }
10170
10171 // Tell mScrollCache when we should start fading. This may
10172 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010173 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010174 scrollCache.fadeStartTime = fadeStartTime;
10175 scrollCache.state = ScrollabilityCache.ON;
10176
10177 // Schedule our fader to run, unscheduling any old ones first
10178 if (mAttachInfo != null) {
10179 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10180 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10181 }
10182
10183 return true;
10184 }
10185
10186 return false;
10187 }
10188
10189 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010190 * Do not invalidate views which are not visible and which are not running an animation. They
10191 * will not get drawn and they should not set dirty flags as if they will be drawn
10192 */
10193 private boolean skipInvalidate() {
10194 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10195 (!(mParent instanceof ViewGroup) ||
10196 !((ViewGroup) mParent).isViewTransitioning(this));
10197 }
10198 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010199 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010200 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10201 * in the future. This must be called from a UI thread. To call from a non-UI
10202 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 *
10204 * WARNING: This method is destructive to dirty.
10205 * @param dirty the rectangle representing the bounds of the dirty region
10206 */
10207 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010208 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010209 return;
10210 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010211 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10212 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10213 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10214 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10215 mPrivateFlags |= PFLAG_INVALIDATED;
10216 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 final ViewParent p = mParent;
10218 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010219 //noinspection PointlessBooleanExpression,ConstantConditions
10220 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10221 if (p != null && ai != null && ai.mHardwareAccelerated) {
10222 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010223 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010224 p.invalidateChild(this, null);
10225 return;
10226 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010228 if (p != null && ai != null) {
10229 final int scrollX = mScrollX;
10230 final int scrollY = mScrollY;
10231 final Rect r = ai.mTmpInvalRect;
10232 r.set(dirty.left - scrollX, dirty.top - scrollY,
10233 dirty.right - scrollX, dirty.bottom - scrollY);
10234 mParent.invalidateChild(this, r);
10235 }
10236 }
10237 }
10238
10239 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010240 * 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 -080010241 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010242 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10243 * will be called at some point in the future. This must be called from
10244 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 * @param l the left position of the dirty region
10246 * @param t the top position of the dirty region
10247 * @param r the right position of the dirty region
10248 * @param b the bottom position of the dirty region
10249 */
10250 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010251 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010252 return;
10253 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010254 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10255 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10256 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10257 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10258 mPrivateFlags |= PFLAG_INVALIDATED;
10259 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010260 final ViewParent p = mParent;
10261 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010262 //noinspection PointlessBooleanExpression,ConstantConditions
10263 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10264 if (p != null && ai != null && ai.mHardwareAccelerated) {
10265 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010266 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010267 p.invalidateChild(this, null);
10268 return;
10269 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010270 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 if (p != null && ai != null && l < r && t < b) {
10272 final int scrollX = mScrollX;
10273 final int scrollY = mScrollY;
10274 final Rect tmpr = ai.mTmpInvalRect;
10275 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10276 p.invalidateChild(this, tmpr);
10277 }
10278 }
10279 }
10280
10281 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010282 * Invalidate the whole view. If the view is visible,
10283 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10284 * the future. This must be called from a UI thread. To call from a non-UI thread,
10285 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 */
10287 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010288 invalidate(true);
10289 }
Joe Malin32736f02011-01-19 16:14:20 -080010290
Chet Haaseed032702010-10-01 14:05:54 -070010291 /**
10292 * This is where the invalidate() work actually happens. A full invalidate()
10293 * causes the drawing cache to be invalidated, but this function can be called with
10294 * invalidateCache set to false to skip that invalidation step for cases that do not
10295 * need it (for example, a component that remains at the same dimensions with the same
10296 * content).
10297 *
10298 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10299 * well. This is usually true for a full invalidate, but may be set to false if the
10300 * View's contents or dimensions have not changed.
10301 */
Romain Guy849d0a32011-02-01 17:20:48 -080010302 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010303 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010304 return;
10305 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010306 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10307 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10308 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010309 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010310 mPrivateFlags &= ~PFLAG_DRAWN;
10311 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010312 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010313 mPrivateFlags |= PFLAG_INVALIDATED;
10314 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010317 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010318 //noinspection PointlessBooleanExpression,ConstantConditions
10319 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10320 if (p != null && ai != null && ai.mHardwareAccelerated) {
10321 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010322 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010323 p.invalidateChild(this, null);
10324 return;
10325 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010326 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 if (p != null && ai != null) {
10329 final Rect r = ai.mTmpInvalRect;
10330 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10331 // Don't call invalidate -- we don't want to internally scroll
10332 // our own bounds
10333 p.invalidateChild(this, r);
10334 }
10335 }
10336 }
10337
10338 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010339 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10340 * set any flags or handle all of the cases handled by the default invalidation methods.
10341 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10342 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10343 * walk up the hierarchy, transforming the dirty rect as necessary.
10344 *
10345 * The method also handles normal invalidation logic if display list properties are not
10346 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10347 * backup approach, to handle these cases used in the various property-setting methods.
10348 *
10349 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10350 * are not being used in this view
10351 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10352 * list properties are not being used in this view
10353 */
10354 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010355 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010356 if (invalidateParent) {
10357 invalidateParentCaches();
10358 }
10359 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010360 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010361 }
10362 invalidate(false);
10363 } else {
10364 final AttachInfo ai = mAttachInfo;
10365 final ViewParent p = mParent;
10366 if (p != null && ai != null) {
10367 final Rect r = ai.mTmpInvalRect;
10368 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10369 if (mParent instanceof ViewGroup) {
10370 ((ViewGroup) mParent).invalidateChildFast(this, r);
10371 } else {
10372 mParent.invalidateChild(this, r);
10373 }
10374 }
10375 }
10376 }
10377
10378 /**
10379 * Utility method to transform a given Rect by the current matrix of this view.
10380 */
10381 void transformRect(final Rect rect) {
10382 if (!getMatrix().isIdentity()) {
10383 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10384 boundingRect.set(rect);
10385 getMatrix().mapRect(boundingRect);
10386 rect.set((int) (boundingRect.left - 0.5f),
10387 (int) (boundingRect.top - 0.5f),
10388 (int) (boundingRect.right + 0.5f),
10389 (int) (boundingRect.bottom + 0.5f));
10390 }
10391 }
10392
10393 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010394 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010395 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10396 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010397 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10398 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010399 *
10400 * @hide
10401 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010402 protected void invalidateParentCaches() {
10403 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010404 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010405 }
10406 }
Joe Malin32736f02011-01-19 16:14:20 -080010407
Romain Guy0fd89bf2011-01-26 15:41:30 -080010408 /**
10409 * Used to indicate that the parent of this view should be invalidated. This functionality
10410 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10411 * which is necessary when various parent-managed properties of the view change, such as
10412 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10413 * an invalidation event to the parent.
10414 *
10415 * @hide
10416 */
10417 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010418 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010419 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010420 }
10421 }
10422
10423 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010424 * Indicates whether this View is opaque. An opaque View guarantees that it will
10425 * draw all the pixels overlapping its bounds using a fully opaque color.
10426 *
10427 * Subclasses of View should override this method whenever possible to indicate
10428 * whether an instance is opaque. Opaque Views are treated in a special way by
10429 * the View hierarchy, possibly allowing it to perform optimizations during
10430 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010431 *
Romain Guy24443ea2009-05-11 11:56:30 -070010432 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010433 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010434 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010435 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010436 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010437 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010438 }
10439
Adam Powell20232d02010-12-08 21:08:53 -080010440 /**
10441 * @hide
10442 */
10443 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010444 // Opaque if:
10445 // - Has a background
10446 // - Background is opaque
10447 // - Doesn't have scrollbars or scrollbars are inside overlay
10448
Philip Milne6c8ea062012-04-03 17:38:43 -070010449 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010450 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010451 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010452 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010453 }
10454
10455 final int flags = mViewFlags;
10456 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10457 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010458 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010459 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010460 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010461 }
10462 }
10463
10464 /**
10465 * @hide
10466 */
10467 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010468 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010469 }
10470
10471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010472 * @return A handler associated with the thread running the View. This
10473 * handler can be used to pump events in the UI events queue.
10474 */
10475 public Handler getHandler() {
10476 if (mAttachInfo != null) {
10477 return mAttachInfo.mHandler;
10478 }
10479 return null;
10480 }
10481
10482 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010483 * Gets the view root associated with the View.
10484 * @return The view root, or null if none.
10485 * @hide
10486 */
10487 public ViewRootImpl getViewRootImpl() {
10488 if (mAttachInfo != null) {
10489 return mAttachInfo.mViewRootImpl;
10490 }
10491 return null;
10492 }
10493
10494 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010495 * <p>Causes the Runnable to be added to the message queue.
10496 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010497 *
Romain Guye63a4f32011-08-11 11:33:31 -070010498 * <p>This method can be invoked from outside of the UI thread
10499 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010500 *
10501 * @param action The Runnable that will be executed.
10502 *
10503 * @return Returns true if the Runnable was successfully placed in to the
10504 * message queue. Returns false on failure, usually because the
10505 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010506 *
10507 * @see #postDelayed
10508 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 */
10510 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010511 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010512 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010513 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010514 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010515 // Assume that post will succeed later
10516 ViewRootImpl.getRunQueue().post(action);
10517 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010518 }
10519
10520 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010521 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010523 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010524 *
Romain Guye63a4f32011-08-11 11:33:31 -070010525 * <p>This method can be invoked from outside of the UI thread
10526 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527 *
10528 * @param action The Runnable that will be executed.
10529 * @param delayMillis The delay (in milliseconds) until the Runnable
10530 * will be executed.
10531 *
10532 * @return true if the Runnable was successfully placed in to the
10533 * message queue. Returns false on failure, usually because the
10534 * looper processing the message queue is exiting. Note that a
10535 * result of true does not mean the Runnable will be processed --
10536 * if the looper is quit before the delivery time of the message
10537 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010538 *
10539 * @see #post
10540 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 */
10542 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010543 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010544 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010545 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010547 // Assume that post will succeed later
10548 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10549 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 }
10551
10552 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010553 * <p>Causes the Runnable to execute on the next animation time step.
10554 * The runnable will be run on the user interface thread.</p>
10555 *
10556 * <p>This method can be invoked from outside of the UI thread
10557 * only when this View is attached to a window.</p>
10558 *
10559 * @param action The Runnable that will be executed.
10560 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010561 * @see #postOnAnimationDelayed
10562 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010563 */
10564 public void postOnAnimation(Runnable action) {
10565 final AttachInfo attachInfo = mAttachInfo;
10566 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010567 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10568 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010569 } else {
10570 // Assume that post will succeed later
10571 ViewRootImpl.getRunQueue().post(action);
10572 }
10573 }
10574
10575 /**
10576 * <p>Causes the Runnable to execute on the next animation time step,
10577 * after the specified amount of time elapses.
10578 * The runnable will be run on the user interface thread.</p>
10579 *
10580 * <p>This method can be invoked from outside of the UI thread
10581 * only when this View is attached to a window.</p>
10582 *
10583 * @param action The Runnable that will be executed.
10584 * @param delayMillis The delay (in milliseconds) until the Runnable
10585 * will be executed.
10586 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010587 * @see #postOnAnimation
10588 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010589 */
10590 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10591 final AttachInfo attachInfo = mAttachInfo;
10592 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010593 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10594 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010595 } else {
10596 // Assume that post will succeed later
10597 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10598 }
10599 }
10600
10601 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010602 * <p>Removes the specified Runnable from the message queue.</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 action The Runnable to remove from the message handling queue
10608 *
10609 * @return true if this view could ask the Handler to remove the Runnable,
10610 * false otherwise. When the returned value is true, the Runnable
10611 * may or may not have been actually removed from the message queue
10612 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010613 *
10614 * @see #post
10615 * @see #postDelayed
10616 * @see #postOnAnimation
10617 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 */
10619 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010620 if (action != null) {
10621 final AttachInfo attachInfo = mAttachInfo;
10622 if (attachInfo != null) {
10623 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010624 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10625 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010626 } else {
10627 // Assume that post will succeed later
10628 ViewRootImpl.getRunQueue().removeCallbacks(action);
10629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 return true;
10632 }
10633
10634 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010635 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10636 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 *
Romain Guye63a4f32011-08-11 11:33:31 -070010638 * <p>This method can be invoked from outside of the UI thread
10639 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010642 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 */
10644 public void postInvalidate() {
10645 postInvalidateDelayed(0);
10646 }
10647
10648 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010649 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10650 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010651 *
Romain Guye63a4f32011-08-11 11:33:31 -070010652 * <p>This method can be invoked from outside of the UI thread
10653 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 *
10655 * @param left The left coordinate of the rectangle to invalidate.
10656 * @param top The top coordinate of the rectangle to invalidate.
10657 * @param right The right coordinate of the rectangle to invalidate.
10658 * @param bottom The bottom coordinate of the rectangle to invalidate.
10659 *
10660 * @see #invalidate(int, int, int, int)
10661 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010662 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 */
10664 public void postInvalidate(int left, int top, int right, int bottom) {
10665 postInvalidateDelayed(0, left, top, right, bottom);
10666 }
10667
10668 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010669 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10670 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010671 *
Romain Guye63a4f32011-08-11 11:33:31 -070010672 * <p>This method can be invoked from outside of the UI thread
10673 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 *
10675 * @param delayMilliseconds the duration in milliseconds to delay the
10676 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010677 *
10678 * @see #invalidate()
10679 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 */
10681 public void postInvalidateDelayed(long delayMilliseconds) {
10682 // We try only with the AttachInfo because there's no point in invalidating
10683 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010684 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010685 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010686 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010687 }
10688 }
10689
10690 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010691 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10692 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010693 *
Romain Guye63a4f32011-08-11 11:33:31 -070010694 * <p>This method can be invoked from outside of the UI thread
10695 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 *
10697 * @param delayMilliseconds the duration in milliseconds to delay the
10698 * invalidation by
10699 * @param left The left coordinate of the rectangle to invalidate.
10700 * @param top The top coordinate of the rectangle to invalidate.
10701 * @param right The right coordinate of the rectangle to invalidate.
10702 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010703 *
10704 * @see #invalidate(int, int, int, int)
10705 * @see #invalidate(Rect)
10706 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 */
10708 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10709 int right, int bottom) {
10710
10711 // We try only with the AttachInfo because there's no point in invalidating
10712 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010713 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010714 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10716 info.target = this;
10717 info.left = left;
10718 info.top = top;
10719 info.right = right;
10720 info.bottom = bottom;
10721
Jeff Browna175a5b2012-02-15 19:18:31 -080010722 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 }
10724 }
10725
10726 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010727 * <p>Cause an invalidate to happen on the next animation time step, typically the
10728 * next display frame.</p>
10729 *
10730 * <p>This method can be invoked from outside of the UI thread
10731 * only when this View is attached to a window.</p>
10732 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010733 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010734 */
10735 public void postInvalidateOnAnimation() {
10736 // We try only with the AttachInfo because there's no point in invalidating
10737 // if we are not attached to our window
10738 final AttachInfo attachInfo = mAttachInfo;
10739 if (attachInfo != null) {
10740 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10741 }
10742 }
10743
10744 /**
10745 * <p>Cause an invalidate of the specified area to happen on the next animation
10746 * time step, typically the next display frame.</p>
10747 *
10748 * <p>This method can be invoked from outside of the UI thread
10749 * only when this View is attached to a window.</p>
10750 *
10751 * @param left The left coordinate of the rectangle to invalidate.
10752 * @param top The top coordinate of the rectangle to invalidate.
10753 * @param right The right coordinate of the rectangle to invalidate.
10754 * @param bottom The bottom coordinate of the rectangle to invalidate.
10755 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010756 * @see #invalidate(int, int, int, int)
10757 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010758 */
10759 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10760 // We try only with the AttachInfo because there's no point in invalidating
10761 // if we are not attached to our window
10762 final AttachInfo attachInfo = mAttachInfo;
10763 if (attachInfo != null) {
10764 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10765 info.target = this;
10766 info.left = left;
10767 info.top = top;
10768 info.right = right;
10769 info.bottom = bottom;
10770
10771 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10772 }
10773 }
10774
10775 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010776 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10777 * This event is sent at most once every
10778 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10779 */
10780 private void postSendViewScrolledAccessibilityEventCallback() {
10781 if (mSendViewScrolledAccessibilityEvent == null) {
10782 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10783 }
10784 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10785 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10786 postDelayed(mSendViewScrolledAccessibilityEvent,
10787 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10788 }
10789 }
10790
10791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010792 * Called by a parent to request that a child update its values for mScrollX
10793 * and mScrollY if necessary. This will typically be done if the child is
10794 * animating a scroll using a {@link android.widget.Scroller Scroller}
10795 * object.
10796 */
10797 public void computeScroll() {
10798 }
10799
10800 /**
10801 * <p>Indicate whether the horizontal edges are faded when the view is
10802 * scrolled horizontally.</p>
10803 *
10804 * @return true if the horizontal edges should are faded on scroll, false
10805 * otherwise
10806 *
10807 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010808 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010809 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 */
10811 public boolean isHorizontalFadingEdgeEnabled() {
10812 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10813 }
10814
10815 /**
10816 * <p>Define whether the horizontal edges should be faded when this view
10817 * is scrolled horizontally.</p>
10818 *
10819 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10820 * be faded when the view is scrolled
10821 * horizontally
10822 *
10823 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010824 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010825 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010826 */
10827 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10828 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10829 if (horizontalFadingEdgeEnabled) {
10830 initScrollCache();
10831 }
10832
10833 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10834 }
10835 }
10836
10837 /**
10838 * <p>Indicate whether the vertical edges are faded when the view is
10839 * scrolled horizontally.</p>
10840 *
10841 * @return true if the vertical edges should are faded on scroll, false
10842 * otherwise
10843 *
10844 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010845 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010846 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010847 */
10848 public boolean isVerticalFadingEdgeEnabled() {
10849 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10850 }
10851
10852 /**
10853 * <p>Define whether the vertical edges should be faded when this view
10854 * is scrolled vertically.</p>
10855 *
10856 * @param verticalFadingEdgeEnabled true if the vertical edges should
10857 * be faded when the view is scrolled
10858 * vertically
10859 *
10860 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010861 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010862 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010863 */
10864 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10865 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10866 if (verticalFadingEdgeEnabled) {
10867 initScrollCache();
10868 }
10869
10870 mViewFlags ^= FADING_EDGE_VERTICAL;
10871 }
10872 }
10873
10874 /**
10875 * Returns the strength, or intensity, of the top faded edge. The strength is
10876 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10877 * returns 0.0 or 1.0 but no value in between.
10878 *
10879 * Subclasses should override this method to provide a smoother fade transition
10880 * when scrolling occurs.
10881 *
10882 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10883 */
10884 protected float getTopFadingEdgeStrength() {
10885 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10886 }
10887
10888 /**
10889 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10890 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10891 * returns 0.0 or 1.0 but no value in between.
10892 *
10893 * Subclasses should override this method to provide a smoother fade transition
10894 * when scrolling occurs.
10895 *
10896 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10897 */
10898 protected float getBottomFadingEdgeStrength() {
10899 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10900 computeVerticalScrollRange() ? 1.0f : 0.0f;
10901 }
10902
10903 /**
10904 * Returns the strength, or intensity, of the left faded edge. The strength is
10905 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10906 * returns 0.0 or 1.0 but no value in between.
10907 *
10908 * Subclasses should override this method to provide a smoother fade transition
10909 * when scrolling occurs.
10910 *
10911 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10912 */
10913 protected float getLeftFadingEdgeStrength() {
10914 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10915 }
10916
10917 /**
10918 * Returns the strength, or intensity, of the right faded edge. The strength is
10919 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10920 * returns 0.0 or 1.0 but no value in between.
10921 *
10922 * Subclasses should override this method to provide a smoother fade transition
10923 * when scrolling occurs.
10924 *
10925 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10926 */
10927 protected float getRightFadingEdgeStrength() {
10928 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10929 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10930 }
10931
10932 /**
10933 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10934 * scrollbar is not drawn by default.</p>
10935 *
10936 * @return true if the horizontal scrollbar should be painted, false
10937 * otherwise
10938 *
10939 * @see #setHorizontalScrollBarEnabled(boolean)
10940 */
10941 public boolean isHorizontalScrollBarEnabled() {
10942 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10943 }
10944
10945 /**
10946 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10947 * scrollbar is not drawn by default.</p>
10948 *
10949 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10950 * be painted
10951 *
10952 * @see #isHorizontalScrollBarEnabled()
10953 */
10954 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10955 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10956 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010957 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010958 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 }
10960 }
10961
10962 /**
10963 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10964 * scrollbar is not drawn by default.</p>
10965 *
10966 * @return true if the vertical scrollbar should be painted, false
10967 * otherwise
10968 *
10969 * @see #setVerticalScrollBarEnabled(boolean)
10970 */
10971 public boolean isVerticalScrollBarEnabled() {
10972 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10973 }
10974
10975 /**
10976 * <p>Define whether the vertical scrollbar should be drawn or not. The
10977 * scrollbar is not drawn by default.</p>
10978 *
10979 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10980 * be painted
10981 *
10982 * @see #isVerticalScrollBarEnabled()
10983 */
10984 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10985 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10986 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010987 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010988 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 }
10990 }
10991
Adam Powell20232d02010-12-08 21:08:53 -080010992 /**
10993 * @hide
10994 */
10995 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010996 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010997 }
Joe Malin32736f02011-01-19 16:14:20 -080010998
Mike Cleronfe81d382009-09-28 14:22:16 -070010999 /**
11000 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011001 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011002 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011003 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011004 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011005 */
11006 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11007 initScrollCache();
11008 final ScrollabilityCache scrollabilityCache = mScrollCache;
11009 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011010 if (fadeScrollbars) {
11011 scrollabilityCache.state = ScrollabilityCache.OFF;
11012 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011013 scrollabilityCache.state = ScrollabilityCache.ON;
11014 }
11015 }
Joe Malin32736f02011-01-19 16:14:20 -080011016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 /**
Joe Malin32736f02011-01-19 16:14:20 -080011018 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011019 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011020 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011021 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011022 *
11023 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011024 */
11025 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011026 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011027 }
Joe Malin32736f02011-01-19 16:14:20 -080011028
Mike Cleron52f0a642009-09-28 18:21:37 -070011029 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011030 *
11031 * Returns the delay before scrollbars fade.
11032 *
11033 * @return the delay before scrollbars fade
11034 *
11035 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11036 */
11037 public int getScrollBarDefaultDelayBeforeFade() {
11038 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11039 mScrollCache.scrollBarDefaultDelayBeforeFade;
11040 }
11041
11042 /**
11043 * Define the delay before scrollbars fade.
11044 *
11045 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11046 *
11047 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11048 */
11049 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11050 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11051 }
11052
11053 /**
11054 *
11055 * Returns the scrollbar fade duration.
11056 *
11057 * @return the scrollbar fade duration
11058 *
11059 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11060 */
11061 public int getScrollBarFadeDuration() {
11062 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11063 mScrollCache.scrollBarFadeDuration;
11064 }
11065
11066 /**
11067 * Define the scrollbar fade duration.
11068 *
11069 * @param scrollBarFadeDuration - the scrollbar fade duration
11070 *
11071 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11072 */
11073 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11074 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11075 }
11076
11077 /**
11078 *
11079 * Returns the scrollbar size.
11080 *
11081 * @return the scrollbar size
11082 *
11083 * @attr ref android.R.styleable#View_scrollbarSize
11084 */
11085 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011086 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011087 mScrollCache.scrollBarSize;
11088 }
11089
11090 /**
11091 * Define the scrollbar size.
11092 *
11093 * @param scrollBarSize - the scrollbar size
11094 *
11095 * @attr ref android.R.styleable#View_scrollbarSize
11096 */
11097 public void setScrollBarSize(int scrollBarSize) {
11098 getScrollCache().scrollBarSize = scrollBarSize;
11099 }
11100
11101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011102 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11103 * inset. When inset, they add to the padding of the view. And the scrollbars
11104 * can be drawn inside the padding area or on the edge of the view. For example,
11105 * if a view has a background drawable and you want to draw the scrollbars
11106 * inside the padding specified by the drawable, you can use
11107 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11108 * appear at the edge of the view, ignoring the padding, then you can use
11109 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11110 * @param style the style of the scrollbars. Should be one of
11111 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11112 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11113 * @see #SCROLLBARS_INSIDE_OVERLAY
11114 * @see #SCROLLBARS_INSIDE_INSET
11115 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11116 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011117 *
11118 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011119 */
11120 public void setScrollBarStyle(int style) {
11121 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11122 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011123 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011124 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011125 }
11126 }
11127
11128 /**
11129 * <p>Returns the current scrollbar style.</p>
11130 * @return the current scrollbar style
11131 * @see #SCROLLBARS_INSIDE_OVERLAY
11132 * @see #SCROLLBARS_INSIDE_INSET
11133 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11134 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011135 *
11136 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011138 @ViewDebug.ExportedProperty(mapping = {
11139 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11140 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11141 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11142 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11143 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 public int getScrollBarStyle() {
11145 return mViewFlags & SCROLLBARS_STYLE_MASK;
11146 }
11147
11148 /**
11149 * <p>Compute the horizontal range that the horizontal scrollbar
11150 * represents.</p>
11151 *
11152 * <p>The range is expressed in arbitrary units that must be the same as the
11153 * units used by {@link #computeHorizontalScrollExtent()} and
11154 * {@link #computeHorizontalScrollOffset()}.</p>
11155 *
11156 * <p>The default range is the drawing width of this view.</p>
11157 *
11158 * @return the total horizontal range represented by the horizontal
11159 * scrollbar
11160 *
11161 * @see #computeHorizontalScrollExtent()
11162 * @see #computeHorizontalScrollOffset()
11163 * @see android.widget.ScrollBarDrawable
11164 */
11165 protected int computeHorizontalScrollRange() {
11166 return getWidth();
11167 }
11168
11169 /**
11170 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11171 * within the horizontal range. This value is used to compute the position
11172 * of the thumb within the scrollbar's track.</p>
11173 *
11174 * <p>The range is expressed in arbitrary units that must be the same as the
11175 * units used by {@link #computeHorizontalScrollRange()} and
11176 * {@link #computeHorizontalScrollExtent()}.</p>
11177 *
11178 * <p>The default offset is the scroll offset of this view.</p>
11179 *
11180 * @return the horizontal offset of the scrollbar's thumb
11181 *
11182 * @see #computeHorizontalScrollRange()
11183 * @see #computeHorizontalScrollExtent()
11184 * @see android.widget.ScrollBarDrawable
11185 */
11186 protected int computeHorizontalScrollOffset() {
11187 return mScrollX;
11188 }
11189
11190 /**
11191 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11192 * within the horizontal range. This value is used to compute the length
11193 * of the thumb within the scrollbar's track.</p>
11194 *
11195 * <p>The range is expressed in arbitrary units that must be the same as the
11196 * units used by {@link #computeHorizontalScrollRange()} and
11197 * {@link #computeHorizontalScrollOffset()}.</p>
11198 *
11199 * <p>The default extent is the drawing width of this view.</p>
11200 *
11201 * @return the horizontal extent of the scrollbar's thumb
11202 *
11203 * @see #computeHorizontalScrollRange()
11204 * @see #computeHorizontalScrollOffset()
11205 * @see android.widget.ScrollBarDrawable
11206 */
11207 protected int computeHorizontalScrollExtent() {
11208 return getWidth();
11209 }
11210
11211 /**
11212 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11213 *
11214 * <p>The range is expressed in arbitrary units that must be the same as the
11215 * units used by {@link #computeVerticalScrollExtent()} and
11216 * {@link #computeVerticalScrollOffset()}.</p>
11217 *
11218 * @return the total vertical range represented by the vertical scrollbar
11219 *
11220 * <p>The default range is the drawing height of this view.</p>
11221 *
11222 * @see #computeVerticalScrollExtent()
11223 * @see #computeVerticalScrollOffset()
11224 * @see android.widget.ScrollBarDrawable
11225 */
11226 protected int computeVerticalScrollRange() {
11227 return getHeight();
11228 }
11229
11230 /**
11231 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11232 * within the horizontal range. This value is used to compute the position
11233 * of the thumb within the scrollbar's track.</p>
11234 *
11235 * <p>The range is expressed in arbitrary units that must be the same as the
11236 * units used by {@link #computeVerticalScrollRange()} and
11237 * {@link #computeVerticalScrollExtent()}.</p>
11238 *
11239 * <p>The default offset is the scroll offset of this view.</p>
11240 *
11241 * @return the vertical offset of the scrollbar's thumb
11242 *
11243 * @see #computeVerticalScrollRange()
11244 * @see #computeVerticalScrollExtent()
11245 * @see android.widget.ScrollBarDrawable
11246 */
11247 protected int computeVerticalScrollOffset() {
11248 return mScrollY;
11249 }
11250
11251 /**
11252 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11253 * within the vertical range. This value is used to compute the length
11254 * of the thumb within the scrollbar's track.</p>
11255 *
11256 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011257 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 * {@link #computeVerticalScrollOffset()}.</p>
11259 *
11260 * <p>The default extent is the drawing height of this view.</p>
11261 *
11262 * @return the vertical extent of the scrollbar's thumb
11263 *
11264 * @see #computeVerticalScrollRange()
11265 * @see #computeVerticalScrollOffset()
11266 * @see android.widget.ScrollBarDrawable
11267 */
11268 protected int computeVerticalScrollExtent() {
11269 return getHeight();
11270 }
11271
11272 /**
Adam Powell69159442011-06-13 17:53:06 -070011273 * Check if this view can be scrolled horizontally in a certain direction.
11274 *
11275 * @param direction Negative to check scrolling left, positive to check scrolling right.
11276 * @return true if this view can be scrolled in the specified direction, false otherwise.
11277 */
11278 public boolean canScrollHorizontally(int direction) {
11279 final int offset = computeHorizontalScrollOffset();
11280 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11281 if (range == 0) return false;
11282 if (direction < 0) {
11283 return offset > 0;
11284 } else {
11285 return offset < range - 1;
11286 }
11287 }
11288
11289 /**
11290 * Check if this view can be scrolled vertically in a certain direction.
11291 *
11292 * @param direction Negative to check scrolling up, positive to check scrolling down.
11293 * @return true if this view can be scrolled in the specified direction, false otherwise.
11294 */
11295 public boolean canScrollVertically(int direction) {
11296 final int offset = computeVerticalScrollOffset();
11297 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11298 if (range == 0) return false;
11299 if (direction < 0) {
11300 return offset > 0;
11301 } else {
11302 return offset < range - 1;
11303 }
11304 }
11305
11306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011307 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11308 * scrollbars are painted only if they have been awakened first.</p>
11309 *
11310 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011311 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011312 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011313 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011314 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 // scrollbars are drawn only when the animation is running
11316 final ScrollabilityCache cache = mScrollCache;
11317 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011318
Mike Cleronf116bf82009-09-27 19:14:12 -070011319 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011320
Mike Cleronf116bf82009-09-27 19:14:12 -070011321 if (state == ScrollabilityCache.OFF) {
11322 return;
11323 }
Joe Malin32736f02011-01-19 16:14:20 -080011324
Mike Cleronf116bf82009-09-27 19:14:12 -070011325 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011326
Mike Cleronf116bf82009-09-27 19:14:12 -070011327 if (state == ScrollabilityCache.FADING) {
11328 // We're fading -- get our fade interpolation
11329 if (cache.interpolatorValues == null) {
11330 cache.interpolatorValues = new float[1];
11331 }
Joe Malin32736f02011-01-19 16:14:20 -080011332
Mike Cleronf116bf82009-09-27 19:14:12 -070011333 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011334
Mike Cleronf116bf82009-09-27 19:14:12 -070011335 // Stops the animation if we're done
11336 if (cache.scrollBarInterpolator.timeToValues(values) ==
11337 Interpolator.Result.FREEZE_END) {
11338 cache.state = ScrollabilityCache.OFF;
11339 } else {
11340 cache.scrollBar.setAlpha(Math.round(values[0]));
11341 }
Joe Malin32736f02011-01-19 16:14:20 -080011342
11343 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011344 // drawing. We only want this when we're fading so that
11345 // we prevent excessive redraws
11346 invalidate = true;
11347 } else {
11348 // We're just on -- but we may have been fading before so
11349 // reset alpha
11350 cache.scrollBar.setAlpha(255);
11351 }
11352
Joe Malin32736f02011-01-19 16:14:20 -080011353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354 final int viewFlags = mViewFlags;
11355
11356 final boolean drawHorizontalScrollBar =
11357 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11358 final boolean drawVerticalScrollBar =
11359 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11360 && !isVerticalScrollBarHidden();
11361
11362 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11363 final int width = mRight - mLeft;
11364 final int height = mBottom - mTop;
11365
11366 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011367
Mike Reede8853fc2009-09-04 14:01:48 -040011368 final int scrollX = mScrollX;
11369 final int scrollY = mScrollY;
11370 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11371
Mike Cleronf116bf82009-09-27 19:14:12 -070011372 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011375 int size = scrollBar.getSize(false);
11376 if (size <= 0) {
11377 size = cache.scrollBarSize;
11378 }
11379
Mike Cleronf116bf82009-09-27 19:14:12 -070011380 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011381 computeHorizontalScrollOffset(),
11382 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011383 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011384 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011385 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011386 left = scrollX + (mPaddingLeft & inside);
11387 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11388 bottom = top + size;
11389 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11390 if (invalidate) {
11391 invalidate(left, top, right, bottom);
11392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 }
11394
11395 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011396 int size = scrollBar.getSize(true);
11397 if (size <= 0) {
11398 size = cache.scrollBarSize;
11399 }
11400
Mike Reede8853fc2009-09-04 14:01:48 -040011401 scrollBar.setParameters(computeVerticalScrollRange(),
11402 computeVerticalScrollOffset(),
11403 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011404 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11405 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11406 verticalScrollbarPosition = isLayoutRtl() ?
11407 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11408 }
11409 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011410 default:
Adam Powell20232d02010-12-08 21:08:53 -080011411 case SCROLLBAR_POSITION_RIGHT:
11412 left = scrollX + width - size - (mUserPaddingRight & inside);
11413 break;
11414 case SCROLLBAR_POSITION_LEFT:
11415 left = scrollX + (mUserPaddingLeft & inside);
11416 break;
11417 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011418 top = scrollY + (mPaddingTop & inside);
11419 right = left + size;
11420 bottom = scrollY + height - (mUserPaddingBottom & inside);
11421 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11422 if (invalidate) {
11423 invalidate(left, top, right, bottom);
11424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425 }
11426 }
11427 }
11428 }
Romain Guy8506ab42009-06-11 17:35:47 -070011429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011430 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011431 * 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 -080011432 * FastScroller is visible.
11433 * @return whether to temporarily hide the vertical scrollbar
11434 * @hide
11435 */
11436 protected boolean isVerticalScrollBarHidden() {
11437 return false;
11438 }
11439
11440 /**
11441 * <p>Draw the horizontal scrollbar if
11442 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11443 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011444 * @param canvas the canvas on which to draw the scrollbar
11445 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011446 *
11447 * @see #isHorizontalScrollBarEnabled()
11448 * @see #computeHorizontalScrollRange()
11449 * @see #computeHorizontalScrollExtent()
11450 * @see #computeHorizontalScrollOffset()
11451 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011452 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011453 */
Romain Guy8fb95422010-08-17 18:38:51 -070011454 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11455 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011456 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011457 scrollBar.draw(canvas);
11458 }
Mike Reede8853fc2009-09-04 14:01:48 -040011459
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011460 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11462 * returns true.</p>
11463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 * @param canvas the canvas on which to draw the scrollbar
11465 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 *
11467 * @see #isVerticalScrollBarEnabled()
11468 * @see #computeVerticalScrollRange()
11469 * @see #computeVerticalScrollExtent()
11470 * @see #computeVerticalScrollOffset()
11471 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011472 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011473 */
Romain Guy8fb95422010-08-17 18:38:51 -070011474 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11475 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011476 scrollBar.setBounds(l, t, r, b);
11477 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478 }
11479
11480 /**
11481 * Implement this to do your drawing.
11482 *
11483 * @param canvas the canvas on which the background will be drawn
11484 */
11485 protected void onDraw(Canvas canvas) {
11486 }
11487
11488 /*
11489 * Caller is responsible for calling requestLayout if necessary.
11490 * (This allows addViewInLayout to not request a new layout.)
11491 */
11492 void assignParent(ViewParent parent) {
11493 if (mParent == null) {
11494 mParent = parent;
11495 } else if (parent == null) {
11496 mParent = null;
11497 } else {
11498 throw new RuntimeException("view " + this + " being added, but"
11499 + " it already has a parent");
11500 }
11501 }
11502
11503 /**
11504 * This is called when the view is attached to a window. At this point it
11505 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011506 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11507 * however it may be called any time before the first onDraw -- including
11508 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 *
11510 * @see #onDetachedFromWindow()
11511 */
11512 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011513 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 mParent.requestTransparentRegion(this);
11515 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011516
Dianne Hackborn4702a852012-08-17 15:18:29 -070011517 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011518 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011519 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011520 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011521
Chet Haasea9b61ac2010-12-20 07:40:25 -080011522 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011523
Svetoslav Ganov42138042012-03-20 11:51:39 -070011524 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011525 if (isFocused()) {
11526 InputMethodManager imm = InputMethodManager.peekInstance();
11527 imm.focusIn(this);
11528 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011529
11530 if (mAttachInfo != null && mDisplayList != null) {
11531 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11532 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011533 }
Cibu Johny86666632010-02-22 13:01:02 -080011534
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011535 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011536 * Resolve all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011537 */
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011538 void resolveRtlPropertiesIfNeeded() {
11539 if (!needRtlPropertiesResolution()) return;
11540
11541 // Order is important here: LayoutDirection MUST be resolved first
11542 if (!isLayoutDirectionResolved()) {
11543 resolveLayoutDirection();
11544 resolveLayoutParams();
11545 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011546 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011547 if (!isTextDirectionResolved()) {
11548 resolveTextDirection();
11549 }
11550 if (!isTextAlignmentResolved()) {
11551 resolveTextAlignment();
11552 }
11553 if (!isPaddingResolved()) {
11554 resolvePadding();
11555 }
11556 if (!isDrawablesResolved()) {
11557 resolveDrawables();
11558 }
11559 requestLayout();
11560 invalidate(true);
11561 onRtlPropertiesChanged();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011562 }
11563
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011564 // Reset resolution of all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011565 void resetRtlProperties() {
11566 resetResolvedLayoutDirection();
11567 resetResolvedTextDirection();
11568 resetResolvedTextAlignment();
11569 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011570 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011571 }
11572
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011573 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011574 * @see #onScreenStateChanged(int)
11575 */
11576 void dispatchScreenStateChanged(int screenState) {
11577 onScreenStateChanged(screenState);
11578 }
11579
11580 /**
11581 * This method is called whenever the state of the screen this view is
11582 * attached to changes. A state change will usually occurs when the screen
11583 * turns on or off (whether it happens automatically or the user does it
11584 * manually.)
11585 *
11586 * @param screenState The new state of the screen. Can be either
11587 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11588 */
11589 public void onScreenStateChanged(int screenState) {
11590 }
11591
11592 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011593 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11594 */
11595 private boolean hasRtlSupport() {
11596 return mContext.getApplicationInfo().hasRtlSupport();
11597 }
11598
11599 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011600 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11601 * RTL not supported)
11602 */
11603 private boolean isRtlCompatibilityMode() {
11604 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11605 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11606 }
11607
11608 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011609 * @return true if RTL properties need resolution.
11610 */
11611 private boolean needRtlPropertiesResolution() {
11612 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11613 }
11614
11615 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011616 * Called when any RTL property (layout direction or text direction or text alignment) has
11617 * been changed.
11618 *
11619 * Subclasses need to override this method to take care of cached information that depends on the
11620 * resolved layout direction, or to inform child views that inherit their layout direction.
11621 *
11622 * The default implementation does nothing.
11623 */
11624 public void onRtlPropertiesChanged() {
11625 }
11626
11627 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011628 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11629 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011630 *
11631 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011632 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011633 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011634 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011635 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011636
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011637 if (hasRtlSupport()) {
11638 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011639 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11640 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011641 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011642 // We cannot resolve yet. LTR is by default and let the resolution happen again
11643 // later to get the correct resolved value
11644 if (!canResolveLayoutDirection()) return;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011645
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011646 ViewGroup viewGroup = ((ViewGroup) mParent);
11647
11648 // We cannot resolve yet on the parent too. LTR is by default and let the
11649 // resolution happen again later
11650 if (!viewGroup.canResolveLayoutDirection()) return;
11651
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011652 if (viewGroup.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011653 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011654 }
11655 break;
11656 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011657 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011658 break;
11659 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011660 if((LAYOUT_DIRECTION_RTL ==
11661 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011662 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011663 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011664 break;
11665 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011666 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011667 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011668 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011669
11670 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011671 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011672 }
11673
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011674 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011675 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011676 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011677 * @return true if layout direction resolution can be done otherwise return false.
11678 *
11679 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011680 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011681 public boolean canResolveLayoutDirection() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011682 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11683 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011684 case LAYOUT_DIRECTION_INHERIT:
11685 return (mParent != null) && (mParent instanceof ViewGroup);
11686 default:
11687 return true;
11688 }
11689 }
11690
11691 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011692 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11693 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011694 *
11695 * @hide
11696 */
11697 public void resetResolvedLayoutDirection() {
11698 // Reset the current resolved bits
11699 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011700 }
11701
11702 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011703 * @return true if the layout direction is inherited.
11704 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011705 * @hide
11706 */
11707 public boolean isLayoutDirectionInherited() {
11708 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11709 }
11710
11711 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011712 * @return true if layout direction has been resolved.
11713 */
11714 private boolean isLayoutDirectionResolved() {
11715 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11716 }
11717
11718 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011719 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011720 *
11721 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011722 */
11723 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011724 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011725 }
11726
11727 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011728 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011729 *
11730 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011731 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011732 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011733 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011734 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11735 // If start / end padding are defined, they will be resolved (hence overriding) to
11736 // left / right or right / left depending on the resolved layout direction.
11737 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011738 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011739 // Set user padding to initial values ...
11740 mUserPaddingLeft = (mUserPaddingLeftInitial == UNDEFINED_PADDING) ?
11741 0 : mUserPaddingLeftInitial;
11742 mUserPaddingRight = (mUserPaddingRightInitial == UNDEFINED_PADDING) ?
11743 0 : mUserPaddingRightInitial;
11744 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011745 switch (resolvedLayoutDirection) {
11746 case LAYOUT_DIRECTION_RTL:
11747 if (mUserPaddingStart != UNDEFINED_PADDING) {
11748 mUserPaddingRight = mUserPaddingStart;
11749 }
11750 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11751 mUserPaddingLeft = mUserPaddingEnd;
11752 }
11753 break;
11754 case LAYOUT_DIRECTION_LTR:
11755 default:
11756 if (mUserPaddingStart != UNDEFINED_PADDING) {
11757 mUserPaddingLeft = mUserPaddingStart;
11758 }
11759 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11760 mUserPaddingRight = mUserPaddingEnd;
11761 }
11762 }
11763
11764 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11765
11766 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11767 mUserPaddingBottom);
11768 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011769 }
11770
Dianne Hackborn4702a852012-08-17 15:18:29 -070011771 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011772 }
11773
11774 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011775 * Reset the resolved layout direction.
11776 *
11777 * @hide
11778 */
11779 public void resetResolvedPadding() {
11780 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11781 }
11782
11783 /**
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011784 * Resolve padding depending on the layout direction. Subclasses that care about
11785 * padding resolution should override this method. The default implementation does
11786 * nothing.
11787 *
11788 * @param layoutDirection the direction of the layout
11789 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070011790 * @see #LAYOUT_DIRECTION_LTR
11791 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011792 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011793 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011794 }
11795
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011797 * This is called when the view is detached from a window. At this point it
11798 * no longer has a surface for drawing.
11799 *
11800 * @see #onAttachedToWindow()
11801 */
11802 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011803 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011804
Romain Guya440b002010-02-24 15:57:54 -080011805 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011806 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011807 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011808 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011810 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011811
Romain Guya998dff2012-03-23 18:58:36 -070011812 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011813
11814 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011815 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011816 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011817 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011818 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011819 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011820 // Should never happen
11821 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011822 }
11823
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011824 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011825
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011826 resetRtlProperties();
11827 onRtlPropertiesChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011828 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011829 }
11830
11831 /**
11832 * @return The number of times this view has been attached to a window
11833 */
11834 protected int getWindowAttachCount() {
11835 return mWindowAttachCount;
11836 }
11837
11838 /**
11839 * Retrieve a unique token identifying the window this view is attached to.
11840 * @return Return the window's token for use in
11841 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11842 */
11843 public IBinder getWindowToken() {
11844 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11845 }
11846
11847 /**
11848 * Retrieve a unique token identifying the top-level "real" window of
11849 * the window that this view is attached to. That is, this is like
11850 * {@link #getWindowToken}, except if the window this view in is a panel
11851 * window (attached to another containing window), then the token of
11852 * the containing window is returned instead.
11853 *
11854 * @return Returns the associated window token, either
11855 * {@link #getWindowToken()} or the containing window's token.
11856 */
11857 public IBinder getApplicationWindowToken() {
11858 AttachInfo ai = mAttachInfo;
11859 if (ai != null) {
11860 IBinder appWindowToken = ai.mPanelParentWindowToken;
11861 if (appWindowToken == null) {
11862 appWindowToken = ai.mWindowToken;
11863 }
11864 return appWindowToken;
11865 }
11866 return null;
11867 }
11868
11869 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011870 * Gets the logical display to which the view's window has been attached.
11871 *
11872 * @return The logical display, or null if the view is not currently attached to a window.
11873 */
11874 public Display getDisplay() {
11875 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11876 }
11877
11878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 * Retrieve private session object this view hierarchy is using to
11880 * communicate with the window manager.
11881 * @return the session object to communicate with the window manager
11882 */
11883 /*package*/ IWindowSession getWindowSession() {
11884 return mAttachInfo != null ? mAttachInfo.mSession : null;
11885 }
11886
11887 /**
11888 * @param info the {@link android.view.View.AttachInfo} to associated with
11889 * this view
11890 */
11891 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11892 //System.out.println("Attached! " + this);
11893 mAttachInfo = info;
11894 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011895 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011896 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897 if (mFloatingTreeObserver != null) {
11898 info.mTreeObserver.merge(mFloatingTreeObserver);
11899 mFloatingTreeObserver = null;
11900 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011901 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011902 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011903 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011905 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011907
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011908 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011909 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011910 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011911 if (listeners != null && listeners.size() > 0) {
11912 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11913 // perform the dispatching. The iterator is a safe guard against listeners that
11914 // could mutate the list by calling the various add/remove methods. This prevents
11915 // the array from being modified while we iterate it.
11916 for (OnAttachStateChangeListener listener : listeners) {
11917 listener.onViewAttachedToWindow(this);
11918 }
11919 }
11920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921 int vis = info.mWindowVisibility;
11922 if (vis != GONE) {
11923 onWindowVisibilityChanged(vis);
11924 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011925 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011926 // If nobody has evaluated the drawable state yet, then do it now.
11927 refreshDrawableState();
11928 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011929 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 }
11931
11932 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011933 AttachInfo info = mAttachInfo;
11934 if (info != null) {
11935 int vis = info.mWindowVisibility;
11936 if (vis != GONE) {
11937 onWindowVisibilityChanged(GONE);
11938 }
11939 }
11940
11941 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011942
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011943 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011944 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011945 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011946 if (listeners != null && listeners.size() > 0) {
11947 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11948 // perform the dispatching. The iterator is a safe guard against listeners that
11949 // could mutate the list by calling the various add/remove methods. This prevents
11950 // the array from being modified while we iterate it.
11951 for (OnAttachStateChangeListener listener : listeners) {
11952 listener.onViewDetachedFromWindow(this);
11953 }
11954 }
11955
Dianne Hackborn4702a852012-08-17 15:18:29 -070011956 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011957 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011958 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011959 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961 mAttachInfo = null;
11962 }
11963
11964 /**
11965 * Store this view hierarchy's frozen state into the given container.
11966 *
11967 * @param container The SparseArray in which to save the view's state.
11968 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011969 * @see #restoreHierarchyState(android.util.SparseArray)
11970 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11971 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972 */
11973 public void saveHierarchyState(SparseArray<Parcelable> container) {
11974 dispatchSaveInstanceState(container);
11975 }
11976
11977 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011978 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11979 * this view and its children. May be overridden to modify how freezing happens to a
11980 * 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 -080011981 *
11982 * @param container The SparseArray in which to save the view's state.
11983 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011984 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11985 * @see #saveHierarchyState(android.util.SparseArray)
11986 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 */
11988 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11989 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011990 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011991 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011992 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993 throw new IllegalStateException(
11994 "Derived class did not call super.onSaveInstanceState()");
11995 }
11996 if (state != null) {
11997 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11998 // + ": " + state);
11999 container.put(mID, state);
12000 }
12001 }
12002 }
12003
12004 /**
12005 * Hook allowing a view to generate a representation of its internal state
12006 * that can later be used to create a new instance with that same state.
12007 * This state should only contain information that is not persistent or can
12008 * not be reconstructed later. For example, you will never store your
12009 * current position on screen because that will be computed again when a
12010 * new instance of the view is placed in its view hierarchy.
12011 * <p>
12012 * Some examples of things you may store here: the current cursor position
12013 * in a text view (but usually not the text itself since that is stored in a
12014 * content provider or other persistent storage), the currently selected
12015 * item in a list view.
12016 *
12017 * @return Returns a Parcelable object containing the view's current dynamic
12018 * state, or null if there is nothing interesting to save. The
12019 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012020 * @see #onRestoreInstanceState(android.os.Parcelable)
12021 * @see #saveHierarchyState(android.util.SparseArray)
12022 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012023 * @see #setSaveEnabled(boolean)
12024 */
12025 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012026 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012027 return BaseSavedState.EMPTY_STATE;
12028 }
12029
12030 /**
12031 * Restore this view hierarchy's frozen state from the given container.
12032 *
12033 * @param container The SparseArray which holds previously frozen states.
12034 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012035 * @see #saveHierarchyState(android.util.SparseArray)
12036 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12037 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012038 */
12039 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12040 dispatchRestoreInstanceState(container);
12041 }
12042
12043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012044 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12045 * state for this view and its children. May be overridden to modify how restoring
12046 * happens to a view's children; for example, some views may want to not store state
12047 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012048 *
12049 * @param container The SparseArray which holds previously saved state.
12050 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012051 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12052 * @see #restoreHierarchyState(android.util.SparseArray)
12053 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012054 */
12055 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12056 if (mID != NO_ID) {
12057 Parcelable state = container.get(mID);
12058 if (state != null) {
12059 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12060 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012061 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012062 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012063 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012064 throw new IllegalStateException(
12065 "Derived class did not call super.onRestoreInstanceState()");
12066 }
12067 }
12068 }
12069 }
12070
12071 /**
12072 * Hook allowing a view to re-apply a representation of its internal state that had previously
12073 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12074 * null state.
12075 *
12076 * @param state The frozen state that had previously been returned by
12077 * {@link #onSaveInstanceState}.
12078 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012079 * @see #onSaveInstanceState()
12080 * @see #restoreHierarchyState(android.util.SparseArray)
12081 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012082 */
12083 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012084 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012086 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12087 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012088 + "when two views of different type have the same id in the same hierarchy. "
12089 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012090 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012091 }
12092 }
12093
12094 /**
12095 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12096 *
12097 * @return the drawing start time in milliseconds
12098 */
12099 public long getDrawingTime() {
12100 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12101 }
12102
12103 /**
12104 * <p>Enables or disables the duplication of the parent's state into this view. When
12105 * duplication is enabled, this view gets its drawable state from its parent rather
12106 * than from its own internal properties.</p>
12107 *
12108 * <p>Note: in the current implementation, setting this property to true after the
12109 * view was added to a ViewGroup might have no effect at all. This property should
12110 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12111 *
12112 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12113 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012114 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012115 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12116 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012117 *
12118 * @param enabled True to enable duplication of the parent's drawable state, false
12119 * to disable it.
12120 *
12121 * @see #getDrawableState()
12122 * @see #isDuplicateParentStateEnabled()
12123 */
12124 public void setDuplicateParentStateEnabled(boolean enabled) {
12125 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12126 }
12127
12128 /**
12129 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12130 *
12131 * @return True if this view's drawable state is duplicated from the parent,
12132 * false otherwise
12133 *
12134 * @see #getDrawableState()
12135 * @see #setDuplicateParentStateEnabled(boolean)
12136 */
12137 public boolean isDuplicateParentStateEnabled() {
12138 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12139 }
12140
12141 /**
Romain Guy171c5922011-01-06 10:04:23 -080012142 * <p>Specifies the type of layer backing this view. The layer can be
12143 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12144 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012145 *
Romain Guy171c5922011-01-06 10:04:23 -080012146 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12147 * instance that controls how the layer is composed on screen. The following
12148 * properties of the paint are taken into account when composing the layer:</p>
12149 * <ul>
12150 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12151 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12152 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12153 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012154 *
Romain Guy171c5922011-01-06 10:04:23 -080012155 * <p>If this view has an alpha value set to < 1.0 by calling
12156 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12157 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12158 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012159 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012160 *
Romain Guy171c5922011-01-06 10:04:23 -080012161 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12162 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12163 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012164 *
Chet Haased15ebf22012-09-05 11:40:29 -070012165 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012166 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12167 * {@link #LAYER_TYPE_HARDWARE}
12168 * @param paint The paint used to compose the layer. This argument is optional
12169 * and can be null. It is ignored when the layer type is
12170 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012171 *
12172 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012173 * @see #LAYER_TYPE_NONE
12174 * @see #LAYER_TYPE_SOFTWARE
12175 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012176 * @see #setAlpha(float)
12177 *
Romain Guy171c5922011-01-06 10:04:23 -080012178 * @attr ref android.R.styleable#View_layerType
12179 */
12180 public void setLayerType(int layerType, Paint paint) {
12181 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012182 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012183 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12184 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012185
Romain Guyd6cd5722011-01-17 14:42:41 -080012186 if (layerType == mLayerType) {
12187 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12188 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012189 invalidateParentCaches();
12190 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012191 }
12192 return;
12193 }
Romain Guy171c5922011-01-06 10:04:23 -080012194
12195 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012196 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012197 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012198 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012199 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012200 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012201 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012202 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012203 default:
12204 break;
Romain Guy171c5922011-01-06 10:04:23 -080012205 }
12206
12207 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012208 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12209 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12210 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012211
Romain Guy0fd89bf2011-01-26 15:41:30 -080012212 invalidateParentCaches();
12213 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012214 }
12215
12216 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012217 * Updates the {@link Paint} object used with the current layer (used only if the current
12218 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12219 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12220 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12221 * ensure that the view gets redrawn immediately.
12222 *
12223 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12224 * instance that controls how the layer is composed on screen. The following
12225 * properties of the paint are taken into account when composing the layer:</p>
12226 * <ul>
12227 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12228 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12229 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12230 * </ul>
12231 *
12232 * <p>If this view has an alpha value set to < 1.0 by calling
12233 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12234 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12235 * equivalent to setting a hardware layer on this view and providing a paint with
12236 * the desired alpha value.</p>
12237 *
12238 * @param paint The paint used to compose the layer. This argument is optional
12239 * and can be null. It is ignored when the layer type is
12240 * {@link #LAYER_TYPE_NONE}
12241 *
12242 * @see #setLayerType(int, android.graphics.Paint)
12243 */
12244 public void setLayerPaint(Paint paint) {
12245 int layerType = getLayerType();
12246 if (layerType != LAYER_TYPE_NONE) {
12247 mLayerPaint = paint == null ? new Paint() : paint;
12248 if (layerType == LAYER_TYPE_HARDWARE) {
12249 HardwareLayer layer = getHardwareLayer();
12250 if (layer != null) {
12251 layer.setLayerPaint(paint);
12252 }
12253 invalidateViewProperty(false, false);
12254 } else {
12255 invalidate();
12256 }
12257 }
12258 }
12259
12260 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012261 * Indicates whether this view has a static layer. A view with layer type
12262 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12263 * dynamic.
12264 */
12265 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012266 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012267 }
12268
12269 /**
Romain Guy171c5922011-01-06 10:04:23 -080012270 * Indicates what type of layer is currently associated with this view. By default
12271 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12272 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12273 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012274 *
Romain Guy171c5922011-01-06 10:04:23 -080012275 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12276 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012277 *
12278 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012279 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012280 * @see #LAYER_TYPE_NONE
12281 * @see #LAYER_TYPE_SOFTWARE
12282 * @see #LAYER_TYPE_HARDWARE
12283 */
12284 public int getLayerType() {
12285 return mLayerType;
12286 }
Joe Malin32736f02011-01-19 16:14:20 -080012287
Romain Guy6c319ca2011-01-11 14:29:25 -080012288 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012289 * Forces this view's layer to be created and this view to be rendered
12290 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12291 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012292 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012293 * This method can for instance be used to render a view into its layer before
12294 * starting an animation. If this view is complex, rendering into the layer
12295 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012296 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012297 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012298 *
12299 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012300 */
12301 public void buildLayer() {
12302 if (mLayerType == LAYER_TYPE_NONE) return;
12303
12304 if (mAttachInfo == null) {
12305 throw new IllegalStateException("This view must be attached to a window first");
12306 }
12307
12308 switch (mLayerType) {
12309 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012310 if (mAttachInfo.mHardwareRenderer != null &&
12311 mAttachInfo.mHardwareRenderer.isEnabled() &&
12312 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012313 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012314 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012315 break;
12316 case LAYER_TYPE_SOFTWARE:
12317 buildDrawingCache(true);
12318 break;
12319 }
12320 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012321
Romain Guyf1ae1062011-03-02 18:16:04 -080012322 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012323 * <p>Returns a hardware layer that can be used to draw this view again
12324 * without executing its draw method.</p>
12325 *
12326 * @return A HardwareLayer ready to render, or null if an error occurred.
12327 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012328 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012329 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12330 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012331 return null;
12332 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012333
Romain Guy9c4b79a2011-11-10 19:23:58 -080012334 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012335
12336 final int width = mRight - mLeft;
12337 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012338
Romain Guy6c319ca2011-01-11 14:29:25 -080012339 if (width == 0 || height == 0) {
12340 return null;
12341 }
12342
Dianne Hackborn4702a852012-08-17 15:18:29 -070012343 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012344 if (mHardwareLayer == null) {
12345 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12346 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012347 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012348 } else {
12349 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012350 if (mHardwareLayer.resize(width, height)) {
12351 mLocalDirtyRect.set(0, 0, width, height);
12352 }
Romain Guy846a5332012-07-11 17:44:57 -070012353 }
12354
12355 // This should not be necessary but applications that change
12356 // the parameters of their background drawable without calling
12357 // this.setBackground(Drawable) can leave the view in a bad state
12358 // (for instance isOpaque() returns true, but the background is
12359 // not opaque.)
12360 computeOpaqueFlags();
12361
12362 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012363 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012364 mHardwareLayer.setOpaque(opaque);
12365 mLocalDirtyRect.set(0, 0, width, height);
12366 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012367 }
12368
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012369 // The layer is not valid if the underlying GPU resources cannot be allocated
12370 if (!mHardwareLayer.isValid()) {
12371 return null;
12372 }
12373
Romain Guy11cb6422012-09-21 00:39:43 -070012374 mHardwareLayer.setLayerPaint(mLayerPaint);
12375 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12376 ViewRootImpl viewRoot = getViewRootImpl();
12377 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12378
Michael Jurka7e52caf2012-03-06 15:57:06 -080012379 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012380 }
12381
12382 return mHardwareLayer;
12383 }
Romain Guy171c5922011-01-06 10:04:23 -080012384
Romain Guy589b0bb2011-10-10 13:57:47 -070012385 /**
12386 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012387 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012388 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012389 *
12390 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012391 * @see #LAYER_TYPE_HARDWARE
12392 */
Romain Guya998dff2012-03-23 18:58:36 -070012393 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012394 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012395 AttachInfo info = mAttachInfo;
12396 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012397 info.mHardwareRenderer.isEnabled() &&
12398 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012399 mHardwareLayer.destroy();
12400 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012401
Romain Guy9c4b79a2011-11-10 19:23:58 -080012402 invalidate(true);
12403 invalidateParentCaches();
12404 }
Romain Guy65b345f2011-07-27 18:51:50 -070012405 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012406 }
Romain Guy65b345f2011-07-27 18:51:50 -070012407 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012408 }
12409
Romain Guy171c5922011-01-06 10:04:23 -080012410 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012411 * Destroys all hardware rendering resources. This method is invoked
12412 * when the system needs to reclaim resources. Upon execution of this
12413 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012414 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012415 * Note: you <strong>must</strong> call
12416 * <code>super.destroyHardwareResources()</code> when overriding
12417 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012418 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012419 * @hide
12420 */
12421 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012422 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012423 }
12424
12425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012426 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12427 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12428 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12429 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12430 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12431 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012432 *
Romain Guy171c5922011-01-06 10:04:23 -080012433 * <p>Enabling the drawing cache is similar to
12434 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012435 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12436 * drawing cache has no effect on rendering because the system uses a different mechanism
12437 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12438 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12439 * for information on how to enable software and hardware layers.</p>
12440 *
12441 * <p>This API can be used to manually generate
12442 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12443 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012444 *
12445 * @param enabled true to enable the drawing cache, false otherwise
12446 *
12447 * @see #isDrawingCacheEnabled()
12448 * @see #getDrawingCache()
12449 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012450 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451 */
12452 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012453 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012454 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12455 }
12456
12457 /**
12458 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12459 *
12460 * @return true if the drawing cache is enabled
12461 *
12462 * @see #setDrawingCacheEnabled(boolean)
12463 * @see #getDrawingCache()
12464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012465 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012466 public boolean isDrawingCacheEnabled() {
12467 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12468 }
12469
12470 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012471 * Debugging utility which recursively outputs the dirty state of a view and its
12472 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012473 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012474 * @hide
12475 */
Romain Guy676b1732011-02-14 14:45:33 -080012476 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012477 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012478 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12479 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12480 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12481 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012482 if (clear) {
12483 mPrivateFlags &= clearMask;
12484 }
12485 if (this instanceof ViewGroup) {
12486 ViewGroup parent = (ViewGroup) this;
12487 final int count = parent.getChildCount();
12488 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012489 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012490 child.outputDirtyFlags(indent + " ", clear, clearMask);
12491 }
12492 }
12493 }
12494
12495 /**
12496 * This method is used by ViewGroup to cause its children to restore or recreate their
12497 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12498 * to recreate its own display list, which would happen if it went through the normal
12499 * draw/dispatchDraw mechanisms.
12500 *
12501 * @hide
12502 */
12503 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012504
12505 /**
12506 * A view that is not attached or hardware accelerated cannot create a display list.
12507 * This method checks these conditions and returns the appropriate result.
12508 *
12509 * @return true if view has the ability to create a display list, false otherwise.
12510 *
12511 * @hide
12512 */
12513 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012514 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012515 }
Joe Malin32736f02011-01-19 16:14:20 -080012516
Chet Haasedaf98e92011-01-10 14:10:36 -080012517 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012518 * @return The HardwareRenderer associated with that view or null if hardware rendering
12519 * is not supported or this this has not been attached to a window.
12520 *
12521 * @hide
12522 */
12523 public HardwareRenderer getHardwareRenderer() {
12524 if (mAttachInfo != null) {
12525 return mAttachInfo.mHardwareRenderer;
12526 }
12527 return null;
12528 }
12529
12530 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012531 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12532 * Otherwise, the same display list will be returned (after having been rendered into
12533 * along the way, depending on the invalidation state of the view).
12534 *
12535 * @param displayList The previous version of this displayList, could be null.
12536 * @param isLayer Whether the requester of the display list is a layer. If so,
12537 * the view will avoid creating a layer inside the resulting display list.
12538 * @return A new or reused DisplayList object.
12539 */
12540 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12541 if (!canHaveDisplayList()) {
12542 return null;
12543 }
12544
Dianne Hackborn4702a852012-08-17 15:18:29 -070012545 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012546 displayList == null || !displayList.isValid() ||
12547 (!isLayer && mRecreateDisplayList))) {
12548 // Don't need to recreate the display list, just need to tell our
12549 // children to restore/recreate theirs
12550 if (displayList != null && displayList.isValid() &&
12551 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012552 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12553 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012554 dispatchGetDisplayList();
12555
12556 return displayList;
12557 }
12558
12559 if (!isLayer) {
12560 // If we got here, we're recreating it. Mark it as such to ensure that
12561 // we copy in child display lists into ours in drawChild()
12562 mRecreateDisplayList = true;
12563 }
12564 if (displayList == null) {
12565 final String name = getClass().getSimpleName();
12566 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12567 // If we're creating a new display list, make sure our parent gets invalidated
12568 // since they will need to recreate their display list to account for this
12569 // new child display list.
12570 invalidateParentCaches();
12571 }
12572
12573 boolean caching = false;
12574 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012575 int width = mRight - mLeft;
12576 int height = mBottom - mTop;
12577
12578 try {
12579 canvas.setViewport(width, height);
12580 // The dirty rect should always be null for a display list
12581 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012582 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012583 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012584 if (layerType == LAYER_TYPE_HARDWARE) {
12585 final HardwareLayer layer = getHardwareLayer();
12586 if (layer != null && layer.isValid()) {
12587 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12588 } else {
12589 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12590 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12591 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12592 }
12593 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012594 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012595 buildDrawingCache(true);
12596 Bitmap cache = getDrawingCache(true);
12597 if (cache != null) {
12598 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12599 caching = true;
12600 }
Chet Haasea1cff502012-02-21 13:43:44 -080012601 }
Chet Haasea1cff502012-02-21 13:43:44 -080012602 } else {
12603
12604 computeScroll();
12605
Chet Haasea1cff502012-02-21 13:43:44 -080012606 canvas.translate(-mScrollX, -mScrollY);
12607 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012608 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12609 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012610 }
12611
12612 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012613 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012614 dispatchDraw(canvas);
12615 } else {
12616 draw(canvas);
12617 }
12618 }
12619 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012620 canvas.onPostDraw();
12621
12622 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012623 displayList.setCaching(caching);
12624 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012625 displayList.setLeftTopRightBottom(0, 0, width, height);
12626 } else {
12627 setDisplayListProperties(displayList);
12628 }
12629 }
12630 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012631 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12632 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012633 }
12634
12635 return displayList;
12636 }
12637
12638 /**
12639 * Get the DisplayList for the HardwareLayer
12640 *
12641 * @param layer The HardwareLayer whose DisplayList we want
12642 * @return A DisplayList fopr the specified HardwareLayer
12643 */
12644 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12645 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12646 layer.setDisplayList(displayList);
12647 return displayList;
12648 }
12649
12650
12651 /**
Romain Guyb051e892010-09-28 19:09:36 -070012652 * <p>Returns a display list that can be used to draw this view again
12653 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012654 *
Romain Guyb051e892010-09-28 19:09:36 -070012655 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012656 *
12657 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012658 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012659 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012660 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012661 return mDisplayList;
12662 }
12663
Romain Guy38c2ece2012-05-24 14:20:56 -070012664 private void clearDisplayList() {
12665 if (mDisplayList != null) {
12666 mDisplayList.invalidate();
12667 mDisplayList.clear();
12668 }
12669 }
12670
Romain Guyb051e892010-09-28 19:09:36 -070012671 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012672 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012673 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012674 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012675 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012676 * @see #getDrawingCache(boolean)
12677 */
12678 public Bitmap getDrawingCache() {
12679 return getDrawingCache(false);
12680 }
12681
12682 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012683 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12684 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12685 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12686 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12687 * request the drawing cache by calling this method and draw it on screen if the
12688 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012689 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012690 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12691 * this method will create a bitmap of the same size as this view. Because this bitmap
12692 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12693 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12694 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12695 * size than the view. This implies that your application must be able to handle this
12696 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012697 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012698 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12699 * the current density of the screen when the application is in compatibility
12700 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012701 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012702 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012703 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012704 * @see #setDrawingCacheEnabled(boolean)
12705 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012706 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 * @see #destroyDrawingCache()
12708 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012709 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12711 return null;
12712 }
12713 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012714 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012715 }
Romain Guy02890fd2010-08-06 17:58:44 -070012716 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 }
12718
12719 /**
12720 * <p>Frees the resources used by the drawing cache. If you call
12721 * {@link #buildDrawingCache()} manually without calling
12722 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12723 * should cleanup the cache with this method afterwards.</p>
12724 *
12725 * @see #setDrawingCacheEnabled(boolean)
12726 * @see #buildDrawingCache()
12727 * @see #getDrawingCache()
12728 */
12729 public void destroyDrawingCache() {
12730 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012731 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 mDrawingCache = null;
12733 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012734 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012735 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012736 mUnscaledDrawingCache = null;
12737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 }
12739
12740 /**
12741 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012742 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012743 * view will always be drawn on top of a solid color.
12744 *
12745 * @param color The background color to use for the drawing cache's bitmap
12746 *
12747 * @see #setDrawingCacheEnabled(boolean)
12748 * @see #buildDrawingCache()
12749 * @see #getDrawingCache()
12750 */
12751 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012752 if (color != mDrawingCacheBackgroundColor) {
12753 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012754 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 }
12757
12758 /**
12759 * @see #setDrawingCacheBackgroundColor(int)
12760 *
12761 * @return The background color to used for the drawing cache's bitmap
12762 */
12763 public int getDrawingCacheBackgroundColor() {
12764 return mDrawingCacheBackgroundColor;
12765 }
12766
12767 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012768 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012769 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012770 * @see #buildDrawingCache(boolean)
12771 */
12772 public void buildDrawingCache() {
12773 buildDrawingCache(false);
12774 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012775
Romain Guyfbd8f692009-06-26 14:51:58 -070012776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12778 *
12779 * <p>If you call {@link #buildDrawingCache()} manually without calling
12780 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12781 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012782 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012783 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12784 * this method will create a bitmap of the same size as this view. Because this bitmap
12785 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12786 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12787 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12788 * size than the view. This implies that your application must be able to handle this
12789 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012790 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012791 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12792 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012793 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012794 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012795 *
12796 * @see #getDrawingCache()
12797 * @see #destroyDrawingCache()
12798 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012799 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012800 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012801 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012802 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012803
Romain Guy8506ab42009-06-11 17:35:47 -070012804 int width = mRight - mLeft;
12805 int height = mBottom - mTop;
12806
12807 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012808 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012809
Romain Guye1123222009-06-29 14:24:56 -070012810 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012811 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12812 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012814
12815 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012816 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012817 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012818
Chris Craik10e9d1d2012-09-06 14:42:44 -070012819 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12820 final int drawingCacheSize =
12821 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12822 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012823 if (width > 0 && height > 0) {
12824 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12825 + projectedBitmapSize + " bytes, only "
12826 + drawingCacheSize + " available");
12827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012828 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012829 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012830 return;
12831 }
12832
12833 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012834 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012835
12836 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837 Bitmap.Config quality;
12838 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012839 // Never pick ARGB_4444 because it looks awful
12840 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012841 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12842 case DRAWING_CACHE_QUALITY_AUTO:
12843 quality = Bitmap.Config.ARGB_8888;
12844 break;
12845 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012846 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012847 break;
12848 case DRAWING_CACHE_QUALITY_HIGH:
12849 quality = Bitmap.Config.ARGB_8888;
12850 break;
12851 default:
12852 quality = Bitmap.Config.ARGB_8888;
12853 break;
12854 }
12855 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012856 // Optimization for translucent windows
12857 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012858 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859 }
12860
12861 // Try to cleanup memory
12862 if (bitmap != null) bitmap.recycle();
12863
12864 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012865 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12866 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012867 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012868 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012869 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012870 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012871 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012872 }
Adam Powell26153a32010-11-08 15:22:27 -080012873 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012874 } catch (OutOfMemoryError e) {
12875 // If there is not enough memory to create the bitmap cache, just
12876 // ignore the issue as bitmap caches are not required to draw the
12877 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012878 if (autoScale) {
12879 mDrawingCache = null;
12880 } else {
12881 mUnscaledDrawingCache = null;
12882 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012883 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012884 return;
12885 }
12886
12887 clear = drawingCacheBackgroundColor != 0;
12888 }
12889
12890 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 if (attachInfo != null) {
12892 canvas = attachInfo.mCanvas;
12893 if (canvas == null) {
12894 canvas = new Canvas();
12895 }
12896 canvas.setBitmap(bitmap);
12897 // Temporarily clobber the cached Canvas in case one of our children
12898 // is also using a drawing cache. Without this, the children would
12899 // steal the canvas by attaching their own bitmap to it and bad, bad
12900 // thing would happen (invisible views, corrupted drawings, etc.)
12901 attachInfo.mCanvas = null;
12902 } else {
12903 // This case should hopefully never or seldom happen
12904 canvas = new Canvas(bitmap);
12905 }
12906
12907 if (clear) {
12908 bitmap.eraseColor(drawingCacheBackgroundColor);
12909 }
12910
12911 computeScroll();
12912 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012913
Romain Guye1123222009-06-29 14:24:56 -070012914 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012915 final float scale = attachInfo.mApplicationScale;
12916 canvas.scale(scale, scale);
12917 }
Joe Malin32736f02011-01-19 16:14:20 -080012918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919 canvas.translate(-mScrollX, -mScrollY);
12920
Dianne Hackborn4702a852012-08-17 15:18:29 -070012921 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012922 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12923 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012924 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012926
12927 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012928 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12929 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012930 dispatchDraw(canvas);
12931 } else {
12932 draw(canvas);
12933 }
12934
12935 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012936 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937
12938 if (attachInfo != null) {
12939 // Restore the cached Canvas for our siblings
12940 attachInfo.mCanvas = canvas;
12941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012942 }
12943 }
12944
12945 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012946 * Create a snapshot of the view into a bitmap. We should probably make
12947 * some form of this public, but should think about the API.
12948 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012949 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012950 int width = mRight - mLeft;
12951 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012952
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012953 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012954 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012955 width = (int) ((width * scale) + 0.5f);
12956 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012957
Dianne Hackborndde331c2012-08-03 14:01:57 -070012958 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12959 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012960 if (bitmap == null) {
12961 throw new OutOfMemoryError();
12962 }
12963
Romain Guyc529d8d2011-09-06 15:01:39 -070012964 Resources resources = getResources();
12965 if (resources != null) {
12966 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12967 }
Joe Malin32736f02011-01-19 16:14:20 -080012968
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012969 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012970 if (attachInfo != null) {
12971 canvas = attachInfo.mCanvas;
12972 if (canvas == null) {
12973 canvas = new Canvas();
12974 }
12975 canvas.setBitmap(bitmap);
12976 // Temporarily clobber the cached Canvas in case one of our children
12977 // is also using a drawing cache. Without this, the children would
12978 // steal the canvas by attaching their own bitmap to it and bad, bad
12979 // things would happen (invisible views, corrupted drawings, etc.)
12980 attachInfo.mCanvas = null;
12981 } else {
12982 // This case should hopefully never or seldom happen
12983 canvas = new Canvas(bitmap);
12984 }
12985
Romain Guy5bcdff42009-05-14 21:27:18 -070012986 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012987 bitmap.eraseColor(backgroundColor);
12988 }
12989
12990 computeScroll();
12991 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012992 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012993 canvas.translate(-mScrollX, -mScrollY);
12994
Romain Guy5bcdff42009-05-14 21:27:18 -070012995 // Temporarily remove the dirty mask
12996 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012997 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012998
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012999 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013000 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013001 dispatchDraw(canvas);
13002 } else {
13003 draw(canvas);
13004 }
13005
Romain Guy5bcdff42009-05-14 21:27:18 -070013006 mPrivateFlags = flags;
13007
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013008 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013009 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013010
13011 if (attachInfo != null) {
13012 // Restore the cached Canvas for our siblings
13013 attachInfo.mCanvas = canvas;
13014 }
Romain Guy8506ab42009-06-11 17:35:47 -070013015
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013016 return bitmap;
13017 }
13018
13019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013020 * Indicates whether this View is currently in edit mode. A View is usually
13021 * in edit mode when displayed within a developer tool. For instance, if
13022 * this View is being drawn by a visual user interface builder, this method
13023 * should return true.
13024 *
13025 * Subclasses should check the return value of this method to provide
13026 * different behaviors if their normal behavior might interfere with the
13027 * host environment. For instance: the class spawns a thread in its
13028 * constructor, the drawing code relies on device-specific features, etc.
13029 *
13030 * This method is usually checked in the drawing code of custom widgets.
13031 *
13032 * @return True if this View is in edit mode, false otherwise.
13033 */
13034 public boolean isInEditMode() {
13035 return false;
13036 }
13037
13038 /**
13039 * If the View draws content inside its padding and enables fading edges,
13040 * it needs to support padding offsets. Padding offsets are added to the
13041 * fading edges to extend the length of the fade so that it covers pixels
13042 * drawn inside the padding.
13043 *
13044 * Subclasses of this class should override this method if they need
13045 * to draw content inside the padding.
13046 *
13047 * @return True if padding offset must be applied, false otherwise.
13048 *
13049 * @see #getLeftPaddingOffset()
13050 * @see #getRightPaddingOffset()
13051 * @see #getTopPaddingOffset()
13052 * @see #getBottomPaddingOffset()
13053 *
13054 * @since CURRENT
13055 */
13056 protected boolean isPaddingOffsetRequired() {
13057 return false;
13058 }
13059
13060 /**
13061 * Amount by which to extend the left fading region. Called only when
13062 * {@link #isPaddingOffsetRequired()} returns true.
13063 *
13064 * @return The left padding offset in pixels.
13065 *
13066 * @see #isPaddingOffsetRequired()
13067 *
13068 * @since CURRENT
13069 */
13070 protected int getLeftPaddingOffset() {
13071 return 0;
13072 }
13073
13074 /**
13075 * Amount by which to extend the right fading region. Called only when
13076 * {@link #isPaddingOffsetRequired()} returns true.
13077 *
13078 * @return The right padding offset in pixels.
13079 *
13080 * @see #isPaddingOffsetRequired()
13081 *
13082 * @since CURRENT
13083 */
13084 protected int getRightPaddingOffset() {
13085 return 0;
13086 }
13087
13088 /**
13089 * Amount by which to extend the top fading region. Called only when
13090 * {@link #isPaddingOffsetRequired()} returns true.
13091 *
13092 * @return The top padding offset in pixels.
13093 *
13094 * @see #isPaddingOffsetRequired()
13095 *
13096 * @since CURRENT
13097 */
13098 protected int getTopPaddingOffset() {
13099 return 0;
13100 }
13101
13102 /**
13103 * Amount by which to extend the bottom fading region. Called only when
13104 * {@link #isPaddingOffsetRequired()} returns true.
13105 *
13106 * @return The bottom padding offset in pixels.
13107 *
13108 * @see #isPaddingOffsetRequired()
13109 *
13110 * @since CURRENT
13111 */
13112 protected int getBottomPaddingOffset() {
13113 return 0;
13114 }
13115
13116 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013117 * @hide
13118 * @param offsetRequired
13119 */
13120 protected int getFadeTop(boolean offsetRequired) {
13121 int top = mPaddingTop;
13122 if (offsetRequired) top += getTopPaddingOffset();
13123 return top;
13124 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013125
Romain Guyf2fc4602011-07-19 15:20:03 -070013126 /**
13127 * @hide
13128 * @param offsetRequired
13129 */
13130 protected int getFadeHeight(boolean offsetRequired) {
13131 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013132 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013133 return mBottom - mTop - mPaddingBottom - padding;
13134 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013135
Romain Guyf2fc4602011-07-19 15:20:03 -070013136 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013137 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013138 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013139 *
Romain Guy2bffd262010-09-12 17:40:02 -070013140 * <p>Even if this method returns true, it does not mean that every call
13141 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13142 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013143 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013144 * window is hardware accelerated,
13145 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13146 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013147 *
Romain Guy2bffd262010-09-12 17:40:02 -070013148 * @return True if the view is attached to a window and the window is
13149 * hardware accelerated; false in any other case.
13150 */
13151 public boolean isHardwareAccelerated() {
13152 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13153 }
Joe Malin32736f02011-01-19 16:14:20 -080013154
Romain Guy2bffd262010-09-12 17:40:02 -070013155 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013156 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13157 * case of an active Animation being run on the view.
13158 */
13159 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13160 Animation a, boolean scalingRequired) {
13161 Transformation invalidationTransform;
13162 final int flags = parent.mGroupFlags;
13163 final boolean initialized = a.isInitialized();
13164 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013165 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013166 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013167 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013168 onAnimationStart();
13169 }
13170
13171 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13172 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13173 if (parent.mInvalidationTransformation == null) {
13174 parent.mInvalidationTransformation = new Transformation();
13175 }
13176 invalidationTransform = parent.mInvalidationTransformation;
13177 a.getTransformation(drawingTime, invalidationTransform, 1f);
13178 } else {
13179 invalidationTransform = parent.mChildTransformation;
13180 }
Romain Guy393a52c2012-05-22 20:21:08 -070013181
Chet Haasebcca79a2012-02-14 08:45:14 -080013182 if (more) {
13183 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013184 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13185 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13186 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13187 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013188 // The child need to draw an animation, potentially offscreen, so
13189 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013190 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013191 parent.invalidate(mLeft, mTop, mRight, mBottom);
13192 }
13193 } else {
13194 if (parent.mInvalidateRegion == null) {
13195 parent.mInvalidateRegion = new RectF();
13196 }
13197 final RectF region = parent.mInvalidateRegion;
13198 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13199 invalidationTransform);
13200
13201 // The child need to draw an animation, potentially offscreen, so
13202 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013203 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013204
13205 final int left = mLeft + (int) region.left;
13206 final int top = mTop + (int) region.top;
13207 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13208 top + (int) (region.height() + .5f));
13209 }
13210 }
13211 return more;
13212 }
13213
Chet Haasea1cff502012-02-21 13:43:44 -080013214 /**
13215 * This method is called by getDisplayList() when a display list is created or re-rendered.
13216 * It sets or resets the current value of all properties on that display list (resetting is
13217 * necessary when a display list is being re-created, because we need to make sure that
13218 * previously-set transform values
13219 */
13220 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013221 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013222 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013223 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013224 if (mParent instanceof ViewGroup) {
13225 displayList.setClipChildren(
13226 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13227 }
Chet Haase9420abd2012-03-29 16:28:32 -070013228 float alpha = 1;
13229 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13230 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13231 ViewGroup parentVG = (ViewGroup) mParent;
13232 final boolean hasTransform =
13233 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13234 if (hasTransform) {
13235 Transformation transform = parentVG.mChildTransformation;
13236 final int transformType = parentVG.mChildTransformation.getTransformationType();
13237 if (transformType != Transformation.TYPE_IDENTITY) {
13238 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13239 alpha = transform.getAlpha();
13240 }
13241 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13242 displayList.setStaticMatrix(transform.getMatrix());
13243 }
13244 }
13245 }
Chet Haasea1cff502012-02-21 13:43:44 -080013246 }
13247 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013248 alpha *= mTransformationInfo.mAlpha;
13249 if (alpha < 1) {
13250 final int multipliedAlpha = (int) (255 * alpha);
13251 if (onSetAlpha(multipliedAlpha)) {
13252 alpha = 1;
13253 }
13254 }
13255 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013256 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13257 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13258 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13259 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013260 if (mTransformationInfo.mCamera == null) {
13261 mTransformationInfo.mCamera = new Camera();
13262 mTransformationInfo.matrix3D = new Matrix();
13263 }
13264 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013265 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013266 displayList.setPivotX(getPivotX());
13267 displayList.setPivotY(getPivotY());
13268 }
Chet Haase9420abd2012-03-29 16:28:32 -070013269 } else if (alpha < 1) {
13270 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013271 }
13272 }
13273 }
13274
Chet Haasebcca79a2012-02-14 08:45:14 -080013275 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013276 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13277 * This draw() method is an implementation detail and is not intended to be overridden or
13278 * to be called from anywhere else other than ViewGroup.drawChild().
13279 */
13280 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013281 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013282 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013283 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013284 final int flags = parent.mGroupFlags;
13285
Chet Haasea1cff502012-02-21 13:43:44 -080013286 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013287 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013288 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013289 }
13290
13291 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013292 boolean concatMatrix = false;
13293
13294 boolean scalingRequired = false;
13295 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013296 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013297
13298 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013299 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13300 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013301 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013302 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013303 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13304 } else {
13305 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13306 }
13307
Chet Haasebcca79a2012-02-14 08:45:14 -080013308 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013309 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013310 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013311 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013312 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013313 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013314 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013315 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013316 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013317 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013318 mDisplayList != null) {
13319 // No longer animating: clear out old animation matrix
13320 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013321 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013322 }
13323 if (!useDisplayListProperties &&
13324 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13325 final boolean hasTransform =
13326 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13327 if (hasTransform) {
13328 final int transformType = parent.mChildTransformation.getTransformationType();
13329 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13330 parent.mChildTransformation : null;
13331 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13332 }
Chet Haase64a48c12012-02-13 16:33:29 -080013333 }
13334 }
13335
13336 concatMatrix |= !childHasIdentityMatrix;
13337
13338 // Sets the flag as early as possible to allow draw() implementations
13339 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013340 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013341
Romain Guyfbb43212012-08-30 15:19:27 -070013342 if (!concatMatrix &&
13343 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13344 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013345 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013346 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13347 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013348 return more;
13349 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013350 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013351
13352 if (hardwareAccelerated) {
13353 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13354 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013355 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13356 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013357 }
13358
Chet Haase64a48c12012-02-13 16:33:29 -080013359 DisplayList displayList = null;
13360 Bitmap cache = null;
13361 boolean hasDisplayList = false;
13362 if (caching) {
13363 if (!hardwareAccelerated) {
13364 if (layerType != LAYER_TYPE_NONE) {
13365 layerType = LAYER_TYPE_SOFTWARE;
13366 buildDrawingCache(true);
13367 }
13368 cache = getDrawingCache(true);
13369 } else {
13370 switch (layerType) {
13371 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013372 if (useDisplayListProperties) {
13373 hasDisplayList = canHaveDisplayList();
13374 } else {
13375 buildDrawingCache(true);
13376 cache = getDrawingCache(true);
13377 }
Chet Haase64a48c12012-02-13 16:33:29 -080013378 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013379 case LAYER_TYPE_HARDWARE:
13380 if (useDisplayListProperties) {
13381 hasDisplayList = canHaveDisplayList();
13382 }
13383 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013384 case LAYER_TYPE_NONE:
13385 // Delay getting the display list until animation-driven alpha values are
13386 // set up and possibly passed on to the view
13387 hasDisplayList = canHaveDisplayList();
13388 break;
13389 }
13390 }
13391 }
Chet Haasea1cff502012-02-21 13:43:44 -080013392 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013393 if (useDisplayListProperties) {
13394 displayList = getDisplayList();
13395 if (!displayList.isValid()) {
13396 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13397 // to getDisplayList(), the display list will be marked invalid and we should not
13398 // try to use it again.
13399 displayList = null;
13400 hasDisplayList = false;
13401 useDisplayListProperties = false;
13402 }
13403 }
Chet Haase64a48c12012-02-13 16:33:29 -080013404
Chet Haase526057b2012-07-12 17:50:41 -070013405 int sx = 0;
13406 int sy = 0;
13407 if (!hasDisplayList) {
13408 computeScroll();
13409 sx = mScrollX;
13410 sy = mScrollY;
13411 }
13412
Chet Haase64a48c12012-02-13 16:33:29 -080013413 final boolean hasNoCache = cache == null || hasDisplayList;
13414 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13415 layerType != LAYER_TYPE_HARDWARE;
13416
Chet Haasea1cff502012-02-21 13:43:44 -080013417 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013418 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013419 restoreTo = canvas.save();
13420 }
Chet Haase64a48c12012-02-13 16:33:29 -080013421 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013422 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013423 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013424 if (!useDisplayListProperties) {
13425 canvas.translate(mLeft, mTop);
13426 }
Chet Haase64a48c12012-02-13 16:33:29 -080013427 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013428 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013429 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013430 restoreTo = canvas.save();
13431 }
Chet Haase64a48c12012-02-13 16:33:29 -080013432 // mAttachInfo cannot be null, otherwise scalingRequired == false
13433 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13434 canvas.scale(scale, scale);
13435 }
13436 }
13437
Chet Haasea1cff502012-02-21 13:43:44 -080013438 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013439 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013440 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013441 if (transformToApply != null || !childHasIdentityMatrix) {
13442 int transX = 0;
13443 int transY = 0;
13444
13445 if (offsetForScroll) {
13446 transX = -sx;
13447 transY = -sy;
13448 }
13449
13450 if (transformToApply != null) {
13451 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013452 if (useDisplayListProperties) {
13453 displayList.setAnimationMatrix(transformToApply.getMatrix());
13454 } else {
13455 // Undo the scroll translation, apply the transformation matrix,
13456 // then redo the scroll translate to get the correct result.
13457 canvas.translate(-transX, -transY);
13458 canvas.concat(transformToApply.getMatrix());
13459 canvas.translate(transX, transY);
13460 }
Chet Haasea1cff502012-02-21 13:43:44 -080013461 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013462 }
13463
13464 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013465 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013466 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013467 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013468 }
13469 }
13470
Chet Haasea1cff502012-02-21 13:43:44 -080013471 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013472 canvas.translate(-transX, -transY);
13473 canvas.concat(getMatrix());
13474 canvas.translate(transX, transY);
13475 }
13476 }
13477
Chet Haase21433372012-06-05 07:54:09 -070013478 // Deal with alpha if it is or used to be <1
13479 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013480 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013481 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013482 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013483 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013484 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013485 }
Chet Haasea1cff502012-02-21 13:43:44 -080013486 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013487 if (hasNoCache) {
13488 final int multipliedAlpha = (int) (255 * alpha);
13489 if (!onSetAlpha(multipliedAlpha)) {
13490 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013491 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013492 layerType != LAYER_TYPE_NONE) {
13493 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13494 }
Chet Haase9420abd2012-03-29 16:28:32 -070013495 if (useDisplayListProperties) {
13496 displayList.setAlpha(alpha * getAlpha());
13497 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013498 final int scrollX = hasDisplayList ? 0 : sx;
13499 final int scrollY = hasDisplayList ? 0 : sy;
13500 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13501 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013502 }
13503 } else {
13504 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013505 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013506 }
13507 }
13508 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013509 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013510 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013511 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013512 }
13513
Chet Haasea1cff502012-02-21 13:43:44 -080013514 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13515 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013516 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013517 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013518 } else {
13519 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013520 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013521 } else {
13522 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13523 }
13524 }
13525 }
13526
Chet Haase9420abd2012-03-29 16:28:32 -070013527 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013528 displayList = getDisplayList();
13529 if (!displayList.isValid()) {
13530 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13531 // to getDisplayList(), the display list will be marked invalid and we should not
13532 // try to use it again.
13533 displayList = null;
13534 hasDisplayList = false;
13535 }
13536 }
13537
13538 if (hasNoCache) {
13539 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013540 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013541 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013542 if (layer != null && layer.isValid()) {
13543 mLayerPaint.setAlpha((int) (alpha * 255));
13544 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13545 layerRendered = true;
13546 } else {
13547 final int scrollX = hasDisplayList ? 0 : sx;
13548 final int scrollY = hasDisplayList ? 0 : sy;
13549 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013550 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013551 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13552 }
13553 }
13554
13555 if (!layerRendered) {
13556 if (!hasDisplayList) {
13557 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013558 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13559 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013560 dispatchDraw(canvas);
13561 } else {
13562 draw(canvas);
13563 }
13564 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013565 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013566 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013567 }
13568 }
13569 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013570 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013571 Paint cachePaint;
13572
13573 if (layerType == LAYER_TYPE_NONE) {
13574 cachePaint = parent.mCachePaint;
13575 if (cachePaint == null) {
13576 cachePaint = new Paint();
13577 cachePaint.setDither(false);
13578 parent.mCachePaint = cachePaint;
13579 }
Chet Haase9420abd2012-03-29 16:28:32 -070013580 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013581 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013582 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13583 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013584 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013585 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013586 }
13587 } else {
13588 cachePaint = mLayerPaint;
13589 cachePaint.setAlpha((int) (alpha * 255));
13590 }
13591 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13592 }
13593
Chet Haasea1cff502012-02-21 13:43:44 -080013594 if (restoreTo >= 0) {
13595 canvas.restoreToCount(restoreTo);
13596 }
Chet Haase64a48c12012-02-13 16:33:29 -080013597
13598 if (a != null && !more) {
13599 if (!hardwareAccelerated && !a.getFillAfter()) {
13600 onSetAlpha(255);
13601 }
13602 parent.finishAnimatingView(this, a);
13603 }
13604
13605 if (more && hardwareAccelerated) {
13606 // invalidation is the trigger to recreate display lists, so if we're using
13607 // display lists to render, force an invalidate to allow the animation to
13608 // continue drawing another frame
13609 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013610 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013611 // alpha animations should cause the child to recreate its display list
13612 invalidate(true);
13613 }
13614 }
13615
13616 mRecreateDisplayList = false;
13617
13618 return more;
13619 }
13620
13621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 * Manually render this view (and all of its children) to the given Canvas.
13623 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013624 * called. When implementing a view, implement
13625 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13626 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013627 *
13628 * @param canvas The Canvas to which the View is rendered.
13629 */
13630 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013631 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013632 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013633 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013634 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013636 /*
13637 * Draw traversal performs several drawing steps which must be executed
13638 * in the appropriate order:
13639 *
13640 * 1. Draw the background
13641 * 2. If necessary, save the canvas' layers to prepare for fading
13642 * 3. Draw view's content
13643 * 4. Draw children
13644 * 5. If necessary, draw the fading edges and restore layers
13645 * 6. Draw decorations (scrollbars for instance)
13646 */
13647
13648 // Step 1, draw the background, if needed
13649 int saveCount;
13650
Romain Guy24443ea2009-05-11 11:56:30 -070013651 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013652 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013653 if (background != null) {
13654 final int scrollX = mScrollX;
13655 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656
Romain Guy24443ea2009-05-11 11:56:30 -070013657 if (mBackgroundSizeChanged) {
13658 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13659 mBackgroundSizeChanged = false;
13660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013661
Romain Guy24443ea2009-05-11 11:56:30 -070013662 if ((scrollX | scrollY) == 0) {
13663 background.draw(canvas);
13664 } else {
13665 canvas.translate(scrollX, scrollY);
13666 background.draw(canvas);
13667 canvas.translate(-scrollX, -scrollY);
13668 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013669 }
13670 }
13671
13672 // skip step 2 & 5 if possible (common case)
13673 final int viewFlags = mViewFlags;
13674 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13675 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13676 if (!verticalEdges && !horizontalEdges) {
13677 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013678 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013679
13680 // Step 4, draw the children
13681 dispatchDraw(canvas);
13682
13683 // Step 6, draw decorations (scrollbars)
13684 onDrawScrollBars(canvas);
13685
13686 // we're done...
13687 return;
13688 }
13689
13690 /*
13691 * Here we do the full fledged routine...
13692 * (this is an uncommon case where speed matters less,
13693 * this is why we repeat some of the tests that have been
13694 * done above)
13695 */
13696
13697 boolean drawTop = false;
13698 boolean drawBottom = false;
13699 boolean drawLeft = false;
13700 boolean drawRight = false;
13701
13702 float topFadeStrength = 0.0f;
13703 float bottomFadeStrength = 0.0f;
13704 float leftFadeStrength = 0.0f;
13705 float rightFadeStrength = 0.0f;
13706
13707 // Step 2, save the canvas' layers
13708 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013709
13710 final boolean offsetRequired = isPaddingOffsetRequired();
13711 if (offsetRequired) {
13712 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013713 }
13714
13715 int left = mScrollX + paddingLeft;
13716 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013717 int top = mScrollY + getFadeTop(offsetRequired);
13718 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013719
13720 if (offsetRequired) {
13721 right += getRightPaddingOffset();
13722 bottom += getBottomPaddingOffset();
13723 }
13724
13725 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013726 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013727 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728
13729 // clip the fade length if top and bottom fades overlap
13730 // overlapping fades produce odd-looking artifacts
13731 if (verticalEdges && (top + length > bottom - length)) {
13732 length = (bottom - top) / 2;
13733 }
13734
13735 // also clip horizontal fades if necessary
13736 if (horizontalEdges && (left + length > right - length)) {
13737 length = (right - left) / 2;
13738 }
13739
13740 if (verticalEdges) {
13741 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013742 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013744 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 }
13746
13747 if (horizontalEdges) {
13748 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013749 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013751 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 }
13753
13754 saveCount = canvas.getSaveCount();
13755
13756 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013757 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013758 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13759
13760 if (drawTop) {
13761 canvas.saveLayer(left, top, right, top + length, null, flags);
13762 }
13763
13764 if (drawBottom) {
13765 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13766 }
13767
13768 if (drawLeft) {
13769 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13770 }
13771
13772 if (drawRight) {
13773 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13774 }
13775 } else {
13776 scrollabilityCache.setFadeColor(solidColor);
13777 }
13778
13779 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013780 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013781
13782 // Step 4, draw the children
13783 dispatchDraw(canvas);
13784
13785 // Step 5, draw the fade effect and restore layers
13786 final Paint p = scrollabilityCache.paint;
13787 final Matrix matrix = scrollabilityCache.matrix;
13788 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013789
13790 if (drawTop) {
13791 matrix.setScale(1, fadeHeight * topFadeStrength);
13792 matrix.postTranslate(left, top);
13793 fade.setLocalMatrix(matrix);
13794 canvas.drawRect(left, top, right, top + length, p);
13795 }
13796
13797 if (drawBottom) {
13798 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13799 matrix.postRotate(180);
13800 matrix.postTranslate(left, bottom);
13801 fade.setLocalMatrix(matrix);
13802 canvas.drawRect(left, bottom - length, right, bottom, p);
13803 }
13804
13805 if (drawLeft) {
13806 matrix.setScale(1, fadeHeight * leftFadeStrength);
13807 matrix.postRotate(-90);
13808 matrix.postTranslate(left, top);
13809 fade.setLocalMatrix(matrix);
13810 canvas.drawRect(left, top, left + length, bottom, p);
13811 }
13812
13813 if (drawRight) {
13814 matrix.setScale(1, fadeHeight * rightFadeStrength);
13815 matrix.postRotate(90);
13816 matrix.postTranslate(right, top);
13817 fade.setLocalMatrix(matrix);
13818 canvas.drawRect(right - length, top, right, bottom, p);
13819 }
13820
13821 canvas.restoreToCount(saveCount);
13822
13823 // Step 6, draw decorations (scrollbars)
13824 onDrawScrollBars(canvas);
13825 }
13826
13827 /**
13828 * Override this if your view is known to always be drawn on top of a solid color background,
13829 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13830 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13831 * should be set to 0xFF.
13832 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013833 * @see #setVerticalFadingEdgeEnabled(boolean)
13834 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 *
13836 * @return The known solid color background for this view, or 0 if the color may vary
13837 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013838 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013839 public int getSolidColor() {
13840 return 0;
13841 }
13842
13843 /**
13844 * Build a human readable string representation of the specified view flags.
13845 *
13846 * @param flags the view flags to convert to a string
13847 * @return a String representing the supplied flags
13848 */
13849 private static String printFlags(int flags) {
13850 String output = "";
13851 int numFlags = 0;
13852 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13853 output += "TAKES_FOCUS";
13854 numFlags++;
13855 }
13856
13857 switch (flags & VISIBILITY_MASK) {
13858 case INVISIBLE:
13859 if (numFlags > 0) {
13860 output += " ";
13861 }
13862 output += "INVISIBLE";
13863 // USELESS HERE numFlags++;
13864 break;
13865 case GONE:
13866 if (numFlags > 0) {
13867 output += " ";
13868 }
13869 output += "GONE";
13870 // USELESS HERE numFlags++;
13871 break;
13872 default:
13873 break;
13874 }
13875 return output;
13876 }
13877
13878 /**
13879 * Build a human readable string representation of the specified private
13880 * view flags.
13881 *
13882 * @param privateFlags the private view flags to convert to a string
13883 * @return a String representing the supplied flags
13884 */
13885 private static String printPrivateFlags(int privateFlags) {
13886 String output = "";
13887 int numFlags = 0;
13888
Dianne Hackborn4702a852012-08-17 15:18:29 -070013889 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 output += "WANTS_FOCUS";
13891 numFlags++;
13892 }
13893
Dianne Hackborn4702a852012-08-17 15:18:29 -070013894 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 if (numFlags > 0) {
13896 output += " ";
13897 }
13898 output += "FOCUSED";
13899 numFlags++;
13900 }
13901
Dianne Hackborn4702a852012-08-17 15:18:29 -070013902 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 if (numFlags > 0) {
13904 output += " ";
13905 }
13906 output += "SELECTED";
13907 numFlags++;
13908 }
13909
Dianne Hackborn4702a852012-08-17 15:18:29 -070013910 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013911 if (numFlags > 0) {
13912 output += " ";
13913 }
13914 output += "IS_ROOT_NAMESPACE";
13915 numFlags++;
13916 }
13917
Dianne Hackborn4702a852012-08-17 15:18:29 -070013918 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013919 if (numFlags > 0) {
13920 output += " ";
13921 }
13922 output += "HAS_BOUNDS";
13923 numFlags++;
13924 }
13925
Dianne Hackborn4702a852012-08-17 15:18:29 -070013926 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013927 if (numFlags > 0) {
13928 output += " ";
13929 }
13930 output += "DRAWN";
13931 // USELESS HERE numFlags++;
13932 }
13933 return output;
13934 }
13935
13936 /**
13937 * <p>Indicates whether or not this view's layout will be requested during
13938 * the next hierarchy layout pass.</p>
13939 *
13940 * @return true if the layout will be forced during next layout pass
13941 */
13942 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013943 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 }
13945
13946 /**
13947 * Assign a size and position to a view and all of its
13948 * descendants
13949 *
13950 * <p>This is the second phase of the layout mechanism.
13951 * (The first is measuring). In this phase, each parent calls
13952 * layout on all of its children to position them.
13953 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013954 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 *
Chet Haase9c087442011-01-12 16:20:16 -080013956 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013957 * Derived classes with children should override
13958 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013959 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013960 *
13961 * @param l Left position, relative to parent
13962 * @param t Top position, relative to parent
13963 * @param r Right position, relative to parent
13964 * @param b Bottom position, relative to parent
13965 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013966 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013967 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013968 int oldL = mLeft;
13969 int oldT = mTop;
13970 int oldB = mBottom;
13971 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013973 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013975 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013976
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013977 ListenerInfo li = mListenerInfo;
13978 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013979 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013980 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013981 int numListeners = listenersCopy.size();
13982 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013983 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013984 }
13985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013986 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013987 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013988 }
13989
13990 /**
13991 * Called from layout when this view should
13992 * assign a size and position to each of its children.
13993 *
13994 * Derived classes with children should override
13995 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013996 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 * @param changed This is a new size or position for this view
13998 * @param left Left position, relative to parent
13999 * @param top Top position, relative to parent
14000 * @param right Right position, relative to parent
14001 * @param bottom Bottom position, relative to parent
14002 */
14003 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14004 }
14005
14006 /**
14007 * Assign a size and position to this view.
14008 *
14009 * This is called from layout.
14010 *
14011 * @param left Left position, relative to parent
14012 * @param top Top position, relative to parent
14013 * @param right Right position, relative to parent
14014 * @param bottom Bottom position, relative to parent
14015 * @return true if the new size and position are different than the
14016 * previous ones
14017 * {@hide}
14018 */
14019 protected boolean setFrame(int left, int top, int right, int bottom) {
14020 boolean changed = false;
14021
14022 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014023 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 + right + "," + bottom + ")");
14025 }
14026
14027 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14028 changed = true;
14029
14030 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014031 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014033 int oldWidth = mRight - mLeft;
14034 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014035 int newWidth = right - left;
14036 int newHeight = bottom - top;
14037 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14038
14039 // Invalidate our old position
14040 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014041
14042 mLeft = left;
14043 mTop = top;
14044 mRight = right;
14045 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014046 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014047 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049
Dianne Hackborn4702a852012-08-17 15:18:29 -070014050 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014052
Chet Haase75755e22011-07-18 17:48:25 -070014053 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014054 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014055 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014056 if (mTransformationInfo != null) {
14057 mTransformationInfo.mMatrixDirty = true;
14058 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014060 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14061 }
14062
14063 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14064 // If we are visible, force the DRAWN bit to on so that
14065 // this invalidate will go through (at least to our parent).
14066 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014067 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014069 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014070 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014071 // parent display list may need to be recreated based on a change in the bounds
14072 // of any child
14073 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 }
14075
14076 // Reset drawn bit to original value (invalidate turns it off)
14077 mPrivateFlags |= drawn;
14078
14079 mBackgroundSizeChanged = true;
14080 }
14081 return changed;
14082 }
14083
14084 /**
14085 * Finalize inflating a view from XML. This is called as the last phase
14086 * of inflation, after all child views have been added.
14087 *
14088 * <p>Even if the subclass overrides onFinishInflate, they should always be
14089 * sure to call the super method, so that we get called.
14090 */
14091 protected void onFinishInflate() {
14092 }
14093
14094 /**
14095 * Returns the resources associated with this view.
14096 *
14097 * @return Resources object.
14098 */
14099 public Resources getResources() {
14100 return mResources;
14101 }
14102
14103 /**
14104 * Invalidates the specified Drawable.
14105 *
14106 * @param drawable the drawable to invalidate
14107 */
14108 public void invalidateDrawable(Drawable drawable) {
14109 if (verifyDrawable(drawable)) {
14110 final Rect dirty = drawable.getBounds();
14111 final int scrollX = mScrollX;
14112 final int scrollY = mScrollY;
14113
14114 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14115 dirty.right + scrollX, dirty.bottom + scrollY);
14116 }
14117 }
14118
14119 /**
14120 * Schedules an action on a drawable to occur at a specified time.
14121 *
14122 * @param who the recipient of the action
14123 * @param what the action to run on the drawable
14124 * @param when the time at which the action must occur. Uses the
14125 * {@link SystemClock#uptimeMillis} timebase.
14126 */
14127 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014128 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014129 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014130 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014131 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14132 Choreographer.CALLBACK_ANIMATION, what, who,
14133 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014134 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014135 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014137 }
14138 }
14139
14140 /**
14141 * Cancels a scheduled action on a drawable.
14142 *
14143 * @param who the recipient of the action
14144 * @param what the action to cancel
14145 */
14146 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014147 if (verifyDrawable(who) && what != null) {
14148 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014149 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14150 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014151 } else {
14152 ViewRootImpl.getRunQueue().removeCallbacks(what);
14153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 }
14155 }
14156
14157 /**
14158 * Unschedule any events associated with the given Drawable. This can be
14159 * used when selecting a new Drawable into a view, so that the previous
14160 * one is completely unscheduled.
14161 *
14162 * @param who The Drawable to unschedule.
14163 *
14164 * @see #drawableStateChanged
14165 */
14166 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014167 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014168 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14169 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014170 }
14171 }
14172
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014173 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014174 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14175 * that the View directionality can and will be resolved before its Drawables.
14176 *
14177 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014178 *
14179 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014180 */
14181 public void resolveDrawables() {
14182 if (mBackground != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014183 mBackground.setLayoutDirection(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014184 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014185 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014186 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014187 }
14188
14189 /**
14190 * Called when layout direction has been resolved.
14191 *
14192 * The default implementation does nothing.
14193 *
14194 * @param layoutDirection The resolved layout direction.
14195 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014196 * @see #LAYOUT_DIRECTION_LTR
14197 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014198 *
14199 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014200 */
14201 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014202 }
14203
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014204 private void resetResolvedDrawables() {
14205 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14206 }
14207
14208 private boolean isDrawablesResolved() {
14209 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14210 }
14211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212 /**
14213 * If your view subclass is displaying its own Drawable objects, it should
14214 * override this function and return true for any Drawable it is
14215 * displaying. This allows animations for those drawables to be
14216 * scheduled.
14217 *
14218 * <p>Be sure to call through to the super class when overriding this
14219 * function.
14220 *
14221 * @param who The Drawable to verify. Return true if it is one you are
14222 * displaying, else return the result of calling through to the
14223 * super class.
14224 *
14225 * @return boolean If true than the Drawable is being displayed in the
14226 * view; else false and it is not allowed to animate.
14227 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014228 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14229 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014230 */
14231 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014232 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014233 }
14234
14235 /**
14236 * This function is called whenever the state of the view changes in such
14237 * a way that it impacts the state of drawables being shown.
14238 *
14239 * <p>Be sure to call through to the superclass when overriding this
14240 * function.
14241 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 */
14244 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014245 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246 if (d != null && d.isStateful()) {
14247 d.setState(getDrawableState());
14248 }
14249 }
14250
14251 /**
14252 * Call this to force a view to update its drawable state. This will cause
14253 * drawableStateChanged to be called on this view. Views that are interested
14254 * in the new state should call getDrawableState.
14255 *
14256 * @see #drawableStateChanged
14257 * @see #getDrawableState
14258 */
14259 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014260 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014261 drawableStateChanged();
14262
14263 ViewParent parent = mParent;
14264 if (parent != null) {
14265 parent.childDrawableStateChanged(this);
14266 }
14267 }
14268
14269 /**
14270 * Return an array of resource IDs of the drawable states representing the
14271 * current state of the view.
14272 *
14273 * @return The current drawable state
14274 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014275 * @see Drawable#setState(int[])
14276 * @see #drawableStateChanged()
14277 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014278 */
14279 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014280 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014281 return mDrawableState;
14282 } else {
14283 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014284 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014285 return mDrawableState;
14286 }
14287 }
14288
14289 /**
14290 * Generate the new {@link android.graphics.drawable.Drawable} state for
14291 * this view. This is called by the view
14292 * system when the cached Drawable state is determined to be invalid. To
14293 * retrieve the current state, you should use {@link #getDrawableState}.
14294 *
14295 * @param extraSpace if non-zero, this is the number of extra entries you
14296 * would like in the returned array in which you can place your own
14297 * states.
14298 *
14299 * @return Returns an array holding the current {@link Drawable} state of
14300 * the view.
14301 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014302 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014303 */
14304 protected int[] onCreateDrawableState(int extraSpace) {
14305 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14306 mParent instanceof View) {
14307 return ((View) mParent).onCreateDrawableState(extraSpace);
14308 }
14309
14310 int[] drawableState;
14311
14312 int privateFlags = mPrivateFlags;
14313
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014314 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014315 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014316 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14317 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014318 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014319 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014320 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014321 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14322 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014323 // This is set if HW acceleration is requested, even if the current
14324 // process doesn't allow it. This is just to allow app preview
14325 // windows to better match their app.
14326 viewStateIndex |= VIEW_STATE_ACCELERATED;
14327 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014328 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014329
Christopher Tate3d4bf172011-03-28 16:16:46 -070014330 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014331 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14332 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014334 drawableState = VIEW_STATE_SETS[viewStateIndex];
14335
14336 //noinspection ConstantIfStatement
14337 if (false) {
14338 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14339 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014340 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14342 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014343 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014344 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014345 + ": " + Arrays.toString(drawableState));
14346 }
14347
14348 if (extraSpace == 0) {
14349 return drawableState;
14350 }
14351
14352 final int[] fullState;
14353 if (drawableState != null) {
14354 fullState = new int[drawableState.length + extraSpace];
14355 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14356 } else {
14357 fullState = new int[extraSpace];
14358 }
14359
14360 return fullState;
14361 }
14362
14363 /**
14364 * Merge your own state values in <var>additionalState</var> into the base
14365 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014366 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014367 *
14368 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014369 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014370 * own additional state values.
14371 *
14372 * @param additionalState The additional state values you would like
14373 * added to <var>baseState</var>; this array is not modified.
14374 *
14375 * @return As a convenience, the <var>baseState</var> array you originally
14376 * passed into the function is returned.
14377 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014378 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014379 */
14380 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14381 final int N = baseState.length;
14382 int i = N - 1;
14383 while (i >= 0 && baseState[i] == 0) {
14384 i--;
14385 }
14386 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14387 return baseState;
14388 }
14389
14390 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014391 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14392 * on all Drawable objects associated with this view.
14393 */
14394 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014395 if (mBackground != null) {
14396 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014397 }
14398 }
14399
14400 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014401 * Sets the background color for this view.
14402 * @param color the color of the background
14403 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014404 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014405 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014406 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014407 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014408 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014409 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014410 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412 }
14413
14414 /**
14415 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014416 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014418 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014419 * @attr ref android.R.styleable#View_background
14420 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014421 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 public void setBackgroundResource(int resid) {
14423 if (resid != 0 && resid == mBackgroundResource) {
14424 return;
14425 }
14426
14427 Drawable d= null;
14428 if (resid != 0) {
14429 d = mResources.getDrawable(resid);
14430 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014431 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432
14433 mBackgroundResource = resid;
14434 }
14435
14436 /**
14437 * Set the background to a given Drawable, or remove the background. If the
14438 * background has padding, this View's padding is set to the background's
14439 * padding. However, when a background is removed, this View's padding isn't
14440 * touched. If setting the padding is desired, please use
14441 * {@link #setPadding(int, int, int, int)}.
14442 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014443 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014444 * background
14445 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014446 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014447 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014448 setBackgroundDrawable(background);
14449 }
14450
14451 /**
14452 * @deprecated use {@link #setBackground(Drawable)} instead
14453 */
14454 @Deprecated
14455 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014456 computeOpaqueFlags();
14457
Philip Milne6c8ea062012-04-03 17:38:43 -070014458 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014459 return;
14460 }
14461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014462 boolean requestLayout = false;
14463
14464 mBackgroundResource = 0;
14465
14466 /*
14467 * Regardless of whether we're setting a new background or not, we want
14468 * to clear the previous drawable.
14469 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014470 if (mBackground != null) {
14471 mBackground.setCallback(null);
14472 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014473 }
14474
Philip Milne6c8ea062012-04-03 17:38:43 -070014475 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014476 Rect padding = sThreadLocal.get();
14477 if (padding == null) {
14478 padding = new Rect();
14479 sThreadLocal.set(padding);
14480 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014481 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014482 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014483 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014484 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014485 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014486 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014487 mUserPaddingLeftInitial = padding.right;
14488 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014489 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014490 break;
14491 case LAYOUT_DIRECTION_LTR:
14492 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014493 mUserPaddingLeftInitial = padding.left;
14494 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014495 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014497 }
14498
14499 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14500 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014501 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14502 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014503 requestLayout = true;
14504 }
14505
Philip Milne6c8ea062012-04-03 17:38:43 -070014506 background.setCallback(this);
14507 if (background.isStateful()) {
14508 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014509 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014510 background.setVisible(getVisibility() == VISIBLE, false);
14511 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014512
Dianne Hackborn4702a852012-08-17 15:18:29 -070014513 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14514 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14515 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516 requestLayout = true;
14517 }
14518 } else {
14519 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014520 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014521
Dianne Hackborn4702a852012-08-17 15:18:29 -070014522 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014523 /*
14524 * This view ONLY drew the background before and we're removing
14525 * the background, so now it won't draw anything
14526 * (hence we SKIP_DRAW)
14527 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014528 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14529 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014530 }
14531
14532 /*
14533 * When the background is set, we try to apply its padding to this
14534 * View. When the background is removed, we don't touch this View's
14535 * padding. This is noted in the Javadocs. Hence, we don't need to
14536 * requestLayout(), the invalidate() below is sufficient.
14537 */
14538
14539 // The old background's minimum size could have affected this
14540 // View's layout, so let's requestLayout
14541 requestLayout = true;
14542 }
14543
Romain Guy8f1344f52009-05-15 16:03:59 -070014544 computeOpaqueFlags();
14545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 if (requestLayout) {
14547 requestLayout();
14548 }
14549
14550 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014551 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 }
14553
14554 /**
14555 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014557 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014558 *
14559 * @see #setBackground(Drawable)
14560 *
14561 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 */
14563 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014564 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014565 }
14566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014567 /**
14568 * Sets the padding. The view may add on the space required to display
14569 * the scrollbars, depending on the style and visibility of the scrollbars.
14570 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14571 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14572 * from the values set in this call.
14573 *
14574 * @attr ref android.R.styleable#View_padding
14575 * @attr ref android.R.styleable#View_paddingBottom
14576 * @attr ref android.R.styleable#View_paddingLeft
14577 * @attr ref android.R.styleable#View_paddingRight
14578 * @attr ref android.R.styleable#View_paddingTop
14579 * @param left the left padding in pixels
14580 * @param top the top padding in pixels
14581 * @param right the right padding in pixels
14582 * @param bottom the bottom padding in pixels
14583 */
14584 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014585 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014586
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014587 mUserPaddingStart = UNDEFINED_PADDING;
14588 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014589
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014590 mUserPaddingLeftInitial = left;
14591 mUserPaddingRightInitial = right;
14592
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014593 internalSetPadding(left, top, right, bottom);
14594 }
14595
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014596 /**
14597 * @hide
14598 */
14599 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014600 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014601 mUserPaddingRight = right;
14602 mUserPaddingBottom = bottom;
14603
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014604 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014605 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014606
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014607 // Common case is there are no scroll bars.
14608 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014609 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014610 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014611 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014612 switch (mVerticalScrollbarPosition) {
14613 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014614 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014615 left += offset;
14616 } else {
14617 right += offset;
14618 }
14619 break;
Adam Powell20232d02010-12-08 21:08:53 -080014620 case SCROLLBAR_POSITION_RIGHT:
14621 right += offset;
14622 break;
14623 case SCROLLBAR_POSITION_LEFT:
14624 left += offset;
14625 break;
14626 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014627 }
Adam Powell20232d02010-12-08 21:08:53 -080014628 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014629 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14630 ? 0 : getHorizontalScrollbarHeight();
14631 }
14632 }
Romain Guy8506ab42009-06-11 17:35:47 -070014633
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014634 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014635 changed = true;
14636 mPaddingLeft = left;
14637 }
14638 if (mPaddingTop != top) {
14639 changed = true;
14640 mPaddingTop = top;
14641 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014642 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014643 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014644 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014645 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014646 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014647 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014648 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014649 }
14650
14651 if (changed) {
14652 requestLayout();
14653 }
14654 }
14655
14656 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014657 * Sets the relative padding. The view may add on the space required to display
14658 * the scrollbars, depending on the style and visibility of the scrollbars.
14659 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14660 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14661 * from the values set in this call.
14662 *
14663 * @attr ref android.R.styleable#View_padding
14664 * @attr ref android.R.styleable#View_paddingBottom
14665 * @attr ref android.R.styleable#View_paddingStart
14666 * @attr ref android.R.styleable#View_paddingEnd
14667 * @attr ref android.R.styleable#View_paddingTop
14668 * @param start the start padding in pixels
14669 * @param top the top padding in pixels
14670 * @param end the end padding in pixels
14671 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014672 */
14673 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014674 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014675
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014676 mUserPaddingStart = start;
14677 mUserPaddingEnd = end;
14678
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014679 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014680 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014681 mUserPaddingLeftInitial = end;
14682 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014683 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014684 break;
14685 case LAYOUT_DIRECTION_LTR:
14686 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014687 mUserPaddingLeftInitial = start;
14688 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014689 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014690 }
14691 }
14692
14693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014694 * Returns the top padding of this view.
14695 *
14696 * @return the top padding in pixels
14697 */
14698 public int getPaddingTop() {
14699 return mPaddingTop;
14700 }
14701
14702 /**
14703 * Returns the bottom padding of this view. If there are inset and enabled
14704 * scrollbars, this value may include the space required to display the
14705 * scrollbars as well.
14706 *
14707 * @return the bottom padding in pixels
14708 */
14709 public int getPaddingBottom() {
14710 return mPaddingBottom;
14711 }
14712
14713 /**
14714 * Returns the left padding of this view. If there are inset and enabled
14715 * scrollbars, this value may include the space required to display the
14716 * scrollbars as well.
14717 *
14718 * @return the left padding in pixels
14719 */
14720 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014721 if (!isPaddingResolved()) {
14722 resolvePadding();
14723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014724 return mPaddingLeft;
14725 }
14726
14727 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014728 * Returns the start padding of this view depending on its resolved layout direction.
14729 * If there are inset and enabled scrollbars, this value may include the space
14730 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014731 *
14732 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014733 */
14734 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014735 if (!isPaddingResolved()) {
14736 resolvePadding();
14737 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014738 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014739 mPaddingRight : mPaddingLeft;
14740 }
14741
14742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014743 * Returns the right padding of this view. If there are inset and enabled
14744 * scrollbars, this value may include the space required to display the
14745 * scrollbars as well.
14746 *
14747 * @return the right padding in pixels
14748 */
14749 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014750 if (!isPaddingResolved()) {
14751 resolvePadding();
14752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014753 return mPaddingRight;
14754 }
14755
14756 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014757 * Returns the end padding of this view depending on its resolved layout direction.
14758 * If there are inset and enabled scrollbars, this value may include the space
14759 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014760 *
14761 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014762 */
14763 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014764 if (!isPaddingResolved()) {
14765 resolvePadding();
14766 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014767 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014768 mPaddingLeft : mPaddingRight;
14769 }
14770
14771 /**
14772 * Return if the padding as been set thru relative values
14773 * {@link #setPaddingRelative(int, int, int, int)} or thru
14774 * @attr ref android.R.styleable#View_paddingStart or
14775 * @attr ref android.R.styleable#View_paddingEnd
14776 *
14777 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014778 */
14779 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014780 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014781 }
14782
14783 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014784 * @hide
14785 */
Philip Milne7a23b492012-04-24 22:12:36 -070014786 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014787 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014788 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014789 }
14790 return mLayoutInsets;
14791 }
14792
14793 /**
14794 * @hide
14795 */
14796 public void setLayoutInsets(Insets layoutInsets) {
14797 mLayoutInsets = layoutInsets;
14798 }
14799
14800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014801 * Changes the selection state of this view. A view can be selected or not.
14802 * Note that selection is not the same as focus. Views are typically
14803 * selected in the context of an AdapterView like ListView or GridView;
14804 * the selected view is the view that is highlighted.
14805 *
14806 * @param selected true if the view must be selected, false otherwise
14807 */
14808 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014809 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14810 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014811 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014812 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014813 refreshDrawableState();
14814 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014815 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14816 notifyAccessibilityStateChanged();
14817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014818 }
14819 }
14820
14821 /**
14822 * Dispatch setSelected to all of this View's children.
14823 *
14824 * @see #setSelected(boolean)
14825 *
14826 * @param selected The new selected state
14827 */
14828 protected void dispatchSetSelected(boolean selected) {
14829 }
14830
14831 /**
14832 * Indicates the selection state of this view.
14833 *
14834 * @return true if the view is selected, false otherwise
14835 */
14836 @ViewDebug.ExportedProperty
14837 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014838 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014839 }
14840
14841 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014842 * Changes the activated state of this view. A view can be activated or not.
14843 * Note that activation is not the same as selection. Selection is
14844 * a transient property, representing the view (hierarchy) the user is
14845 * currently interacting with. Activation is a longer-term state that the
14846 * user can move views in and out of. For example, in a list view with
14847 * single or multiple selection enabled, the views in the current selection
14848 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14849 * here.) The activated state is propagated down to children of the view it
14850 * is set on.
14851 *
14852 * @param activated true if the view must be activated, false otherwise
14853 */
14854 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014855 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14856 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014857 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014858 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014859 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014860 }
14861 }
14862
14863 /**
14864 * Dispatch setActivated to all of this View's children.
14865 *
14866 * @see #setActivated(boolean)
14867 *
14868 * @param activated The new activated state
14869 */
14870 protected void dispatchSetActivated(boolean activated) {
14871 }
14872
14873 /**
14874 * Indicates the activation state of this view.
14875 *
14876 * @return true if the view is activated, false otherwise
14877 */
14878 @ViewDebug.ExportedProperty
14879 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014880 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014881 }
14882
14883 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014884 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14885 * observer can be used to get notifications when global events, like
14886 * layout, happen.
14887 *
14888 * The returned ViewTreeObserver observer is not guaranteed to remain
14889 * valid for the lifetime of this View. If the caller of this method keeps
14890 * a long-lived reference to ViewTreeObserver, it should always check for
14891 * the return value of {@link ViewTreeObserver#isAlive()}.
14892 *
14893 * @return The ViewTreeObserver for this view's hierarchy.
14894 */
14895 public ViewTreeObserver getViewTreeObserver() {
14896 if (mAttachInfo != null) {
14897 return mAttachInfo.mTreeObserver;
14898 }
14899 if (mFloatingTreeObserver == null) {
14900 mFloatingTreeObserver = new ViewTreeObserver();
14901 }
14902 return mFloatingTreeObserver;
14903 }
14904
14905 /**
14906 * <p>Finds the topmost view in the current view hierarchy.</p>
14907 *
14908 * @return the topmost view containing this view
14909 */
14910 public View getRootView() {
14911 if (mAttachInfo != null) {
14912 final View v = mAttachInfo.mRootView;
14913 if (v != null) {
14914 return v;
14915 }
14916 }
Romain Guy8506ab42009-06-11 17:35:47 -070014917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014918 View parent = this;
14919
14920 while (parent.mParent != null && parent.mParent instanceof View) {
14921 parent = (View) parent.mParent;
14922 }
14923
14924 return parent;
14925 }
14926
14927 /**
14928 * <p>Computes the coordinates of this view on the screen. The argument
14929 * must be an array of two integers. After the method returns, the array
14930 * contains the x and y location in that order.</p>
14931 *
14932 * @param location an array of two integers in which to hold the coordinates
14933 */
14934 public void getLocationOnScreen(int[] location) {
14935 getLocationInWindow(location);
14936
14937 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014938 if (info != null) {
14939 location[0] += info.mWindowLeft;
14940 location[1] += info.mWindowTop;
14941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014942 }
14943
14944 /**
14945 * <p>Computes the coordinates of this view in its window. The argument
14946 * must be an array of two integers. After the method returns, the array
14947 * contains the x and y location in that order.</p>
14948 *
14949 * @param location an array of two integers in which to hold the coordinates
14950 */
14951 public void getLocationInWindow(int[] location) {
14952 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014953 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014954 }
14955
Gilles Debunne6583ce52011-12-06 18:09:02 -080014956 if (mAttachInfo == null) {
14957 // When the view is not attached to a window, this method does not make sense
14958 location[0] = location[1] = 0;
14959 return;
14960 }
14961
Gilles Debunnecea45132011-11-24 02:19:27 +010014962 float[] position = mAttachInfo.mTmpTransformLocation;
14963 position[0] = position[1] = 0.0f;
14964
14965 if (!hasIdentityMatrix()) {
14966 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968
Gilles Debunnecea45132011-11-24 02:19:27 +010014969 position[0] += mLeft;
14970 position[1] += mTop;
14971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014972 ViewParent viewParent = mParent;
14973 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014974 final View view = (View) viewParent;
14975
14976 position[0] -= view.mScrollX;
14977 position[1] -= view.mScrollY;
14978
14979 if (!view.hasIdentityMatrix()) {
14980 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014981 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014982
14983 position[0] += view.mLeft;
14984 position[1] += view.mTop;
14985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014986 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014987 }
Romain Guy8506ab42009-06-11 17:35:47 -070014988
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014989 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014990 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014991 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14992 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014994
14995 location[0] = (int) (position[0] + 0.5f);
14996 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014997 }
14998
14999 /**
15000 * {@hide}
15001 * @param id the id of the view to be found
15002 * @return the view of the specified id, null if cannot be found
15003 */
15004 protected View findViewTraversal(int id) {
15005 if (id == mID) {
15006 return this;
15007 }
15008 return null;
15009 }
15010
15011 /**
15012 * {@hide}
15013 * @param tag the tag of the view to be found
15014 * @return the view of specified tag, null if cannot be found
15015 */
15016 protected View findViewWithTagTraversal(Object tag) {
15017 if (tag != null && tag.equals(mTag)) {
15018 return this;
15019 }
15020 return null;
15021 }
15022
15023 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015024 * {@hide}
15025 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015026 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015027 * @return The first view that matches the predicate or null.
15028 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015029 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015030 if (predicate.apply(this)) {
15031 return this;
15032 }
15033 return null;
15034 }
15035
15036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015037 * Look for a child view with the given id. If this view has the given
15038 * id, return this view.
15039 *
15040 * @param id The id to search for.
15041 * @return The view that has the given id in the hierarchy or null
15042 */
15043 public final View findViewById(int id) {
15044 if (id < 0) {
15045 return null;
15046 }
15047 return findViewTraversal(id);
15048 }
15049
15050 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015051 * Finds a view by its unuque and stable accessibility id.
15052 *
15053 * @param accessibilityId The searched accessibility id.
15054 * @return The found view.
15055 */
15056 final View findViewByAccessibilityId(int accessibilityId) {
15057 if (accessibilityId < 0) {
15058 return null;
15059 }
15060 return findViewByAccessibilityIdTraversal(accessibilityId);
15061 }
15062
15063 /**
15064 * Performs the traversal to find a view by its unuque and stable accessibility id.
15065 *
15066 * <strong>Note:</strong>This method does not stop at the root namespace
15067 * boundary since the user can touch the screen at an arbitrary location
15068 * potentially crossing the root namespace bounday which will send an
15069 * accessibility event to accessibility services and they should be able
15070 * to obtain the event source. Also accessibility ids are guaranteed to be
15071 * unique in the window.
15072 *
15073 * @param accessibilityId The accessibility id.
15074 * @return The found view.
15075 */
15076 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15077 if (getAccessibilityViewId() == accessibilityId) {
15078 return this;
15079 }
15080 return null;
15081 }
15082
15083 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015084 * Look for a child view with the given tag. If this view has the given
15085 * tag, return this view.
15086 *
15087 * @param tag The tag to search for, using "tag.equals(getTag())".
15088 * @return The View that has the given tag in the hierarchy or null
15089 */
15090 public final View findViewWithTag(Object tag) {
15091 if (tag == null) {
15092 return null;
15093 }
15094 return findViewWithTagTraversal(tag);
15095 }
15096
15097 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015098 * {@hide}
15099 * Look for a child view that matches the specified predicate.
15100 * If this view matches the predicate, return this view.
15101 *
15102 * @param predicate The predicate to evaluate.
15103 * @return The first view that matches the predicate or null.
15104 */
15105 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015106 return findViewByPredicateTraversal(predicate, null);
15107 }
15108
15109 /**
15110 * {@hide}
15111 * Look for a child view that matches the specified predicate,
15112 * starting with the specified view and its descendents and then
15113 * recusively searching the ancestors and siblings of that view
15114 * until this view is reached.
15115 *
15116 * This method is useful in cases where the predicate does not match
15117 * a single unique view (perhaps multiple views use the same id)
15118 * and we are trying to find the view that is "closest" in scope to the
15119 * starting view.
15120 *
15121 * @param start The view to start from.
15122 * @param predicate The predicate to evaluate.
15123 * @return The first view that matches the predicate or null.
15124 */
15125 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15126 View childToSkip = null;
15127 for (;;) {
15128 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15129 if (view != null || start == this) {
15130 return view;
15131 }
15132
15133 ViewParent parent = start.getParent();
15134 if (parent == null || !(parent instanceof View)) {
15135 return null;
15136 }
15137
15138 childToSkip = start;
15139 start = (View) parent;
15140 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015141 }
15142
15143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015144 * Sets the identifier for this view. The identifier does not have to be
15145 * unique in this view's hierarchy. The identifier should be a positive
15146 * number.
15147 *
15148 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015149 * @see #getId()
15150 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015151 *
15152 * @param id a number used to identify the view
15153 *
15154 * @attr ref android.R.styleable#View_id
15155 */
15156 public void setId(int id) {
15157 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015158 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15159 mID = generateViewId();
15160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015161 }
15162
15163 /**
15164 * {@hide}
15165 *
15166 * @param isRoot true if the view belongs to the root namespace, false
15167 * otherwise
15168 */
15169 public void setIsRootNamespace(boolean isRoot) {
15170 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015171 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015172 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015173 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015174 }
15175 }
15176
15177 /**
15178 * {@hide}
15179 *
15180 * @return true if the view belongs to the root namespace, false otherwise
15181 */
15182 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015183 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015184 }
15185
15186 /**
15187 * Returns this view's identifier.
15188 *
15189 * @return a positive integer used to identify the view or {@link #NO_ID}
15190 * if the view has no ID
15191 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015192 * @see #setId(int)
15193 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015194 * @attr ref android.R.styleable#View_id
15195 */
15196 @ViewDebug.CapturedViewProperty
15197 public int getId() {
15198 return mID;
15199 }
15200
15201 /**
15202 * Returns this view's tag.
15203 *
15204 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015205 *
15206 * @see #setTag(Object)
15207 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015208 */
15209 @ViewDebug.ExportedProperty
15210 public Object getTag() {
15211 return mTag;
15212 }
15213
15214 /**
15215 * Sets the tag associated with this view. A tag can be used to mark
15216 * a view in its hierarchy and does not have to be unique within the
15217 * hierarchy. Tags can also be used to store data within a view without
15218 * resorting to another data structure.
15219 *
15220 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015221 *
15222 * @see #getTag()
15223 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015224 */
15225 public void setTag(final Object tag) {
15226 mTag = tag;
15227 }
15228
15229 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015230 * Returns the tag associated with this view and the specified key.
15231 *
15232 * @param key The key identifying the tag
15233 *
15234 * @return the Object stored in this view as a tag
15235 *
15236 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015237 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015238 */
15239 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015240 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015241 return null;
15242 }
15243
15244 /**
15245 * Sets a tag associated with this view and a key. A tag can be used
15246 * to mark a view in its hierarchy and does not have to be unique within
15247 * the hierarchy. Tags can also be used to store data within a view
15248 * without resorting to another data structure.
15249 *
15250 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015251 * application to ensure it is unique (see the <a
15252 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15253 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015254 * the Android framework or not associated with any package will cause
15255 * an {@link IllegalArgumentException} to be thrown.
15256 *
15257 * @param key The key identifying the tag
15258 * @param tag An Object to tag the view with
15259 *
15260 * @throws IllegalArgumentException If they specified key is not valid
15261 *
15262 * @see #setTag(Object)
15263 * @see #getTag(int)
15264 */
15265 public void setTag(int key, final Object tag) {
15266 // If the package id is 0x00 or 0x01, it's either an undefined package
15267 // or a framework id
15268 if ((key >>> 24) < 2) {
15269 throw new IllegalArgumentException("The key must be an application-specific "
15270 + "resource id.");
15271 }
15272
Adam Powell2b2f6d62011-09-23 11:15:39 -070015273 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015274 }
15275
15276 /**
15277 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15278 * framework id.
15279 *
15280 * @hide
15281 */
15282 public void setTagInternal(int key, Object tag) {
15283 if ((key >>> 24) != 0x1) {
15284 throw new IllegalArgumentException("The key must be a framework-specific "
15285 + "resource id.");
15286 }
15287
Adam Powell2b2f6d62011-09-23 11:15:39 -070015288 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015289 }
15290
Adam Powell2b2f6d62011-09-23 11:15:39 -070015291 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015292 if (mKeyedTags == null) {
15293 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015294 }
15295
Adam Powell7db82ac2011-09-22 19:44:04 -070015296 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015297 }
15298
15299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015300 * Prints information about this view in the log output, with the tag
15301 * {@link #VIEW_LOG_TAG}.
15302 *
15303 * @hide
15304 */
15305 public void debug() {
15306 debug(0);
15307 }
15308
15309 /**
15310 * Prints information about this view in the log output, with the tag
15311 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15312 * indentation defined by the <code>depth</code>.
15313 *
15314 * @param depth the indentation level
15315 *
15316 * @hide
15317 */
15318 protected void debug(int depth) {
15319 String output = debugIndent(depth - 1);
15320
15321 output += "+ " + this;
15322 int id = getId();
15323 if (id != -1) {
15324 output += " (id=" + id + ")";
15325 }
15326 Object tag = getTag();
15327 if (tag != null) {
15328 output += " (tag=" + tag + ")";
15329 }
15330 Log.d(VIEW_LOG_TAG, output);
15331
Dianne Hackborn4702a852012-08-17 15:18:29 -070015332 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015333 output = debugIndent(depth) + " FOCUSED";
15334 Log.d(VIEW_LOG_TAG, output);
15335 }
15336
15337 output = debugIndent(depth);
15338 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15339 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15340 + "} ";
15341 Log.d(VIEW_LOG_TAG, output);
15342
15343 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15344 || mPaddingBottom != 0) {
15345 output = debugIndent(depth);
15346 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15347 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15348 Log.d(VIEW_LOG_TAG, output);
15349 }
15350
15351 output = debugIndent(depth);
15352 output += "mMeasureWidth=" + mMeasuredWidth +
15353 " mMeasureHeight=" + mMeasuredHeight;
15354 Log.d(VIEW_LOG_TAG, output);
15355
15356 output = debugIndent(depth);
15357 if (mLayoutParams == null) {
15358 output += "BAD! no layout params";
15359 } else {
15360 output = mLayoutParams.debug(output);
15361 }
15362 Log.d(VIEW_LOG_TAG, output);
15363
15364 output = debugIndent(depth);
15365 output += "flags={";
15366 output += View.printFlags(mViewFlags);
15367 output += "}";
15368 Log.d(VIEW_LOG_TAG, output);
15369
15370 output = debugIndent(depth);
15371 output += "privateFlags={";
15372 output += View.printPrivateFlags(mPrivateFlags);
15373 output += "}";
15374 Log.d(VIEW_LOG_TAG, output);
15375 }
15376
15377 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015378 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015379 *
15380 * @param depth the indentation level
15381 * @return a String containing (depth * 2 + 3) * 2 white spaces
15382 *
15383 * @hide
15384 */
15385 protected static String debugIndent(int depth) {
15386 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15387 for (int i = 0; i < (depth * 2) + 3; i++) {
15388 spaces.append(' ').append(' ');
15389 }
15390 return spaces.toString();
15391 }
15392
15393 /**
15394 * <p>Return the offset of the widget's text baseline from the widget's top
15395 * boundary. If this widget does not support baseline alignment, this
15396 * method returns -1. </p>
15397 *
15398 * @return the offset of the baseline within the widget's bounds or -1
15399 * if baseline alignment is not supported
15400 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015401 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015402 public int getBaseline() {
15403 return -1;
15404 }
15405
15406 /**
15407 * Call this when something has changed which has invalidated the
15408 * layout of this view. This will schedule a layout pass of the view
15409 * tree.
15410 */
15411 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015412 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15413 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015414
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015415 if (mParent != null && !mParent.isLayoutRequested()) {
15416 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417 }
15418 }
15419
15420 /**
15421 * Forces this view to be laid out during the next layout pass.
15422 * This method does not call requestLayout() or forceLayout()
15423 * on the parent.
15424 */
15425 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015426 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15427 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015428 }
15429
15430 /**
15431 * <p>
15432 * This is called to find out how big a view should be. The parent
15433 * supplies constraint information in the width and height parameters.
15434 * </p>
15435 *
15436 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015437 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015438 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015439 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015440 * </p>
15441 *
15442 *
15443 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15444 * parent
15445 * @param heightMeasureSpec Vertical space requirements as imposed by the
15446 * parent
15447 *
15448 * @see #onMeasure(int, int)
15449 */
15450 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015451 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 widthMeasureSpec != mOldWidthMeasureSpec ||
15453 heightMeasureSpec != mOldHeightMeasureSpec) {
15454
15455 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015456 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015457
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015458 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015460 // measure ourselves, this should set the measured dimension flag back
15461 onMeasure(widthMeasureSpec, heightMeasureSpec);
15462
15463 // flag not set, setMeasuredDimension() was not invoked, we raise
15464 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015465 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015466 throw new IllegalStateException("onMeasure() did not set the"
15467 + " measured dimension by calling"
15468 + " setMeasuredDimension()");
15469 }
15470
Dianne Hackborn4702a852012-08-17 15:18:29 -070015471 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015472 }
15473
15474 mOldWidthMeasureSpec = widthMeasureSpec;
15475 mOldHeightMeasureSpec = heightMeasureSpec;
15476 }
15477
15478 /**
15479 * <p>
15480 * Measure the view and its content to determine the measured width and the
15481 * measured height. This method is invoked by {@link #measure(int, int)} and
15482 * should be overriden by subclasses to provide accurate and efficient
15483 * measurement of their contents.
15484 * </p>
15485 *
15486 * <p>
15487 * <strong>CONTRACT:</strong> When overriding this method, you
15488 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15489 * measured width and height of this view. Failure to do so will trigger an
15490 * <code>IllegalStateException</code>, thrown by
15491 * {@link #measure(int, int)}. Calling the superclass'
15492 * {@link #onMeasure(int, int)} is a valid use.
15493 * </p>
15494 *
15495 * <p>
15496 * The base class implementation of measure defaults to the background size,
15497 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15498 * override {@link #onMeasure(int, int)} to provide better measurements of
15499 * their content.
15500 * </p>
15501 *
15502 * <p>
15503 * If this method is overridden, it is the subclass's responsibility to make
15504 * sure the measured height and width are at least the view's minimum height
15505 * and width ({@link #getSuggestedMinimumHeight()} and
15506 * {@link #getSuggestedMinimumWidth()}).
15507 * </p>
15508 *
15509 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15510 * The requirements are encoded with
15511 * {@link android.view.View.MeasureSpec}.
15512 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15513 * The requirements are encoded with
15514 * {@link android.view.View.MeasureSpec}.
15515 *
15516 * @see #getMeasuredWidth()
15517 * @see #getMeasuredHeight()
15518 * @see #setMeasuredDimension(int, int)
15519 * @see #getSuggestedMinimumHeight()
15520 * @see #getSuggestedMinimumWidth()
15521 * @see android.view.View.MeasureSpec#getMode(int)
15522 * @see android.view.View.MeasureSpec#getSize(int)
15523 */
15524 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15525 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15526 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15527 }
15528
15529 /**
15530 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15531 * measured width and measured height. Failing to do so will trigger an
15532 * exception at measurement time.</p>
15533 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015534 * @param measuredWidth The measured width of this view. May be a complex
15535 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15536 * {@link #MEASURED_STATE_TOO_SMALL}.
15537 * @param measuredHeight The measured height of this view. May be a complex
15538 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15539 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015540 */
15541 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15542 mMeasuredWidth = measuredWidth;
15543 mMeasuredHeight = measuredHeight;
15544
Dianne Hackborn4702a852012-08-17 15:18:29 -070015545 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015546 }
15547
15548 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015549 * Merge two states as returned by {@link #getMeasuredState()}.
15550 * @param curState The current state as returned from a view or the result
15551 * of combining multiple views.
15552 * @param newState The new view state to combine.
15553 * @return Returns a new integer reflecting the combination of the two
15554 * states.
15555 */
15556 public static int combineMeasuredStates(int curState, int newState) {
15557 return curState | newState;
15558 }
15559
15560 /**
15561 * Version of {@link #resolveSizeAndState(int, int, int)}
15562 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15563 */
15564 public static int resolveSize(int size, int measureSpec) {
15565 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15566 }
15567
15568 /**
15569 * Utility to reconcile a desired size and state, with constraints imposed
15570 * by a MeasureSpec. Will take the desired size, unless a different size
15571 * is imposed by the constraints. The returned value is a compound integer,
15572 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15573 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15574 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015575 *
15576 * @param size How big the view wants to be
15577 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015578 * @return Size information bit mask as defined by
15579 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015580 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015581 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015582 int result = size;
15583 int specMode = MeasureSpec.getMode(measureSpec);
15584 int specSize = MeasureSpec.getSize(measureSpec);
15585 switch (specMode) {
15586 case MeasureSpec.UNSPECIFIED:
15587 result = size;
15588 break;
15589 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015590 if (specSize < size) {
15591 result = specSize | MEASURED_STATE_TOO_SMALL;
15592 } else {
15593 result = size;
15594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015595 break;
15596 case MeasureSpec.EXACTLY:
15597 result = specSize;
15598 break;
15599 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015600 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015601 }
15602
15603 /**
15604 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015605 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015606 * by the MeasureSpec.
15607 *
15608 * @param size Default size for this view
15609 * @param measureSpec Constraints imposed by the parent
15610 * @return The size this view should be.
15611 */
15612 public static int getDefaultSize(int size, int measureSpec) {
15613 int result = size;
15614 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015615 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015616
15617 switch (specMode) {
15618 case MeasureSpec.UNSPECIFIED:
15619 result = size;
15620 break;
15621 case MeasureSpec.AT_MOST:
15622 case MeasureSpec.EXACTLY:
15623 result = specSize;
15624 break;
15625 }
15626 return result;
15627 }
15628
15629 /**
15630 * Returns the suggested minimum height that the view should use. This
15631 * returns the maximum of the view's minimum height
15632 * and the background's minimum height
15633 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15634 * <p>
15635 * When being used in {@link #onMeasure(int, int)}, the caller should still
15636 * ensure the returned height is within the requirements of the parent.
15637 *
15638 * @return The suggested minimum height of the view.
15639 */
15640 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015641 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015643 }
15644
15645 /**
15646 * Returns the suggested minimum width that the view should use. This
15647 * returns the maximum of the view's minimum width)
15648 * and the background's minimum width
15649 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15650 * <p>
15651 * When being used in {@link #onMeasure(int, int)}, the caller should still
15652 * ensure the returned width is within the requirements of the parent.
15653 *
15654 * @return The suggested minimum width of the view.
15655 */
15656 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015657 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015659
Philip Milne6c8ea062012-04-03 17:38:43 -070015660 /**
15661 * Returns the minimum height of the view.
15662 *
15663 * @return the minimum height the view will try to be.
15664 *
15665 * @see #setMinimumHeight(int)
15666 *
15667 * @attr ref android.R.styleable#View_minHeight
15668 */
15669 public int getMinimumHeight() {
15670 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015671 }
15672
15673 /**
15674 * Sets the minimum height of the view. It is not guaranteed the view will
15675 * be able to achieve this minimum height (for example, if its parent layout
15676 * constrains it with less available height).
15677 *
15678 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015679 *
15680 * @see #getMinimumHeight()
15681 *
15682 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015683 */
15684 public void setMinimumHeight(int minHeight) {
15685 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015686 requestLayout();
15687 }
15688
15689 /**
15690 * Returns the minimum width of the view.
15691 *
15692 * @return the minimum width the view will try to be.
15693 *
15694 * @see #setMinimumWidth(int)
15695 *
15696 * @attr ref android.R.styleable#View_minWidth
15697 */
15698 public int getMinimumWidth() {
15699 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015700 }
15701
15702 /**
15703 * Sets the minimum width of the view. It is not guaranteed the view will
15704 * be able to achieve this minimum width (for example, if its parent layout
15705 * constrains it with less available width).
15706 *
15707 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015708 *
15709 * @see #getMinimumWidth()
15710 *
15711 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015712 */
15713 public void setMinimumWidth(int minWidth) {
15714 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015715 requestLayout();
15716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015717 }
15718
15719 /**
15720 * Get the animation currently associated with this view.
15721 *
15722 * @return The animation that is currently playing or
15723 * scheduled to play for this view.
15724 */
15725 public Animation getAnimation() {
15726 return mCurrentAnimation;
15727 }
15728
15729 /**
15730 * Start the specified animation now.
15731 *
15732 * @param animation the animation to start now
15733 */
15734 public void startAnimation(Animation animation) {
15735 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15736 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015737 invalidateParentCaches();
15738 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015739 }
15740
15741 /**
15742 * Cancels any animations for this view.
15743 */
15744 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015745 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015746 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015748 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015749 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015750 }
15751
15752 /**
15753 * Sets the next animation to play for this view.
15754 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015755 * {@link #startAnimation(android.view.animation.Animation)} instead.
15756 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015757 * control over the start time and invalidation, but you
15758 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015759 * 2) the view's parent (which controls animations on its children)
15760 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015761 * start.
15762 *
15763 * @param animation The next animation, or null.
15764 */
15765 public void setAnimation(Animation animation) {
15766 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015768 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015769 // If the screen is off assume the animation start time is now instead of
15770 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15771 // would cause the animation to start when the screen turns back on
15772 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15773 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15774 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015776 animation.reset();
15777 }
15778 }
15779
15780 /**
15781 * Invoked by a parent ViewGroup to notify the start of the animation
15782 * currently associated with this view. If you override this method,
15783 * always call super.onAnimationStart();
15784 *
15785 * @see #setAnimation(android.view.animation.Animation)
15786 * @see #getAnimation()
15787 */
15788 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015789 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015790 }
15791
15792 /**
15793 * Invoked by a parent ViewGroup to notify the end of the animation
15794 * currently associated with this view. If you override this method,
15795 * always call super.onAnimationEnd();
15796 *
15797 * @see #setAnimation(android.view.animation.Animation)
15798 * @see #getAnimation()
15799 */
15800 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015801 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015802 }
15803
15804 /**
15805 * Invoked if there is a Transform that involves alpha. Subclass that can
15806 * draw themselves with the specified alpha should return true, and then
15807 * respect that alpha when their onDraw() is called. If this returns false
15808 * then the view may be redirected to draw into an offscreen buffer to
15809 * fulfill the request, which will look fine, but may be slower than if the
15810 * subclass handles it internally. The default implementation returns false.
15811 *
15812 * @param alpha The alpha (0..255) to apply to the view's drawing
15813 * @return true if the view can draw with the specified alpha.
15814 */
15815 protected boolean onSetAlpha(int alpha) {
15816 return false;
15817 }
15818
15819 /**
15820 * This is used by the RootView to perform an optimization when
15821 * the view hierarchy contains one or several SurfaceView.
15822 * SurfaceView is always considered transparent, but its children are not,
15823 * therefore all View objects remove themselves from the global transparent
15824 * region (passed as a parameter to this function).
15825 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015826 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015827 *
15828 * @return Returns true if the effective visibility of the view at this
15829 * point is opaque, regardless of the transparent region; returns false
15830 * if it is possible for underlying windows to be seen behind the view.
15831 *
15832 * {@hide}
15833 */
15834 public boolean gatherTransparentRegion(Region region) {
15835 final AttachInfo attachInfo = mAttachInfo;
15836 if (region != null && attachInfo != null) {
15837 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015838 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015839 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15840 // remove it from the transparent region.
15841 final int[] location = attachInfo.mTransparentLocation;
15842 getLocationInWindow(location);
15843 region.op(location[0], location[1], location[0] + mRight - mLeft,
15844 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015845 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015846 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15847 // exists, so we remove the background drawable's non-transparent
15848 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015849 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015850 }
15851 }
15852 return true;
15853 }
15854
15855 /**
15856 * Play a sound effect for this view.
15857 *
15858 * <p>The framework will play sound effects for some built in actions, such as
15859 * clicking, but you may wish to play these effects in your widget,
15860 * for instance, for internal navigation.
15861 *
15862 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15863 * {@link #isSoundEffectsEnabled()} is true.
15864 *
15865 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15866 */
15867 public void playSoundEffect(int soundConstant) {
15868 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15869 return;
15870 }
15871 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15872 }
15873
15874 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015875 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015876 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015877 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015878 *
15879 * <p>The framework will provide haptic feedback for some built in actions,
15880 * such as long presses, but you may wish to provide feedback for your
15881 * own widget.
15882 *
15883 * <p>The feedback will only be performed if
15884 * {@link #isHapticFeedbackEnabled()} is true.
15885 *
15886 * @param feedbackConstant One of the constants defined in
15887 * {@link HapticFeedbackConstants}
15888 */
15889 public boolean performHapticFeedback(int feedbackConstant) {
15890 return performHapticFeedback(feedbackConstant, 0);
15891 }
15892
15893 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015894 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015895 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015896 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015897 *
15898 * @param feedbackConstant One of the constants defined in
15899 * {@link HapticFeedbackConstants}
15900 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15901 */
15902 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15903 if (mAttachInfo == null) {
15904 return false;
15905 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015906 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015907 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015908 && !isHapticFeedbackEnabled()) {
15909 return false;
15910 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015911 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15912 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015913 }
15914
15915 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015916 * Request that the visibility of the status bar or other screen/window
15917 * decorations be changed.
15918 *
15919 * <p>This method is used to put the over device UI into temporary modes
15920 * where the user's attention is focused more on the application content,
15921 * by dimming or hiding surrounding system affordances. This is typically
15922 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15923 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15924 * to be placed behind the action bar (and with these flags other system
15925 * affordances) so that smooth transitions between hiding and showing them
15926 * can be done.
15927 *
15928 * <p>Two representative examples of the use of system UI visibility is
15929 * implementing a content browsing application (like a magazine reader)
15930 * and a video playing application.
15931 *
15932 * <p>The first code shows a typical implementation of a View in a content
15933 * browsing application. In this implementation, the application goes
15934 * into a content-oriented mode by hiding the status bar and action bar,
15935 * and putting the navigation elements into lights out mode. The user can
15936 * then interact with content while in this mode. Such an application should
15937 * provide an easy way for the user to toggle out of the mode (such as to
15938 * check information in the status bar or access notifications). In the
15939 * implementation here, this is done simply by tapping on the content.
15940 *
15941 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15942 * content}
15943 *
15944 * <p>This second code sample shows a typical implementation of a View
15945 * in a video playing application. In this situation, while the video is
15946 * playing the application would like to go into a complete full-screen mode,
15947 * to use as much of the display as possible for the video. When in this state
15948 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015949 * touching on the screen to pop the UI out of full screen mode. See
15950 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015951 *
15952 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15953 * content}
15954 *
15955 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15956 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15957 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15958 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015959 */
15960 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015961 if (visibility != mSystemUiVisibility) {
15962 mSystemUiVisibility = visibility;
15963 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15964 mParent.recomputeViewAttributes(this);
15965 }
Joe Onorato664644d2011-01-23 17:53:23 -080015966 }
15967 }
15968
15969 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015970 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15971 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15972 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15973 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15974 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015975 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015976 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015977 return mSystemUiVisibility;
15978 }
15979
Scott Mainec6331b2011-05-24 16:55:56 -070015980 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015981 * Returns the current system UI visibility that is currently set for
15982 * the entire window. This is the combination of the
15983 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15984 * views in the window.
15985 */
15986 public int getWindowSystemUiVisibility() {
15987 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15988 }
15989
15990 /**
15991 * Override to find out when the window's requested system UI visibility
15992 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15993 * This is different from the callbacks recieved through
15994 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15995 * in that this is only telling you about the local request of the window,
15996 * not the actual values applied by the system.
15997 */
15998 public void onWindowSystemUiVisibilityChanged(int visible) {
15999 }
16000
16001 /**
16002 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16003 * the view hierarchy.
16004 */
16005 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16006 onWindowSystemUiVisibilityChanged(visible);
16007 }
16008
16009 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016010 * Set a listener to receive callbacks when the visibility of the system bar changes.
16011 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16012 */
Joe Onorato664644d2011-01-23 17:53:23 -080016013 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016014 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016015 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16016 mParent.recomputeViewAttributes(this);
16017 }
16018 }
16019
16020 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016021 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16022 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016023 */
16024 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016025 ListenerInfo li = mListenerInfo;
16026 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16027 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016028 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016029 }
16030 }
16031
Dianne Hackborncf675782012-05-10 15:07:24 -070016032 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016033 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16034 if (val != mSystemUiVisibility) {
16035 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016036 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016037 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016038 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016039 }
16040
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016041 /** @hide */
16042 public void setDisabledSystemUiVisibility(int flags) {
16043 if (mAttachInfo != null) {
16044 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16045 mAttachInfo.mDisabledSystemUiVisibility = flags;
16046 if (mParent != null) {
16047 mParent.recomputeViewAttributes(this);
16048 }
16049 }
16050 }
16051 }
16052
Joe Onorato664644d2011-01-23 17:53:23 -080016053 /**
Joe Malin32736f02011-01-19 16:14:20 -080016054 * Creates an image that the system displays during the drag and drop
16055 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16056 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16057 * appearance as the given View. The default also positions the center of the drag shadow
16058 * directly under the touch point. If no View is provided (the constructor with no parameters
16059 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16060 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16061 * default is an invisible drag shadow.
16062 * <p>
16063 * You are not required to use the View you provide to the constructor as the basis of the
16064 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16065 * anything you want as the drag shadow.
16066 * </p>
16067 * <p>
16068 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16069 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16070 * size and position of the drag shadow. It uses this data to construct a
16071 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16072 * so that your application can draw the shadow image in the Canvas.
16073 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016074 *
16075 * <div class="special reference">
16076 * <h3>Developer Guides</h3>
16077 * <p>For a guide to implementing drag and drop features, read the
16078 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16079 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016080 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016081 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016082 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016083
16084 /**
Joe Malin32736f02011-01-19 16:14:20 -080016085 * Constructs a shadow image builder based on a View. By default, the resulting drag
16086 * shadow will have the same appearance and dimensions as the View, with the touch point
16087 * over the center of the View.
16088 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016089 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016090 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016091 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016092 }
16093
Christopher Tate17ed60c2011-01-18 12:50:26 -080016094 /**
16095 * Construct a shadow builder object with no associated View. This
16096 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16097 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16098 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016099 * reference to any View object. If they are not overridden, then the result is an
16100 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016101 */
16102 public DragShadowBuilder() {
16103 mView = new WeakReference<View>(null);
16104 }
16105
16106 /**
16107 * Returns the View object that had been passed to the
16108 * {@link #View.DragShadowBuilder(View)}
16109 * constructor. If that View parameter was {@code null} or if the
16110 * {@link #View.DragShadowBuilder()}
16111 * constructor was used to instantiate the builder object, this method will return
16112 * null.
16113 *
16114 * @return The View object associate with this builder object.
16115 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016116 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016117 final public View getView() {
16118 return mView.get();
16119 }
16120
Christopher Tate2c095f32010-10-04 14:13:40 -070016121 /**
Joe Malin32736f02011-01-19 16:14:20 -080016122 * Provides the metrics for the shadow image. These include the dimensions of
16123 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016124 * be centered under the touch location while dragging.
16125 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016126 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016127 * same as the dimensions of the View itself and centers the shadow under
16128 * the touch point.
16129 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016130 *
Joe Malin32736f02011-01-19 16:14:20 -080016131 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16132 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16133 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16134 * image.
16135 *
16136 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16137 * shadow image that should be underneath the touch point during the drag and drop
16138 * operation. Your application must set {@link android.graphics.Point#x} to the
16139 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016140 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016141 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016142 final View view = mView.get();
16143 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016144 shadowSize.set(view.getWidth(), view.getHeight());
16145 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016146 } else {
16147 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16148 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016149 }
16150
16151 /**
Joe Malin32736f02011-01-19 16:14:20 -080016152 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16153 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016154 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016155 *
Joe Malin32736f02011-01-19 16:14:20 -080016156 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016157 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016158 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016159 final View view = mView.get();
16160 if (view != null) {
16161 view.draw(canvas);
16162 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016163 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016164 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016165 }
16166 }
16167
16168 /**
Joe Malin32736f02011-01-19 16:14:20 -080016169 * Starts a drag and drop operation. When your application calls this method, it passes a
16170 * {@link android.view.View.DragShadowBuilder} object to the system. The
16171 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16172 * to get metrics for the drag shadow, and then calls the object's
16173 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16174 * <p>
16175 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16176 * drag events to all the View objects in your application that are currently visible. It does
16177 * this either by calling the View object's drag listener (an implementation of
16178 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16179 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16180 * Both are passed a {@link android.view.DragEvent} object that has a
16181 * {@link android.view.DragEvent#getAction()} value of
16182 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16183 * </p>
16184 * <p>
16185 * Your application can invoke startDrag() on any attached View object. The View object does not
16186 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16187 * be related to the View the user selected for dragging.
16188 * </p>
16189 * @param data A {@link android.content.ClipData} object pointing to the data to be
16190 * transferred by the drag and drop operation.
16191 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16192 * drag shadow.
16193 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16194 * drop operation. This Object is put into every DragEvent object sent by the system during the
16195 * current drag.
16196 * <p>
16197 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16198 * to the target Views. For example, it can contain flags that differentiate between a
16199 * a copy operation and a move operation.
16200 * </p>
16201 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16202 * so the parameter should be set to 0.
16203 * @return {@code true} if the method completes successfully, or
16204 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16205 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016206 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016207 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016208 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016209 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016210 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016211 }
16212 boolean okay = false;
16213
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016214 Point shadowSize = new Point();
16215 Point shadowTouchPoint = new Point();
16216 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016217
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016218 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16219 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16220 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016221 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016222
Chris Tatea32dcf72010-10-14 12:13:50 -070016223 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016224 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16225 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016226 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016227 Surface surface = new Surface();
16228 try {
16229 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016230 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016231 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016232 + " surface=" + surface);
16233 if (token != null) {
16234 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016235 try {
Chris Tate6b391282010-10-14 15:48:59 -070016236 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016237 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016238 } finally {
16239 surface.unlockCanvasAndPost(canvas);
16240 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016241
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016242 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016243
16244 // Cache the local state object for delivery with DragEvents
16245 root.setLocalDragState(myLocalState);
16246
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016247 // repurpose 'shadowSize' for the last touch point
16248 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016249
Christopher Tatea53146c2010-09-07 11:57:52 -070016250 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016251 shadowSize.x, shadowSize.y,
16252 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016253 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016254
16255 // Off and running! Release our local surface instance; the drag
16256 // shadow surface is now managed by the system process.
16257 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016258 }
16259 } catch (Exception e) {
16260 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16261 surface.destroy();
16262 }
16263
16264 return okay;
16265 }
16266
Christopher Tatea53146c2010-09-07 11:57:52 -070016267 /**
Joe Malin32736f02011-01-19 16:14:20 -080016268 * Handles drag events sent by the system following a call to
16269 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16270 *<p>
16271 * When the system calls this method, it passes a
16272 * {@link android.view.DragEvent} object. A call to
16273 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16274 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16275 * operation.
16276 * @param event The {@link android.view.DragEvent} sent by the system.
16277 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16278 * in DragEvent, indicating the type of drag event represented by this object.
16279 * @return {@code true} if the method was successful, otherwise {@code false}.
16280 * <p>
16281 * The method should return {@code true} in response to an action type of
16282 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16283 * operation.
16284 * </p>
16285 * <p>
16286 * The method should also return {@code true} in response to an action type of
16287 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16288 * {@code false} if it didn't.
16289 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016290 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016291 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016292 return false;
16293 }
16294
16295 /**
Joe Malin32736f02011-01-19 16:14:20 -080016296 * Detects if this View is enabled and has a drag event listener.
16297 * If both are true, then it calls the drag event listener with the
16298 * {@link android.view.DragEvent} it received. If the drag event listener returns
16299 * {@code true}, then dispatchDragEvent() returns {@code true}.
16300 * <p>
16301 * For all other cases, the method calls the
16302 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16303 * method and returns its result.
16304 * </p>
16305 * <p>
16306 * This ensures that a drag event is always consumed, even if the View does not have a drag
16307 * event listener. However, if the View has a listener and the listener returns true, then
16308 * onDragEvent() is not called.
16309 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016310 */
16311 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016312 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016313 ListenerInfo li = mListenerInfo;
16314 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16315 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016316 return true;
16317 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016318 return onDragEvent(event);
16319 }
16320
Christopher Tate3d4bf172011-03-28 16:16:46 -070016321 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016322 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016323 }
16324
Christopher Tatea53146c2010-09-07 11:57:52 -070016325 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016326 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16327 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016328 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016329 */
16330 public void onCloseSystemDialogs(String reason) {
16331 }
Joe Malin32736f02011-01-19 16:14:20 -080016332
Dianne Hackbornffa42482009-09-23 22:20:11 -070016333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016334 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016335 * update a Region being computed for
16336 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016337 * that any non-transparent parts of the Drawable are removed from the
16338 * given transparent region.
16339 *
16340 * @param dr The Drawable whose transparency is to be applied to the region.
16341 * @param region A Region holding the current transparency information,
16342 * where any parts of the region that are set are considered to be
16343 * transparent. On return, this region will be modified to have the
16344 * transparency information reduced by the corresponding parts of the
16345 * Drawable that are not transparent.
16346 * {@hide}
16347 */
16348 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16349 if (DBG) {
16350 Log.i("View", "Getting transparent region for: " + this);
16351 }
16352 final Region r = dr.getTransparentRegion();
16353 final Rect db = dr.getBounds();
16354 final AttachInfo attachInfo = mAttachInfo;
16355 if (r != null && attachInfo != null) {
16356 final int w = getRight()-getLeft();
16357 final int h = getBottom()-getTop();
16358 if (db.left > 0) {
16359 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16360 r.op(0, 0, db.left, h, Region.Op.UNION);
16361 }
16362 if (db.right < w) {
16363 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16364 r.op(db.right, 0, w, h, Region.Op.UNION);
16365 }
16366 if (db.top > 0) {
16367 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16368 r.op(0, 0, w, db.top, Region.Op.UNION);
16369 }
16370 if (db.bottom < h) {
16371 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16372 r.op(0, db.bottom, w, h, Region.Op.UNION);
16373 }
16374 final int[] location = attachInfo.mTransparentLocation;
16375 getLocationInWindow(location);
16376 r.translate(location[0], location[1]);
16377 region.op(r, Region.Op.INTERSECT);
16378 } else {
16379 region.op(db, Region.Op.DIFFERENCE);
16380 }
16381 }
16382
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016383 private void checkForLongClick(int delayOffset) {
16384 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16385 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016386
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016387 if (mPendingCheckForLongPress == null) {
16388 mPendingCheckForLongPress = new CheckForLongPress();
16389 }
16390 mPendingCheckForLongPress.rememberWindowAttachCount();
16391 postDelayed(mPendingCheckForLongPress,
16392 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016394 }
16395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016396 /**
16397 * Inflate a view from an XML resource. This convenience method wraps the {@link
16398 * LayoutInflater} class, which provides a full range of options for view inflation.
16399 *
16400 * @param context The Context object for your activity or application.
16401 * @param resource The resource ID to inflate
16402 * @param root A view group that will be the parent. Used to properly inflate the
16403 * layout_* parameters.
16404 * @see LayoutInflater
16405 */
16406 public static View inflate(Context context, int resource, ViewGroup root) {
16407 LayoutInflater factory = LayoutInflater.from(context);
16408 return factory.inflate(resource, root);
16409 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016411 /**
Adam Powell637d3372010-08-25 14:37:03 -070016412 * Scroll the view with standard behavior for scrolling beyond the normal
16413 * content boundaries. Views that call this method should override
16414 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16415 * results of an over-scroll operation.
16416 *
16417 * Views can use this method to handle any touch or fling-based scrolling.
16418 *
16419 * @param deltaX Change in X in pixels
16420 * @param deltaY Change in Y in pixels
16421 * @param scrollX Current X scroll value in pixels before applying deltaX
16422 * @param scrollY Current Y scroll value in pixels before applying deltaY
16423 * @param scrollRangeX Maximum content scroll range along the X axis
16424 * @param scrollRangeY Maximum content scroll range along the Y axis
16425 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16426 * along the X axis.
16427 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16428 * along the Y axis.
16429 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16430 * @return true if scrolling was clamped to an over-scroll boundary along either
16431 * axis, false otherwise.
16432 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016433 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016434 protected boolean overScrollBy(int deltaX, int deltaY,
16435 int scrollX, int scrollY,
16436 int scrollRangeX, int scrollRangeY,
16437 int maxOverScrollX, int maxOverScrollY,
16438 boolean isTouchEvent) {
16439 final int overScrollMode = mOverScrollMode;
16440 final boolean canScrollHorizontal =
16441 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16442 final boolean canScrollVertical =
16443 computeVerticalScrollRange() > computeVerticalScrollExtent();
16444 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16445 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16446 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16447 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16448
16449 int newScrollX = scrollX + deltaX;
16450 if (!overScrollHorizontal) {
16451 maxOverScrollX = 0;
16452 }
16453
16454 int newScrollY = scrollY + deltaY;
16455 if (!overScrollVertical) {
16456 maxOverScrollY = 0;
16457 }
16458
16459 // Clamp values if at the limits and record
16460 final int left = -maxOverScrollX;
16461 final int right = maxOverScrollX + scrollRangeX;
16462 final int top = -maxOverScrollY;
16463 final int bottom = maxOverScrollY + scrollRangeY;
16464
16465 boolean clampedX = false;
16466 if (newScrollX > right) {
16467 newScrollX = right;
16468 clampedX = true;
16469 } else if (newScrollX < left) {
16470 newScrollX = left;
16471 clampedX = true;
16472 }
16473
16474 boolean clampedY = false;
16475 if (newScrollY > bottom) {
16476 newScrollY = bottom;
16477 clampedY = true;
16478 } else if (newScrollY < top) {
16479 newScrollY = top;
16480 clampedY = true;
16481 }
16482
16483 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16484
16485 return clampedX || clampedY;
16486 }
16487
16488 /**
16489 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16490 * respond to the results of an over-scroll operation.
16491 *
16492 * @param scrollX New X scroll value in pixels
16493 * @param scrollY New Y scroll value in pixels
16494 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16495 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16496 */
16497 protected void onOverScrolled(int scrollX, int scrollY,
16498 boolean clampedX, boolean clampedY) {
16499 // Intentionally empty.
16500 }
16501
16502 /**
16503 * Returns the over-scroll mode for this view. The result will be
16504 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16505 * (allow over-scrolling only if the view content is larger than the container),
16506 * or {@link #OVER_SCROLL_NEVER}.
16507 *
16508 * @return This view's over-scroll mode.
16509 */
16510 public int getOverScrollMode() {
16511 return mOverScrollMode;
16512 }
16513
16514 /**
16515 * Set the over-scroll mode for this view. Valid over-scroll modes are
16516 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16517 * (allow over-scrolling only if the view content is larger than the container),
16518 * or {@link #OVER_SCROLL_NEVER}.
16519 *
16520 * Setting the over-scroll mode of a view will have an effect only if the
16521 * view is capable of scrolling.
16522 *
16523 * @param overScrollMode The new over-scroll mode for this view.
16524 */
16525 public void setOverScrollMode(int overScrollMode) {
16526 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16527 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16528 overScrollMode != OVER_SCROLL_NEVER) {
16529 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16530 }
16531 mOverScrollMode = overScrollMode;
16532 }
16533
16534 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016535 * Gets a scale factor that determines the distance the view should scroll
16536 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16537 * @return The vertical scroll scale factor.
16538 * @hide
16539 */
16540 protected float getVerticalScrollFactor() {
16541 if (mVerticalScrollFactor == 0) {
16542 TypedValue outValue = new TypedValue();
16543 if (!mContext.getTheme().resolveAttribute(
16544 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16545 throw new IllegalStateException(
16546 "Expected theme to define listPreferredItemHeight.");
16547 }
16548 mVerticalScrollFactor = outValue.getDimension(
16549 mContext.getResources().getDisplayMetrics());
16550 }
16551 return mVerticalScrollFactor;
16552 }
16553
16554 /**
16555 * Gets a scale factor that determines the distance the view should scroll
16556 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16557 * @return The horizontal scroll scale factor.
16558 * @hide
16559 */
16560 protected float getHorizontalScrollFactor() {
16561 // TODO: Should use something else.
16562 return getVerticalScrollFactor();
16563 }
16564
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016565 /**
16566 * Return the value specifying the text direction or policy that was set with
16567 * {@link #setTextDirection(int)}.
16568 *
16569 * @return the defined text direction. It can be one of:
16570 *
16571 * {@link #TEXT_DIRECTION_INHERIT},
16572 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16573 * {@link #TEXT_DIRECTION_ANY_RTL},
16574 * {@link #TEXT_DIRECTION_LTR},
16575 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016576 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016577 *
16578 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016579 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016580 @ViewDebug.ExportedProperty(category = "text", mapping = {
16581 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16582 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16583 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16584 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16585 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16586 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16587 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016588 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016589 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016590 }
16591
16592 /**
16593 * Set the text direction.
16594 *
16595 * @param textDirection the direction to set. Should be one of:
16596 *
16597 * {@link #TEXT_DIRECTION_INHERIT},
16598 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16599 * {@link #TEXT_DIRECTION_ANY_RTL},
16600 * {@link #TEXT_DIRECTION_LTR},
16601 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016602 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016603 *
16604 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16605 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16606 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016607 */
16608 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016609 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016610 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016611 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016612 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016613 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016614 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016615 // Do resolution
16616 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016617 // Notify change
16618 onRtlPropertiesChanged();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016619 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016620 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016621 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016622 }
16623 }
16624
16625 /**
16626 * Return the resolved text direction.
16627 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016628 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016629 *
Doug Feltcb3791202011-07-07 11:57:48 -070016630 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16631 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016632 * {@link #TEXT_DIRECTION_LTR},
16633 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016634 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016635 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016636 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016637 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016638 }
16639
16640 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016641 * Resolve the text direction.
16642 *
16643 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016644 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016645 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016646 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016647 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016648
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016649 if (hasRtlSupport()) {
16650 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016651 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016652 switch(textDirection) {
16653 case TEXT_DIRECTION_INHERIT:
16654 if (canResolveTextDirection()) {
16655 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016656
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016657 // Set current resolved direction to the same value as the parent's one
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016658 final int parentResolvedDirection = viewGroup.getTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016659 switch (parentResolvedDirection) {
16660 case TEXT_DIRECTION_FIRST_STRONG:
16661 case TEXT_DIRECTION_ANY_RTL:
16662 case TEXT_DIRECTION_LTR:
16663 case TEXT_DIRECTION_RTL:
16664 case TEXT_DIRECTION_LOCALE:
16665 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016666 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016667 break;
16668 default:
16669 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016670 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016671 }
16672 } else {
16673 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016674 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016675 // Resolution will need to happen again later
16676 return;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016677 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016678 break;
16679 case TEXT_DIRECTION_FIRST_STRONG:
16680 case TEXT_DIRECTION_ANY_RTL:
16681 case TEXT_DIRECTION_LTR:
16682 case TEXT_DIRECTION_RTL:
16683 case TEXT_DIRECTION_LOCALE:
16684 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016685 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016686 break;
16687 default:
16688 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016689 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016690 }
16691 } else {
16692 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016693 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016694 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016695
16696 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016697 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016698 }
16699
16700 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016701 * Check if text direction resolution can be done.
16702 *
16703 * @return true if text direction resolution can be done otherwise return false.
16704 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016705 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016706 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016707 case TEXT_DIRECTION_INHERIT:
16708 return (mParent != null) && (mParent instanceof ViewGroup);
16709 default:
16710 return true;
16711 }
16712 }
16713
16714 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016715 * Reset resolved text direction. Text direction will be resolved during a call to
16716 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016717 *
16718 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016719 */
16720 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016721 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016722 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016723 // Set to default value
16724 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016725 }
16726
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016727 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016728 * @return true if text direction is inherited.
16729 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016730 * @hide
16731 */
16732 public boolean isTextDirectionInherited() {
16733 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16734 }
16735
16736 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016737 * @return true if text direction is resolved.
16738 */
16739 private boolean isTextDirectionResolved() {
16740 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16741 }
16742
16743 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016744 * Return the value specifying the text alignment or policy that was set with
16745 * {@link #setTextAlignment(int)}.
16746 *
16747 * @return the defined text alignment. It can be one of:
16748 *
16749 * {@link #TEXT_ALIGNMENT_INHERIT},
16750 * {@link #TEXT_ALIGNMENT_GRAVITY},
16751 * {@link #TEXT_ALIGNMENT_CENTER},
16752 * {@link #TEXT_ALIGNMENT_TEXT_START},
16753 * {@link #TEXT_ALIGNMENT_TEXT_END},
16754 * {@link #TEXT_ALIGNMENT_VIEW_START},
16755 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016756 *
16757 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016758 */
16759 @ViewDebug.ExportedProperty(category = "text", mapping = {
16760 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16761 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16762 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16763 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16764 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16765 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16766 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16767 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016768 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016769 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016770 }
16771
16772 /**
16773 * Set the text alignment.
16774 *
16775 * @param textAlignment The text alignment to set. Should be one of
16776 *
16777 * {@link #TEXT_ALIGNMENT_INHERIT},
16778 * {@link #TEXT_ALIGNMENT_GRAVITY},
16779 * {@link #TEXT_ALIGNMENT_CENTER},
16780 * {@link #TEXT_ALIGNMENT_TEXT_START},
16781 * {@link #TEXT_ALIGNMENT_TEXT_END},
16782 * {@link #TEXT_ALIGNMENT_VIEW_START},
16783 * {@link #TEXT_ALIGNMENT_VIEW_END}
16784 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016785 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16786 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16787 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16788 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016789 * @attr ref android.R.styleable#View_textAlignment
16790 */
16791 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016792 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016793 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016794 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016795 resetResolvedTextAlignment();
16796 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016797 mPrivateFlags2 |=
16798 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16799 // Do resolution
16800 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016801 // Notify change
16802 onRtlPropertiesChanged();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016803 // Refresh
16804 requestLayout();
16805 invalidate(true);
16806 }
16807 }
16808
16809 /**
16810 * Return the resolved text alignment.
16811 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016812 * @return the resolved text alignment. Returns one of:
16813 *
16814 * {@link #TEXT_ALIGNMENT_GRAVITY},
16815 * {@link #TEXT_ALIGNMENT_CENTER},
16816 * {@link #TEXT_ALIGNMENT_TEXT_START},
16817 * {@link #TEXT_ALIGNMENT_TEXT_END},
16818 * {@link #TEXT_ALIGNMENT_VIEW_START},
16819 * {@link #TEXT_ALIGNMENT_VIEW_END}
16820 */
16821 @ViewDebug.ExportedProperty(category = "text", mapping = {
16822 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16823 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16824 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16825 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16826 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16827 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16828 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16829 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016830 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016831 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16832 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016833 }
16834
16835 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016836 * Resolve the text alignment.
16837 *
16838 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016839 */
16840 public void resolveTextAlignment() {
16841 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016842 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016843
16844 if (hasRtlSupport()) {
16845 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016846 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016847 switch (textAlignment) {
16848 case TEXT_ALIGNMENT_INHERIT:
16849 // Check if we can resolve the text alignment
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016850 if (canResolveTextAlignment() && mParent instanceof View) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016851 View view = (View) mParent;
16852
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016853 final int parentResolvedTextAlignment = view.getTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016854 switch (parentResolvedTextAlignment) {
16855 case TEXT_ALIGNMENT_GRAVITY:
16856 case TEXT_ALIGNMENT_TEXT_START:
16857 case TEXT_ALIGNMENT_TEXT_END:
16858 case TEXT_ALIGNMENT_CENTER:
16859 case TEXT_ALIGNMENT_VIEW_START:
16860 case TEXT_ALIGNMENT_VIEW_END:
16861 // Resolved text alignment is the same as the parent resolved
16862 // text alignment
16863 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -070016864 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016865 break;
16866 default:
16867 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016868 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016869 }
16870 }
16871 else {
16872 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016873 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016874 // Resolution will need to happen again later
16875 return;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016876 }
16877 break;
16878 case TEXT_ALIGNMENT_GRAVITY:
16879 case TEXT_ALIGNMENT_TEXT_START:
16880 case TEXT_ALIGNMENT_TEXT_END:
16881 case TEXT_ALIGNMENT_CENTER:
16882 case TEXT_ALIGNMENT_VIEW_START:
16883 case TEXT_ALIGNMENT_VIEW_END:
16884 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016885 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016886 break;
16887 default:
16888 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016889 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016890 }
16891 } else {
16892 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016893 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016894 }
16895
16896 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016897 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016898 }
16899
16900 /**
16901 * Check if text alignment resolution can be done.
16902 *
16903 * @return true if text alignment resolution can be done otherwise return false.
16904 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016905 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016906 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016907 case TEXT_DIRECTION_INHERIT:
16908 return (mParent != null);
16909 default:
16910 return true;
16911 }
16912 }
16913
16914 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016915 * Reset resolved text alignment. Text alignment will be resolved during a call to
16916 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016917 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016918 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016919 */
16920 public void resetResolvedTextAlignment() {
16921 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016922 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016923 // Set to default
16924 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016925 }
16926
Adam Powella9108a22012-07-18 11:18:09 -070016927 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016928 * @return true if text alignment is inherited.
16929 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016930 * @hide
16931 */
16932 public boolean isTextAlignmentInherited() {
16933 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16934 }
16935
16936 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016937 * @return true if text alignment is resolved.
16938 */
16939 private boolean isTextAlignmentResolved() {
16940 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
16941 }
16942
16943 /**
Adam Powella9108a22012-07-18 11:18:09 -070016944 * Generate a value suitable for use in {@link #setId(int)}.
16945 * This value will not collide with ID values generated at build time by aapt for R.id.
16946 *
16947 * @return a generated ID value
16948 */
16949 public static int generateViewId() {
16950 for (;;) {
16951 final int result = sNextGeneratedId.get();
16952 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16953 int newValue = result + 1;
16954 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16955 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16956 return result;
16957 }
16958 }
16959 }
16960
Chet Haaseb39f0512011-05-24 14:36:40 -070016961 //
16962 // Properties
16963 //
16964 /**
16965 * A Property wrapper around the <code>alpha</code> functionality handled by the
16966 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16967 */
Chet Haased47f1532011-12-16 11:18:52 -080016968 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016969 @Override
16970 public void setValue(View object, float value) {
16971 object.setAlpha(value);
16972 }
16973
16974 @Override
16975 public Float get(View object) {
16976 return object.getAlpha();
16977 }
16978 };
16979
16980 /**
16981 * A Property wrapper around the <code>translationX</code> functionality handled by the
16982 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16983 */
Chet Haased47f1532011-12-16 11:18:52 -080016984 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016985 @Override
16986 public void setValue(View object, float value) {
16987 object.setTranslationX(value);
16988 }
16989
16990 @Override
16991 public Float get(View object) {
16992 return object.getTranslationX();
16993 }
16994 };
16995
16996 /**
16997 * A Property wrapper around the <code>translationY</code> functionality handled by the
16998 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16999 */
Chet Haased47f1532011-12-16 11:18:52 -080017000 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017001 @Override
17002 public void setValue(View object, float value) {
17003 object.setTranslationY(value);
17004 }
17005
17006 @Override
17007 public Float get(View object) {
17008 return object.getTranslationY();
17009 }
17010 };
17011
17012 /**
17013 * A Property wrapper around the <code>x</code> functionality handled by the
17014 * {@link View#setX(float)} and {@link View#getX()} methods.
17015 */
Chet Haased47f1532011-12-16 11:18:52 -080017016 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017017 @Override
17018 public void setValue(View object, float value) {
17019 object.setX(value);
17020 }
17021
17022 @Override
17023 public Float get(View object) {
17024 return object.getX();
17025 }
17026 };
17027
17028 /**
17029 * A Property wrapper around the <code>y</code> functionality handled by the
17030 * {@link View#setY(float)} and {@link View#getY()} methods.
17031 */
Chet Haased47f1532011-12-16 11:18:52 -080017032 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017033 @Override
17034 public void setValue(View object, float value) {
17035 object.setY(value);
17036 }
17037
17038 @Override
17039 public Float get(View object) {
17040 return object.getY();
17041 }
17042 };
17043
17044 /**
17045 * A Property wrapper around the <code>rotation</code> functionality handled by the
17046 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17047 */
Chet Haased47f1532011-12-16 11:18:52 -080017048 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017049 @Override
17050 public void setValue(View object, float value) {
17051 object.setRotation(value);
17052 }
17053
17054 @Override
17055 public Float get(View object) {
17056 return object.getRotation();
17057 }
17058 };
17059
17060 /**
17061 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17062 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17063 */
Chet Haased47f1532011-12-16 11:18:52 -080017064 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017065 @Override
17066 public void setValue(View object, float value) {
17067 object.setRotationX(value);
17068 }
17069
17070 @Override
17071 public Float get(View object) {
17072 return object.getRotationX();
17073 }
17074 };
17075
17076 /**
17077 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17078 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17079 */
Chet Haased47f1532011-12-16 11:18:52 -080017080 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017081 @Override
17082 public void setValue(View object, float value) {
17083 object.setRotationY(value);
17084 }
17085
17086 @Override
17087 public Float get(View object) {
17088 return object.getRotationY();
17089 }
17090 };
17091
17092 /**
17093 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17094 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17095 */
Chet Haased47f1532011-12-16 11:18:52 -080017096 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017097 @Override
17098 public void setValue(View object, float value) {
17099 object.setScaleX(value);
17100 }
17101
17102 @Override
17103 public Float get(View object) {
17104 return object.getScaleX();
17105 }
17106 };
17107
17108 /**
17109 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17110 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17111 */
Chet Haased47f1532011-12-16 11:18:52 -080017112 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017113 @Override
17114 public void setValue(View object, float value) {
17115 object.setScaleY(value);
17116 }
17117
17118 @Override
17119 public Float get(View object) {
17120 return object.getScaleY();
17121 }
17122 };
17123
Jeff Brown33bbfd22011-02-24 20:55:35 -080017124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017125 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17126 * Each MeasureSpec represents a requirement for either the width or the height.
17127 * A MeasureSpec is comprised of a size and a mode. There are three possible
17128 * modes:
17129 * <dl>
17130 * <dt>UNSPECIFIED</dt>
17131 * <dd>
17132 * The parent has not imposed any constraint on the child. It can be whatever size
17133 * it wants.
17134 * </dd>
17135 *
17136 * <dt>EXACTLY</dt>
17137 * <dd>
17138 * The parent has determined an exact size for the child. The child is going to be
17139 * given those bounds regardless of how big it wants to be.
17140 * </dd>
17141 *
17142 * <dt>AT_MOST</dt>
17143 * <dd>
17144 * The child can be as large as it wants up to the specified size.
17145 * </dd>
17146 * </dl>
17147 *
17148 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17149 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17150 */
17151 public static class MeasureSpec {
17152 private static final int MODE_SHIFT = 30;
17153 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17154
17155 /**
17156 * Measure specification mode: The parent has not imposed any constraint
17157 * on the child. It can be whatever size it wants.
17158 */
17159 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17160
17161 /**
17162 * Measure specification mode: The parent has determined an exact size
17163 * for the child. The child is going to be given those bounds regardless
17164 * of how big it wants to be.
17165 */
17166 public static final int EXACTLY = 1 << MODE_SHIFT;
17167
17168 /**
17169 * Measure specification mode: The child can be as large as it wants up
17170 * to the specified size.
17171 */
17172 public static final int AT_MOST = 2 << MODE_SHIFT;
17173
17174 /**
17175 * Creates a measure specification based on the supplied size and mode.
17176 *
17177 * The mode must always be one of the following:
17178 * <ul>
17179 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17180 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17181 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17182 * </ul>
17183 *
17184 * @param size the size of the measure specification
17185 * @param mode the mode of the measure specification
17186 * @return the measure specification based on size and mode
17187 */
17188 public static int makeMeasureSpec(int size, int mode) {
17189 return size + mode;
17190 }
17191
17192 /**
17193 * Extracts the mode from the supplied measure specification.
17194 *
17195 * @param measureSpec the measure specification to extract the mode from
17196 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17197 * {@link android.view.View.MeasureSpec#AT_MOST} or
17198 * {@link android.view.View.MeasureSpec#EXACTLY}
17199 */
17200 public static int getMode(int measureSpec) {
17201 return (measureSpec & MODE_MASK);
17202 }
17203
17204 /**
17205 * Extracts the size from the supplied measure specification.
17206 *
17207 * @param measureSpec the measure specification to extract the size from
17208 * @return the size in pixels defined in the supplied measure specification
17209 */
17210 public static int getSize(int measureSpec) {
17211 return (measureSpec & ~MODE_MASK);
17212 }
17213
17214 /**
17215 * Returns a String representation of the specified measure
17216 * specification.
17217 *
17218 * @param measureSpec the measure specification to convert to a String
17219 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17220 */
17221 public static String toString(int measureSpec) {
17222 int mode = getMode(measureSpec);
17223 int size = getSize(measureSpec);
17224
17225 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17226
17227 if (mode == UNSPECIFIED)
17228 sb.append("UNSPECIFIED ");
17229 else if (mode == EXACTLY)
17230 sb.append("EXACTLY ");
17231 else if (mode == AT_MOST)
17232 sb.append("AT_MOST ");
17233 else
17234 sb.append(mode).append(" ");
17235
17236 sb.append(size);
17237 return sb.toString();
17238 }
17239 }
17240
17241 class CheckForLongPress implements Runnable {
17242
17243 private int mOriginalWindowAttachCount;
17244
17245 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017246 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017247 && mOriginalWindowAttachCount == mWindowAttachCount) {
17248 if (performLongClick()) {
17249 mHasPerformedLongPress = true;
17250 }
17251 }
17252 }
17253
17254 public void rememberWindowAttachCount() {
17255 mOriginalWindowAttachCount = mWindowAttachCount;
17256 }
17257 }
Joe Malin32736f02011-01-19 16:14:20 -080017258
Adam Powelle14579b2009-12-16 18:39:52 -080017259 private final class CheckForTap implements Runnable {
17260 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017261 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017262 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017263 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017264 }
17265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017266
Adam Powella35d7682010-03-12 14:48:13 -080017267 private final class PerformClick implements Runnable {
17268 public void run() {
17269 performClick();
17270 }
17271 }
17272
Dianne Hackborn63042d62011-01-26 18:56:29 -080017273 /** @hide */
17274 public void hackTurnOffWindowResizeAnim(boolean off) {
17275 mAttachInfo.mTurnOffWindowResizeAnim = off;
17276 }
Joe Malin32736f02011-01-19 16:14:20 -080017277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017278 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017279 * This method returns a ViewPropertyAnimator object, which can be used to animate
17280 * specific properties on this View.
17281 *
17282 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17283 */
17284 public ViewPropertyAnimator animate() {
17285 if (mAnimator == null) {
17286 mAnimator = new ViewPropertyAnimator(this);
17287 }
17288 return mAnimator;
17289 }
17290
17291 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017292 * Interface definition for a callback to be invoked when a hardware key event is
17293 * dispatched to this view. The callback will be invoked before the key event is
17294 * given to the view. This is only useful for hardware keyboards; a software input
17295 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017296 */
17297 public interface OnKeyListener {
17298 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017299 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017300 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017301 * <p>Key presses in software keyboards will generally NOT trigger this method,
17302 * although some may elect to do so in some situations. Do not assume a
17303 * software input method has to be key-based; even if it is, it may use key presses
17304 * in a different way than you expect, so there is no way to reliably catch soft
17305 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017306 *
17307 * @param v The view the key has been dispatched to.
17308 * @param keyCode The code for the physical key that was pressed
17309 * @param event The KeyEvent object containing full information about
17310 * the event.
17311 * @return True if the listener has consumed the event, false otherwise.
17312 */
17313 boolean onKey(View v, int keyCode, KeyEvent event);
17314 }
17315
17316 /**
17317 * Interface definition for a callback to be invoked when a touch event is
17318 * dispatched to this view. The callback will be invoked before the touch
17319 * event is given to the view.
17320 */
17321 public interface OnTouchListener {
17322 /**
17323 * Called when a touch event is dispatched to a view. This allows listeners to
17324 * get a chance to respond before the target view.
17325 *
17326 * @param v The view the touch event has been dispatched to.
17327 * @param event The MotionEvent object containing full information about
17328 * the event.
17329 * @return True if the listener has consumed the event, false otherwise.
17330 */
17331 boolean onTouch(View v, MotionEvent event);
17332 }
17333
17334 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017335 * Interface definition for a callback to be invoked when a hover event is
17336 * dispatched to this view. The callback will be invoked before the hover
17337 * event is given to the view.
17338 */
17339 public interface OnHoverListener {
17340 /**
17341 * Called when a hover event is dispatched to a view. This allows listeners to
17342 * get a chance to respond before the target view.
17343 *
17344 * @param v The view the hover event has been dispatched to.
17345 * @param event The MotionEvent object containing full information about
17346 * the event.
17347 * @return True if the listener has consumed the event, false otherwise.
17348 */
17349 boolean onHover(View v, MotionEvent event);
17350 }
17351
17352 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017353 * Interface definition for a callback to be invoked when a generic motion event is
17354 * dispatched to this view. The callback will be invoked before the generic motion
17355 * event is given to the view.
17356 */
17357 public interface OnGenericMotionListener {
17358 /**
17359 * Called when a generic motion event is dispatched to a view. This allows listeners to
17360 * get a chance to respond before the target view.
17361 *
17362 * @param v The view the generic motion event has been dispatched to.
17363 * @param event The MotionEvent object containing full information about
17364 * the event.
17365 * @return True if the listener has consumed the event, false otherwise.
17366 */
17367 boolean onGenericMotion(View v, MotionEvent event);
17368 }
17369
17370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017371 * Interface definition for a callback to be invoked when a view has been clicked and held.
17372 */
17373 public interface OnLongClickListener {
17374 /**
17375 * Called when a view has been clicked and held.
17376 *
17377 * @param v The view that was clicked and held.
17378 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017379 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017380 */
17381 boolean onLongClick(View v);
17382 }
17383
17384 /**
Chris Tate32affef2010-10-18 15:29:21 -070017385 * Interface definition for a callback to be invoked when a drag is being dispatched
17386 * to this view. The callback will be invoked before the hosting view's own
17387 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17388 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017389 *
17390 * <div class="special reference">
17391 * <h3>Developer Guides</h3>
17392 * <p>For a guide to implementing drag and drop features, read the
17393 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17394 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017395 */
17396 public interface OnDragListener {
17397 /**
17398 * Called when a drag event is dispatched to a view. This allows listeners
17399 * to get a chance to override base View behavior.
17400 *
Joe Malin32736f02011-01-19 16:14:20 -080017401 * @param v The View that received the drag event.
17402 * @param event The {@link android.view.DragEvent} object for the drag event.
17403 * @return {@code true} if the drag event was handled successfully, or {@code false}
17404 * if the drag event was not handled. Note that {@code false} will trigger the View
17405 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017406 */
17407 boolean onDrag(View v, DragEvent event);
17408 }
17409
17410 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017411 * Interface definition for a callback to be invoked when the focus state of
17412 * a view changed.
17413 */
17414 public interface OnFocusChangeListener {
17415 /**
17416 * Called when the focus state of a view has changed.
17417 *
17418 * @param v The view whose state has changed.
17419 * @param hasFocus The new focus state of v.
17420 */
17421 void onFocusChange(View v, boolean hasFocus);
17422 }
17423
17424 /**
17425 * Interface definition for a callback to be invoked when a view is clicked.
17426 */
17427 public interface OnClickListener {
17428 /**
17429 * Called when a view has been clicked.
17430 *
17431 * @param v The view that was clicked.
17432 */
17433 void onClick(View v);
17434 }
17435
17436 /**
17437 * Interface definition for a callback to be invoked when the context menu
17438 * for this view is being built.
17439 */
17440 public interface OnCreateContextMenuListener {
17441 /**
17442 * Called when the context menu for this view is being built. It is not
17443 * safe to hold onto the menu after this method returns.
17444 *
17445 * @param menu The context menu that is being built
17446 * @param v The view for which the context menu is being built
17447 * @param menuInfo Extra information about the item for which the
17448 * context menu should be shown. This information will vary
17449 * depending on the class of v.
17450 */
17451 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17452 }
17453
Joe Onorato664644d2011-01-23 17:53:23 -080017454 /**
17455 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017456 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017457 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017458 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017459 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017460 */
17461 public interface OnSystemUiVisibilityChangeListener {
17462 /**
17463 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017464 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017465 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017466 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17467 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17468 * This tells you the <strong>global</strong> state of these UI visibility
17469 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017470 */
17471 public void onSystemUiVisibilityChange(int visibility);
17472 }
17473
Adam Powell4afd62b2011-02-18 15:02:18 -080017474 /**
17475 * Interface definition for a callback to be invoked when this view is attached
17476 * or detached from its window.
17477 */
17478 public interface OnAttachStateChangeListener {
17479 /**
17480 * Called when the view is attached to a window.
17481 * @param v The view that was attached
17482 */
17483 public void onViewAttachedToWindow(View v);
17484 /**
17485 * Called when the view is detached from a window.
17486 * @param v The view that was detached
17487 */
17488 public void onViewDetachedFromWindow(View v);
17489 }
17490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017491 private final class UnsetPressedState implements Runnable {
17492 public void run() {
17493 setPressed(false);
17494 }
17495 }
17496
17497 /**
17498 * Base class for derived classes that want to save and restore their own
17499 * state in {@link android.view.View#onSaveInstanceState()}.
17500 */
17501 public static class BaseSavedState extends AbsSavedState {
17502 /**
17503 * Constructor used when reading from a parcel. Reads the state of the superclass.
17504 *
17505 * @param source
17506 */
17507 public BaseSavedState(Parcel source) {
17508 super(source);
17509 }
17510
17511 /**
17512 * Constructor called by derived classes when creating their SavedState objects
17513 *
17514 * @param superState The state of the superclass of this view
17515 */
17516 public BaseSavedState(Parcelable superState) {
17517 super(superState);
17518 }
17519
17520 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17521 new Parcelable.Creator<BaseSavedState>() {
17522 public BaseSavedState createFromParcel(Parcel in) {
17523 return new BaseSavedState(in);
17524 }
17525
17526 public BaseSavedState[] newArray(int size) {
17527 return new BaseSavedState[size];
17528 }
17529 };
17530 }
17531
17532 /**
17533 * A set of information given to a view when it is attached to its parent
17534 * window.
17535 */
17536 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017537 interface Callbacks {
17538 void playSoundEffect(int effectId);
17539 boolean performHapticFeedback(int effectId, boolean always);
17540 }
17541
17542 /**
17543 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17544 * to a Handler. This class contains the target (View) to invalidate and
17545 * the coordinates of the dirty rectangle.
17546 *
17547 * For performance purposes, this class also implements a pool of up to
17548 * POOL_LIMIT objects that get reused. This reduces memory allocations
17549 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017550 */
Romain Guyd928d682009-03-31 17:52:16 -070017551 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017552 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017553 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17554 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017555 public InvalidateInfo newInstance() {
17556 return new InvalidateInfo();
17557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017558
Romain Guyd928d682009-03-31 17:52:16 -070017559 public void onAcquired(InvalidateInfo element) {
17560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017561
Romain Guyd928d682009-03-31 17:52:16 -070017562 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017563 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017564 }
17565 }, POOL_LIMIT)
17566 );
17567
17568 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017569 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017570
17571 View target;
17572
17573 int left;
17574 int top;
17575 int right;
17576 int bottom;
17577
Romain Guyd928d682009-03-31 17:52:16 -070017578 public void setNextPoolable(InvalidateInfo element) {
17579 mNext = element;
17580 }
17581
17582 public InvalidateInfo getNextPoolable() {
17583 return mNext;
17584 }
17585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017586 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017587 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017588 }
17589
17590 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017591 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017592 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017593
17594 public boolean isPooled() {
17595 return mIsPooled;
17596 }
17597
17598 public void setPooled(boolean isPooled) {
17599 mIsPooled = isPooled;
17600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017601 }
17602
17603 final IWindowSession mSession;
17604
17605 final IWindow mWindow;
17606
17607 final IBinder mWindowToken;
17608
Jeff Brown98365d72012-08-19 20:30:52 -070017609 final Display mDisplay;
17610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017611 final Callbacks mRootCallbacks;
17612
Romain Guy59a12ca2011-06-09 17:48:21 -070017613 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017615 /**
17616 * The top view of the hierarchy.
17617 */
17618 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017620 IBinder mPanelParentWindowToken;
17621 Surface mSurface;
17622
Romain Guyb051e892010-09-28 19:09:36 -070017623 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017624 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017625 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017626
Romain Guy7e4e5612012-03-05 14:37:29 -080017627 boolean mScreenOn;
17628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017629 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017630 * Scale factor used by the compatibility mode
17631 */
17632 float mApplicationScale;
17633
17634 /**
17635 * Indicates whether the application is in compatibility mode
17636 */
17637 boolean mScalingRequired;
17638
17639 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017640 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017641 */
17642 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017643
Dianne Hackborn63042d62011-01-26 18:56:29 -080017644 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017645 * Left position of this view's window
17646 */
17647 int mWindowLeft;
17648
17649 /**
17650 * Top position of this view's window
17651 */
17652 int mWindowTop;
17653
17654 /**
Adam Powell26153a32010-11-08 15:22:27 -080017655 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017656 */
Adam Powell26153a32010-11-08 15:22:27 -080017657 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017658
17659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017660 * For windows that are full-screen but using insets to layout inside
17661 * of the screen decorations, these are the current insets for the
17662 * content of the window.
17663 */
17664 final Rect mContentInsets = new Rect();
17665
17666 /**
17667 * For windows that are full-screen but using insets to layout inside
17668 * of the screen decorations, these are the current insets for the
17669 * actual visible parts of the window.
17670 */
17671 final Rect mVisibleInsets = new Rect();
17672
17673 /**
17674 * The internal insets given by this window. This value is
17675 * supplied by the client (through
17676 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17677 * be given to the window manager when changed to be used in laying
17678 * out windows behind it.
17679 */
17680 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17681 = new ViewTreeObserver.InternalInsetsInfo();
17682
17683 /**
17684 * All views in the window's hierarchy that serve as scroll containers,
17685 * used to determine if the window can be resized or must be panned
17686 * to adjust for a soft input area.
17687 */
17688 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17689
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017690 final KeyEvent.DispatcherState mKeyDispatchState
17691 = new KeyEvent.DispatcherState();
17692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017693 /**
17694 * Indicates whether the view's window currently has the focus.
17695 */
17696 boolean mHasWindowFocus;
17697
17698 /**
17699 * The current visibility of the window.
17700 */
17701 int mWindowVisibility;
17702
17703 /**
17704 * Indicates the time at which drawing started to occur.
17705 */
17706 long mDrawingTime;
17707
17708 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017709 * Indicates whether or not ignoring the DIRTY_MASK flags.
17710 */
17711 boolean mIgnoreDirtyState;
17712
17713 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017714 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17715 * to avoid clearing that flag prematurely.
17716 */
17717 boolean mSetIgnoreDirtyState = false;
17718
17719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017720 * Indicates whether the view's window is currently in touch mode.
17721 */
17722 boolean mInTouchMode;
17723
17724 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017725 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017726 * the next time it performs a traversal
17727 */
17728 boolean mRecomputeGlobalAttributes;
17729
17730 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017731 * Always report new attributes at next traversal.
17732 */
17733 boolean mForceReportNewAttributes;
17734
17735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017736 * Set during a traveral if any views want to keep the screen on.
17737 */
17738 boolean mKeepScreenOn;
17739
17740 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017741 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17742 */
17743 int mSystemUiVisibility;
17744
17745 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017746 * Hack to force certain system UI visibility flags to be cleared.
17747 */
17748 int mDisabledSystemUiVisibility;
17749
17750 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017751 * Last global system UI visibility reported by the window manager.
17752 */
17753 int mGlobalSystemUiVisibility;
17754
17755 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017756 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17757 * attached.
17758 */
17759 boolean mHasSystemUiListeners;
17760
17761 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017762 * Set if the visibility of any views has changed.
17763 */
17764 boolean mViewVisibilityChanged;
17765
17766 /**
17767 * Set to true if a view has been scrolled.
17768 */
17769 boolean mViewScrollChanged;
17770
17771 /**
17772 * Global to the view hierarchy used as a temporary for dealing with
17773 * x/y points in the transparent region computations.
17774 */
17775 final int[] mTransparentLocation = new int[2];
17776
17777 /**
17778 * Global to the view hierarchy used as a temporary for dealing with
17779 * x/y points in the ViewGroup.invalidateChild implementation.
17780 */
17781 final int[] mInvalidateChildLocation = new int[2];
17782
Chet Haasec3aa3612010-06-17 08:50:37 -070017783
17784 /**
17785 * Global to the view hierarchy used as a temporary for dealing with
17786 * x/y location when view is transformed.
17787 */
17788 final float[] mTmpTransformLocation = new float[2];
17789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017790 /**
17791 * The view tree observer used to dispatch global events like
17792 * layout, pre-draw, touch mode change, etc.
17793 */
17794 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17795
17796 /**
17797 * A Canvas used by the view hierarchy to perform bitmap caching.
17798 */
17799 Canvas mCanvas;
17800
17801 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017802 * The view root impl.
17803 */
17804 final ViewRootImpl mViewRootImpl;
17805
17806 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017807 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017808 * handler can be used to pump events in the UI events queue.
17809 */
17810 final Handler mHandler;
17811
17812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017813 * Temporary for use in computing invalidate rectangles while
17814 * calling up the hierarchy.
17815 */
17816 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017817
17818 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017819 * Temporary for use in computing hit areas with transformed views
17820 */
17821 final RectF mTmpTransformRect = new RectF();
17822
17823 /**
Chet Haase599913d2012-07-23 16:22:05 -070017824 * Temporary for use in transforming invalidation rect
17825 */
17826 final Matrix mTmpMatrix = new Matrix();
17827
17828 /**
17829 * Temporary for use in transforming invalidation rect
17830 */
17831 final Transformation mTmpTransformation = new Transformation();
17832
17833 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017834 * Temporary list for use in collecting focusable descendents of a view.
17835 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017836 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017838 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017839 * The id of the window for accessibility purposes.
17840 */
17841 int mAccessibilityWindowId = View.NO_ID;
17842
17843 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017844 * Whether to ingore not exposed for accessibility Views when
17845 * reporting the view tree to accessibility services.
17846 */
17847 boolean mIncludeNotImportantViews;
17848
17849 /**
17850 * The drawable for highlighting accessibility focus.
17851 */
17852 Drawable mAccessibilityFocusDrawable;
17853
17854 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017855 * Show where the margins, bounds and layout bounds are for each view.
17856 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017857 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017858
17859 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017860 * Point used to compute visible regions.
17861 */
17862 final Point mPoint = new Point();
17863
17864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017865 * Creates a new set of attachment information with the specified
17866 * events handler and thread.
17867 *
17868 * @param handler the events handler the view must use
17869 */
Jeff Brown98365d72012-08-19 20:30:52 -070017870 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017871 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017872 mSession = session;
17873 mWindow = window;
17874 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017875 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017876 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017877 mHandler = handler;
17878 mRootCallbacks = effectPlayer;
17879 }
17880 }
17881
17882 /**
17883 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17884 * is supported. This avoids keeping too many unused fields in most
17885 * instances of View.</p>
17886 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017887 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017888
Mike Cleronf116bf82009-09-27 19:14:12 -070017889 /**
17890 * Scrollbars are not visible
17891 */
17892 public static final int OFF = 0;
17893
17894 /**
17895 * Scrollbars are visible
17896 */
17897 public static final int ON = 1;
17898
17899 /**
17900 * Scrollbars are fading away
17901 */
17902 public static final int FADING = 2;
17903
17904 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017906 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017907 public int scrollBarDefaultDelayBeforeFade;
17908 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017909
17910 public int scrollBarSize;
17911 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017912 public float[] interpolatorValues;
17913 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017914
17915 public final Paint paint;
17916 public final Matrix matrix;
17917 public Shader shader;
17918
Mike Cleronf116bf82009-09-27 19:14:12 -070017919 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17920
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017921 private static final float[] OPAQUE = { 255 };
17922 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017923
Mike Cleronf116bf82009-09-27 19:14:12 -070017924 /**
17925 * When fading should start. This time moves into the future every time
17926 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17927 */
17928 public long fadeStartTime;
17929
17930
17931 /**
17932 * The current state of the scrollbars: ON, OFF, or FADING
17933 */
17934 public int state = OFF;
17935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017936 private int mLastColor;
17937
Mike Cleronf116bf82009-09-27 19:14:12 -070017938 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017939 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17940 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017941 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17942 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017943
17944 paint = new Paint();
17945 matrix = new Matrix();
17946 // use use a height of 1, and then wack the matrix each time we
17947 // actually use it.
17948 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017949 paint.setShader(shader);
17950 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017951
Mike Cleronf116bf82009-09-27 19:14:12 -070017952 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017953 }
Romain Guy8506ab42009-06-11 17:35:47 -070017954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017955 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017956 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017957 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017958
Romain Guyd679b572012-08-29 21:49:00 -070017959 if (color != 0) {
17960 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17961 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17962 paint.setShader(shader);
17963 // Restore the default transfer mode (src_over)
17964 paint.setXfermode(null);
17965 } else {
17966 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17967 paint.setShader(shader);
17968 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017970 }
17971 }
Joe Malin32736f02011-01-19 16:14:20 -080017972
Mike Cleronf116bf82009-09-27 19:14:12 -070017973 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017974 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017975 if (now >= fadeStartTime) {
17976
17977 // the animation fades the scrollbars out by changing
17978 // the opacity (alpha) from fully opaque to fully
17979 // transparent
17980 int nextFrame = (int) now;
17981 int framesCount = 0;
17982
17983 Interpolator interpolator = scrollBarInterpolator;
17984
17985 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017986 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017987
17988 // End transparent
17989 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017990 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017991
17992 state = FADING;
17993
17994 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017995 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017996 }
17997 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017998 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017999
Svetoslav Ganova0156172011-06-26 17:55:44 -070018000 /**
18001 * Resuable callback for sending
18002 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18003 */
18004 private class SendViewScrolledAccessibilityEvent implements Runnable {
18005 public volatile boolean mIsPending;
18006
18007 public void run() {
18008 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18009 mIsPending = false;
18010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018011 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018012
18013 /**
18014 * <p>
18015 * This class represents a delegate that can be registered in a {@link View}
18016 * to enhance accessibility support via composition rather via inheritance.
18017 * It is specifically targeted to widget developers that extend basic View
18018 * classes i.e. classes in package android.view, that would like their
18019 * applications to be backwards compatible.
18020 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018021 * <div class="special reference">
18022 * <h3>Developer Guides</h3>
18023 * <p>For more information about making applications accessible, read the
18024 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18025 * developer guide.</p>
18026 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018027 * <p>
18028 * A scenario in which a developer would like to use an accessibility delegate
18029 * is overriding a method introduced in a later API version then the minimal API
18030 * version supported by the application. For example, the method
18031 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18032 * in API version 4 when the accessibility APIs were first introduced. If a
18033 * developer would like his application to run on API version 4 devices (assuming
18034 * all other APIs used by the application are version 4 or lower) and take advantage
18035 * of this method, instead of overriding the method which would break the application's
18036 * backwards compatibility, he can override the corresponding method in this
18037 * delegate and register the delegate in the target View if the API version of
18038 * the system is high enough i.e. the API version is same or higher to the API
18039 * version that introduced
18040 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18041 * </p>
18042 * <p>
18043 * Here is an example implementation:
18044 * </p>
18045 * <code><pre><p>
18046 * if (Build.VERSION.SDK_INT >= 14) {
18047 * // If the API version is equal of higher than the version in
18048 * // which onInitializeAccessibilityNodeInfo was introduced we
18049 * // register a delegate with a customized implementation.
18050 * View view = findViewById(R.id.view_id);
18051 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18052 * public void onInitializeAccessibilityNodeInfo(View host,
18053 * AccessibilityNodeInfo info) {
18054 * // Let the default implementation populate the info.
18055 * super.onInitializeAccessibilityNodeInfo(host, info);
18056 * // Set some other information.
18057 * info.setEnabled(host.isEnabled());
18058 * }
18059 * });
18060 * }
18061 * </code></pre></p>
18062 * <p>
18063 * This delegate contains methods that correspond to the accessibility methods
18064 * in View. If a delegate has been specified the implementation in View hands
18065 * off handling to the corresponding method in this delegate. The default
18066 * implementation the delegate methods behaves exactly as the corresponding
18067 * method in View for the case of no accessibility delegate been set. Hence,
18068 * to customize the behavior of a View method, clients can override only the
18069 * corresponding delegate method without altering the behavior of the rest
18070 * accessibility related methods of the host view.
18071 * </p>
18072 */
18073 public static class AccessibilityDelegate {
18074
18075 /**
18076 * Sends an accessibility event of the given type. If accessibility is not
18077 * enabled this method has no effect.
18078 * <p>
18079 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18080 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18081 * been set.
18082 * </p>
18083 *
18084 * @param host The View hosting the delegate.
18085 * @param eventType The type of the event to send.
18086 *
18087 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18088 */
18089 public void sendAccessibilityEvent(View host, int eventType) {
18090 host.sendAccessibilityEventInternal(eventType);
18091 }
18092
18093 /**
alanv8eeefef2012-05-07 16:57:53 -070018094 * Performs the specified accessibility action on the view. For
18095 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18096 * <p>
18097 * The default implementation behaves as
18098 * {@link View#performAccessibilityAction(int, Bundle)
18099 * View#performAccessibilityAction(int, Bundle)} for the case of
18100 * no accessibility delegate been set.
18101 * </p>
18102 *
18103 * @param action The action to perform.
18104 * @return Whether the action was performed.
18105 *
18106 * @see View#performAccessibilityAction(int, Bundle)
18107 * View#performAccessibilityAction(int, Bundle)
18108 */
18109 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18110 return host.performAccessibilityActionInternal(action, args);
18111 }
18112
18113 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018114 * Sends an accessibility event. This method behaves exactly as
18115 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18116 * empty {@link AccessibilityEvent} and does not perform a check whether
18117 * accessibility is enabled.
18118 * <p>
18119 * The default implementation behaves as
18120 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18121 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18122 * the case of no accessibility delegate been set.
18123 * </p>
18124 *
18125 * @param host The View hosting the delegate.
18126 * @param event The event to send.
18127 *
18128 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18129 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18130 */
18131 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18132 host.sendAccessibilityEventUncheckedInternal(event);
18133 }
18134
18135 /**
18136 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18137 * to its children for adding their text content to the event.
18138 * <p>
18139 * The default implementation behaves as
18140 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18141 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18142 * the case of no accessibility delegate been set.
18143 * </p>
18144 *
18145 * @param host The View hosting the delegate.
18146 * @param event The event.
18147 * @return True if the event population was completed.
18148 *
18149 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18150 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18151 */
18152 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18153 return host.dispatchPopulateAccessibilityEventInternal(event);
18154 }
18155
18156 /**
18157 * Gives a chance to the host View to populate the accessibility event with its
18158 * text content.
18159 * <p>
18160 * The default implementation behaves as
18161 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18162 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18163 * the case of no accessibility delegate been set.
18164 * </p>
18165 *
18166 * @param host The View hosting the delegate.
18167 * @param event The accessibility event which to populate.
18168 *
18169 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18170 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18171 */
18172 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18173 host.onPopulateAccessibilityEventInternal(event);
18174 }
18175
18176 /**
18177 * Initializes an {@link AccessibilityEvent} with information about the
18178 * the host View which is the event source.
18179 * <p>
18180 * The default implementation behaves as
18181 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18182 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18183 * the case of no accessibility delegate been set.
18184 * </p>
18185 *
18186 * @param host The View hosting the delegate.
18187 * @param event The event to initialize.
18188 *
18189 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18190 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18191 */
18192 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18193 host.onInitializeAccessibilityEventInternal(event);
18194 }
18195
18196 /**
18197 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18198 * <p>
18199 * The default implementation behaves as
18200 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18201 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18202 * the case of no accessibility delegate been set.
18203 * </p>
18204 *
18205 * @param host The View hosting the delegate.
18206 * @param info The instance to initialize.
18207 *
18208 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18209 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18210 */
18211 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18212 host.onInitializeAccessibilityNodeInfoInternal(info);
18213 }
18214
18215 /**
18216 * Called when a child of the host View has requested sending an
18217 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18218 * to augment the event.
18219 * <p>
18220 * The default implementation behaves as
18221 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18222 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18223 * the case of no accessibility delegate been set.
18224 * </p>
18225 *
18226 * @param host The View hosting the delegate.
18227 * @param child The child which requests sending the event.
18228 * @param event The event to be sent.
18229 * @return True if the event should be sent
18230 *
18231 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18232 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18233 */
18234 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18235 AccessibilityEvent event) {
18236 return host.onRequestSendAccessibilityEventInternal(child, event);
18237 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018238
18239 /**
18240 * Gets the provider for managing a virtual view hierarchy rooted at this View
18241 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18242 * that explore the window content.
18243 * <p>
18244 * The default implementation behaves as
18245 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18246 * the case of no accessibility delegate been set.
18247 * </p>
18248 *
18249 * @return The provider.
18250 *
18251 * @see AccessibilityNodeProvider
18252 */
18253 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18254 return null;
18255 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018256 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018257
18258 private class MatchIdPredicate implements Predicate<View> {
18259 public int mId;
18260
18261 @Override
18262 public boolean apply(View view) {
18263 return (view.mID == mId);
18264 }
18265 }
18266
18267 private class MatchLabelForPredicate implements Predicate<View> {
18268 private int mLabeledId;
18269
18270 @Override
18271 public boolean apply(View view) {
18272 return (view.mLabelForId == mLabeledId);
18273 }
18274 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018275
18276 /**
18277 * Dump all private flags in readable format, useful for documentation and
18278 * sanity checking.
18279 */
18280 private static void dumpFlags() {
18281 final HashMap<String, String> found = Maps.newHashMap();
18282 try {
18283 for (Field field : View.class.getDeclaredFields()) {
18284 final int modifiers = field.getModifiers();
18285 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18286 if (field.getType().equals(int.class)) {
18287 final int value = field.getInt(null);
18288 dumpFlag(found, field.getName(), value);
18289 } else if (field.getType().equals(int[].class)) {
18290 final int[] values = (int[]) field.get(null);
18291 for (int i = 0; i < values.length; i++) {
18292 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18293 }
18294 }
18295 }
18296 }
18297 } catch (IllegalAccessException e) {
18298 throw new RuntimeException(e);
18299 }
18300
18301 final ArrayList<String> keys = Lists.newArrayList();
18302 keys.addAll(found.keySet());
18303 Collections.sort(keys);
18304 for (String key : keys) {
18305 Log.d(VIEW_LOG_TAG, found.get(key));
18306 }
18307 }
18308
18309 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18310 // Sort flags by prefix, then by bits, always keeping unique keys
18311 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18312 final int prefix = name.indexOf('_');
18313 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18314 final String output = bits + " " + name;
18315 found.put(key, output);
18316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018317}