blob: dd9646de9e0118e2bbbdb09101ddbf9099e38560 [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 Meglio9a048562012-09-26 14:55:56 -07005890 *
5891 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005892 */
5893 @ViewDebug.ExportedProperty(category = "layout")
5894 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005895 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005896 }
5897
5898 /**
Adam Powell539ee872012-02-03 19:00:49 -08005899 * Indicates whether the view is currently tracking transient state that the
5900 * app should not need to concern itself with saving and restoring, but that
5901 * the framework should take special note to preserve when possible.
5902 *
Adam Powell785c4472012-05-02 21:25:39 -07005903 * <p>A view with transient state cannot be trivially rebound from an external
5904 * data source, such as an adapter binding item views in a list. This may be
5905 * because the view is performing an animation, tracking user selection
5906 * of content, or similar.</p>
5907 *
Adam Powell539ee872012-02-03 19:00:49 -08005908 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005909 */
5910 @ViewDebug.ExportedProperty(category = "layout")
5911 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005912 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005913 }
5914
5915 /**
5916 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005917 * framework should attempt to preserve when possible. This flag is reference counted,
5918 * so every call to setHasTransientState(true) should be paired with a later call
5919 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005920 *
Adam Powell785c4472012-05-02 21:25:39 -07005921 * <p>A view with transient state cannot be trivially rebound from an external
5922 * data source, such as an adapter binding item views in a list. This may be
5923 * because the view is performing an animation, tracking user selection
5924 * of content, or similar.</p>
5925 *
Adam Powell539ee872012-02-03 19:00:49 -08005926 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005927 */
5928 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005929 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5930 mTransientStateCount - 1;
5931 if (mTransientStateCount < 0) {
5932 mTransientStateCount = 0;
5933 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5934 "unmatched pair of setHasTransientState calls");
5935 }
5936 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005937 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005938 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005939 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5940 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005941 if (mParent != null) {
5942 try {
5943 mParent.childHasTransientStateChanged(this, hasTransientState);
5944 } catch (AbstractMethodError e) {
5945 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5946 " does not fully implement ViewParent", e);
5947 }
Adam Powell539ee872012-02-03 19:00:49 -08005948 }
5949 }
5950 }
5951
5952 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 * If this view doesn't do any drawing on its own, set this flag to
5954 * allow further optimizations. By default, this flag is not set on
5955 * View, but could be set on some View subclasses such as ViewGroup.
5956 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005957 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5958 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 *
5960 * @param willNotDraw whether or not this View draw on its own
5961 */
5962 public void setWillNotDraw(boolean willNotDraw) {
5963 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5964 }
5965
5966 /**
5967 * Returns whether or not this View draws on its own.
5968 *
5969 * @return true if this view has nothing to draw, false otherwise
5970 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005971 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 public boolean willNotDraw() {
5973 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5974 }
5975
5976 /**
5977 * When a View's drawing cache is enabled, drawing is redirected to an
5978 * offscreen bitmap. Some views, like an ImageView, must be able to
5979 * bypass this mechanism if they already draw a single bitmap, to avoid
5980 * unnecessary usage of the memory.
5981 *
5982 * @param willNotCacheDrawing true if this view does not cache its
5983 * drawing, false otherwise
5984 */
5985 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5986 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5987 }
5988
5989 /**
5990 * Returns whether or not this View can cache its drawing or not.
5991 *
5992 * @return true if this view does not cache its drawing, false otherwise
5993 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005994 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 public boolean willNotCacheDrawing() {
5996 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5997 }
5998
5999 /**
6000 * Indicates whether this view reacts to click events or not.
6001 *
6002 * @return true if the view is clickable, false otherwise
6003 *
6004 * @see #setClickable(boolean)
6005 * @attr ref android.R.styleable#View_clickable
6006 */
6007 @ViewDebug.ExportedProperty
6008 public boolean isClickable() {
6009 return (mViewFlags & CLICKABLE) == CLICKABLE;
6010 }
6011
6012 /**
6013 * Enables or disables click events for this view. When a view
6014 * is clickable it will change its state to "pressed" on every click.
6015 * Subclasses should set the view clickable to visually react to
6016 * user's clicks.
6017 *
6018 * @param clickable true to make the view clickable, false otherwise
6019 *
6020 * @see #isClickable()
6021 * @attr ref android.R.styleable#View_clickable
6022 */
6023 public void setClickable(boolean clickable) {
6024 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6025 }
6026
6027 /**
6028 * Indicates whether this view reacts to long click events or not.
6029 *
6030 * @return true if the view is long clickable, false otherwise
6031 *
6032 * @see #setLongClickable(boolean)
6033 * @attr ref android.R.styleable#View_longClickable
6034 */
6035 public boolean isLongClickable() {
6036 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6037 }
6038
6039 /**
6040 * Enables or disables long click events for this view. When a view is long
6041 * clickable it reacts to the user holding down the button for a longer
6042 * duration than a tap. This event can either launch the listener or a
6043 * context menu.
6044 *
6045 * @param longClickable true to make the view long clickable, false otherwise
6046 * @see #isLongClickable()
6047 * @attr ref android.R.styleable#View_longClickable
6048 */
6049 public void setLongClickable(boolean longClickable) {
6050 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6051 }
6052
6053 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006054 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 *
6056 * @see #isClickable()
6057 * @see #setClickable(boolean)
6058 *
6059 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6060 * the View's internal state from a previously set "pressed" state.
6061 */
6062 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006063 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006065 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006066 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006068 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006070
6071 if (needsRefresh) {
6072 refreshDrawableState();
6073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 dispatchSetPressed(pressed);
6075 }
6076
6077 /**
6078 * Dispatch setPressed to all of this View's children.
6079 *
6080 * @see #setPressed(boolean)
6081 *
6082 * @param pressed The new pressed state
6083 */
6084 protected void dispatchSetPressed(boolean pressed) {
6085 }
6086
6087 /**
6088 * Indicates whether the view is currently in pressed state. Unless
6089 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6090 * the pressed state.
6091 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006092 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 * @see #isClickable()
6094 * @see #setClickable(boolean)
6095 *
6096 * @return true if the view is currently pressed, false otherwise
6097 */
6098 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006099 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 }
6101
6102 /**
6103 * Indicates whether this view will save its state (that is,
6104 * whether its {@link #onSaveInstanceState} method will be called).
6105 *
6106 * @return Returns true if the view state saving is enabled, else false.
6107 *
6108 * @see #setSaveEnabled(boolean)
6109 * @attr ref android.R.styleable#View_saveEnabled
6110 */
6111 public boolean isSaveEnabled() {
6112 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6113 }
6114
6115 /**
6116 * Controls whether the saving of this view's state is
6117 * enabled (that is, whether its {@link #onSaveInstanceState} method
6118 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006119 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 * for its state to be saved. This flag can only disable the
6121 * saving of this view; any child views may still have their state saved.
6122 *
6123 * @param enabled Set to false to <em>disable</em> state saving, or true
6124 * (the default) to allow it.
6125 *
6126 * @see #isSaveEnabled()
6127 * @see #setId(int)
6128 * @see #onSaveInstanceState()
6129 * @attr ref android.R.styleable#View_saveEnabled
6130 */
6131 public void setSaveEnabled(boolean enabled) {
6132 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6133 }
6134
Jeff Brown85a31762010-09-01 17:01:00 -07006135 /**
6136 * Gets whether the framework should discard touches when the view's
6137 * window is obscured by another visible window.
6138 * Refer to the {@link View} security documentation for more details.
6139 *
6140 * @return True if touch filtering is enabled.
6141 *
6142 * @see #setFilterTouchesWhenObscured(boolean)
6143 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6144 */
6145 @ViewDebug.ExportedProperty
6146 public boolean getFilterTouchesWhenObscured() {
6147 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6148 }
6149
6150 /**
6151 * Sets whether the framework should discard touches when the view's
6152 * window is obscured by another visible window.
6153 * Refer to the {@link View} security documentation for more details.
6154 *
6155 * @param enabled True if touch filtering should be enabled.
6156 *
6157 * @see #getFilterTouchesWhenObscured
6158 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6159 */
6160 public void setFilterTouchesWhenObscured(boolean enabled) {
6161 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6162 FILTER_TOUCHES_WHEN_OBSCURED);
6163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164
6165 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006166 * Indicates whether the entire hierarchy under this view will save its
6167 * state when a state saving traversal occurs from its parent. The default
6168 * is true; if false, these views will not be saved unless
6169 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6170 *
6171 * @return Returns true if the view state saving from parent is enabled, else false.
6172 *
6173 * @see #setSaveFromParentEnabled(boolean)
6174 */
6175 public boolean isSaveFromParentEnabled() {
6176 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6177 }
6178
6179 /**
6180 * Controls whether the entire hierarchy under this view will save its
6181 * state when a state saving traversal occurs from its parent. The default
6182 * is true; if false, these views will not be saved unless
6183 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6184 *
6185 * @param enabled Set to false to <em>disable</em> state saving, or true
6186 * (the default) to allow it.
6187 *
6188 * @see #isSaveFromParentEnabled()
6189 * @see #setId(int)
6190 * @see #onSaveInstanceState()
6191 */
6192 public void setSaveFromParentEnabled(boolean enabled) {
6193 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6194 }
6195
6196
6197 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006198 * Returns whether this View is able to take focus.
6199 *
6200 * @return True if this view can take focus, or false otherwise.
6201 * @attr ref android.R.styleable#View_focusable
6202 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006203 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204 public final boolean isFocusable() {
6205 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6206 }
6207
6208 /**
6209 * When a view is focusable, it may not want to take focus when in touch mode.
6210 * For example, a button would like focus when the user is navigating via a D-pad
6211 * so that the user can click on it, but once the user starts touching the screen,
6212 * the button shouldn't take focus
6213 * @return Whether the view is focusable in touch mode.
6214 * @attr ref android.R.styleable#View_focusableInTouchMode
6215 */
6216 @ViewDebug.ExportedProperty
6217 public final boolean isFocusableInTouchMode() {
6218 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6219 }
6220
6221 /**
6222 * Find the nearest view in the specified direction that can take focus.
6223 * This does not actually give focus to that view.
6224 *
6225 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6226 *
6227 * @return The nearest focusable in the specified direction, or null if none
6228 * can be found.
6229 */
6230 public View focusSearch(int direction) {
6231 if (mParent != null) {
6232 return mParent.focusSearch(this, direction);
6233 } else {
6234 return null;
6235 }
6236 }
6237
6238 /**
6239 * This method is the last chance for the focused view and its ancestors to
6240 * respond to an arrow key. This is called when the focused view did not
6241 * consume the key internally, nor could the view system find a new view in
6242 * the requested direction to give focus to.
6243 *
6244 * @param focused The currently focused view.
6245 * @param direction The direction focus wants to move. One of FOCUS_UP,
6246 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6247 * @return True if the this view consumed this unhandled move.
6248 */
6249 public boolean dispatchUnhandledMove(View focused, int direction) {
6250 return false;
6251 }
6252
6253 /**
6254 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006255 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006257 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6258 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 * @return The user specified next view, or null if there is none.
6260 */
6261 View findUserSetNextFocus(View root, int direction) {
6262 switch (direction) {
6263 case FOCUS_LEFT:
6264 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006265 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006266 case FOCUS_RIGHT:
6267 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006268 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 case FOCUS_UP:
6270 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006271 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 case FOCUS_DOWN:
6273 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006274 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006275 case FOCUS_FORWARD:
6276 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006277 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006278 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006279 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006280 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006281 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006282 @Override
6283 public boolean apply(View t) {
6284 return t.mNextFocusForwardId == id;
6285 }
6286 });
6287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 }
6289 return null;
6290 }
6291
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006292 private View findViewInsideOutShouldExist(View root, int id) {
6293 if (mMatchIdPredicate == null) {
6294 mMatchIdPredicate = new MatchIdPredicate();
6295 }
6296 mMatchIdPredicate.mId = id;
6297 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006299 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 }
6301 return result;
6302 }
6303
6304 /**
6305 * Find and return all focusable views that are descendants of this view,
6306 * possibly including this view if it is focusable itself.
6307 *
6308 * @param direction The direction of the focus
6309 * @return A list of focusable views
6310 */
6311 public ArrayList<View> getFocusables(int direction) {
6312 ArrayList<View> result = new ArrayList<View>(24);
6313 addFocusables(result, direction);
6314 return result;
6315 }
6316
6317 /**
6318 * Add any focusable views that are descendants of this view (possibly
6319 * including this view if it is focusable itself) to views. If we are in touch mode,
6320 * only add views that are also focusable in touch mode.
6321 *
6322 * @param views Focusable views found so far
6323 * @param direction The direction of the focus
6324 */
6325 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006326 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328
svetoslavganov75986cf2009-05-14 22:28:01 -07006329 /**
6330 * Adds any focusable views that are descendants of this view (possibly
6331 * including this view if it is focusable itself) to views. This method
6332 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006333 * only views focusable in touch mode if we are in touch mode or
6334 * only views that can take accessibility focus if accessibility is enabeld
6335 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006336 *
6337 * @param views Focusable views found so far or null if all we are interested is
6338 * the number of focusables.
6339 * @param direction The direction of the focus.
6340 * @param focusableMode The type of focusables to be added.
6341 *
6342 * @see #FOCUSABLES_ALL
6343 * @see #FOCUSABLES_TOUCH_MODE
6344 */
6345 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006346 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006347 return;
6348 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006349 if (!isFocusable()) {
6350 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006351 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006352 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6353 && isInTouchMode() && !isFocusableInTouchMode()) {
6354 return;
6355 }
6356 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 }
6358
6359 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006360 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006361 * The search is performed by either the text that the View renders or the content
6362 * description that describes the view for accessibility purposes and the view does
6363 * not render or both. Clients can specify how the search is to be performed via
6364 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6365 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006366 *
6367 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006368 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006369 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006370 * @see #FIND_VIEWS_WITH_TEXT
6371 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6372 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006373 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006374 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006375 if (getAccessibilityNodeProvider() != null) {
6376 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6377 outViews.add(this);
6378 }
6379 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006380 && (searched != null && searched.length() > 0)
6381 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006382 String searchedLowerCase = searched.toString().toLowerCase();
6383 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6384 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6385 outViews.add(this);
6386 }
6387 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006388 }
6389
6390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 * Find and return all touchable views that are descendants of this view,
6392 * possibly including this view if it is touchable itself.
6393 *
6394 * @return A list of touchable views
6395 */
6396 public ArrayList<View> getTouchables() {
6397 ArrayList<View> result = new ArrayList<View>();
6398 addTouchables(result);
6399 return result;
6400 }
6401
6402 /**
6403 * Add any touchable views that are descendants of this view (possibly
6404 * including this view if it is touchable itself) to views.
6405 *
6406 * @param views Touchable views found so far
6407 */
6408 public void addTouchables(ArrayList<View> views) {
6409 final int viewFlags = mViewFlags;
6410
6411 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6412 && (viewFlags & ENABLED_MASK) == ENABLED) {
6413 views.add(this);
6414 }
6415 }
6416
6417 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006418 * Returns whether this View is accessibility focused.
6419 *
6420 * @return True if this View is accessibility focused.
6421 */
6422 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006423 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006424 }
6425
6426 /**
6427 * Call this to try to give accessibility focus to this view.
6428 *
6429 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6430 * returns false or the view is no visible or the view already has accessibility
6431 * focus.
6432 *
6433 * See also {@link #focusSearch(int)}, which is what you call to say that you
6434 * have focus, and you want your parent to look for the next one.
6435 *
6436 * @return Whether this view actually took accessibility focus.
6437 *
6438 * @hide
6439 */
6440 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006441 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6442 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006443 return false;
6444 }
6445 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6446 return false;
6447 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006448 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6449 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006450 ViewRootImpl viewRootImpl = getViewRootImpl();
6451 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006452 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006453 }
Svetoslav Ganov1cf70bb2012-08-06 10:53:34 -07006454 if (mAttachInfo != null) {
6455 Rect rectangle = mAttachInfo.mTmpInvalRect;
6456 getDrawingRect(rectangle);
6457 requestRectangleOnScreen(rectangle);
6458 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006459 invalidate();
6460 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6461 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006462 return true;
6463 }
6464 return false;
6465 }
6466
6467 /**
6468 * Call this to try to clear accessibility focus of this view.
6469 *
6470 * See also {@link #focusSearch(int)}, which is what you call to say that you
6471 * have focus, and you want your parent to look for the next one.
6472 *
6473 * @hide
6474 */
6475 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006476 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6477 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006478 invalidate();
6479 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6480 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006481 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006482 // Clear the global reference of accessibility focus if this
6483 // view or any of its descendants had accessibility focus.
6484 ViewRootImpl viewRootImpl = getViewRootImpl();
6485 if (viewRootImpl != null) {
6486 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6487 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006488 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006489 }
6490 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006491 }
6492
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006493 private void sendAccessibilityHoverEvent(int eventType) {
6494 // Since we are not delivering to a client accessibility events from not
6495 // important views (unless the clinet request that) we need to fire the
6496 // event from the deepest view exposed to the client. As a consequence if
6497 // the user crosses a not exposed view the client will see enter and exit
6498 // of the exposed predecessor followed by and enter and exit of that same
6499 // predecessor when entering and exiting the not exposed descendant. This
6500 // is fine since the client has a clear idea which view is hovered at the
6501 // price of a couple more events being sent. This is a simple and
6502 // working solution.
6503 View source = this;
6504 while (true) {
6505 if (source.includeForAccessibility()) {
6506 source.sendAccessibilityEvent(eventType);
6507 return;
6508 }
6509 ViewParent parent = source.getParent();
6510 if (parent instanceof View) {
6511 source = (View) parent;
6512 } else {
6513 return;
6514 }
6515 }
6516 }
6517
Svetoslav Ganov42138042012-03-20 11:51:39 -07006518 /**
6519 * Clears accessibility focus without calling any callback methods
6520 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6521 * is used for clearing accessibility focus when giving this focus to
6522 * another view.
6523 */
6524 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006525 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6526 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006527 invalidate();
6528 }
6529 }
6530
6531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 * Call this to try to give focus to a specific view or to one of its
6533 * descendants.
6534 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006535 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6536 * false), or if it is focusable and it is not focusable in touch mode
6537 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006539 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 * have focus, and you want your parent to look for the next one.
6541 *
6542 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6543 * {@link #FOCUS_DOWN} and <code>null</code>.
6544 *
6545 * @return Whether this view or one of its descendants actually took focus.
6546 */
6547 public final boolean requestFocus() {
6548 return requestFocus(View.FOCUS_DOWN);
6549 }
6550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 /**
6552 * Call this to try to give focus to a specific view or to one of its
6553 * descendants and give it a hint about what direction focus is heading.
6554 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006555 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6556 * false), or if it is focusable and it is not focusable in touch mode
6557 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006559 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 * have focus, and you want your parent to look for the next one.
6561 *
6562 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6563 * <code>null</code> set for the previously focused rectangle.
6564 *
6565 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6566 * @return Whether this view or one of its descendants actually took focus.
6567 */
6568 public final boolean requestFocus(int direction) {
6569 return requestFocus(direction, null);
6570 }
6571
6572 /**
6573 * Call this to try to give focus to a specific view or to one of its descendants
6574 * and give it hints about the direction and a specific rectangle that the focus
6575 * is coming from. The rectangle can help give larger views a finer grained hint
6576 * about where focus is coming from, and therefore, where to show selection, or
6577 * forward focus change internally.
6578 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006579 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6580 * false), or if it is focusable and it is not focusable in touch mode
6581 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 *
6583 * A View will not take focus if it is not visible.
6584 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006585 * A View will not take focus if one of its parents has
6586 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6587 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006589 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006590 * have focus, and you want your parent to look for the next one.
6591 *
6592 * You may wish to override this method if your custom {@link View} has an internal
6593 * {@link View} that it wishes to forward the request to.
6594 *
6595 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6596 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6597 * to give a finer grained hint about where focus is coming from. May be null
6598 * if there is no hint.
6599 * @return Whether this view or one of its descendants actually took focus.
6600 */
6601 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006602 return requestFocusNoSearch(direction, previouslyFocusedRect);
6603 }
6604
6605 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 // need to be focusable
6607 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6608 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6609 return false;
6610 }
6611
6612 // need to be focusable in touch mode if in touch mode
6613 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006614 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6615 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 }
6617
6618 // need to not have any parents blocking us
6619 if (hasAncestorThatBlocksDescendantFocus()) {
6620 return false;
6621 }
6622
6623 handleFocusGainInternal(direction, previouslyFocusedRect);
6624 return true;
6625 }
6626
6627 /**
6628 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6629 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6630 * touch mode to request focus when they are touched.
6631 *
6632 * @return Whether this view or one of its descendants actually took focus.
6633 *
6634 * @see #isInTouchMode()
6635 *
6636 */
6637 public final boolean requestFocusFromTouch() {
6638 // Leave touch mode if we need to
6639 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006640 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006641 if (viewRoot != null) {
6642 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 }
6644 }
6645 return requestFocus(View.FOCUS_DOWN);
6646 }
6647
6648 /**
6649 * @return Whether any ancestor of this view blocks descendant focus.
6650 */
6651 private boolean hasAncestorThatBlocksDescendantFocus() {
6652 ViewParent ancestor = mParent;
6653 while (ancestor instanceof ViewGroup) {
6654 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6655 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6656 return true;
6657 } else {
6658 ancestor = vgAncestor.getParent();
6659 }
6660 }
6661 return false;
6662 }
6663
6664 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006665 * Gets the mode for determining whether this View is important for accessibility
6666 * which is if it fires accessibility events and if it is reported to
6667 * accessibility services that query the screen.
6668 *
6669 * @return The mode for determining whether a View is important for accessibility.
6670 *
6671 * @attr ref android.R.styleable#View_importantForAccessibility
6672 *
6673 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6674 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6675 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6676 */
6677 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006678 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6679 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6680 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006681 })
6682 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006683 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6684 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006685 }
6686
6687 /**
6688 * Sets how to determine whether this view is important for accessibility
6689 * which is if it fires accessibility events and if it is reported to
6690 * accessibility services that query the screen.
6691 *
6692 * @param mode How to determine whether this view is important for accessibility.
6693 *
6694 * @attr ref android.R.styleable#View_importantForAccessibility
6695 *
6696 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6697 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6698 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6699 */
6700 public void setImportantForAccessibility(int mode) {
6701 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006702 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6703 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6704 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006705 notifyAccessibilityStateChanged();
6706 }
6707 }
6708
6709 /**
6710 * Gets whether this view should be exposed for accessibility.
6711 *
6712 * @return Whether the view is exposed for accessibility.
6713 *
6714 * @hide
6715 */
6716 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006717 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6718 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006719 switch (mode) {
6720 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6721 return true;
6722 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6723 return false;
6724 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006725 return isActionableForAccessibility() || hasListenersForAccessibility()
6726 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006727 default:
6728 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6729 + mode);
6730 }
6731 }
6732
6733 /**
6734 * Gets the parent for accessibility purposes. Note that the parent for
6735 * accessibility is not necessary the immediate parent. It is the first
6736 * predecessor that is important for accessibility.
6737 *
6738 * @return The parent for accessibility purposes.
6739 */
6740 public ViewParent getParentForAccessibility() {
6741 if (mParent instanceof View) {
6742 View parentView = (View) mParent;
6743 if (parentView.includeForAccessibility()) {
6744 return mParent;
6745 } else {
6746 return mParent.getParentForAccessibility();
6747 }
6748 }
6749 return null;
6750 }
6751
6752 /**
6753 * Adds the children of a given View for accessibility. Since some Views are
6754 * not important for accessibility the children for accessibility are not
6755 * necessarily direct children of the riew, rather they are the first level of
6756 * descendants important for accessibility.
6757 *
6758 * @param children The list of children for accessibility.
6759 */
6760 public void addChildrenForAccessibility(ArrayList<View> children) {
6761 if (includeForAccessibility()) {
6762 children.add(this);
6763 }
6764 }
6765
6766 /**
6767 * Whether to regard this view for accessibility. A view is regarded for
6768 * accessibility if it is important for accessibility or the querying
6769 * accessibility service has explicitly requested that view not
6770 * important for accessibility are regarded.
6771 *
6772 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006773 *
6774 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006775 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006776 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006777 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006778 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006779 }
6780 return false;
6781 }
6782
6783 /**
6784 * Returns whether the View is considered actionable from
6785 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006786 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006787 *
6788 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006789 *
6790 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006791 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006792 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006793 return (isClickable() || isLongClickable() || isFocusable());
6794 }
6795
6796 /**
6797 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006798 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006799 *
6800 * @return True if the view is actionable for accessibility.
6801 */
6802 private boolean hasListenersForAccessibility() {
6803 ListenerInfo info = getListenerInfo();
6804 return mTouchDelegate != null || info.mOnKeyListener != null
6805 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6806 || info.mOnHoverListener != null || info.mOnDragListener != null;
6807 }
6808
6809 /**
6810 * Notifies accessibility services that some view's important for
6811 * accessibility state has changed. Note that such notifications
6812 * are made at most once every
6813 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6814 * to avoid unnecessary load to the system. Also once a view has
6815 * made a notifucation this method is a NOP until the notification has
6816 * been sent to clients.
6817 *
6818 * @hide
6819 *
6820 * TODO: Makse sure this method is called for any view state change
6821 * that is interesting for accessilility purposes.
6822 */
6823 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006824 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6825 return;
6826 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006827 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6828 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006829 if (mParent != null) {
6830 mParent.childAccessibilityStateChanged(this);
6831 }
6832 }
6833 }
6834
6835 /**
6836 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006837 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006838 *
6839 * @hide
6840 */
6841 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006842 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006843 }
6844
6845 /**
6846 * Performs the specified accessibility action on the view. For
6847 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
alanv8eeefef2012-05-07 16:57:53 -07006848 * <p>
6849 * If an {@link AccessibilityDelegate} has been specified via calling
6850 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6851 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6852 * is responsible for handling this call.
6853 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006854 *
6855 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006856 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006857 * @return Whether the action was performed.
6858 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006859 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006860 if (mAccessibilityDelegate != null) {
6861 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6862 } else {
6863 return performAccessibilityActionInternal(action, arguments);
6864 }
6865 }
6866
6867 /**
6868 * @see #performAccessibilityAction(int, Bundle)
6869 *
6870 * Note: Called from the default {@link AccessibilityDelegate}.
6871 */
6872 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006873 switch (action) {
6874 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006875 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006876 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006877 }
6878 } break;
6879 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6880 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006881 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006882 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006883 } break;
6884 case AccessibilityNodeInfo.ACTION_FOCUS: {
6885 if (!hasFocus()) {
6886 // Get out of touch mode since accessibility
6887 // wants to move focus around.
6888 getViewRootImpl().ensureTouchMode(false);
6889 return requestFocus();
6890 }
6891 } break;
6892 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6893 if (hasFocus()) {
6894 clearFocus();
6895 return !isFocused();
6896 }
6897 } break;
6898 case AccessibilityNodeInfo.ACTION_SELECT: {
6899 if (!isSelected()) {
6900 setSelected(true);
6901 return isSelected();
6902 }
6903 } break;
6904 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6905 if (isSelected()) {
6906 setSelected(false);
6907 return !isSelected();
6908 }
6909 } break;
6910 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006911 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006912 return requestAccessibilityFocus();
6913 }
6914 } break;
6915 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6916 if (isAccessibilityFocused()) {
6917 clearAccessibilityFocus();
6918 return true;
6919 }
6920 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006921 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6922 if (arguments != null) {
6923 final int granularity = arguments.getInt(
6924 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6925 return nextAtGranularity(granularity);
6926 }
6927 } break;
6928 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6929 if (arguments != null) {
6930 final int granularity = arguments.getInt(
6931 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6932 return previousAtGranularity(granularity);
6933 }
6934 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006935 }
6936 return false;
6937 }
6938
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006939 private boolean nextAtGranularity(int granularity) {
6940 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006941 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006942 return false;
6943 }
6944 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6945 if (iterator == null) {
6946 return false;
6947 }
6948 final int current = getAccessibilityCursorPosition();
6949 final int[] range = iterator.following(current);
6950 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006951 return false;
6952 }
6953 final int start = range[0];
6954 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006955 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006956 sendViewTextTraversedAtGranularityEvent(
6957 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6958 granularity, start, end);
6959 return true;
6960 }
6961
6962 private boolean previousAtGranularity(int granularity) {
6963 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006964 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006965 return false;
6966 }
6967 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6968 if (iterator == null) {
6969 return false;
6970 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006971 int current = getAccessibilityCursorPosition();
6972 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6973 current = text.length();
6974 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6975 // When traversing by character we always put the cursor after the character
6976 // to ease edit and have to compensate before asking the for previous segment.
6977 current--;
6978 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006979 final int[] range = iterator.preceding(current);
6980 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006981 return false;
6982 }
6983 final int start = range[0];
6984 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006985 // Always put the cursor after the character to ease edit.
6986 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6987 setAccessibilityCursorPosition(end);
6988 } else {
6989 setAccessibilityCursorPosition(start);
6990 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006991 sendViewTextTraversedAtGranularityEvent(
6992 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
6993 granularity, start, end);
6994 return true;
6995 }
6996
6997 /**
6998 * Gets the text reported for accessibility purposes.
6999 *
7000 * @return The accessibility text.
7001 *
7002 * @hide
7003 */
7004 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007005 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007006 }
7007
7008 /**
7009 * @hide
7010 */
7011 public int getAccessibilityCursorPosition() {
7012 return mAccessibilityCursorPosition;
7013 }
7014
7015 /**
7016 * @hide
7017 */
7018 public void setAccessibilityCursorPosition(int position) {
7019 mAccessibilityCursorPosition = position;
7020 }
7021
7022 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7023 int fromIndex, int toIndex) {
7024 if (mParent == null) {
7025 return;
7026 }
7027 AccessibilityEvent event = AccessibilityEvent.obtain(
7028 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7029 onInitializeAccessibilityEvent(event);
7030 onPopulateAccessibilityEvent(event);
7031 event.setFromIndex(fromIndex);
7032 event.setToIndex(toIndex);
7033 event.setAction(action);
7034 event.setMovementGranularity(granularity);
7035 mParent.requestSendAccessibilityEvent(this, event);
7036 }
7037
7038 /**
7039 * @hide
7040 */
7041 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7042 switch (granularity) {
7043 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7044 CharSequence text = getIterableTextForAccessibility();
7045 if (text != null && text.length() > 0) {
7046 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007047 CharacterTextSegmentIterator.getInstance(
7048 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007049 iterator.initialize(text.toString());
7050 return iterator;
7051 }
7052 } break;
7053 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7054 CharSequence text = getIterableTextForAccessibility();
7055 if (text != null && text.length() > 0) {
7056 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007057 WordTextSegmentIterator.getInstance(
7058 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007059 iterator.initialize(text.toString());
7060 return iterator;
7061 }
7062 } break;
7063 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7064 CharSequence text = getIterableTextForAccessibility();
7065 if (text != null && text.length() > 0) {
7066 ParagraphTextSegmentIterator iterator =
7067 ParagraphTextSegmentIterator.getInstance();
7068 iterator.initialize(text.toString());
7069 return iterator;
7070 }
7071 } break;
7072 }
7073 return null;
7074 }
7075
Svetoslav Ganov42138042012-03-20 11:51:39 -07007076 /**
Romain Guya440b002010-02-24 15:57:54 -08007077 * @hide
7078 */
7079 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007080 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007081 clearDisplayList();
7082
Romain Guya440b002010-02-24 15:57:54 -08007083 onStartTemporaryDetach();
7084 }
7085
7086 /**
7087 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7089 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007090 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 */
7092 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007093 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007094 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007095 }
7096
7097 /**
7098 * @hide
7099 */
7100 public void dispatchFinishTemporaryDetach() {
7101 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 }
Romain Guy8506ab42009-06-11 17:35:47 -07007103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 /**
7105 * Called after {@link #onStartTemporaryDetach} when the container is done
7106 * changing the view.
7107 */
7108 public void onFinishTemporaryDetach() {
7109 }
Romain Guy8506ab42009-06-11 17:35:47 -07007110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007112 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7113 * for this view's window. Returns null if the view is not currently attached
7114 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007115 * just use the standard high-level event callbacks like
7116 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007117 */
7118 public KeyEvent.DispatcherState getKeyDispatcherState() {
7119 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7120 }
Joe Malin32736f02011-01-19 16:14:20 -08007121
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 * Dispatch a key event before it is processed by any input method
7124 * associated with the view hierarchy. This can be used to intercept
7125 * key events in special situations before the IME consumes them; a
7126 * typical example would be handling the BACK key to update the application's
7127 * UI instead of allowing the IME to see it and close itself.
7128 *
7129 * @param event The key event to be dispatched.
7130 * @return True if the event was handled, false otherwise.
7131 */
7132 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7133 return onKeyPreIme(event.getKeyCode(), event);
7134 }
7135
7136 /**
7137 * Dispatch a key event to the next view on the focus path. This path runs
7138 * from the top of the view tree down to the currently focused view. If this
7139 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7140 * the next node down the focus path. This method also fires any key
7141 * listeners.
7142 *
7143 * @param event The key event to be dispatched.
7144 * @return True if the event was handled, false otherwise.
7145 */
7146 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007147 if (mInputEventConsistencyVerifier != null) {
7148 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007150
Jeff Brown21bc5c92011-02-28 18:27:14 -08007151 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007152 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007153 ListenerInfo li = mListenerInfo;
7154 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7155 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 return true;
7157 }
7158
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007159 if (event.dispatch(this, mAttachInfo != null
7160 ? mAttachInfo.mKeyDispatchState : null, this)) {
7161 return true;
7162 }
7163
7164 if (mInputEventConsistencyVerifier != null) {
7165 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7166 }
7167 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 }
7169
7170 /**
7171 * Dispatches a key shortcut event.
7172 *
7173 * @param event The key event to be dispatched.
7174 * @return True if the event was handled by the view, false otherwise.
7175 */
7176 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7177 return onKeyShortcut(event.getKeyCode(), event);
7178 }
7179
7180 /**
7181 * Pass the touch screen motion event down to the target view, or this
7182 * view if it is the target.
7183 *
7184 * @param event The motion event to be dispatched.
7185 * @return True if the event was handled by the view, false otherwise.
7186 */
7187 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007188 if (mInputEventConsistencyVerifier != null) {
7189 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7190 }
7191
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007192 if (onFilterTouchEventForSecurity(event)) {
7193 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007194 ListenerInfo li = mListenerInfo;
7195 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7196 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007197 return true;
7198 }
7199
7200 if (onTouchEvent(event)) {
7201 return true;
7202 }
Jeff Brown85a31762010-09-01 17:01:00 -07007203 }
7204
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007205 if (mInputEventConsistencyVerifier != null) {
7206 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007208 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 }
7210
7211 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007212 * Filter the touch event to apply security policies.
7213 *
7214 * @param event The motion event to be filtered.
7215 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007216 *
Jeff Brown85a31762010-09-01 17:01:00 -07007217 * @see #getFilterTouchesWhenObscured
7218 */
7219 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007220 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007221 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7222 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7223 // Window is obscured, drop this touch.
7224 return false;
7225 }
7226 return true;
7227 }
7228
7229 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 * Pass a trackball motion event down to the focused view.
7231 *
7232 * @param event The motion event to be dispatched.
7233 * @return True if the event was handled by the view, false otherwise.
7234 */
7235 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007236 if (mInputEventConsistencyVerifier != null) {
7237 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7238 }
7239
Romain Guy02ccac62011-06-24 13:20:23 -07007240 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 }
7242
7243 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007244 * Dispatch a generic motion event.
7245 * <p>
7246 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7247 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007248 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007249 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007250 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007251 *
7252 * @param event The motion event to be dispatched.
7253 * @return True if the event was handled by the view, false otherwise.
7254 */
7255 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007256 if (mInputEventConsistencyVerifier != null) {
7257 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7258 }
7259
Jeff Browna032cc02011-03-07 16:56:21 -08007260 final int source = event.getSource();
7261 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7262 final int action = event.getAction();
7263 if (action == MotionEvent.ACTION_HOVER_ENTER
7264 || action == MotionEvent.ACTION_HOVER_MOVE
7265 || action == MotionEvent.ACTION_HOVER_EXIT) {
7266 if (dispatchHoverEvent(event)) {
7267 return true;
7268 }
7269 } else if (dispatchGenericPointerEvent(event)) {
7270 return true;
7271 }
7272 } else if (dispatchGenericFocusedEvent(event)) {
7273 return true;
7274 }
7275
Jeff Brown10b62902011-06-20 16:40:37 -07007276 if (dispatchGenericMotionEventInternal(event)) {
7277 return true;
7278 }
7279
7280 if (mInputEventConsistencyVerifier != null) {
7281 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7282 }
7283 return false;
7284 }
7285
7286 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007287 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007288 ListenerInfo li = mListenerInfo;
7289 if (li != null && li.mOnGenericMotionListener != null
7290 && (mViewFlags & ENABLED_MASK) == ENABLED
7291 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007292 return true;
7293 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007294
7295 if (onGenericMotionEvent(event)) {
7296 return true;
7297 }
7298
7299 if (mInputEventConsistencyVerifier != null) {
7300 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7301 }
7302 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007303 }
7304
7305 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007306 * Dispatch a hover event.
7307 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007308 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007309 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007310 * </p>
7311 *
7312 * @param event The motion event to be dispatched.
7313 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007314 */
7315 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007316 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007317 ListenerInfo li = mListenerInfo;
7318 if (li != null && li.mOnHoverListener != null
7319 && (mViewFlags & ENABLED_MASK) == ENABLED
7320 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007321 return true;
7322 }
7323
Jeff Browna032cc02011-03-07 16:56:21 -08007324 return onHoverEvent(event);
7325 }
7326
7327 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007328 * Returns true if the view has a child to which it has recently sent
7329 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7330 * it does not have a hovered child, then it must be the innermost hovered view.
7331 * @hide
7332 */
7333 protected boolean hasHoveredChild() {
7334 return false;
7335 }
7336
7337 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007338 * Dispatch a generic motion event to the view under the first pointer.
7339 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007340 * Do not call this method directly.
7341 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007342 * </p>
7343 *
7344 * @param event The motion event to be dispatched.
7345 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007346 */
7347 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7348 return false;
7349 }
7350
7351 /**
7352 * Dispatch a generic motion event to the currently focused view.
7353 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007354 * Do not call this method directly.
7355 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007356 * </p>
7357 *
7358 * @param event The motion event to be dispatched.
7359 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007360 */
7361 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7362 return false;
7363 }
7364
7365 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007366 * Dispatch a pointer event.
7367 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007368 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7369 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7370 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007371 * and should not be expected to handle other pointing device features.
7372 * </p>
7373 *
7374 * @param event The motion event to be dispatched.
7375 * @return True if the event was handled by the view, false otherwise.
7376 * @hide
7377 */
7378 public final boolean dispatchPointerEvent(MotionEvent event) {
7379 if (event.isTouchEvent()) {
7380 return dispatchTouchEvent(event);
7381 } else {
7382 return dispatchGenericMotionEvent(event);
7383 }
7384 }
7385
7386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 * Called when the window containing this view gains or loses window focus.
7388 * ViewGroups should override to route to their children.
7389 *
7390 * @param hasFocus True if the window containing this view now has focus,
7391 * false otherwise.
7392 */
7393 public void dispatchWindowFocusChanged(boolean hasFocus) {
7394 onWindowFocusChanged(hasFocus);
7395 }
7396
7397 /**
7398 * Called when the window containing this view gains or loses focus. Note
7399 * that this is separate from view focus: to receive key events, both
7400 * your view and its window must have focus. If a window is displayed
7401 * on top of yours that takes input focus, then your own window will lose
7402 * focus but the view focus will remain unchanged.
7403 *
7404 * @param hasWindowFocus True if the window containing this view now has
7405 * focus, false otherwise.
7406 */
7407 public void onWindowFocusChanged(boolean hasWindowFocus) {
7408 InputMethodManager imm = InputMethodManager.peekInstance();
7409 if (!hasWindowFocus) {
7410 if (isPressed()) {
7411 setPressed(false);
7412 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007413 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 imm.focusOut(this);
7415 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007416 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007417 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007418 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007419 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007420 imm.focusIn(this);
7421 }
7422 refreshDrawableState();
7423 }
7424
7425 /**
7426 * Returns true if this view is in a window that currently has window focus.
7427 * Note that this is not the same as the view itself having focus.
7428 *
7429 * @return True if this view is in a window that currently has window focus.
7430 */
7431 public boolean hasWindowFocus() {
7432 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7433 }
7434
7435 /**
Adam Powell326d8082009-12-09 15:10:07 -08007436 * Dispatch a view visibility change down the view hierarchy.
7437 * ViewGroups should override to route to their children.
7438 * @param changedView The view whose visibility changed. Could be 'this' or
7439 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007440 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7441 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007442 */
7443 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7444 onVisibilityChanged(changedView, visibility);
7445 }
7446
7447 /**
7448 * Called when the visibility of the view or an ancestor of the view is changed.
7449 * @param changedView The view whose visibility changed. Could be 'this' or
7450 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007451 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7452 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007453 */
7454 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007455 if (visibility == VISIBLE) {
7456 if (mAttachInfo != null) {
7457 initialAwakenScrollBars();
7458 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007459 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007460 }
7461 }
Adam Powell326d8082009-12-09 15:10:07 -08007462 }
7463
7464 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007465 * Dispatch a hint about whether this view is displayed. For instance, when
7466 * a View moves out of the screen, it might receives a display hint indicating
7467 * the view is not displayed. Applications should not <em>rely</em> on this hint
7468 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007469 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007470 * @param hint A hint about whether or not this view is displayed:
7471 * {@link #VISIBLE} or {@link #INVISIBLE}.
7472 */
7473 public void dispatchDisplayHint(int hint) {
7474 onDisplayHint(hint);
7475 }
7476
7477 /**
7478 * Gives this view a hint about whether is displayed or not. For instance, when
7479 * a View moves out of the screen, it might receives a display hint indicating
7480 * the view is not displayed. Applications should not <em>rely</em> on this hint
7481 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007482 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007483 * @param hint A hint about whether or not this view is displayed:
7484 * {@link #VISIBLE} or {@link #INVISIBLE}.
7485 */
7486 protected void onDisplayHint(int hint) {
7487 }
7488
7489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 * Dispatch a window visibility change down the view hierarchy.
7491 * ViewGroups should override to route to their children.
7492 *
7493 * @param visibility The new visibility of the window.
7494 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007495 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 */
7497 public void dispatchWindowVisibilityChanged(int visibility) {
7498 onWindowVisibilityChanged(visibility);
7499 }
7500
7501 /**
7502 * Called when the window containing has change its visibility
7503 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7504 * that this tells you whether or not your window is being made visible
7505 * to the window manager; this does <em>not</em> tell you whether or not
7506 * your window is obscured by other windows on the screen, even if it
7507 * is itself visible.
7508 *
7509 * @param visibility The new visibility of the window.
7510 */
7511 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007512 if (visibility == VISIBLE) {
7513 initialAwakenScrollBars();
7514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 }
7516
7517 /**
7518 * Returns the current visibility of the window this view is attached to
7519 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7520 *
7521 * @return Returns the current visibility of the view's window.
7522 */
7523 public int getWindowVisibility() {
7524 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7525 }
7526
7527 /**
7528 * Retrieve the overall visible display size in which the window this view is
7529 * attached to has been positioned in. This takes into account screen
7530 * decorations above the window, for both cases where the window itself
7531 * is being position inside of them or the window is being placed under
7532 * then and covered insets are used for the window to position its content
7533 * inside. In effect, this tells you the available area where content can
7534 * be placed and remain visible to users.
7535 *
7536 * <p>This function requires an IPC back to the window manager to retrieve
7537 * the requested information, so should not be used in performance critical
7538 * code like drawing.
7539 *
7540 * @param outRect Filled in with the visible display frame. If the view
7541 * is not attached to a window, this is simply the raw display size.
7542 */
7543 public void getWindowVisibleDisplayFrame(Rect outRect) {
7544 if (mAttachInfo != null) {
7545 try {
7546 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7547 } catch (RemoteException e) {
7548 return;
7549 }
7550 // XXX This is really broken, and probably all needs to be done
7551 // in the window manager, and we need to know more about whether
7552 // we want the area behind or in front of the IME.
7553 final Rect insets = mAttachInfo.mVisibleInsets;
7554 outRect.left += insets.left;
7555 outRect.top += insets.top;
7556 outRect.right -= insets.right;
7557 outRect.bottom -= insets.bottom;
7558 return;
7559 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007560 // The view is not attached to a display so we don't have a context.
7561 // Make a best guess about the display size.
7562 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007563 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007564 }
7565
7566 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007567 * Dispatch a notification about a resource configuration change down
7568 * the view hierarchy.
7569 * ViewGroups should override to route to their children.
7570 *
7571 * @param newConfig The new resource configuration.
7572 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007573 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007574 */
7575 public void dispatchConfigurationChanged(Configuration newConfig) {
7576 onConfigurationChanged(newConfig);
7577 }
7578
7579 /**
7580 * Called when the current configuration of the resources being used
7581 * by the application have changed. You can use this to decide when
7582 * to reload resources that can changed based on orientation and other
7583 * configuration characterstics. You only need to use this if you are
7584 * not relying on the normal {@link android.app.Activity} mechanism of
7585 * recreating the activity instance upon a configuration change.
7586 *
7587 * @param newConfig The new resource configuration.
7588 */
7589 protected void onConfigurationChanged(Configuration newConfig) {
7590 }
7591
7592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007593 * Private function to aggregate all per-view attributes in to the view
7594 * root.
7595 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007596 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7597 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598 }
7599
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007600 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7601 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007602 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007603 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007604 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007605 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007606 ListenerInfo li = mListenerInfo;
7607 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007608 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 }
7611 }
7612
7613 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007614 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007615 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007616 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7617 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 ai.mRecomputeGlobalAttributes = true;
7619 }
7620 }
7621 }
7622
7623 /**
7624 * Returns whether the device is currently in touch mode. Touch mode is entered
7625 * once the user begins interacting with the device by touch, and affects various
7626 * things like whether focus is always visible to the user.
7627 *
7628 * @return Whether the device is in touch mode.
7629 */
7630 @ViewDebug.ExportedProperty
7631 public boolean isInTouchMode() {
7632 if (mAttachInfo != null) {
7633 return mAttachInfo.mInTouchMode;
7634 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007635 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 }
7637 }
7638
7639 /**
7640 * Returns the context the view is running in, through which it can
7641 * access the current theme, resources, etc.
7642 *
7643 * @return The view's Context.
7644 */
7645 @ViewDebug.CapturedViewProperty
7646 public final Context getContext() {
7647 return mContext;
7648 }
7649
7650 /**
7651 * Handle a key event before it is processed by any input method
7652 * associated with the view hierarchy. This can be used to intercept
7653 * key events in special situations before the IME consumes them; a
7654 * typical example would be handling the BACK key to update the application's
7655 * UI instead of allowing the IME to see it and close itself.
7656 *
7657 * @param keyCode The value in event.getKeyCode().
7658 * @param event Description of the key event.
7659 * @return If you handled the event, return true. If you want to allow the
7660 * event to be handled by the next receiver, return false.
7661 */
7662 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7663 return false;
7664 }
7665
7666 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007667 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7668 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7670 * is released, if the view is enabled and clickable.
7671 *
Jean Chalard405bc512012-05-29 19:12:34 +09007672 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7673 * although some may elect to do so in some situations. Do not rely on this to
7674 * catch software key presses.
7675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 * @param keyCode A key code that represents the button pressed, from
7677 * {@link android.view.KeyEvent}.
7678 * @param event The KeyEvent object that defines the button action.
7679 */
7680 public boolean onKeyDown(int keyCode, KeyEvent event) {
7681 boolean result = false;
7682
7683 switch (keyCode) {
7684 case KeyEvent.KEYCODE_DPAD_CENTER:
7685 case KeyEvent.KEYCODE_ENTER: {
7686 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7687 return true;
7688 }
7689 // Long clickable items don't necessarily have to be clickable
7690 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7691 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7692 (event.getRepeatCount() == 0)) {
7693 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007694 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 return true;
7696 }
7697 break;
7698 }
7699 }
7700 return result;
7701 }
7702
7703 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007704 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7705 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7706 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007707 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7708 * although some may elect to do so in some situations. Do not rely on this to
7709 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007710 */
7711 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7712 return false;
7713 }
7714
7715 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007716 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7717 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7719 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007720 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7721 * although some may elect to do so in some situations. Do not rely on this to
7722 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 *
7724 * @param keyCode A key code that represents the button pressed, from
7725 * {@link android.view.KeyEvent}.
7726 * @param event The KeyEvent object that defines the button action.
7727 */
7728 public boolean onKeyUp(int keyCode, KeyEvent event) {
7729 boolean result = false;
7730
7731 switch (keyCode) {
7732 case KeyEvent.KEYCODE_DPAD_CENTER:
7733 case KeyEvent.KEYCODE_ENTER: {
7734 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7735 return true;
7736 }
7737 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7738 setPressed(false);
7739
7740 if (!mHasPerformedLongPress) {
7741 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007742 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743
7744 result = performClick();
7745 }
7746 }
7747 break;
7748 }
7749 }
7750 return result;
7751 }
7752
7753 /**
7754 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7755 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7756 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007757 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7758 * although some may elect to do so in some situations. Do not rely on this to
7759 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007760 *
7761 * @param keyCode A key code that represents the button pressed, from
7762 * {@link android.view.KeyEvent}.
7763 * @param repeatCount The number of times the action was made.
7764 * @param event The KeyEvent object that defines the button action.
7765 */
7766 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7767 return false;
7768 }
7769
7770 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007771 * Called on the focused view when a key shortcut event is not handled.
7772 * Override this method to implement local key shortcuts for the View.
7773 * Key shortcuts can also be implemented by setting the
7774 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 *
7776 * @param keyCode The value in event.getKeyCode().
7777 * @param event Description of the key event.
7778 * @return If you handled the event, return true. If you want to allow the
7779 * event to be handled by the next receiver, return false.
7780 */
7781 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7782 return false;
7783 }
7784
7785 /**
7786 * Check whether the called view is a text editor, in which case it
7787 * would make sense to automatically display a soft input window for
7788 * it. Subclasses should override this if they implement
7789 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007790 * a call on that method would return a non-null InputConnection, and
7791 * they are really a first-class editor that the user would normally
7792 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007793 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007794 * <p>The default implementation always returns false. This does
7795 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7796 * will not be called or the user can not otherwise perform edits on your
7797 * view; it is just a hint to the system that this is not the primary
7798 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 * @return Returns true if this view is a text editor, else false.
7801 */
7802 public boolean onCheckIsTextEditor() {
7803 return false;
7804 }
Romain Guy8506ab42009-06-11 17:35:47 -07007805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 /**
7807 * Create a new InputConnection for an InputMethod to interact
7808 * with the view. The default implementation returns null, since it doesn't
7809 * support input methods. You can override this to implement such support.
7810 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007811 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 * <p>When implementing this, you probably also want to implement
7813 * {@link #onCheckIsTextEditor()} to indicate you will return a
7814 * non-null InputConnection.
7815 *
7816 * @param outAttrs Fill in with attribute information about the connection.
7817 */
7818 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7819 return null;
7820 }
7821
7822 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007823 * Called by the {@link android.view.inputmethod.InputMethodManager}
7824 * when a view who is not the current
7825 * input connection target is trying to make a call on the manager. The
7826 * default implementation returns false; you can override this to return
7827 * true for certain views if you are performing InputConnection proxying
7828 * to them.
7829 * @param view The View that is making the InputMethodManager call.
7830 * @return Return true to allow the call, false to reject.
7831 */
7832 public boolean checkInputConnectionProxy(View view) {
7833 return false;
7834 }
Romain Guy8506ab42009-06-11 17:35:47 -07007835
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 * Show the context menu for this view. It is not safe to hold on to the
7838 * menu after returning from this method.
7839 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007840 * You should normally not overload this method. Overload
7841 * {@link #onCreateContextMenu(ContextMenu)} or define an
7842 * {@link OnCreateContextMenuListener} to add items to the context menu.
7843 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007844 * @param menu The context menu to populate
7845 */
7846 public void createContextMenu(ContextMenu menu) {
7847 ContextMenuInfo menuInfo = getContextMenuInfo();
7848
7849 // Sets the current menu info so all items added to menu will have
7850 // my extra info set.
7851 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7852
7853 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007854 ListenerInfo li = mListenerInfo;
7855 if (li != null && li.mOnCreateContextMenuListener != null) {
7856 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 }
7858
7859 // Clear the extra information so subsequent items that aren't mine don't
7860 // have my extra info.
7861 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7862
7863 if (mParent != null) {
7864 mParent.createContextMenu(menu);
7865 }
7866 }
7867
7868 /**
7869 * Views should implement this if they have extra information to associate
7870 * with the context menu. The return result is supplied as a parameter to
7871 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7872 * callback.
7873 *
7874 * @return Extra information about the item for which the context menu
7875 * should be shown. This information will vary across different
7876 * subclasses of View.
7877 */
7878 protected ContextMenuInfo getContextMenuInfo() {
7879 return null;
7880 }
7881
7882 /**
7883 * Views should implement this if the view itself is going to add items to
7884 * the context menu.
7885 *
7886 * @param menu the context menu to populate
7887 */
7888 protected void onCreateContextMenu(ContextMenu menu) {
7889 }
7890
7891 /**
7892 * Implement this method to handle trackball motion events. The
7893 * <em>relative</em> movement of the trackball since the last event
7894 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7895 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7896 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7897 * they will often be fractional values, representing the more fine-grained
7898 * movement information available from a trackball).
7899 *
7900 * @param event The motion event.
7901 * @return True if the event was handled, false otherwise.
7902 */
7903 public boolean onTrackballEvent(MotionEvent event) {
7904 return false;
7905 }
7906
7907 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007908 * Implement this method to handle generic motion events.
7909 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007910 * Generic motion events describe joystick movements, mouse hovers, track pad
7911 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007912 * {@link MotionEvent#getSource() source} of the motion event specifies
7913 * the class of input that was received. Implementations of this method
7914 * must examine the bits in the source before processing the event.
7915 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007916 * </p><p>
7917 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7918 * are delivered to the view under the pointer. All other generic motion events are
7919 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007920 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007921 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007922 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007923 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7924 * // process the joystick movement...
7925 * return true;
7926 * }
7927 * }
7928 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7929 * switch (event.getAction()) {
7930 * case MotionEvent.ACTION_HOVER_MOVE:
7931 * // process the mouse hover movement...
7932 * return true;
7933 * case MotionEvent.ACTION_SCROLL:
7934 * // process the scroll wheel movement...
7935 * return true;
7936 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007937 * }
7938 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007939 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007940 *
7941 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007942 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007943 */
7944 public boolean onGenericMotionEvent(MotionEvent event) {
7945 return false;
7946 }
7947
7948 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007949 * Implement this method to handle hover events.
7950 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007951 * This method is called whenever a pointer is hovering into, over, or out of the
7952 * bounds of a view and the view is not currently being touched.
7953 * Hover events are represented as pointer events with action
7954 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7955 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7956 * </p>
7957 * <ul>
7958 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7959 * when the pointer enters the bounds of the view.</li>
7960 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7961 * when the pointer has already entered the bounds of the view and has moved.</li>
7962 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7963 * when the pointer has exited the bounds of the view or when the pointer is
7964 * about to go down due to a button click, tap, or similar user action that
7965 * causes the view to be touched.</li>
7966 * </ul>
7967 * <p>
7968 * The view should implement this method to return true to indicate that it is
7969 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007970 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007971 * The default implementation calls {@link #setHovered} to update the hovered state
7972 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007973 * is enabled and is clickable. The default implementation also sends hover
7974 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007975 * </p>
7976 *
7977 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007978 * @return True if the view handled the hover event.
7979 *
7980 * @see #isHovered
7981 * @see #setHovered
7982 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007983 */
7984 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007985 // The root view may receive hover (or touch) events that are outside the bounds of
7986 // the window. This code ensures that we only send accessibility events for
7987 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007988 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007989 if (!mSendingHoverAccessibilityEvents) {
7990 if ((action == MotionEvent.ACTION_HOVER_ENTER
7991 || action == MotionEvent.ACTION_HOVER_MOVE)
7992 && !hasHoveredChild()
7993 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07007994 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007995 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007996 }
7997 } else {
7998 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007999 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008000 && !pointInView(event.getX(), event.getY()))) {
8001 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008002 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008003 // If the window does not have input focus we take away accessibility
8004 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008005 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008006 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008007 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008008 }
Jeff Browna1b24182011-07-28 13:38:24 -07008009 }
8010
Jeff Brown87b7f802011-06-21 18:35:45 -07008011 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008012 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008013 case MotionEvent.ACTION_HOVER_ENTER:
8014 setHovered(true);
8015 break;
8016 case MotionEvent.ACTION_HOVER_EXIT:
8017 setHovered(false);
8018 break;
8019 }
Jeff Browna1b24182011-07-28 13:38:24 -07008020
8021 // Dispatch the event to onGenericMotionEvent before returning true.
8022 // This is to provide compatibility with existing applications that
8023 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8024 // break because of the new default handling for hoverable views
8025 // in onHoverEvent.
8026 // Note that onGenericMotionEvent will be called by default when
8027 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8028 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008029 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008030 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008031
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008032 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008033 }
8034
8035 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008036 * Returns true if the view should handle {@link #onHoverEvent}
8037 * by calling {@link #setHovered} to change its hovered state.
8038 *
8039 * @return True if the view is hoverable.
8040 */
8041 private boolean isHoverable() {
8042 final int viewFlags = mViewFlags;
8043 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8044 return false;
8045 }
8046
8047 return (viewFlags & CLICKABLE) == CLICKABLE
8048 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8049 }
8050
8051 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008052 * Returns true if the view is currently hovered.
8053 *
8054 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008055 *
8056 * @see #setHovered
8057 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008058 */
Jeff Brown10b62902011-06-20 16:40:37 -07008059 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008060 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008061 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008062 }
8063
8064 /**
8065 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008066 * <p>
8067 * Calling this method also changes the drawable state of the view. This
8068 * enables the view to react to hover by using different drawable resources
8069 * to change its appearance.
8070 * </p><p>
8071 * The {@link #onHoverChanged} method is called when the hovered state changes.
8072 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008073 *
8074 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008075 *
8076 * @see #isHovered
8077 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008078 */
8079 public void setHovered(boolean hovered) {
8080 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008081 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8082 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008083 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008084 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008085 }
8086 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008087 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8088 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008089 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008090 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008091 }
8092 }
8093 }
8094
8095 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008096 * Implement this method to handle hover state changes.
8097 * <p>
8098 * This method is called whenever the hover state changes as a result of a
8099 * call to {@link #setHovered}.
8100 * </p>
8101 *
8102 * @param hovered The current hover state, as returned by {@link #isHovered}.
8103 *
8104 * @see #isHovered
8105 * @see #setHovered
8106 */
8107 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008108 }
8109
8110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 * Implement this method to handle touch screen motion events.
8112 *
8113 * @param event The motion event.
8114 * @return True if the event was handled, false otherwise.
8115 */
8116 public boolean onTouchEvent(MotionEvent event) {
8117 final int viewFlags = mViewFlags;
8118
8119 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008120 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008121 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 // A disabled view that is clickable still consumes the touch
8124 // events, it just doesn't respond to them.
8125 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8126 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8127 }
8128
8129 if (mTouchDelegate != null) {
8130 if (mTouchDelegate.onTouchEvent(event)) {
8131 return true;
8132 }
8133 }
8134
8135 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8136 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8137 switch (event.getAction()) {
8138 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008139 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8140 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 // take focus if we don't have it already and we should in
8142 // touch mode.
8143 boolean focusTaken = false;
8144 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8145 focusTaken = requestFocus();
8146 }
8147
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008148 if (prepressed) {
8149 // The button is being released before we actually
8150 // showed it as pressed. Make it show the pressed
8151 // state now (before scheduling the click) to ensure
8152 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008153 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008154 }
Joe Malin32736f02011-01-19 16:14:20 -08008155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 if (!mHasPerformedLongPress) {
8157 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008158 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159
8160 // Only perform take click actions if we were in the pressed state
8161 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008162 // Use a Runnable and post this rather than calling
8163 // performClick directly. This lets other visual state
8164 // of the view update before click actions start.
8165 if (mPerformClick == null) {
8166 mPerformClick = new PerformClick();
8167 }
8168 if (!post(mPerformClick)) {
8169 performClick();
8170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 }
8172 }
8173
8174 if (mUnsetPressedState == null) {
8175 mUnsetPressedState = new UnsetPressedState();
8176 }
8177
Adam Powelle14579b2009-12-16 18:39:52 -08008178 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008179 postDelayed(mUnsetPressedState,
8180 ViewConfiguration.getPressedStateDuration());
8181 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182 // If the post failed, unpress right now
8183 mUnsetPressedState.run();
8184 }
Adam Powelle14579b2009-12-16 18:39:52 -08008185 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 }
8187 break;
8188
8189 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008190 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008191
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008192 if (performButtonActionOnTouchDown(event)) {
8193 break;
8194 }
8195
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008196 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008197 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008198
8199 // For views inside a scrolling container, delay the pressed feedback for
8200 // a short period in case this is a scroll.
8201 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008202 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008203 if (mPendingCheckForTap == null) {
8204 mPendingCheckForTap = new CheckForTap();
8205 }
8206 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8207 } else {
8208 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008209 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008210 checkForLongClick(0);
8211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 break;
8213
8214 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008215 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008216 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 break;
8218
8219 case MotionEvent.ACTION_MOVE:
8220 final int x = (int) event.getX();
8221 final int y = (int) event.getY();
8222
8223 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008224 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008225 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008226 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008227 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008228 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008229 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230
Adam Powell4d6f0662012-02-21 15:11:11 -08008231 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 }
8234 break;
8235 }
8236 return true;
8237 }
8238
8239 return false;
8240 }
8241
8242 /**
Adam Powell10298662011-08-14 18:26:30 -07008243 * @hide
8244 */
8245 public boolean isInScrollingContainer() {
8246 ViewParent p = getParent();
8247 while (p != null && p instanceof ViewGroup) {
8248 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8249 return true;
8250 }
8251 p = p.getParent();
8252 }
8253 return false;
8254 }
8255
8256 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008257 * Remove the longpress detection timer.
8258 */
8259 private void removeLongPressCallback() {
8260 if (mPendingCheckForLongPress != null) {
8261 removeCallbacks(mPendingCheckForLongPress);
8262 }
8263 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008264
8265 /**
8266 * Remove the pending click action
8267 */
8268 private void removePerformClickCallback() {
8269 if (mPerformClick != null) {
8270 removeCallbacks(mPerformClick);
8271 }
8272 }
8273
Adam Powelle14579b2009-12-16 18:39:52 -08008274 /**
Romain Guya440b002010-02-24 15:57:54 -08008275 * Remove the prepress detection timer.
8276 */
8277 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008278 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008279 setPressed(false);
8280 removeCallbacks(mUnsetPressedState);
8281 }
8282 }
8283
8284 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008285 * Remove the tap detection timer.
8286 */
8287 private void removeTapCallback() {
8288 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008289 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008290 removeCallbacks(mPendingCheckForTap);
8291 }
8292 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008293
8294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295 * Cancels a pending long press. Your subclass can use this if you
8296 * want the context menu to come up if the user presses and holds
8297 * at the same place, but you don't want it to come up if they press
8298 * and then move around enough to cause scrolling.
8299 */
8300 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008301 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008302
8303 /*
8304 * The prepressed state handled by the tap callback is a display
8305 * construct, but the tap callback will post a long press callback
8306 * less its own timeout. Remove it here.
8307 */
8308 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 }
8310
8311 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008312 * Remove the pending callback for sending a
8313 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8314 */
8315 private void removeSendViewScrolledAccessibilityEventCallback() {
8316 if (mSendViewScrolledAccessibilityEvent != null) {
8317 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008318 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008319 }
8320 }
8321
8322 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008323 * Sets the TouchDelegate for this View.
8324 */
8325 public void setTouchDelegate(TouchDelegate delegate) {
8326 mTouchDelegate = delegate;
8327 }
8328
8329 /**
8330 * Gets the TouchDelegate for this View.
8331 */
8332 public TouchDelegate getTouchDelegate() {
8333 return mTouchDelegate;
8334 }
8335
8336 /**
8337 * Set flags controlling behavior of this view.
8338 *
8339 * @param flags Constant indicating the value which should be set
8340 * @param mask Constant indicating the bit range that should be changed
8341 */
8342 void setFlags(int flags, int mask) {
8343 int old = mViewFlags;
8344 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8345
8346 int changed = mViewFlags ^ old;
8347 if (changed == 0) {
8348 return;
8349 }
8350 int privateFlags = mPrivateFlags;
8351
8352 /* Check if the FOCUSABLE bit has changed */
8353 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008354 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008356 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 /* Give up focus if we are no longer focusable */
8358 clearFocus();
8359 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008360 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 /*
8362 * Tell the view system that we are now available to take focus
8363 * if no one else already has it.
8364 */
8365 if (mParent != null) mParent.focusableViewAvailable(this);
8366 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008367 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8368 notifyAccessibilityStateChanged();
8369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008370 }
8371
8372 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8373 if ((changed & VISIBILITY_MASK) != 0) {
8374 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008375 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008376 * it was not visible. Marking it drawn ensures that the invalidation will
8377 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008379 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008380 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381
8382 needGlobalAttributesUpdate(true);
8383
8384 // a view becoming visible is worth notifying the parent
8385 // about in case nothing has focus. even if this specific view
8386 // isn't focusable, it may contain something that is, so let
8387 // the root view try to give this focus if nothing else does.
8388 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8389 mParent.focusableViewAvailable(this);
8390 }
8391 }
8392 }
8393
8394 /* Check if the GONE bit has changed */
8395 if ((changed & GONE) != 0) {
8396 needGlobalAttributesUpdate(false);
8397 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398
Romain Guyecd80ee2009-12-03 17:13:02 -08008399 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8400 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008401 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008402 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008403 if (mParent instanceof View) {
8404 // GONE views noop invalidation, so invalidate the parent
8405 ((View) mParent).invalidate(true);
8406 }
8407 // Mark the view drawn to ensure that it gets invalidated properly the next
8408 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008409 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 }
8411 if (mAttachInfo != null) {
8412 mAttachInfo.mViewVisibilityChanged = true;
8413 }
8414 }
8415
8416 /* Check if the VISIBLE bit has changed */
8417 if ((changed & INVISIBLE) != 0) {
8418 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008419 /*
8420 * If this view is becoming invisible, set the DRAWN flag so that
8421 * the next invalidate() will not be skipped.
8422 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008423 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424
8425 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008426 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427 if (getRootView() != this) {
8428 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008429 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008430 }
8431 }
8432 if (mAttachInfo != null) {
8433 mAttachInfo.mViewVisibilityChanged = true;
8434 }
8435 }
8436
Adam Powell326d8082009-12-09 15:10:07 -08008437 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008438 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008439 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8440 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008441 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008442 } else if (mParent != null) {
8443 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008444 }
Adam Powell326d8082009-12-09 15:10:07 -08008445 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8446 }
8447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8449 destroyDrawingCache();
8450 }
8451
8452 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8453 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008454 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008455 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 }
8457
8458 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8459 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008460 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 }
8462
8463 if ((changed & DRAW_MASK) != 0) {
8464 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008465 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008466 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8467 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008469 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 }
8471 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008472 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 }
8474 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008475 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 }
8477
8478 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008479 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 mParent.recomputeViewAttributes(this);
8481 }
8482 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008483
8484 if (AccessibilityManager.getInstance(mContext).isEnabled()
8485 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8486 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8487 notifyAccessibilityStateChanged();
8488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 }
8490
8491 /**
8492 * Change the view's z order in the tree, so it's on top of other sibling
8493 * views
8494 */
8495 public void bringToFront() {
8496 if (mParent != null) {
8497 mParent.bringChildToFront(this);
8498 }
8499 }
8500
8501 /**
8502 * This is called in response to an internal scroll in this view (i.e., the
8503 * view scrolled its own contents). This is typically as a result of
8504 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8505 * called.
8506 *
8507 * @param l Current horizontal scroll origin.
8508 * @param t Current vertical scroll origin.
8509 * @param oldl Previous horizontal scroll origin.
8510 * @param oldt Previous vertical scroll origin.
8511 */
8512 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008513 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8514 postSendViewScrolledAccessibilityEventCallback();
8515 }
8516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008517 mBackgroundSizeChanged = true;
8518
8519 final AttachInfo ai = mAttachInfo;
8520 if (ai != null) {
8521 ai.mViewScrollChanged = true;
8522 }
8523 }
8524
8525 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008526 * Interface definition for a callback to be invoked when the layout bounds of a view
8527 * changes due to layout processing.
8528 */
8529 public interface OnLayoutChangeListener {
8530 /**
8531 * Called when the focus state of a view has changed.
8532 *
8533 * @param v The view whose state has changed.
8534 * @param left The new value of the view's left property.
8535 * @param top The new value of the view's top property.
8536 * @param right The new value of the view's right property.
8537 * @param bottom The new value of the view's bottom property.
8538 * @param oldLeft The previous value of the view's left property.
8539 * @param oldTop The previous value of the view's top property.
8540 * @param oldRight The previous value of the view's right property.
8541 * @param oldBottom The previous value of the view's bottom property.
8542 */
8543 void onLayoutChange(View v, int left, int top, int right, int bottom,
8544 int oldLeft, int oldTop, int oldRight, int oldBottom);
8545 }
8546
8547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 * This is called during layout when the size of this view has changed. If
8549 * you were just added to the view hierarchy, you're called with the old
8550 * values of 0.
8551 *
8552 * @param w Current width of this view.
8553 * @param h Current height of this view.
8554 * @param oldw Old width of this view.
8555 * @param oldh Old height of this view.
8556 */
8557 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8558 }
8559
8560 /**
8561 * Called by draw to draw the child views. This may be overridden
8562 * by derived classes to gain control just before its children are drawn
8563 * (but after its own view has been drawn).
8564 * @param canvas the canvas on which to draw the view
8565 */
8566 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 }
8569
8570 /**
8571 * Gets the parent of this view. Note that the parent is a
8572 * ViewParent and not necessarily a View.
8573 *
8574 * @return Parent of this view.
8575 */
8576 public final ViewParent getParent() {
8577 return mParent;
8578 }
8579
8580 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008581 * Set the horizontal scrolled position of your view. This will cause a call to
8582 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8583 * invalidated.
8584 * @param value the x position to scroll to
8585 */
8586 public void setScrollX(int value) {
8587 scrollTo(value, mScrollY);
8588 }
8589
8590 /**
8591 * Set the vertical scrolled position of your view. This will cause a call to
8592 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8593 * invalidated.
8594 * @param value the y position to scroll to
8595 */
8596 public void setScrollY(int value) {
8597 scrollTo(mScrollX, value);
8598 }
8599
8600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 * Return the scrolled left position of this view. This is the left edge of
8602 * the displayed part of your view. You do not need to draw any pixels
8603 * farther left, since those are outside of the frame of your view on
8604 * screen.
8605 *
8606 * @return The left edge of the displayed part of your view, in pixels.
8607 */
8608 public final int getScrollX() {
8609 return mScrollX;
8610 }
8611
8612 /**
8613 * Return the scrolled top position of this view. This is the top edge of
8614 * the displayed part of your view. You do not need to draw any pixels above
8615 * it, since those are outside of the frame of your view on screen.
8616 *
8617 * @return The top edge of the displayed part of your view, in pixels.
8618 */
8619 public final int getScrollY() {
8620 return mScrollY;
8621 }
8622
8623 /**
8624 * Return the width of the your view.
8625 *
8626 * @return The width of your view, in pixels.
8627 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008628 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008629 public final int getWidth() {
8630 return mRight - mLeft;
8631 }
8632
8633 /**
8634 * Return the height of your view.
8635 *
8636 * @return The height of your view, in pixels.
8637 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008638 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 public final int getHeight() {
8640 return mBottom - mTop;
8641 }
8642
8643 /**
8644 * Return the visible drawing bounds of your view. Fills in the output
8645 * rectangle with the values from getScrollX(), getScrollY(),
8646 * getWidth(), and getHeight().
8647 *
8648 * @param outRect The (scrolled) drawing bounds of the view.
8649 */
8650 public void getDrawingRect(Rect outRect) {
8651 outRect.left = mScrollX;
8652 outRect.top = mScrollY;
8653 outRect.right = mScrollX + (mRight - mLeft);
8654 outRect.bottom = mScrollY + (mBottom - mTop);
8655 }
8656
8657 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008658 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8659 * raw width component (that is the result is masked by
8660 * {@link #MEASURED_SIZE_MASK}).
8661 *
8662 * @return The raw measured width of this view.
8663 */
8664 public final int getMeasuredWidth() {
8665 return mMeasuredWidth & MEASURED_SIZE_MASK;
8666 }
8667
8668 /**
8669 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008670 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008671 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 * This should be used during measurement and layout calculations only. Use
8673 * {@link #getWidth()} to see how wide a view is after layout.
8674 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008675 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008677 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 return mMeasuredWidth;
8679 }
8680
8681 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008682 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8683 * raw width component (that is the result is masked by
8684 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008686 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687 */
8688 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008689 return mMeasuredHeight & MEASURED_SIZE_MASK;
8690 }
8691
8692 /**
8693 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008694 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008695 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8696 * This should be used during measurement and layout calculations only. Use
8697 * {@link #getHeight()} to see how wide a view is after layout.
8698 *
8699 * @return The measured width of this view as a bit mask.
8700 */
8701 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008702 return mMeasuredHeight;
8703 }
8704
8705 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008706 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8707 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8708 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8709 * and the height component is at the shifted bits
8710 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8711 */
8712 public final int getMeasuredState() {
8713 return (mMeasuredWidth&MEASURED_STATE_MASK)
8714 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8715 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8716 }
8717
8718 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008719 * The transform matrix of this view, which is calculated based on the current
8720 * roation, scale, and pivot properties.
8721 *
8722 * @see #getRotation()
8723 * @see #getScaleX()
8724 * @see #getScaleY()
8725 * @see #getPivotX()
8726 * @see #getPivotY()
8727 * @return The current transform matrix for the view
8728 */
8729 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008730 if (mTransformationInfo != null) {
8731 updateMatrix();
8732 return mTransformationInfo.mMatrix;
8733 }
8734 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008735 }
8736
8737 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008738 * Utility function to determine if the value is far enough away from zero to be
8739 * considered non-zero.
8740 * @param value A floating point value to check for zero-ness
8741 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8742 */
8743 private static boolean nonzero(float value) {
8744 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8745 }
8746
8747 /**
Jeff Brown86671742010-09-30 20:00:15 -07008748 * Returns true if the transform matrix is the identity matrix.
8749 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008750 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008751 * @return True if the transform matrix is the identity matrix, false otherwise.
8752 */
Jeff Brown86671742010-09-30 20:00:15 -07008753 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008754 if (mTransformationInfo != null) {
8755 updateMatrix();
8756 return mTransformationInfo.mMatrixIsIdentity;
8757 }
8758 return true;
8759 }
8760
8761 void ensureTransformationInfo() {
8762 if (mTransformationInfo == null) {
8763 mTransformationInfo = new TransformationInfo();
8764 }
Jeff Brown86671742010-09-30 20:00:15 -07008765 }
8766
8767 /**
8768 * Recomputes the transform matrix if necessary.
8769 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008770 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008771 final TransformationInfo info = mTransformationInfo;
8772 if (info == null) {
8773 return;
8774 }
8775 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008776 // transform-related properties have changed since the last time someone
8777 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008778
8779 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008780 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008781 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8782 info.mPrevWidth = mRight - mLeft;
8783 info.mPrevHeight = mBottom - mTop;
8784 info.mPivotX = info.mPrevWidth / 2f;
8785 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008786 }
8787 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008788 info.mMatrix.reset();
8789 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8790 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8791 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8792 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008793 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008794 if (info.mCamera == null) {
8795 info.mCamera = new Camera();
8796 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008797 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008798 info.mCamera.save();
8799 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8800 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8801 info.mCamera.getMatrix(info.matrix3D);
8802 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8803 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8804 info.mPivotY + info.mTranslationY);
8805 info.mMatrix.postConcat(info.matrix3D);
8806 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008807 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008808 info.mMatrixDirty = false;
8809 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8810 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008811 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008812 }
8813
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008814 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008815 * Utility method to retrieve the inverse of the current mMatrix property.
8816 * We cache the matrix to avoid recalculating it when transform properties
8817 * have not changed.
8818 *
8819 * @return The inverse of the current matrix of this view.
8820 */
Jeff Brown86671742010-09-30 20:00:15 -07008821 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008822 final TransformationInfo info = mTransformationInfo;
8823 if (info != null) {
8824 updateMatrix();
8825 if (info.mInverseMatrixDirty) {
8826 if (info.mInverseMatrix == null) {
8827 info.mInverseMatrix = new Matrix();
8828 }
8829 info.mMatrix.invert(info.mInverseMatrix);
8830 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008832 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008833 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008834 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008835 }
8836
8837 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008838 * Gets the distance along the Z axis from the camera to this view.
8839 *
8840 * @see #setCameraDistance(float)
8841 *
8842 * @return The distance along the Z axis.
8843 */
8844 public float getCameraDistance() {
8845 ensureTransformationInfo();
8846 final float dpi = mResources.getDisplayMetrics().densityDpi;
8847 final TransformationInfo info = mTransformationInfo;
8848 if (info.mCamera == null) {
8849 info.mCamera = new Camera();
8850 info.matrix3D = new Matrix();
8851 }
8852 return -(info.mCamera.getLocationZ() * dpi);
8853 }
8854
8855 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008856 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8857 * views are drawn) from the camera to this view. The camera's distance
8858 * affects 3D transformations, for instance rotations around the X and Y
8859 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008860 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008861 * use a camera distance that's greater than the height (X axis rotation) or
8862 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008863 *
Romain Guya5364ee2011-02-24 14:46:04 -08008864 * <p>The distance of the camera from the view plane can have an affect on the
8865 * perspective distortion of the view when it is rotated around the x or y axis.
8866 * For example, a large distance will result in a large viewing angle, and there
8867 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008868 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008869 * also result in some drawing artifacts if the rotated view ends up partially
8870 * behind the camera (which is why the recommendation is to use a distance at
8871 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008872 *
Romain Guya5364ee2011-02-24 14:46:04 -08008873 * <p>The distance is expressed in "depth pixels." The default distance depends
8874 * on the screen density. For instance, on a medium density display, the
8875 * default distance is 1280. On a high density display, the default distance
8876 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008877 *
Romain Guya5364ee2011-02-24 14:46:04 -08008878 * <p>If you want to specify a distance that leads to visually consistent
8879 * results across various densities, use the following formula:</p>
8880 * <pre>
8881 * float scale = context.getResources().getDisplayMetrics().density;
8882 * view.setCameraDistance(distance * scale);
8883 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008884 *
Romain Guya5364ee2011-02-24 14:46:04 -08008885 * <p>The density scale factor of a high density display is 1.5,
8886 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008887 *
Romain Guya5364ee2011-02-24 14:46:04 -08008888 * @param distance The distance in "depth pixels", if negative the opposite
8889 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008890 *
8891 * @see #setRotationX(float)
8892 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008893 */
8894 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008895 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008896
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008897 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008898 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008899 final TransformationInfo info = mTransformationInfo;
8900 if (info.mCamera == null) {
8901 info.mCamera = new Camera();
8902 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008903 }
8904
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008905 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8906 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008907
Chet Haase9d1992d2012-03-13 11:03:25 -07008908 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008909 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008910 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008911 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008912 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008913 // View was rejected last time it was drawn by its parent; this may have changed
8914 invalidateParentIfNeeded();
8915 }
Romain Guya5364ee2011-02-24 14:46:04 -08008916 }
8917
8918 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008919 * The degrees that the view is rotated around the pivot point.
8920 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008921 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008922 * @see #getPivotX()
8923 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008924 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008925 * @return The degrees of rotation.
8926 */
Chet Haasea5531132012-02-02 13:41:44 -08008927 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008928 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008929 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008930 }
8931
8932 /**
Chet Haase897247b2010-09-09 14:54:47 -07008933 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8934 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008935 *
8936 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008937 *
8938 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 * @see #getPivotX()
8940 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008941 * @see #setRotationX(float)
8942 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008943 *
8944 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008945 */
8946 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008947 ensureTransformationInfo();
8948 final TransformationInfo info = mTransformationInfo;
8949 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008951 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008952 info.mRotation = rotation;
8953 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008954 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008955 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008956 mDisplayList.setRotation(rotation);
8957 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008958 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008959 // View was rejected last time it was drawn by its parent; this may have changed
8960 invalidateParentIfNeeded();
8961 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008962 }
8963 }
8964
8965 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008966 * The degrees that the view is rotated around the vertical axis through the pivot point.
8967 *
8968 * @see #getPivotX()
8969 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008970 * @see #setRotationY(float)
8971 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008972 * @return The degrees of Y rotation.
8973 */
Chet Haasea5531132012-02-02 13:41:44 -08008974 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008975 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008976 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008977 }
8978
8979 /**
Chet Haase897247b2010-09-09 14:54:47 -07008980 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8981 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8982 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008983 *
Romain Guya5364ee2011-02-24 14:46:04 -08008984 * When rotating large views, it is recommended to adjust the camera distance
8985 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008986 *
8987 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008988 *
8989 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008990 * @see #getPivotX()
8991 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008992 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008993 * @see #setRotationX(float)
8994 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008995 *
8996 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008997 */
8998 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008999 ensureTransformationInfo();
9000 final TransformationInfo info = mTransformationInfo;
9001 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009002 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009003 info.mRotationY = rotationY;
9004 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009005 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009006 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009007 mDisplayList.setRotationY(rotationY);
9008 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009009 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009010 // View was rejected last time it was drawn by its parent; this may have changed
9011 invalidateParentIfNeeded();
9012 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009013 }
9014 }
9015
9016 /**
9017 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9018 *
9019 * @see #getPivotX()
9020 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009021 * @see #setRotationX(float)
9022 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009023 * @return The degrees of X rotation.
9024 */
Chet Haasea5531132012-02-02 13:41:44 -08009025 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009026 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009027 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009028 }
9029
9030 /**
Chet Haase897247b2010-09-09 14:54:47 -07009031 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9032 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9033 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009034 *
Romain Guya5364ee2011-02-24 14:46:04 -08009035 * When rotating large views, it is recommended to adjust the camera distance
9036 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009037 *
9038 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009039 *
9040 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009041 * @see #getPivotX()
9042 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009043 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009044 * @see #setRotationY(float)
9045 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009046 *
9047 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009048 */
9049 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009050 ensureTransformationInfo();
9051 final TransformationInfo info = mTransformationInfo;
9052 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009053 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009054 info.mRotationX = rotationX;
9055 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009056 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009057 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009058 mDisplayList.setRotationX(rotationX);
9059 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009060 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009061 // View was rejected last time it was drawn by its parent; this may have changed
9062 invalidateParentIfNeeded();
9063 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009064 }
9065 }
9066
9067 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009068 * The amount that the view is scaled in x around the pivot point, as a proportion of
9069 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9070 *
Joe Onorato93162322010-09-16 15:42:01 -04009071 * <p>By default, this is 1.0f.
9072 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009073 * @see #getPivotX()
9074 * @see #getPivotY()
9075 * @return The scaling factor.
9076 */
Chet Haasea5531132012-02-02 13:41:44 -08009077 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009078 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009079 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009080 }
9081
9082 /**
9083 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9084 * the view's unscaled width. A value of 1 means that no scaling is applied.
9085 *
9086 * @param scaleX The scaling factor.
9087 * @see #getPivotX()
9088 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009089 *
9090 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009091 */
9092 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009093 ensureTransformationInfo();
9094 final TransformationInfo info = mTransformationInfo;
9095 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009096 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009097 info.mScaleX = scaleX;
9098 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009099 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009100 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009101 mDisplayList.setScaleX(scaleX);
9102 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009103 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009104 // View was rejected last time it was drawn by its parent; this may have changed
9105 invalidateParentIfNeeded();
9106 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009107 }
9108 }
9109
9110 /**
9111 * The amount that the view is scaled in y around the pivot point, as a proportion of
9112 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9113 *
Joe Onorato93162322010-09-16 15:42:01 -04009114 * <p>By default, this is 1.0f.
9115 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009116 * @see #getPivotX()
9117 * @see #getPivotY()
9118 * @return The scaling factor.
9119 */
Chet Haasea5531132012-02-02 13:41:44 -08009120 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009121 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009122 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009123 }
9124
9125 /**
9126 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9127 * the view's unscaled width. A value of 1 means that no scaling is applied.
9128 *
9129 * @param scaleY The scaling factor.
9130 * @see #getPivotX()
9131 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009132 *
9133 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009134 */
9135 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009136 ensureTransformationInfo();
9137 final TransformationInfo info = mTransformationInfo;
9138 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009139 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009140 info.mScaleY = scaleY;
9141 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009142 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009143 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009144 mDisplayList.setScaleY(scaleY);
9145 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009146 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009147 // View was rejected last time it was drawn by its parent; this may have changed
9148 invalidateParentIfNeeded();
9149 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009150 }
9151 }
9152
9153 /**
9154 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9155 * and {@link #setScaleX(float) scaled}.
9156 *
9157 * @see #getRotation()
9158 * @see #getScaleX()
9159 * @see #getScaleY()
9160 * @see #getPivotY()
9161 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009162 *
9163 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009164 */
Chet Haasea5531132012-02-02 13:41:44 -08009165 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009166 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009167 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009168 }
9169
9170 /**
9171 * Sets the x location of the point around which the view is
9172 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009173 * By default, the pivot point is centered on the object.
9174 * Setting this property disables this behavior and causes the view to use only the
9175 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009176 *
9177 * @param pivotX The x location of the pivot point.
9178 * @see #getRotation()
9179 * @see #getScaleX()
9180 * @see #getScaleY()
9181 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009182 *
9183 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009184 */
9185 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009186 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009187 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009188 final TransformationInfo info = mTransformationInfo;
9189 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009190 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009191 info.mPivotX = pivotX;
9192 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009193 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009194 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009195 mDisplayList.setPivotX(pivotX);
9196 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009197 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009198 // View was rejected last time it was drawn by its parent; this may have changed
9199 invalidateParentIfNeeded();
9200 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009201 }
9202 }
9203
9204 /**
9205 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9206 * and {@link #setScaleY(float) scaled}.
9207 *
9208 * @see #getRotation()
9209 * @see #getScaleX()
9210 * @see #getScaleY()
9211 * @see #getPivotY()
9212 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009213 *
9214 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009215 */
Chet Haasea5531132012-02-02 13:41:44 -08009216 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009217 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009218 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009219 }
9220
9221 /**
9222 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009223 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9224 * Setting this property disables this behavior and causes the view to use only the
9225 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009226 *
9227 * @param pivotY The y location of the pivot point.
9228 * @see #getRotation()
9229 * @see #getScaleX()
9230 * @see #getScaleY()
9231 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009232 *
9233 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009234 */
9235 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009236 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009237 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009238 final TransformationInfo info = mTransformationInfo;
9239 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009240 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 info.mPivotY = pivotY;
9242 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009243 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009244 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009245 mDisplayList.setPivotY(pivotY);
9246 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009247 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009248 // View was rejected last time it was drawn by its parent; this may have changed
9249 invalidateParentIfNeeded();
9250 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009251 }
9252 }
9253
9254 /**
9255 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9256 * completely transparent and 1 means the view is completely opaque.
9257 *
Joe Onorato93162322010-09-16 15:42:01 -04009258 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009259 * @return The opacity of the view.
9260 */
Chet Haasea5531132012-02-02 13:41:44 -08009261 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009262 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009263 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009264 }
9265
9266 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009267 * Returns whether this View has content which overlaps. This function, intended to be
9268 * overridden by specific View types, is an optimization when alpha is set on a view. If
9269 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9270 * and then composited it into place, which can be expensive. If the view has no overlapping
9271 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9272 * An example of overlapping rendering is a TextView with a background image, such as a
9273 * Button. An example of non-overlapping rendering is a TextView with no background, or
9274 * an ImageView with only the foreground image. The default implementation returns true;
9275 * subclasses should override if they have cases which can be optimized.
9276 *
9277 * @return true if the content in this view might overlap, false otherwise.
9278 */
9279 public boolean hasOverlappingRendering() {
9280 return true;
9281 }
9282
9283 /**
Romain Guy171c5922011-01-06 10:04:23 -08009284 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9285 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009286 *
Romain Guy171c5922011-01-06 10:04:23 -08009287 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9288 * responsible for applying the opacity itself. Otherwise, calling this method is
9289 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009290 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009291 *
Chet Haasea5531132012-02-02 13:41:44 -08009292 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9293 * performance implications. It is generally best to use the alpha property sparingly and
9294 * transiently, as in the case of fading animations.</p>
9295 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009296 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009297 *
Joe Malin32736f02011-01-19 16:14:20 -08009298 * @see #setLayerType(int, android.graphics.Paint)
9299 *
Chet Haase73066682010-11-29 15:55:32 -08009300 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009301 */
9302 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009303 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009304 if (mTransformationInfo.mAlpha != alpha) {
9305 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009306 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009307 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009308 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009309 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009310 invalidate(true);
9311 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009312 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009313 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009314 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009315 mDisplayList.setAlpha(alpha);
9316 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009317 }
Chet Haaseed032702010-10-01 14:05:54 -07009318 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009319 }
9320
9321 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009322 * Faster version of setAlpha() which performs the same steps except there are
9323 * no calls to invalidate(). The caller of this function should perform proper invalidation
9324 * on the parent and this object. The return value indicates whether the subclass handles
9325 * alpha (the return value for onSetAlpha()).
9326 *
9327 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009328 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9329 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009330 */
9331 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009332 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009333 if (mTransformationInfo.mAlpha != alpha) {
9334 mTransformationInfo.mAlpha = alpha;
9335 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9336 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009337 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009338 return true;
9339 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009340 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009341 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009342 mDisplayList.setAlpha(alpha);
9343 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009344 }
Chet Haasea00f3862011-02-22 06:34:40 -08009345 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009346 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009347 }
9348
9349 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009350 * Top position of this view relative to its parent.
9351 *
9352 * @return The top of this view, in pixels.
9353 */
9354 @ViewDebug.CapturedViewProperty
9355 public final int getTop() {
9356 return mTop;
9357 }
9358
9359 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009360 * Sets the top position of this view relative to its parent. This method is meant to be called
9361 * by the layout system and should not generally be called otherwise, because the property
9362 * may be changed at any time by the layout.
9363 *
9364 * @param top The top of this view, in pixels.
9365 */
9366 public final void setTop(int top) {
9367 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009368 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009369 final boolean matrixIsIdentity = mTransformationInfo == null
9370 || mTransformationInfo.mMatrixIsIdentity;
9371 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009372 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009373 int minTop;
9374 int yLoc;
9375 if (top < mTop) {
9376 minTop = top;
9377 yLoc = top - mTop;
9378 } else {
9379 minTop = mTop;
9380 yLoc = 0;
9381 }
Chet Haasee9140a72011-02-16 16:23:29 -08009382 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009383 }
9384 } else {
9385 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009386 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009387 }
9388
Chet Haaseed032702010-10-01 14:05:54 -07009389 int width = mRight - mLeft;
9390 int oldHeight = mBottom - mTop;
9391
Chet Haase21cd1382010-09-01 17:42:29 -07009392 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009393 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009394 mDisplayList.setTop(mTop);
9395 }
Chet Haase21cd1382010-09-01 17:42:29 -07009396
Chet Haaseed032702010-10-01 14:05:54 -07009397 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9398
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009399 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009400 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009401 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009402 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009403 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009404 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009405 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009406 }
Chet Haase55dbb652010-12-21 20:15:08 -08009407 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009408 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009409 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009410 // View was rejected last time it was drawn by its parent; this may have changed
9411 invalidateParentIfNeeded();
9412 }
Chet Haase21cd1382010-09-01 17:42:29 -07009413 }
9414 }
9415
9416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009417 * Bottom position of this view relative to its parent.
9418 *
9419 * @return The bottom of this view, in pixels.
9420 */
9421 @ViewDebug.CapturedViewProperty
9422 public final int getBottom() {
9423 return mBottom;
9424 }
9425
9426 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009427 * True if this view has changed since the last time being drawn.
9428 *
9429 * @return The dirty state of this view.
9430 */
9431 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009433 }
9434
9435 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009436 * Sets the bottom position of this view relative to its parent. This method is meant to be
9437 * called by the layout system and should not generally be called otherwise, because the
9438 * property may be changed at any time by the layout.
9439 *
9440 * @param bottom The bottom of this view, in pixels.
9441 */
9442 public final void setBottom(int bottom) {
9443 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009444 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009445 final boolean matrixIsIdentity = mTransformationInfo == null
9446 || mTransformationInfo.mMatrixIsIdentity;
9447 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009448 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009449 int maxBottom;
9450 if (bottom < mBottom) {
9451 maxBottom = mBottom;
9452 } else {
9453 maxBottom = bottom;
9454 }
Chet Haasee9140a72011-02-16 16:23:29 -08009455 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009456 }
9457 } else {
9458 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009459 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009460 }
9461
Chet Haaseed032702010-10-01 14:05:54 -07009462 int width = mRight - mLeft;
9463 int oldHeight = mBottom - mTop;
9464
Chet Haase21cd1382010-09-01 17:42:29 -07009465 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009466 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009467 mDisplayList.setBottom(mBottom);
9468 }
Chet Haase21cd1382010-09-01 17:42:29 -07009469
Chet Haaseed032702010-10-01 14:05:54 -07009470 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9471
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009472 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009473 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009474 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009475 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009476 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009477 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009478 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009479 }
Chet Haase55dbb652010-12-21 20:15:08 -08009480 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009481 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009482 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009483 // View was rejected last time it was drawn by its parent; this may have changed
9484 invalidateParentIfNeeded();
9485 }
Chet Haase21cd1382010-09-01 17:42:29 -07009486 }
9487 }
9488
9489 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009490 * Left position of this view relative to its parent.
9491 *
9492 * @return The left edge of this view, in pixels.
9493 */
9494 @ViewDebug.CapturedViewProperty
9495 public final int getLeft() {
9496 return mLeft;
9497 }
9498
9499 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009500 * Sets the left position of this view relative to its parent. This method is meant to be called
9501 * by the layout system and should not generally be called otherwise, because the property
9502 * may be changed at any time by the layout.
9503 *
9504 * @param left The bottom of this view, in pixels.
9505 */
9506 public final void setLeft(int left) {
9507 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009508 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009509 final boolean matrixIsIdentity = mTransformationInfo == null
9510 || mTransformationInfo.mMatrixIsIdentity;
9511 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009512 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009513 int minLeft;
9514 int xLoc;
9515 if (left < mLeft) {
9516 minLeft = left;
9517 xLoc = left - mLeft;
9518 } else {
9519 minLeft = mLeft;
9520 xLoc = 0;
9521 }
Chet Haasee9140a72011-02-16 16:23:29 -08009522 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009523 }
9524 } else {
9525 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009526 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009527 }
9528
Chet Haaseed032702010-10-01 14:05:54 -07009529 int oldWidth = mRight - mLeft;
9530 int height = mBottom - mTop;
9531
Chet Haase21cd1382010-09-01 17:42:29 -07009532 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009533 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009534 mDisplayList.setLeft(left);
9535 }
Chet Haase21cd1382010-09-01 17:42:29 -07009536
Chet Haaseed032702010-10-01 14:05:54 -07009537 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9538
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009539 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009540 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009541 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009542 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009543 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009544 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009545 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009546 }
Chet Haase55dbb652010-12-21 20:15:08 -08009547 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009548 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009549 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009550 // View was rejected last time it was drawn by its parent; this may have changed
9551 invalidateParentIfNeeded();
9552 }
Chet Haase21cd1382010-09-01 17:42:29 -07009553 }
9554 }
9555
9556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 * Right position of this view relative to its parent.
9558 *
9559 * @return The right edge of this view, in pixels.
9560 */
9561 @ViewDebug.CapturedViewProperty
9562 public final int getRight() {
9563 return mRight;
9564 }
9565
9566 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009567 * Sets the right position of this view relative to its parent. This method is meant to be called
9568 * by the layout system and should not generally be called otherwise, because the property
9569 * may be changed at any time by the layout.
9570 *
9571 * @param right The bottom of this view, in pixels.
9572 */
9573 public final void setRight(int right) {
9574 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009575 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009576 final boolean matrixIsIdentity = mTransformationInfo == null
9577 || mTransformationInfo.mMatrixIsIdentity;
9578 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009579 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009580 int maxRight;
9581 if (right < mRight) {
9582 maxRight = mRight;
9583 } else {
9584 maxRight = right;
9585 }
Chet Haasee9140a72011-02-16 16:23:29 -08009586 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009587 }
9588 } else {
9589 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009590 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009591 }
9592
Chet Haaseed032702010-10-01 14:05:54 -07009593 int oldWidth = mRight - mLeft;
9594 int height = mBottom - mTop;
9595
Chet Haase21cd1382010-09-01 17:42:29 -07009596 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009597 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009598 mDisplayList.setRight(mRight);
9599 }
Chet Haase21cd1382010-09-01 17:42:29 -07009600
Chet Haaseed032702010-10-01 14:05:54 -07009601 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9602
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009603 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009604 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009605 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009606 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009607 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009608 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009609 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009610 }
Chet Haase55dbb652010-12-21 20:15:08 -08009611 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009612 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009613 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009614 // View was rejected last time it was drawn by its parent; this may have changed
9615 invalidateParentIfNeeded();
9616 }
Chet Haase21cd1382010-09-01 17:42:29 -07009617 }
9618 }
9619
9620 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009621 * The visual x position of this view, in pixels. This is equivalent to the
9622 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009623 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009624 *
Chet Haasedf030d22010-07-30 17:22:38 -07009625 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009626 */
Chet Haasea5531132012-02-02 13:41:44 -08009627 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009628 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009629 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009630 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009631
Chet Haasedf030d22010-07-30 17:22:38 -07009632 /**
9633 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9634 * {@link #setTranslationX(float) translationX} property to be the difference between
9635 * the x value passed in and the current {@link #getLeft() left} property.
9636 *
9637 * @param x The visual x position of this view, in pixels.
9638 */
9639 public void setX(float x) {
9640 setTranslationX(x - mLeft);
9641 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009642
Chet Haasedf030d22010-07-30 17:22:38 -07009643 /**
9644 * The visual y position of this view, in pixels. This is equivalent to the
9645 * {@link #setTranslationY(float) translationY} property plus the current
9646 * {@link #getTop() top} property.
9647 *
9648 * @return The visual y position of this view, in pixels.
9649 */
Chet Haasea5531132012-02-02 13:41:44 -08009650 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009651 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009652 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009653 }
9654
9655 /**
9656 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9657 * {@link #setTranslationY(float) translationY} property to be the difference between
9658 * the y value passed in and the current {@link #getTop() top} property.
9659 *
9660 * @param y The visual y position of this view, in pixels.
9661 */
9662 public void setY(float y) {
9663 setTranslationY(y - mTop);
9664 }
9665
9666
9667 /**
9668 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9669 * This position is post-layout, in addition to wherever the object's
9670 * layout placed it.
9671 *
9672 * @return The horizontal position of this view relative to its left position, in pixels.
9673 */
Chet Haasea5531132012-02-02 13:41:44 -08009674 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009675 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009676 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009677 }
9678
9679 /**
9680 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9681 * This effectively positions the object post-layout, in addition to wherever the object's
9682 * layout placed it.
9683 *
9684 * @param translationX The horizontal position of this view relative to its left position,
9685 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009686 *
9687 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009688 */
9689 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009690 ensureTransformationInfo();
9691 final TransformationInfo info = mTransformationInfo;
9692 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009693 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009694 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009695 info.mTranslationX = translationX;
9696 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009697 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009698 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009699 mDisplayList.setTranslationX(translationX);
9700 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009701 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009702 // View was rejected last time it was drawn by its parent; this may have changed
9703 invalidateParentIfNeeded();
9704 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009705 }
9706 }
9707
9708 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009709 * The horizontal location of this view relative to its {@link #getTop() top} position.
9710 * This position is post-layout, in addition to wherever the object's
9711 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009712 *
Chet Haasedf030d22010-07-30 17:22:38 -07009713 * @return The vertical position of this view relative to its top position,
9714 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009715 */
Chet Haasea5531132012-02-02 13:41:44 -08009716 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009717 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009718 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009719 }
9720
9721 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009722 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9723 * This effectively positions the object post-layout, in addition to wherever the object's
9724 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009725 *
Chet Haasedf030d22010-07-30 17:22:38 -07009726 * @param translationY The vertical position of this view relative to its top position,
9727 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009728 *
9729 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009730 */
Chet Haasedf030d22010-07-30 17:22:38 -07009731 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009732 ensureTransformationInfo();
9733 final TransformationInfo info = mTransformationInfo;
9734 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009735 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009736 info.mTranslationY = translationY;
9737 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009738 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009739 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009740 mDisplayList.setTranslationY(translationY);
9741 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009742 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009743 // View was rejected last time it was drawn by its parent; this may have changed
9744 invalidateParentIfNeeded();
9745 }
Chet Haasedf030d22010-07-30 17:22:38 -07009746 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009747 }
9748
9749 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 * Hit rectangle in parent's coordinates
9751 *
9752 * @param outRect The hit rectangle of the view.
9753 */
9754 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009755 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009756 final TransformationInfo info = mTransformationInfo;
9757 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009758 outRect.set(mLeft, mTop, mRight, mBottom);
9759 } else {
9760 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009761 tmpRect.set(-info.mPivotX, -info.mPivotY,
9762 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9763 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009764 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9765 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009766 }
9767 }
9768
9769 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009770 * Determines whether the given point, in local coordinates is inside the view.
9771 */
9772 /*package*/ final boolean pointInView(float localX, float localY) {
9773 return localX >= 0 && localX < (mRight - mLeft)
9774 && localY >= 0 && localY < (mBottom - mTop);
9775 }
9776
9777 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009778 * Utility method to determine whether the given point, in local coordinates,
9779 * is inside the view, where the area of the view is expanded by the slop factor.
9780 * This method is called while processing touch-move events to determine if the event
9781 * is still within the view.
9782 */
9783 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009784 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009785 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009786 }
9787
9788 /**
9789 * When a view has focus and the user navigates away from it, the next view is searched for
9790 * starting from the rectangle filled in by this method.
9791 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009792 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009793 * of the view. However, if your view maintains some idea of internal selection,
9794 * such as a cursor, or a selected row or column, you should override this method and
9795 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 *
9797 * @param r The rectangle to fill in, in this view's coordinates.
9798 */
9799 public void getFocusedRect(Rect r) {
9800 getDrawingRect(r);
9801 }
9802
9803 /**
9804 * If some part of this view is not clipped by any of its parents, then
9805 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009806 * coordinates (without taking possible View rotations into account), offset
9807 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9808 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 *
9810 * @param r If true is returned, r holds the global coordinates of the
9811 * visible portion of this view.
9812 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9813 * between this view and its root. globalOffet may be null.
9814 * @return true if r is non-empty (i.e. part of the view is visible at the
9815 * root level.
9816 */
9817 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9818 int width = mRight - mLeft;
9819 int height = mBottom - mTop;
9820 if (width > 0 && height > 0) {
9821 r.set(0, 0, width, height);
9822 if (globalOffset != null) {
9823 globalOffset.set(-mScrollX, -mScrollY);
9824 }
9825 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9826 }
9827 return false;
9828 }
9829
9830 public final boolean getGlobalVisibleRect(Rect r) {
9831 return getGlobalVisibleRect(r, null);
9832 }
9833
9834 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009835 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 if (getGlobalVisibleRect(r, offset)) {
9837 r.offset(-offset.x, -offset.y); // make r local
9838 return true;
9839 }
9840 return false;
9841 }
9842
9843 /**
9844 * Offset this view's vertical location by the specified number of pixels.
9845 *
9846 * @param offset the number of pixels to offset the view by
9847 */
9848 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009849 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009850 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009851 final boolean matrixIsIdentity = mTransformationInfo == null
9852 || mTransformationInfo.mMatrixIsIdentity;
9853 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009854 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009855 invalidateViewProperty(false, false);
9856 } else {
9857 final ViewParent p = mParent;
9858 if (p != null && mAttachInfo != null) {
9859 final Rect r = mAttachInfo.mTmpInvalRect;
9860 int minTop;
9861 int maxBottom;
9862 int yLoc;
9863 if (offset < 0) {
9864 minTop = mTop + offset;
9865 maxBottom = mBottom;
9866 yLoc = offset;
9867 } else {
9868 minTop = mTop;
9869 maxBottom = mBottom + offset;
9870 yLoc = 0;
9871 }
9872 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9873 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009874 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009875 }
9876 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009877 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009878 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009879
Chet Haasec3aa3612010-06-17 08:50:37 -07009880 mTop += offset;
9881 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009882 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009883 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009884 invalidateViewProperty(false, false);
9885 } else {
9886 if (!matrixIsIdentity) {
9887 invalidateViewProperty(false, true);
9888 }
9889 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009890 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009892 }
9893
9894 /**
9895 * Offset this view's horizontal location by the specified amount of pixels.
9896 *
9897 * @param offset the numer of pixels to offset the view by
9898 */
9899 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009900 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009901 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009902 final boolean matrixIsIdentity = mTransformationInfo == null
9903 || mTransformationInfo.mMatrixIsIdentity;
9904 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009905 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009906 invalidateViewProperty(false, false);
9907 } else {
9908 final ViewParent p = mParent;
9909 if (p != null && mAttachInfo != null) {
9910 final Rect r = mAttachInfo.mTmpInvalRect;
9911 int minLeft;
9912 int maxRight;
9913 if (offset < 0) {
9914 minLeft = mLeft + offset;
9915 maxRight = mRight;
9916 } else {
9917 minLeft = mLeft;
9918 maxRight = mRight + offset;
9919 }
9920 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9921 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009922 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009923 }
9924 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009925 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009926 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009927
Chet Haasec3aa3612010-06-17 08:50:37 -07009928 mLeft += offset;
9929 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009930 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009931 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009932 invalidateViewProperty(false, false);
9933 } else {
9934 if (!matrixIsIdentity) {
9935 invalidateViewProperty(false, true);
9936 }
9937 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009938 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 }
9941
9942 /**
9943 * Get the LayoutParams associated with this view. All views should have
9944 * layout parameters. These supply parameters to the <i>parent</i> of this
9945 * view specifying how it should be arranged. There are many subclasses of
9946 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9947 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009948 *
9949 * This method may return null if this View is not attached to a parent
9950 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9951 * was not invoked successfully. When a View is attached to a parent
9952 * ViewGroup, this method must not return null.
9953 *
9954 * @return The LayoutParams associated with this view, or null if no
9955 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009957 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 public ViewGroup.LayoutParams getLayoutParams() {
9959 return mLayoutParams;
9960 }
9961
9962 /**
9963 * Set the layout parameters associated with this view. These supply
9964 * parameters to the <i>parent</i> of this view specifying how it should be
9965 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9966 * correspond to the different subclasses of ViewGroup that are responsible
9967 * for arranging their children.
9968 *
Romain Guy01c174b2011-02-22 11:51:06 -08009969 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 */
9971 public void setLayoutParams(ViewGroup.LayoutParams params) {
9972 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009973 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 }
9975 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009976 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009977 if (mParent instanceof ViewGroup) {
9978 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980 requestLayout();
9981 }
9982
9983 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009984 * Resolve the layout parameters depending on the resolved layout direction
9985 */
9986 private void resolveLayoutParams() {
9987 if (mLayoutParams != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07009988 mLayoutParams.onResolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009989 }
9990 }
9991
9992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 * Set the scrolled position of your view. This will cause a call to
9994 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9995 * invalidated.
9996 * @param x the x position to scroll to
9997 * @param y the y position to scroll to
9998 */
9999 public void scrollTo(int x, int y) {
10000 if (mScrollX != x || mScrollY != y) {
10001 int oldX = mScrollX;
10002 int oldY = mScrollY;
10003 mScrollX = x;
10004 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010005 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010006 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010007 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010008 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 }
10011 }
10012
10013 /**
10014 * Move the scrolled position of your view. This will cause a call to
10015 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10016 * invalidated.
10017 * @param x the amount of pixels to scroll by horizontally
10018 * @param y the amount of pixels to scroll by vertically
10019 */
10020 public void scrollBy(int x, int y) {
10021 scrollTo(mScrollX + x, mScrollY + y);
10022 }
10023
10024 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010025 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10026 * animation to fade the scrollbars out after a default delay. If a subclass
10027 * provides animated scrolling, the start delay should equal the duration
10028 * of the scrolling animation.</p>
10029 *
10030 * <p>The animation starts only if at least one of the scrollbars is
10031 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10032 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10033 * this method returns true, and false otherwise. If the animation is
10034 * started, this method calls {@link #invalidate()}; in that case the
10035 * caller should not call {@link #invalidate()}.</p>
10036 *
10037 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010038 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010039 *
10040 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10041 * and {@link #scrollTo(int, int)}.</p>
10042 *
10043 * @return true if the animation is played, false otherwise
10044 *
10045 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010046 * @see #scrollBy(int, int)
10047 * @see #scrollTo(int, int)
10048 * @see #isHorizontalScrollBarEnabled()
10049 * @see #isVerticalScrollBarEnabled()
10050 * @see #setHorizontalScrollBarEnabled(boolean)
10051 * @see #setVerticalScrollBarEnabled(boolean)
10052 */
10053 protected boolean awakenScrollBars() {
10054 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010055 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010056 }
10057
10058 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010059 * Trigger the scrollbars to draw.
10060 * This method differs from awakenScrollBars() only in its default duration.
10061 * initialAwakenScrollBars() will show the scroll bars for longer than
10062 * usual to give the user more of a chance to notice them.
10063 *
10064 * @return true if the animation is played, false otherwise.
10065 */
10066 private boolean initialAwakenScrollBars() {
10067 return mScrollCache != null &&
10068 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10069 }
10070
10071 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010072 * <p>
10073 * Trigger the scrollbars to draw. When invoked this method starts an
10074 * animation to fade the scrollbars out after a fixed delay. If a subclass
10075 * provides animated scrolling, the start delay should equal the duration of
10076 * the scrolling animation.
10077 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010078 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010079 * <p>
10080 * The animation starts only if at least one of the scrollbars is enabled,
10081 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10082 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10083 * this method returns true, and false otherwise. If the animation is
10084 * started, this method calls {@link #invalidate()}; in that case the caller
10085 * should not call {@link #invalidate()}.
10086 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010087 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010088 * <p>
10089 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010090 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010091 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010092 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010093 * @param startDelay the delay, in milliseconds, after which the animation
10094 * should start; when the delay is 0, the animation starts
10095 * immediately
10096 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010097 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010098 * @see #scrollBy(int, int)
10099 * @see #scrollTo(int, int)
10100 * @see #isHorizontalScrollBarEnabled()
10101 * @see #isVerticalScrollBarEnabled()
10102 * @see #setHorizontalScrollBarEnabled(boolean)
10103 * @see #setVerticalScrollBarEnabled(boolean)
10104 */
10105 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010106 return awakenScrollBars(startDelay, true);
10107 }
Joe Malin32736f02011-01-19 16:14:20 -080010108
Mike Cleron290947b2009-09-29 18:34:32 -070010109 /**
10110 * <p>
10111 * Trigger the scrollbars to draw. When invoked this method starts an
10112 * animation to fade the scrollbars out after a fixed delay. If a subclass
10113 * provides animated scrolling, the start delay should equal the duration of
10114 * the scrolling animation.
10115 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010116 *
Mike Cleron290947b2009-09-29 18:34:32 -070010117 * <p>
10118 * The animation starts only if at least one of the scrollbars is enabled,
10119 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10120 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10121 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010122 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010123 * is set to true; in that case the caller
10124 * should not call {@link #invalidate()}.
10125 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010126 *
Mike Cleron290947b2009-09-29 18:34:32 -070010127 * <p>
10128 * This method should be invoked everytime a subclass directly updates the
10129 * scroll parameters.
10130 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010131 *
Mike Cleron290947b2009-09-29 18:34:32 -070010132 * @param startDelay the delay, in milliseconds, after which the animation
10133 * should start; when the delay is 0, the animation starts
10134 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010135 *
Mike Cleron290947b2009-09-29 18:34:32 -070010136 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010137 *
Mike Cleron290947b2009-09-29 18:34:32 -070010138 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010139 *
Mike Cleron290947b2009-09-29 18:34:32 -070010140 * @see #scrollBy(int, int)
10141 * @see #scrollTo(int, int)
10142 * @see #isHorizontalScrollBarEnabled()
10143 * @see #isVerticalScrollBarEnabled()
10144 * @see #setHorizontalScrollBarEnabled(boolean)
10145 * @see #setVerticalScrollBarEnabled(boolean)
10146 */
10147 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010148 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010149
Mike Cleronf116bf82009-09-27 19:14:12 -070010150 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10151 return false;
10152 }
10153
10154 if (scrollCache.scrollBar == null) {
10155 scrollCache.scrollBar = new ScrollBarDrawable();
10156 }
10157
10158 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10159
Mike Cleron290947b2009-09-29 18:34:32 -070010160 if (invalidate) {
10161 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010162 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010163 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010164
10165 if (scrollCache.state == ScrollabilityCache.OFF) {
10166 // FIXME: this is copied from WindowManagerService.
10167 // We should get this value from the system when it
10168 // is possible to do so.
10169 final int KEY_REPEAT_FIRST_DELAY = 750;
10170 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10171 }
10172
10173 // Tell mScrollCache when we should start fading. This may
10174 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010175 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010176 scrollCache.fadeStartTime = fadeStartTime;
10177 scrollCache.state = ScrollabilityCache.ON;
10178
10179 // Schedule our fader to run, unscheduling any old ones first
10180 if (mAttachInfo != null) {
10181 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10182 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10183 }
10184
10185 return true;
10186 }
10187
10188 return false;
10189 }
10190
10191 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010192 * Do not invalidate views which are not visible and which are not running an animation. They
10193 * will not get drawn and they should not set dirty flags as if they will be drawn
10194 */
10195 private boolean skipInvalidate() {
10196 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10197 (!(mParent instanceof ViewGroup) ||
10198 !((ViewGroup) mParent).isViewTransitioning(this));
10199 }
10200 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010201 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010202 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10203 * in the future. This must be called from a UI thread. To call from a non-UI
10204 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 *
10206 * WARNING: This method is destructive to dirty.
10207 * @param dirty the rectangle representing the bounds of the dirty region
10208 */
10209 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010210 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010211 return;
10212 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010213 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10214 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10215 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10216 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10217 mPrivateFlags |= PFLAG_INVALIDATED;
10218 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 final ViewParent p = mParent;
10220 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010221 //noinspection PointlessBooleanExpression,ConstantConditions
10222 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10223 if (p != null && ai != null && ai.mHardwareAccelerated) {
10224 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010225 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010226 p.invalidateChild(this, null);
10227 return;
10228 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 if (p != null && ai != null) {
10231 final int scrollX = mScrollX;
10232 final int scrollY = mScrollY;
10233 final Rect r = ai.mTmpInvalRect;
10234 r.set(dirty.left - scrollX, dirty.top - scrollY,
10235 dirty.right - scrollX, dirty.bottom - scrollY);
10236 mParent.invalidateChild(this, r);
10237 }
10238 }
10239 }
10240
10241 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010242 * 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 -080010243 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010244 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10245 * will be called at some point in the future. This must be called from
10246 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 * @param l the left position of the dirty region
10248 * @param t the top position of the dirty region
10249 * @param r the right position of the dirty region
10250 * @param b the bottom position of the dirty region
10251 */
10252 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010253 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010254 return;
10255 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010256 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10257 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10258 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10259 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10260 mPrivateFlags |= PFLAG_INVALIDATED;
10261 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010262 final ViewParent p = mParent;
10263 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010264 //noinspection PointlessBooleanExpression,ConstantConditions
10265 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10266 if (p != null && ai != null && ai.mHardwareAccelerated) {
10267 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010268 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010269 p.invalidateChild(this, null);
10270 return;
10271 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010273 if (p != null && ai != null && l < r && t < b) {
10274 final int scrollX = mScrollX;
10275 final int scrollY = mScrollY;
10276 final Rect tmpr = ai.mTmpInvalRect;
10277 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10278 p.invalidateChild(this, tmpr);
10279 }
10280 }
10281 }
10282
10283 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010284 * Invalidate the whole view. If the view is visible,
10285 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10286 * the future. This must be called from a UI thread. To call from a non-UI thread,
10287 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010288 */
10289 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010290 invalidate(true);
10291 }
Joe Malin32736f02011-01-19 16:14:20 -080010292
Chet Haaseed032702010-10-01 14:05:54 -070010293 /**
10294 * This is where the invalidate() work actually happens. A full invalidate()
10295 * causes the drawing cache to be invalidated, but this function can be called with
10296 * invalidateCache set to false to skip that invalidation step for cases that do not
10297 * need it (for example, a component that remains at the same dimensions with the same
10298 * content).
10299 *
10300 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10301 * well. This is usually true for a full invalidate, but may be set to false if the
10302 * View's contents or dimensions have not changed.
10303 */
Romain Guy849d0a32011-02-01 17:20:48 -080010304 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010305 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010306 return;
10307 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010308 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10309 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10310 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010311 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010312 mPrivateFlags &= ~PFLAG_DRAWN;
10313 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010314 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010315 mPrivateFlags |= PFLAG_INVALIDATED;
10316 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010319 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010320 //noinspection PointlessBooleanExpression,ConstantConditions
10321 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10322 if (p != null && ai != null && ai.mHardwareAccelerated) {
10323 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010324 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010325 p.invalidateChild(this, null);
10326 return;
10327 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010328 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010330 if (p != null && ai != null) {
10331 final Rect r = ai.mTmpInvalRect;
10332 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10333 // Don't call invalidate -- we don't want to internally scroll
10334 // our own bounds
10335 p.invalidateChild(this, r);
10336 }
10337 }
10338 }
10339
10340 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010341 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10342 * set any flags or handle all of the cases handled by the default invalidation methods.
10343 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10344 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10345 * walk up the hierarchy, transforming the dirty rect as necessary.
10346 *
10347 * The method also handles normal invalidation logic if display list properties are not
10348 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10349 * backup approach, to handle these cases used in the various property-setting methods.
10350 *
10351 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10352 * are not being used in this view
10353 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10354 * list properties are not being used in this view
10355 */
10356 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010357 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010358 if (invalidateParent) {
10359 invalidateParentCaches();
10360 }
10361 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010362 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010363 }
10364 invalidate(false);
10365 } else {
10366 final AttachInfo ai = mAttachInfo;
10367 final ViewParent p = mParent;
10368 if (p != null && ai != null) {
10369 final Rect r = ai.mTmpInvalRect;
10370 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10371 if (mParent instanceof ViewGroup) {
10372 ((ViewGroup) mParent).invalidateChildFast(this, r);
10373 } else {
10374 mParent.invalidateChild(this, r);
10375 }
10376 }
10377 }
10378 }
10379
10380 /**
10381 * Utility method to transform a given Rect by the current matrix of this view.
10382 */
10383 void transformRect(final Rect rect) {
10384 if (!getMatrix().isIdentity()) {
10385 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10386 boundingRect.set(rect);
10387 getMatrix().mapRect(boundingRect);
10388 rect.set((int) (boundingRect.left - 0.5f),
10389 (int) (boundingRect.top - 0.5f),
10390 (int) (boundingRect.right + 0.5f),
10391 (int) (boundingRect.bottom + 0.5f));
10392 }
10393 }
10394
10395 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010396 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010397 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10398 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010399 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10400 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010401 *
10402 * @hide
10403 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010404 protected void invalidateParentCaches() {
10405 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010406 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010407 }
10408 }
Joe Malin32736f02011-01-19 16:14:20 -080010409
Romain Guy0fd89bf2011-01-26 15:41:30 -080010410 /**
10411 * Used to indicate that the parent of this view should be invalidated. This functionality
10412 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10413 * which is necessary when various parent-managed properties of the view change, such as
10414 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10415 * an invalidation event to the parent.
10416 *
10417 * @hide
10418 */
10419 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010420 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010421 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010422 }
10423 }
10424
10425 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010426 * Indicates whether this View is opaque. An opaque View guarantees that it will
10427 * draw all the pixels overlapping its bounds using a fully opaque color.
10428 *
10429 * Subclasses of View should override this method whenever possible to indicate
10430 * whether an instance is opaque. Opaque Views are treated in a special way by
10431 * the View hierarchy, possibly allowing it to perform optimizations during
10432 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010433 *
Romain Guy24443ea2009-05-11 11:56:30 -070010434 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010435 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010436 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010437 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010438 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010439 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010440 }
10441
Adam Powell20232d02010-12-08 21:08:53 -080010442 /**
10443 * @hide
10444 */
10445 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010446 // Opaque if:
10447 // - Has a background
10448 // - Background is opaque
10449 // - Doesn't have scrollbars or scrollbars are inside overlay
10450
Philip Milne6c8ea062012-04-03 17:38:43 -070010451 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010452 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010453 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010454 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010455 }
10456
10457 final int flags = mViewFlags;
10458 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10459 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010460 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010461 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010462 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010463 }
10464 }
10465
10466 /**
10467 * @hide
10468 */
10469 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010470 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010471 }
10472
10473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010474 * @return A handler associated with the thread running the View. This
10475 * handler can be used to pump events in the UI events queue.
10476 */
10477 public Handler getHandler() {
10478 if (mAttachInfo != null) {
10479 return mAttachInfo.mHandler;
10480 }
10481 return null;
10482 }
10483
10484 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010485 * Gets the view root associated with the View.
10486 * @return The view root, or null if none.
10487 * @hide
10488 */
10489 public ViewRootImpl getViewRootImpl() {
10490 if (mAttachInfo != null) {
10491 return mAttachInfo.mViewRootImpl;
10492 }
10493 return null;
10494 }
10495
10496 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010497 * <p>Causes the Runnable to be added to the message queue.
10498 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010499 *
Romain Guye63a4f32011-08-11 11:33:31 -070010500 * <p>This method can be invoked from outside of the UI thread
10501 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010502 *
10503 * @param action The Runnable that will be executed.
10504 *
10505 * @return Returns true if the Runnable was successfully placed in to the
10506 * message queue. Returns false on failure, usually because the
10507 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010508 *
10509 * @see #postDelayed
10510 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 */
10512 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010513 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010514 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010515 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010517 // Assume that post will succeed later
10518 ViewRootImpl.getRunQueue().post(action);
10519 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 }
10521
10522 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010523 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010524 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010525 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010526 *
Romain Guye63a4f32011-08-11 11:33:31 -070010527 * <p>This method can be invoked from outside of the UI thread
10528 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010529 *
10530 * @param action The Runnable that will be executed.
10531 * @param delayMillis The delay (in milliseconds) until the Runnable
10532 * will be executed.
10533 *
10534 * @return true if the Runnable was successfully placed in to the
10535 * message queue. Returns false on failure, usually because the
10536 * looper processing the message queue is exiting. Note that a
10537 * result of true does not mean the Runnable will be processed --
10538 * if the looper is quit before the delivery time of the message
10539 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010540 *
10541 * @see #post
10542 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 */
10544 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010545 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010546 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010547 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010548 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010549 // Assume that post will succeed later
10550 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10551 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552 }
10553
10554 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010555 * <p>Causes the Runnable to execute on the next animation time step.
10556 * The runnable will be run on the user interface thread.</p>
10557 *
10558 * <p>This method can be invoked from outside of the UI thread
10559 * only when this View is attached to a window.</p>
10560 *
10561 * @param action The Runnable that will be executed.
10562 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010563 * @see #postOnAnimationDelayed
10564 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010565 */
10566 public void postOnAnimation(Runnable action) {
10567 final AttachInfo attachInfo = mAttachInfo;
10568 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010569 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10570 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010571 } else {
10572 // Assume that post will succeed later
10573 ViewRootImpl.getRunQueue().post(action);
10574 }
10575 }
10576
10577 /**
10578 * <p>Causes the Runnable to execute on the next animation time step,
10579 * after the specified amount of time elapses.
10580 * The runnable will be run on the user interface thread.</p>
10581 *
10582 * <p>This method can be invoked from outside of the UI thread
10583 * only when this View is attached to a window.</p>
10584 *
10585 * @param action The Runnable that will be executed.
10586 * @param delayMillis The delay (in milliseconds) until the Runnable
10587 * will be executed.
10588 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010589 * @see #postOnAnimation
10590 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010591 */
10592 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10593 final AttachInfo attachInfo = mAttachInfo;
10594 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010595 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10596 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010597 } else {
10598 // Assume that post will succeed later
10599 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10600 }
10601 }
10602
10603 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010604 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010605 *
Romain Guye63a4f32011-08-11 11:33:31 -070010606 * <p>This method can be invoked from outside of the UI thread
10607 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 *
10609 * @param action The Runnable to remove from the message handling queue
10610 *
10611 * @return true if this view could ask the Handler to remove the Runnable,
10612 * false otherwise. When the returned value is true, the Runnable
10613 * may or may not have been actually removed from the message queue
10614 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010615 *
10616 * @see #post
10617 * @see #postDelayed
10618 * @see #postOnAnimation
10619 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 */
10621 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010622 if (action != null) {
10623 final AttachInfo attachInfo = mAttachInfo;
10624 if (attachInfo != null) {
10625 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010626 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10627 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010628 } else {
10629 // Assume that post will succeed later
10630 ViewRootImpl.getRunQueue().removeCallbacks(action);
10631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010633 return true;
10634 }
10635
10636 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010637 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10638 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 *
Romain Guye63a4f32011-08-11 11:33:31 -070010640 * <p>This method can be invoked from outside of the UI thread
10641 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010642 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010644 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 */
10646 public void postInvalidate() {
10647 postInvalidateDelayed(0);
10648 }
10649
10650 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010651 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10652 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010653 *
Romain Guye63a4f32011-08-11 11:33:31 -070010654 * <p>This method can be invoked from outside of the UI thread
10655 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 *
10657 * @param left The left coordinate of the rectangle to invalidate.
10658 * @param top The top coordinate of the rectangle to invalidate.
10659 * @param right The right coordinate of the rectangle to invalidate.
10660 * @param bottom The bottom coordinate of the rectangle to invalidate.
10661 *
10662 * @see #invalidate(int, int, int, int)
10663 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010664 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 */
10666 public void postInvalidate(int left, int top, int right, int bottom) {
10667 postInvalidateDelayed(0, left, top, right, bottom);
10668 }
10669
10670 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010671 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10672 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010673 *
Romain Guye63a4f32011-08-11 11:33:31 -070010674 * <p>This method can be invoked from outside of the UI thread
10675 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010676 *
10677 * @param delayMilliseconds the duration in milliseconds to delay the
10678 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010679 *
10680 * @see #invalidate()
10681 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 */
10683 public void postInvalidateDelayed(long delayMilliseconds) {
10684 // We try only with the AttachInfo because there's no point in invalidating
10685 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010686 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010687 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010688 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 }
10690 }
10691
10692 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010693 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10694 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010695 *
Romain Guye63a4f32011-08-11 11:33:31 -070010696 * <p>This method can be invoked from outside of the UI thread
10697 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010698 *
10699 * @param delayMilliseconds the duration in milliseconds to delay the
10700 * invalidation by
10701 * @param left The left coordinate of the rectangle to invalidate.
10702 * @param top The top coordinate of the rectangle to invalidate.
10703 * @param right The right coordinate of the rectangle to invalidate.
10704 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010705 *
10706 * @see #invalidate(int, int, int, int)
10707 * @see #invalidate(Rect)
10708 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010709 */
10710 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10711 int right, int bottom) {
10712
10713 // We try only with the AttachInfo because there's no point in invalidating
10714 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010715 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010716 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010717 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10718 info.target = this;
10719 info.left = left;
10720 info.top = top;
10721 info.right = right;
10722 info.bottom = bottom;
10723
Jeff Browna175a5b2012-02-15 19:18:31 -080010724 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010725 }
10726 }
10727
10728 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010729 * <p>Cause an invalidate to happen on the next animation time step, typically the
10730 * next display frame.</p>
10731 *
10732 * <p>This method can be invoked from outside of the UI thread
10733 * only when this View is attached to a window.</p>
10734 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010735 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010736 */
10737 public void postInvalidateOnAnimation() {
10738 // We try only with the AttachInfo because there's no point in invalidating
10739 // if we are not attached to our window
10740 final AttachInfo attachInfo = mAttachInfo;
10741 if (attachInfo != null) {
10742 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10743 }
10744 }
10745
10746 /**
10747 * <p>Cause an invalidate of the specified area to happen on the next animation
10748 * time step, typically the next display frame.</p>
10749 *
10750 * <p>This method can be invoked from outside of the UI thread
10751 * only when this View is attached to a window.</p>
10752 *
10753 * @param left The left coordinate of the rectangle to invalidate.
10754 * @param top The top coordinate of the rectangle to invalidate.
10755 * @param right The right coordinate of the rectangle to invalidate.
10756 * @param bottom The bottom coordinate of the rectangle to invalidate.
10757 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010758 * @see #invalidate(int, int, int, int)
10759 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010760 */
10761 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10762 // We try only with the AttachInfo because there's no point in invalidating
10763 // if we are not attached to our window
10764 final AttachInfo attachInfo = mAttachInfo;
10765 if (attachInfo != null) {
10766 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10767 info.target = this;
10768 info.left = left;
10769 info.top = top;
10770 info.right = right;
10771 info.bottom = bottom;
10772
10773 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10774 }
10775 }
10776
10777 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010778 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10779 * This event is sent at most once every
10780 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10781 */
10782 private void postSendViewScrolledAccessibilityEventCallback() {
10783 if (mSendViewScrolledAccessibilityEvent == null) {
10784 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10785 }
10786 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10787 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10788 postDelayed(mSendViewScrolledAccessibilityEvent,
10789 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10790 }
10791 }
10792
10793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 * Called by a parent to request that a child update its values for mScrollX
10795 * and mScrollY if necessary. This will typically be done if the child is
10796 * animating a scroll using a {@link android.widget.Scroller Scroller}
10797 * object.
10798 */
10799 public void computeScroll() {
10800 }
10801
10802 /**
10803 * <p>Indicate whether the horizontal edges are faded when the view is
10804 * scrolled horizontally.</p>
10805 *
10806 * @return true if the horizontal edges should are faded on scroll, false
10807 * otherwise
10808 *
10809 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010810 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010811 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812 */
10813 public boolean isHorizontalFadingEdgeEnabled() {
10814 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10815 }
10816
10817 /**
10818 * <p>Define whether the horizontal edges should be faded when this view
10819 * is scrolled horizontally.</p>
10820 *
10821 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10822 * be faded when the view is scrolled
10823 * horizontally
10824 *
10825 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010826 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010827 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010828 */
10829 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10830 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10831 if (horizontalFadingEdgeEnabled) {
10832 initScrollCache();
10833 }
10834
10835 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10836 }
10837 }
10838
10839 /**
10840 * <p>Indicate whether the vertical edges are faded when the view is
10841 * scrolled horizontally.</p>
10842 *
10843 * @return true if the vertical edges should are faded on scroll, false
10844 * otherwise
10845 *
10846 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010847 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010848 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 */
10850 public boolean isVerticalFadingEdgeEnabled() {
10851 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10852 }
10853
10854 /**
10855 * <p>Define whether the vertical edges should be faded when this view
10856 * is scrolled vertically.</p>
10857 *
10858 * @param verticalFadingEdgeEnabled true if the vertical edges should
10859 * be faded when the view is scrolled
10860 * vertically
10861 *
10862 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010863 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010864 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010865 */
10866 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10867 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10868 if (verticalFadingEdgeEnabled) {
10869 initScrollCache();
10870 }
10871
10872 mViewFlags ^= FADING_EDGE_VERTICAL;
10873 }
10874 }
10875
10876 /**
10877 * Returns the strength, or intensity, of the top faded edge. The strength is
10878 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10879 * returns 0.0 or 1.0 but no value in between.
10880 *
10881 * Subclasses should override this method to provide a smoother fade transition
10882 * when scrolling occurs.
10883 *
10884 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10885 */
10886 protected float getTopFadingEdgeStrength() {
10887 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10888 }
10889
10890 /**
10891 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10892 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10893 * returns 0.0 or 1.0 but no value in between.
10894 *
10895 * Subclasses should override this method to provide a smoother fade transition
10896 * when scrolling occurs.
10897 *
10898 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10899 */
10900 protected float getBottomFadingEdgeStrength() {
10901 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10902 computeVerticalScrollRange() ? 1.0f : 0.0f;
10903 }
10904
10905 /**
10906 * Returns the strength, or intensity, of the left faded edge. The strength is
10907 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10908 * returns 0.0 or 1.0 but no value in between.
10909 *
10910 * Subclasses should override this method to provide a smoother fade transition
10911 * when scrolling occurs.
10912 *
10913 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10914 */
10915 protected float getLeftFadingEdgeStrength() {
10916 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10917 }
10918
10919 /**
10920 * Returns the strength, or intensity, of the right faded edge. The strength is
10921 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10922 * returns 0.0 or 1.0 but no value in between.
10923 *
10924 * Subclasses should override this method to provide a smoother fade transition
10925 * when scrolling occurs.
10926 *
10927 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10928 */
10929 protected float getRightFadingEdgeStrength() {
10930 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10931 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10932 }
10933
10934 /**
10935 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10936 * scrollbar is not drawn by default.</p>
10937 *
10938 * @return true if the horizontal scrollbar should be painted, false
10939 * otherwise
10940 *
10941 * @see #setHorizontalScrollBarEnabled(boolean)
10942 */
10943 public boolean isHorizontalScrollBarEnabled() {
10944 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10945 }
10946
10947 /**
10948 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10949 * scrollbar is not drawn by default.</p>
10950 *
10951 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10952 * be painted
10953 *
10954 * @see #isHorizontalScrollBarEnabled()
10955 */
10956 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10957 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10958 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010959 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010960 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010961 }
10962 }
10963
10964 /**
10965 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10966 * scrollbar is not drawn by default.</p>
10967 *
10968 * @return true if the vertical scrollbar should be painted, false
10969 * otherwise
10970 *
10971 * @see #setVerticalScrollBarEnabled(boolean)
10972 */
10973 public boolean isVerticalScrollBarEnabled() {
10974 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10975 }
10976
10977 /**
10978 * <p>Define whether the vertical scrollbar should be drawn or not. The
10979 * scrollbar is not drawn by default.</p>
10980 *
10981 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10982 * be painted
10983 *
10984 * @see #isVerticalScrollBarEnabled()
10985 */
10986 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10987 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10988 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010989 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010990 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991 }
10992 }
10993
Adam Powell20232d02010-12-08 21:08:53 -080010994 /**
10995 * @hide
10996 */
10997 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070010998 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 }
Joe Malin32736f02011-01-19 16:14:20 -080011000
Mike Cleronfe81d382009-09-28 14:22:16 -070011001 /**
11002 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011003 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011004 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011005 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011006 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011007 */
11008 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11009 initScrollCache();
11010 final ScrollabilityCache scrollabilityCache = mScrollCache;
11011 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011012 if (fadeScrollbars) {
11013 scrollabilityCache.state = ScrollabilityCache.OFF;
11014 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011015 scrollabilityCache.state = ScrollabilityCache.ON;
11016 }
11017 }
Joe Malin32736f02011-01-19 16:14:20 -080011018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019 /**
Joe Malin32736f02011-01-19 16:14:20 -080011020 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011021 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011022 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011023 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011024 *
11025 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011026 */
11027 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011028 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011029 }
Joe Malin32736f02011-01-19 16:14:20 -080011030
Mike Cleron52f0a642009-09-28 18:21:37 -070011031 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011032 *
11033 * Returns the delay before scrollbars fade.
11034 *
11035 * @return the delay before scrollbars fade
11036 *
11037 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11038 */
11039 public int getScrollBarDefaultDelayBeforeFade() {
11040 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11041 mScrollCache.scrollBarDefaultDelayBeforeFade;
11042 }
11043
11044 /**
11045 * Define the delay before scrollbars fade.
11046 *
11047 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11048 *
11049 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11050 */
11051 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11052 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11053 }
11054
11055 /**
11056 *
11057 * Returns the scrollbar fade duration.
11058 *
11059 * @return the scrollbar fade duration
11060 *
11061 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11062 */
11063 public int getScrollBarFadeDuration() {
11064 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11065 mScrollCache.scrollBarFadeDuration;
11066 }
11067
11068 /**
11069 * Define the scrollbar fade duration.
11070 *
11071 * @param scrollBarFadeDuration - the scrollbar fade duration
11072 *
11073 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11074 */
11075 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11076 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11077 }
11078
11079 /**
11080 *
11081 * Returns the scrollbar size.
11082 *
11083 * @return the scrollbar size
11084 *
11085 * @attr ref android.R.styleable#View_scrollbarSize
11086 */
11087 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011088 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011089 mScrollCache.scrollBarSize;
11090 }
11091
11092 /**
11093 * Define the scrollbar size.
11094 *
11095 * @param scrollBarSize - the scrollbar size
11096 *
11097 * @attr ref android.R.styleable#View_scrollbarSize
11098 */
11099 public void setScrollBarSize(int scrollBarSize) {
11100 getScrollCache().scrollBarSize = scrollBarSize;
11101 }
11102
11103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011104 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11105 * inset. When inset, they add to the padding of the view. And the scrollbars
11106 * can be drawn inside the padding area or on the edge of the view. For example,
11107 * if a view has a background drawable and you want to draw the scrollbars
11108 * inside the padding specified by the drawable, you can use
11109 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11110 * appear at the edge of the view, ignoring the padding, then you can use
11111 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11112 * @param style the style of the scrollbars. Should be one of
11113 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11114 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11115 * @see #SCROLLBARS_INSIDE_OVERLAY
11116 * @see #SCROLLBARS_INSIDE_INSET
11117 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11118 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011119 *
11120 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011121 */
11122 public void setScrollBarStyle(int style) {
11123 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11124 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011125 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011126 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 }
11128 }
11129
11130 /**
11131 * <p>Returns the current scrollbar style.</p>
11132 * @return the current scrollbar style
11133 * @see #SCROLLBARS_INSIDE_OVERLAY
11134 * @see #SCROLLBARS_INSIDE_INSET
11135 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11136 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011137 *
11138 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011140 @ViewDebug.ExportedProperty(mapping = {
11141 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11142 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11143 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11144 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11145 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 public int getScrollBarStyle() {
11147 return mViewFlags & SCROLLBARS_STYLE_MASK;
11148 }
11149
11150 /**
11151 * <p>Compute the horizontal range that the horizontal scrollbar
11152 * represents.</p>
11153 *
11154 * <p>The range is expressed in arbitrary units that must be the same as the
11155 * units used by {@link #computeHorizontalScrollExtent()} and
11156 * {@link #computeHorizontalScrollOffset()}.</p>
11157 *
11158 * <p>The default range is the drawing width of this view.</p>
11159 *
11160 * @return the total horizontal range represented by the horizontal
11161 * scrollbar
11162 *
11163 * @see #computeHorizontalScrollExtent()
11164 * @see #computeHorizontalScrollOffset()
11165 * @see android.widget.ScrollBarDrawable
11166 */
11167 protected int computeHorizontalScrollRange() {
11168 return getWidth();
11169 }
11170
11171 /**
11172 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11173 * within the horizontal range. This value is used to compute the position
11174 * of the thumb within the scrollbar's track.</p>
11175 *
11176 * <p>The range is expressed in arbitrary units that must be the same as the
11177 * units used by {@link #computeHorizontalScrollRange()} and
11178 * {@link #computeHorizontalScrollExtent()}.</p>
11179 *
11180 * <p>The default offset is the scroll offset of this view.</p>
11181 *
11182 * @return the horizontal offset of the scrollbar's thumb
11183 *
11184 * @see #computeHorizontalScrollRange()
11185 * @see #computeHorizontalScrollExtent()
11186 * @see android.widget.ScrollBarDrawable
11187 */
11188 protected int computeHorizontalScrollOffset() {
11189 return mScrollX;
11190 }
11191
11192 /**
11193 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11194 * within the horizontal range. This value is used to compute the length
11195 * of the thumb within the scrollbar's track.</p>
11196 *
11197 * <p>The range is expressed in arbitrary units that must be the same as the
11198 * units used by {@link #computeHorizontalScrollRange()} and
11199 * {@link #computeHorizontalScrollOffset()}.</p>
11200 *
11201 * <p>The default extent is the drawing width of this view.</p>
11202 *
11203 * @return the horizontal extent of the scrollbar's thumb
11204 *
11205 * @see #computeHorizontalScrollRange()
11206 * @see #computeHorizontalScrollOffset()
11207 * @see android.widget.ScrollBarDrawable
11208 */
11209 protected int computeHorizontalScrollExtent() {
11210 return getWidth();
11211 }
11212
11213 /**
11214 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11215 *
11216 * <p>The range is expressed in arbitrary units that must be the same as the
11217 * units used by {@link #computeVerticalScrollExtent()} and
11218 * {@link #computeVerticalScrollOffset()}.</p>
11219 *
11220 * @return the total vertical range represented by the vertical scrollbar
11221 *
11222 * <p>The default range is the drawing height of this view.</p>
11223 *
11224 * @see #computeVerticalScrollExtent()
11225 * @see #computeVerticalScrollOffset()
11226 * @see android.widget.ScrollBarDrawable
11227 */
11228 protected int computeVerticalScrollRange() {
11229 return getHeight();
11230 }
11231
11232 /**
11233 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11234 * within the horizontal range. This value is used to compute the position
11235 * of the thumb within the scrollbar's track.</p>
11236 *
11237 * <p>The range is expressed in arbitrary units that must be the same as the
11238 * units used by {@link #computeVerticalScrollRange()} and
11239 * {@link #computeVerticalScrollExtent()}.</p>
11240 *
11241 * <p>The default offset is the scroll offset of this view.</p>
11242 *
11243 * @return the vertical offset of the scrollbar's thumb
11244 *
11245 * @see #computeVerticalScrollRange()
11246 * @see #computeVerticalScrollExtent()
11247 * @see android.widget.ScrollBarDrawable
11248 */
11249 protected int computeVerticalScrollOffset() {
11250 return mScrollY;
11251 }
11252
11253 /**
11254 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11255 * within the vertical range. This value is used to compute the length
11256 * of the thumb within the scrollbar's track.</p>
11257 *
11258 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011259 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011260 * {@link #computeVerticalScrollOffset()}.</p>
11261 *
11262 * <p>The default extent is the drawing height of this view.</p>
11263 *
11264 * @return the vertical extent of the scrollbar's thumb
11265 *
11266 * @see #computeVerticalScrollRange()
11267 * @see #computeVerticalScrollOffset()
11268 * @see android.widget.ScrollBarDrawable
11269 */
11270 protected int computeVerticalScrollExtent() {
11271 return getHeight();
11272 }
11273
11274 /**
Adam Powell69159442011-06-13 17:53:06 -070011275 * Check if this view can be scrolled horizontally in a certain direction.
11276 *
11277 * @param direction Negative to check scrolling left, positive to check scrolling right.
11278 * @return true if this view can be scrolled in the specified direction, false otherwise.
11279 */
11280 public boolean canScrollHorizontally(int direction) {
11281 final int offset = computeHorizontalScrollOffset();
11282 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11283 if (range == 0) return false;
11284 if (direction < 0) {
11285 return offset > 0;
11286 } else {
11287 return offset < range - 1;
11288 }
11289 }
11290
11291 /**
11292 * Check if this view can be scrolled vertically in a certain direction.
11293 *
11294 * @param direction Negative to check scrolling up, positive to check scrolling down.
11295 * @return true if this view can be scrolled in the specified direction, false otherwise.
11296 */
11297 public boolean canScrollVertically(int direction) {
11298 final int offset = computeVerticalScrollOffset();
11299 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11300 if (range == 0) return false;
11301 if (direction < 0) {
11302 return offset > 0;
11303 } else {
11304 return offset < range - 1;
11305 }
11306 }
11307
11308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011309 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11310 * scrollbars are painted only if they have been awakened first.</p>
11311 *
11312 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011313 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011314 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011316 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011317 // scrollbars are drawn only when the animation is running
11318 final ScrollabilityCache cache = mScrollCache;
11319 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011320
Mike Cleronf116bf82009-09-27 19:14:12 -070011321 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011322
Mike Cleronf116bf82009-09-27 19:14:12 -070011323 if (state == ScrollabilityCache.OFF) {
11324 return;
11325 }
Joe Malin32736f02011-01-19 16:14:20 -080011326
Mike Cleronf116bf82009-09-27 19:14:12 -070011327 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011328
Mike Cleronf116bf82009-09-27 19:14:12 -070011329 if (state == ScrollabilityCache.FADING) {
11330 // We're fading -- get our fade interpolation
11331 if (cache.interpolatorValues == null) {
11332 cache.interpolatorValues = new float[1];
11333 }
Joe Malin32736f02011-01-19 16:14:20 -080011334
Mike Cleronf116bf82009-09-27 19:14:12 -070011335 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011336
Mike Cleronf116bf82009-09-27 19:14:12 -070011337 // Stops the animation if we're done
11338 if (cache.scrollBarInterpolator.timeToValues(values) ==
11339 Interpolator.Result.FREEZE_END) {
11340 cache.state = ScrollabilityCache.OFF;
11341 } else {
11342 cache.scrollBar.setAlpha(Math.round(values[0]));
11343 }
Joe Malin32736f02011-01-19 16:14:20 -080011344
11345 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011346 // drawing. We only want this when we're fading so that
11347 // we prevent excessive redraws
11348 invalidate = true;
11349 } else {
11350 // We're just on -- but we may have been fading before so
11351 // reset alpha
11352 cache.scrollBar.setAlpha(255);
11353 }
11354
Joe Malin32736f02011-01-19 16:14:20 -080011355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011356 final int viewFlags = mViewFlags;
11357
11358 final boolean drawHorizontalScrollBar =
11359 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11360 final boolean drawVerticalScrollBar =
11361 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11362 && !isVerticalScrollBarHidden();
11363
11364 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11365 final int width = mRight - mLeft;
11366 final int height = mBottom - mTop;
11367
11368 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011369
Mike Reede8853fc2009-09-04 14:01:48 -040011370 final int scrollX = mScrollX;
11371 final int scrollY = mScrollY;
11372 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11373
Mike Cleronf116bf82009-09-27 19:14:12 -070011374 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011377 int size = scrollBar.getSize(false);
11378 if (size <= 0) {
11379 size = cache.scrollBarSize;
11380 }
11381
Mike Cleronf116bf82009-09-27 19:14:12 -070011382 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011383 computeHorizontalScrollOffset(),
11384 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011385 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011386 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011387 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011388 left = scrollX + (mPaddingLeft & inside);
11389 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11390 bottom = top + size;
11391 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11392 if (invalidate) {
11393 invalidate(left, top, right, bottom);
11394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011395 }
11396
11397 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011398 int size = scrollBar.getSize(true);
11399 if (size <= 0) {
11400 size = cache.scrollBarSize;
11401 }
11402
Mike Reede8853fc2009-09-04 14:01:48 -040011403 scrollBar.setParameters(computeVerticalScrollRange(),
11404 computeVerticalScrollOffset(),
11405 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011406 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11407 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11408 verticalScrollbarPosition = isLayoutRtl() ?
11409 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11410 }
11411 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011412 default:
Adam Powell20232d02010-12-08 21:08:53 -080011413 case SCROLLBAR_POSITION_RIGHT:
11414 left = scrollX + width - size - (mUserPaddingRight & inside);
11415 break;
11416 case SCROLLBAR_POSITION_LEFT:
11417 left = scrollX + (mUserPaddingLeft & inside);
11418 break;
11419 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011420 top = scrollY + (mPaddingTop & inside);
11421 right = left + size;
11422 bottom = scrollY + height - (mUserPaddingBottom & inside);
11423 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11424 if (invalidate) {
11425 invalidate(left, top, right, bottom);
11426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011427 }
11428 }
11429 }
11430 }
Romain Guy8506ab42009-06-11 17:35:47 -070011431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011432 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011433 * 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 -080011434 * FastScroller is visible.
11435 * @return whether to temporarily hide the vertical scrollbar
11436 * @hide
11437 */
11438 protected boolean isVerticalScrollBarHidden() {
11439 return false;
11440 }
11441
11442 /**
11443 * <p>Draw the horizontal scrollbar if
11444 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011446 * @param canvas the canvas on which to draw the scrollbar
11447 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011448 *
11449 * @see #isHorizontalScrollBarEnabled()
11450 * @see #computeHorizontalScrollRange()
11451 * @see #computeHorizontalScrollExtent()
11452 * @see #computeHorizontalScrollOffset()
11453 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011454 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011455 */
Romain Guy8fb95422010-08-17 18:38:51 -070011456 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11457 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011458 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011459 scrollBar.draw(canvas);
11460 }
Mike Reede8853fc2009-09-04 14:01:48 -040011461
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11464 * returns true.</p>
11465 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 * @param canvas the canvas on which to draw the scrollbar
11467 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011468 *
11469 * @see #isVerticalScrollBarEnabled()
11470 * @see #computeVerticalScrollRange()
11471 * @see #computeVerticalScrollExtent()
11472 * @see #computeVerticalScrollOffset()
11473 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011474 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011475 */
Romain Guy8fb95422010-08-17 18:38:51 -070011476 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11477 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011478 scrollBar.setBounds(l, t, r, b);
11479 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011480 }
11481
11482 /**
11483 * Implement this to do your drawing.
11484 *
11485 * @param canvas the canvas on which the background will be drawn
11486 */
11487 protected void onDraw(Canvas canvas) {
11488 }
11489
11490 /*
11491 * Caller is responsible for calling requestLayout if necessary.
11492 * (This allows addViewInLayout to not request a new layout.)
11493 */
11494 void assignParent(ViewParent parent) {
11495 if (mParent == null) {
11496 mParent = parent;
11497 } else if (parent == null) {
11498 mParent = null;
11499 } else {
11500 throw new RuntimeException("view " + this + " being added, but"
11501 + " it already has a parent");
11502 }
11503 }
11504
11505 /**
11506 * This is called when the view is attached to a window. At this point it
11507 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011508 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11509 * however it may be called any time before the first onDraw -- including
11510 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 *
11512 * @see #onDetachedFromWindow()
11513 */
11514 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011515 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011516 mParent.requestTransparentRegion(this);
11517 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011518
Dianne Hackborn4702a852012-08-17 15:18:29 -070011519 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011520 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011521 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011522 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011523
Chet Haasea9b61ac2010-12-20 07:40:25 -080011524 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011525
Svetoslav Ganov42138042012-03-20 11:51:39 -070011526 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011527 if (isFocused()) {
11528 InputMethodManager imm = InputMethodManager.peekInstance();
11529 imm.focusIn(this);
11530 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011531
11532 if (mAttachInfo != null && mDisplayList != null) {
11533 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11534 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011535 }
Cibu Johny86666632010-02-22 13:01:02 -080011536
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011537 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011538 * Resolve all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011539 */
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011540 void resolveRtlPropertiesIfNeeded() {
11541 if (!needRtlPropertiesResolution()) return;
11542
11543 // Order is important here: LayoutDirection MUST be resolved first
11544 if (!isLayoutDirectionResolved()) {
11545 resolveLayoutDirection();
11546 resolveLayoutParams();
11547 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011548 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011549 if (!isTextDirectionResolved()) {
11550 resolveTextDirection();
11551 }
11552 if (!isTextAlignmentResolved()) {
11553 resolveTextAlignment();
11554 }
11555 if (!isPaddingResolved()) {
11556 resolvePadding();
11557 }
11558 if (!isDrawablesResolved()) {
11559 resolveDrawables();
11560 }
11561 requestLayout();
11562 invalidate(true);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011563 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011564 }
11565
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011566 // Reset resolution of all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011567 void resetRtlProperties() {
11568 resetResolvedLayoutDirection();
11569 resetResolvedTextDirection();
11570 resetResolvedTextAlignment();
11571 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011572 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011573 }
11574
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011575 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011576 * @see #onScreenStateChanged(int)
11577 */
11578 void dispatchScreenStateChanged(int screenState) {
11579 onScreenStateChanged(screenState);
11580 }
11581
11582 /**
11583 * This method is called whenever the state of the screen this view is
11584 * attached to changes. A state change will usually occurs when the screen
11585 * turns on or off (whether it happens automatically or the user does it
11586 * manually.)
11587 *
11588 * @param screenState The new state of the screen. Can be either
11589 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11590 */
11591 public void onScreenStateChanged(int screenState) {
11592 }
11593
11594 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011595 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11596 */
11597 private boolean hasRtlSupport() {
11598 return mContext.getApplicationInfo().hasRtlSupport();
11599 }
11600
11601 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011602 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11603 * RTL not supported)
11604 */
11605 private boolean isRtlCompatibilityMode() {
11606 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11607 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11608 }
11609
11610 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011611 * @return true if RTL properties need resolution.
11612 */
11613 private boolean needRtlPropertiesResolution() {
11614 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11615 }
11616
11617 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011618 * Called when any RTL property (layout direction or text direction or text alignment) has
11619 * been changed.
11620 *
11621 * Subclasses need to override this method to take care of cached information that depends on the
11622 * resolved layout direction, or to inform child views that inherit their layout direction.
11623 *
11624 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011625 *
11626 * @param layoutDirection the direction of the layout
11627 *
11628 * @see #LAYOUT_DIRECTION_LTR
11629 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011630 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011631 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011632 }
11633
11634 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011635 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11636 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011637 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011638 * @return true if resolution has been done, false otherwise.
11639 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011640 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011641 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011642 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011643 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011644 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011645
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011646 if (hasRtlSupport()) {
11647 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011648 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11649 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011650 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011651 // We cannot resolve yet. LTR is by default and let the resolution happen again
11652 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011653 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011654
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011655 View parent = ((View) mParent);
11656 // Parent has not yet resolved, LTR is still the default
11657 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011658
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011659 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011660 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011661 }
11662 break;
11663 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011664 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011665 break;
11666 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011667 if((LAYOUT_DIRECTION_RTL ==
11668 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011669 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011670 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011671 break;
11672 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011673 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011674 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011675 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011676
11677 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011678 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011679 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011680 }
11681
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011682 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011683 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011684 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011685 * @return true if layout direction resolution can be done otherwise return false.
11686 *
11687 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011688 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011689 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011690 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011691 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011692 return (mParent != null) && (mParent instanceof ViewGroup) &&
11693 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011694 default:
11695 return true;
11696 }
11697 }
11698
11699 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011700 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11701 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011702 *
11703 * @hide
11704 */
11705 public void resetResolvedLayoutDirection() {
11706 // Reset the current resolved bits
11707 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011708 }
11709
11710 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011711 * @return true if the layout direction is inherited.
11712 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011713 * @hide
11714 */
11715 public boolean isLayoutDirectionInherited() {
11716 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11717 }
11718
11719 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011720 * @return true if layout direction has been resolved.
11721 */
11722 private boolean isLayoutDirectionResolved() {
11723 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11724 }
11725
11726 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011727 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011728 *
11729 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011730 */
11731 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011732 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011733 }
11734
11735 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011736 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011737 *
11738 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011739 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011740 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011741 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011742 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11743 // If start / end padding are defined, they will be resolved (hence overriding) to
11744 // left / right or right / left depending on the resolved layout direction.
11745 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011746 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011747 // Set user padding to initial values ...
11748 mUserPaddingLeft = (mUserPaddingLeftInitial == UNDEFINED_PADDING) ?
11749 0 : mUserPaddingLeftInitial;
11750 mUserPaddingRight = (mUserPaddingRightInitial == UNDEFINED_PADDING) ?
11751 0 : mUserPaddingRightInitial;
11752 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011753 switch (resolvedLayoutDirection) {
11754 case LAYOUT_DIRECTION_RTL:
11755 if (mUserPaddingStart != UNDEFINED_PADDING) {
11756 mUserPaddingRight = mUserPaddingStart;
11757 }
11758 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11759 mUserPaddingLeft = mUserPaddingEnd;
11760 }
11761 break;
11762 case LAYOUT_DIRECTION_LTR:
11763 default:
11764 if (mUserPaddingStart != UNDEFINED_PADDING) {
11765 mUserPaddingLeft = mUserPaddingStart;
11766 }
11767 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11768 mUserPaddingRight = mUserPaddingEnd;
11769 }
11770 }
11771
11772 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11773
11774 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11775 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011776 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011777 }
11778
Dianne Hackborn4702a852012-08-17 15:18:29 -070011779 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011780 }
11781
11782 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011783 * Reset the resolved layout direction.
11784 *
11785 * @hide
11786 */
11787 public void resetResolvedPadding() {
11788 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11789 }
11790
11791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011792 * This is called when the view is detached from a window. At this point it
11793 * no longer has a surface for drawing.
11794 *
11795 * @see #onAttachedToWindow()
11796 */
11797 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011798 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011799
Romain Guya440b002010-02-24 15:57:54 -080011800 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011801 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011802 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011803 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011805 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011806
Romain Guya998dff2012-03-23 18:58:36 -070011807 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011808
11809 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011810 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011811 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011812 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011813 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011814 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011815 // Should never happen
11816 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011817 }
11818
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011819 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011820
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011821 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011822 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011823 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011824 }
11825
11826 /**
11827 * @return The number of times this view has been attached to a window
11828 */
11829 protected int getWindowAttachCount() {
11830 return mWindowAttachCount;
11831 }
11832
11833 /**
11834 * Retrieve a unique token identifying the window this view is attached to.
11835 * @return Return the window's token for use in
11836 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11837 */
11838 public IBinder getWindowToken() {
11839 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11840 }
11841
11842 /**
11843 * Retrieve a unique token identifying the top-level "real" window of
11844 * the window that this view is attached to. That is, this is like
11845 * {@link #getWindowToken}, except if the window this view in is a panel
11846 * window (attached to another containing window), then the token of
11847 * the containing window is returned instead.
11848 *
11849 * @return Returns the associated window token, either
11850 * {@link #getWindowToken()} or the containing window's token.
11851 */
11852 public IBinder getApplicationWindowToken() {
11853 AttachInfo ai = mAttachInfo;
11854 if (ai != null) {
11855 IBinder appWindowToken = ai.mPanelParentWindowToken;
11856 if (appWindowToken == null) {
11857 appWindowToken = ai.mWindowToken;
11858 }
11859 return appWindowToken;
11860 }
11861 return null;
11862 }
11863
11864 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011865 * Gets the logical display to which the view's window has been attached.
11866 *
11867 * @return The logical display, or null if the view is not currently attached to a window.
11868 */
11869 public Display getDisplay() {
11870 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11871 }
11872
11873 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 * Retrieve private session object this view hierarchy is using to
11875 * communicate with the window manager.
11876 * @return the session object to communicate with the window manager
11877 */
11878 /*package*/ IWindowSession getWindowSession() {
11879 return mAttachInfo != null ? mAttachInfo.mSession : null;
11880 }
11881
11882 /**
11883 * @param info the {@link android.view.View.AttachInfo} to associated with
11884 * this view
11885 */
11886 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11887 //System.out.println("Attached! " + this);
11888 mAttachInfo = info;
11889 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011890 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011891 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 if (mFloatingTreeObserver != null) {
11893 info.mTreeObserver.merge(mFloatingTreeObserver);
11894 mFloatingTreeObserver = null;
11895 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011896 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011898 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011899 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011900 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011902
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011903 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011904 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011905 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011906 if (listeners != null && listeners.size() > 0) {
11907 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11908 // perform the dispatching. The iterator is a safe guard against listeners that
11909 // could mutate the list by calling the various add/remove methods. This prevents
11910 // the array from being modified while we iterate it.
11911 for (OnAttachStateChangeListener listener : listeners) {
11912 listener.onViewAttachedToWindow(this);
11913 }
11914 }
11915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 int vis = info.mWindowVisibility;
11917 if (vis != GONE) {
11918 onWindowVisibilityChanged(vis);
11919 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011920 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011921 // If nobody has evaluated the drawable state yet, then do it now.
11922 refreshDrawableState();
11923 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011924 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 }
11926
11927 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011928 AttachInfo info = mAttachInfo;
11929 if (info != null) {
11930 int vis = info.mWindowVisibility;
11931 if (vis != GONE) {
11932 onWindowVisibilityChanged(GONE);
11933 }
11934 }
11935
11936 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011937
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011938 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011939 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011940 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011941 if (listeners != null && listeners.size() > 0) {
11942 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11943 // perform the dispatching. The iterator is a safe guard against listeners that
11944 // could mutate the list by calling the various add/remove methods. This prevents
11945 // the array from being modified while we iterate it.
11946 for (OnAttachStateChangeListener listener : listeners) {
11947 listener.onViewDetachedFromWindow(this);
11948 }
11949 }
11950
Dianne Hackborn4702a852012-08-17 15:18:29 -070011951 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011952 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011953 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011954 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011956 mAttachInfo = null;
11957 }
11958
11959 /**
11960 * Store this view hierarchy's frozen state into the given container.
11961 *
11962 * @param container The SparseArray in which to save the view's state.
11963 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011964 * @see #restoreHierarchyState(android.util.SparseArray)
11965 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11966 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011967 */
11968 public void saveHierarchyState(SparseArray<Parcelable> container) {
11969 dispatchSaveInstanceState(container);
11970 }
11971
11972 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011973 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11974 * this view and its children. May be overridden to modify how freezing happens to a
11975 * 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 -080011976 *
11977 * @param container The SparseArray in which to save the view's state.
11978 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011979 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11980 * @see #saveHierarchyState(android.util.SparseArray)
11981 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011982 */
11983 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11984 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011985 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011987 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011988 throw new IllegalStateException(
11989 "Derived class did not call super.onSaveInstanceState()");
11990 }
11991 if (state != null) {
11992 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11993 // + ": " + state);
11994 container.put(mID, state);
11995 }
11996 }
11997 }
11998
11999 /**
12000 * Hook allowing a view to generate a representation of its internal state
12001 * that can later be used to create a new instance with that same state.
12002 * This state should only contain information that is not persistent or can
12003 * not be reconstructed later. For example, you will never store your
12004 * current position on screen because that will be computed again when a
12005 * new instance of the view is placed in its view hierarchy.
12006 * <p>
12007 * Some examples of things you may store here: the current cursor position
12008 * in a text view (but usually not the text itself since that is stored in a
12009 * content provider or other persistent storage), the currently selected
12010 * item in a list view.
12011 *
12012 * @return Returns a Parcelable object containing the view's current dynamic
12013 * state, or null if there is nothing interesting to save. The
12014 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012015 * @see #onRestoreInstanceState(android.os.Parcelable)
12016 * @see #saveHierarchyState(android.util.SparseArray)
12017 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012018 * @see #setSaveEnabled(boolean)
12019 */
12020 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012021 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012022 return BaseSavedState.EMPTY_STATE;
12023 }
12024
12025 /**
12026 * Restore this view hierarchy's frozen state from the given container.
12027 *
12028 * @param container The SparseArray which holds previously frozen states.
12029 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012030 * @see #saveHierarchyState(android.util.SparseArray)
12031 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12032 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012033 */
12034 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12035 dispatchRestoreInstanceState(container);
12036 }
12037
12038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012039 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12040 * state for this view and its children. May be overridden to modify how restoring
12041 * happens to a view's children; for example, some views may want to not store state
12042 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 *
12044 * @param container The SparseArray which holds previously saved state.
12045 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012046 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12047 * @see #restoreHierarchyState(android.util.SparseArray)
12048 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 */
12050 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12051 if (mID != NO_ID) {
12052 Parcelable state = container.get(mID);
12053 if (state != null) {
12054 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12055 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012056 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012057 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012058 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012059 throw new IllegalStateException(
12060 "Derived class did not call super.onRestoreInstanceState()");
12061 }
12062 }
12063 }
12064 }
12065
12066 /**
12067 * Hook allowing a view to re-apply a representation of its internal state that had previously
12068 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12069 * null state.
12070 *
12071 * @param state The frozen state that had previously been returned by
12072 * {@link #onSaveInstanceState}.
12073 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012074 * @see #onSaveInstanceState()
12075 * @see #restoreHierarchyState(android.util.SparseArray)
12076 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077 */
12078 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012079 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012080 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012081 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12082 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012083 + "when two views of different type have the same id in the same hierarchy. "
12084 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012085 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 }
12087 }
12088
12089 /**
12090 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12091 *
12092 * @return the drawing start time in milliseconds
12093 */
12094 public long getDrawingTime() {
12095 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12096 }
12097
12098 /**
12099 * <p>Enables or disables the duplication of the parent's state into this view. When
12100 * duplication is enabled, this view gets its drawable state from its parent rather
12101 * than from its own internal properties.</p>
12102 *
12103 * <p>Note: in the current implementation, setting this property to true after the
12104 * view was added to a ViewGroup might have no effect at all. This property should
12105 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12106 *
12107 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12108 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012109 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012110 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12111 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112 *
12113 * @param enabled True to enable duplication of the parent's drawable state, false
12114 * to disable it.
12115 *
12116 * @see #getDrawableState()
12117 * @see #isDuplicateParentStateEnabled()
12118 */
12119 public void setDuplicateParentStateEnabled(boolean enabled) {
12120 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12121 }
12122
12123 /**
12124 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12125 *
12126 * @return True if this view's drawable state is duplicated from the parent,
12127 * false otherwise
12128 *
12129 * @see #getDrawableState()
12130 * @see #setDuplicateParentStateEnabled(boolean)
12131 */
12132 public boolean isDuplicateParentStateEnabled() {
12133 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12134 }
12135
12136 /**
Romain Guy171c5922011-01-06 10:04:23 -080012137 * <p>Specifies the type of layer backing this view. The layer can be
12138 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12139 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012140 *
Romain Guy171c5922011-01-06 10:04:23 -080012141 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12142 * instance that controls how the layer is composed on screen. The following
12143 * properties of the paint are taken into account when composing the layer:</p>
12144 * <ul>
12145 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12146 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12147 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12148 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012149 *
Romain Guy171c5922011-01-06 10:04:23 -080012150 * <p>If this view has an alpha value set to < 1.0 by calling
12151 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12152 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12153 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012154 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012155 *
Romain Guy171c5922011-01-06 10:04:23 -080012156 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12157 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12158 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012159 *
Chet Haased15ebf22012-09-05 11:40:29 -070012160 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012161 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12162 * {@link #LAYER_TYPE_HARDWARE}
12163 * @param paint The paint used to compose the layer. This argument is optional
12164 * and can be null. It is ignored when the layer type is
12165 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012166 *
12167 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012168 * @see #LAYER_TYPE_NONE
12169 * @see #LAYER_TYPE_SOFTWARE
12170 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012171 * @see #setAlpha(float)
12172 *
Romain Guy171c5922011-01-06 10:04:23 -080012173 * @attr ref android.R.styleable#View_layerType
12174 */
12175 public void setLayerType(int layerType, Paint paint) {
12176 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012177 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012178 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12179 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012180
Romain Guyd6cd5722011-01-17 14:42:41 -080012181 if (layerType == mLayerType) {
12182 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12183 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012184 invalidateParentCaches();
12185 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012186 }
12187 return;
12188 }
Romain Guy171c5922011-01-06 10:04:23 -080012189
12190 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012191 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012192 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012193 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012194 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012195 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012196 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012197 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012198 default:
12199 break;
Romain Guy171c5922011-01-06 10:04:23 -080012200 }
12201
12202 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012203 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12204 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12205 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012206
Romain Guy0fd89bf2011-01-26 15:41:30 -080012207 invalidateParentCaches();
12208 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012209 }
12210
12211 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012212 * Updates the {@link Paint} object used with the current layer (used only if the current
12213 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12214 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12215 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12216 * ensure that the view gets redrawn immediately.
12217 *
12218 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12219 * instance that controls how the layer is composed on screen. The following
12220 * properties of the paint are taken into account when composing the layer:</p>
12221 * <ul>
12222 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12223 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12224 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12225 * </ul>
12226 *
12227 * <p>If this view has an alpha value set to < 1.0 by calling
12228 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12229 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12230 * equivalent to setting a hardware layer on this view and providing a paint with
12231 * the desired alpha value.</p>
12232 *
12233 * @param paint The paint used to compose the layer. This argument is optional
12234 * and can be null. It is ignored when the layer type is
12235 * {@link #LAYER_TYPE_NONE}
12236 *
12237 * @see #setLayerType(int, android.graphics.Paint)
12238 */
12239 public void setLayerPaint(Paint paint) {
12240 int layerType = getLayerType();
12241 if (layerType != LAYER_TYPE_NONE) {
12242 mLayerPaint = paint == null ? new Paint() : paint;
12243 if (layerType == LAYER_TYPE_HARDWARE) {
12244 HardwareLayer layer = getHardwareLayer();
12245 if (layer != null) {
12246 layer.setLayerPaint(paint);
12247 }
12248 invalidateViewProperty(false, false);
12249 } else {
12250 invalidate();
12251 }
12252 }
12253 }
12254
12255 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012256 * Indicates whether this view has a static layer. A view with layer type
12257 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12258 * dynamic.
12259 */
12260 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012261 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012262 }
12263
12264 /**
Romain Guy171c5922011-01-06 10:04:23 -080012265 * Indicates what type of layer is currently associated with this view. By default
12266 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12267 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12268 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012269 *
Romain Guy171c5922011-01-06 10:04:23 -080012270 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12271 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012272 *
12273 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012274 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012275 * @see #LAYER_TYPE_NONE
12276 * @see #LAYER_TYPE_SOFTWARE
12277 * @see #LAYER_TYPE_HARDWARE
12278 */
12279 public int getLayerType() {
12280 return mLayerType;
12281 }
Joe Malin32736f02011-01-19 16:14:20 -080012282
Romain Guy6c319ca2011-01-11 14:29:25 -080012283 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012284 * Forces this view's layer to be created and this view to be rendered
12285 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12286 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012287 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012288 * This method can for instance be used to render a view into its layer before
12289 * starting an animation. If this view is complex, rendering into the layer
12290 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012291 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012292 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012293 *
12294 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012295 */
12296 public void buildLayer() {
12297 if (mLayerType == LAYER_TYPE_NONE) return;
12298
12299 if (mAttachInfo == null) {
12300 throw new IllegalStateException("This view must be attached to a window first");
12301 }
12302
12303 switch (mLayerType) {
12304 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012305 if (mAttachInfo.mHardwareRenderer != null &&
12306 mAttachInfo.mHardwareRenderer.isEnabled() &&
12307 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012308 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012309 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012310 break;
12311 case LAYER_TYPE_SOFTWARE:
12312 buildDrawingCache(true);
12313 break;
12314 }
12315 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012316
Romain Guyf1ae1062011-03-02 18:16:04 -080012317 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012318 * <p>Returns a hardware layer that can be used to draw this view again
12319 * without executing its draw method.</p>
12320 *
12321 * @return A HardwareLayer ready to render, or null if an error occurred.
12322 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012323 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012324 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12325 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012326 return null;
12327 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012328
Romain Guy9c4b79a2011-11-10 19:23:58 -080012329 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012330
12331 final int width = mRight - mLeft;
12332 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012333
Romain Guy6c319ca2011-01-11 14:29:25 -080012334 if (width == 0 || height == 0) {
12335 return null;
12336 }
12337
Dianne Hackborn4702a852012-08-17 15:18:29 -070012338 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012339 if (mHardwareLayer == null) {
12340 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12341 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012342 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012343 } else {
12344 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012345 if (mHardwareLayer.resize(width, height)) {
12346 mLocalDirtyRect.set(0, 0, width, height);
12347 }
Romain Guy846a5332012-07-11 17:44:57 -070012348 }
12349
12350 // This should not be necessary but applications that change
12351 // the parameters of their background drawable without calling
12352 // this.setBackground(Drawable) can leave the view in a bad state
12353 // (for instance isOpaque() returns true, but the background is
12354 // not opaque.)
12355 computeOpaqueFlags();
12356
12357 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012358 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012359 mHardwareLayer.setOpaque(opaque);
12360 mLocalDirtyRect.set(0, 0, width, height);
12361 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012362 }
12363
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012364 // The layer is not valid if the underlying GPU resources cannot be allocated
12365 if (!mHardwareLayer.isValid()) {
12366 return null;
12367 }
12368
Romain Guy11cb6422012-09-21 00:39:43 -070012369 mHardwareLayer.setLayerPaint(mLayerPaint);
12370 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12371 ViewRootImpl viewRoot = getViewRootImpl();
12372 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12373
Michael Jurka7e52caf2012-03-06 15:57:06 -080012374 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012375 }
12376
12377 return mHardwareLayer;
12378 }
Romain Guy171c5922011-01-06 10:04:23 -080012379
Romain Guy589b0bb2011-10-10 13:57:47 -070012380 /**
12381 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012382 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012383 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012384 *
12385 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012386 * @see #LAYER_TYPE_HARDWARE
12387 */
Romain Guya998dff2012-03-23 18:58:36 -070012388 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012389 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012390 AttachInfo info = mAttachInfo;
12391 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012392 info.mHardwareRenderer.isEnabled() &&
12393 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012394 mHardwareLayer.destroy();
12395 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012396
Romain Guy9c4b79a2011-11-10 19:23:58 -080012397 invalidate(true);
12398 invalidateParentCaches();
12399 }
Romain Guy65b345f2011-07-27 18:51:50 -070012400 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012401 }
Romain Guy65b345f2011-07-27 18:51:50 -070012402 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012403 }
12404
Romain Guy171c5922011-01-06 10:04:23 -080012405 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012406 * Destroys all hardware rendering resources. This method is invoked
12407 * when the system needs to reclaim resources. Upon execution of this
12408 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012409 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012410 * Note: you <strong>must</strong> call
12411 * <code>super.destroyHardwareResources()</code> when overriding
12412 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012413 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012414 * @hide
12415 */
12416 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012417 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012418 }
12419
12420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12422 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12423 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12424 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12425 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12426 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012427 *
Romain Guy171c5922011-01-06 10:04:23 -080012428 * <p>Enabling the drawing cache is similar to
12429 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012430 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12431 * drawing cache has no effect on rendering because the system uses a different mechanism
12432 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12433 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12434 * for information on how to enable software and hardware layers.</p>
12435 *
12436 * <p>This API can be used to manually generate
12437 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12438 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012439 *
12440 * @param enabled true to enable the drawing cache, false otherwise
12441 *
12442 * @see #isDrawingCacheEnabled()
12443 * @see #getDrawingCache()
12444 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012445 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012446 */
12447 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012448 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012449 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12450 }
12451
12452 /**
12453 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12454 *
12455 * @return true if the drawing cache is enabled
12456 *
12457 * @see #setDrawingCacheEnabled(boolean)
12458 * @see #getDrawingCache()
12459 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012460 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 public boolean isDrawingCacheEnabled() {
12462 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12463 }
12464
12465 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012466 * Debugging utility which recursively outputs the dirty state of a view and its
12467 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012468 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012469 * @hide
12470 */
Romain Guy676b1732011-02-14 14:45:33 -080012471 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012472 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012473 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12474 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12475 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12476 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012477 if (clear) {
12478 mPrivateFlags &= clearMask;
12479 }
12480 if (this instanceof ViewGroup) {
12481 ViewGroup parent = (ViewGroup) this;
12482 final int count = parent.getChildCount();
12483 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012484 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012485 child.outputDirtyFlags(indent + " ", clear, clearMask);
12486 }
12487 }
12488 }
12489
12490 /**
12491 * This method is used by ViewGroup to cause its children to restore or recreate their
12492 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12493 * to recreate its own display list, which would happen if it went through the normal
12494 * draw/dispatchDraw mechanisms.
12495 *
12496 * @hide
12497 */
12498 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012499
12500 /**
12501 * A view that is not attached or hardware accelerated cannot create a display list.
12502 * This method checks these conditions and returns the appropriate result.
12503 *
12504 * @return true if view has the ability to create a display list, false otherwise.
12505 *
12506 * @hide
12507 */
12508 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012509 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012510 }
Joe Malin32736f02011-01-19 16:14:20 -080012511
Chet Haasedaf98e92011-01-10 14:10:36 -080012512 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012513 * @return The HardwareRenderer associated with that view or null if hardware rendering
12514 * is not supported or this this has not been attached to a window.
12515 *
12516 * @hide
12517 */
12518 public HardwareRenderer getHardwareRenderer() {
12519 if (mAttachInfo != null) {
12520 return mAttachInfo.mHardwareRenderer;
12521 }
12522 return null;
12523 }
12524
12525 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012526 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12527 * Otherwise, the same display list will be returned (after having been rendered into
12528 * along the way, depending on the invalidation state of the view).
12529 *
12530 * @param displayList The previous version of this displayList, could be null.
12531 * @param isLayer Whether the requester of the display list is a layer. If so,
12532 * the view will avoid creating a layer inside the resulting display list.
12533 * @return A new or reused DisplayList object.
12534 */
12535 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12536 if (!canHaveDisplayList()) {
12537 return null;
12538 }
12539
Dianne Hackborn4702a852012-08-17 15:18:29 -070012540 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012541 displayList == null || !displayList.isValid() ||
12542 (!isLayer && mRecreateDisplayList))) {
12543 // Don't need to recreate the display list, just need to tell our
12544 // children to restore/recreate theirs
12545 if (displayList != null && displayList.isValid() &&
12546 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012547 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12548 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012549 dispatchGetDisplayList();
12550
12551 return displayList;
12552 }
12553
12554 if (!isLayer) {
12555 // If we got here, we're recreating it. Mark it as such to ensure that
12556 // we copy in child display lists into ours in drawChild()
12557 mRecreateDisplayList = true;
12558 }
12559 if (displayList == null) {
12560 final String name = getClass().getSimpleName();
12561 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12562 // If we're creating a new display list, make sure our parent gets invalidated
12563 // since they will need to recreate their display list to account for this
12564 // new child display list.
12565 invalidateParentCaches();
12566 }
12567
12568 boolean caching = false;
12569 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012570 int width = mRight - mLeft;
12571 int height = mBottom - mTop;
12572
12573 try {
12574 canvas.setViewport(width, height);
12575 // The dirty rect should always be null for a display list
12576 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012577 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012578 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012579 if (layerType == LAYER_TYPE_HARDWARE) {
12580 final HardwareLayer layer = getHardwareLayer();
12581 if (layer != null && layer.isValid()) {
12582 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12583 } else {
12584 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12585 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12586 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12587 }
12588 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012589 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012590 buildDrawingCache(true);
12591 Bitmap cache = getDrawingCache(true);
12592 if (cache != null) {
12593 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12594 caching = true;
12595 }
Chet Haasea1cff502012-02-21 13:43:44 -080012596 }
Chet Haasea1cff502012-02-21 13:43:44 -080012597 } else {
12598
12599 computeScroll();
12600
Chet Haasea1cff502012-02-21 13:43:44 -080012601 canvas.translate(-mScrollX, -mScrollY);
12602 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012603 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12604 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012605 }
12606
12607 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012608 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012609 dispatchDraw(canvas);
12610 } else {
12611 draw(canvas);
12612 }
12613 }
12614 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012615 canvas.onPostDraw();
12616
12617 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012618 displayList.setCaching(caching);
12619 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012620 displayList.setLeftTopRightBottom(0, 0, width, height);
12621 } else {
12622 setDisplayListProperties(displayList);
12623 }
12624 }
12625 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012626 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12627 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012628 }
12629
12630 return displayList;
12631 }
12632
12633 /**
12634 * Get the DisplayList for the HardwareLayer
12635 *
12636 * @param layer The HardwareLayer whose DisplayList we want
12637 * @return A DisplayList fopr the specified HardwareLayer
12638 */
12639 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12640 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12641 layer.setDisplayList(displayList);
12642 return displayList;
12643 }
12644
12645
12646 /**
Romain Guyb051e892010-09-28 19:09:36 -070012647 * <p>Returns a display list that can be used to draw this view again
12648 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012649 *
Romain Guyb051e892010-09-28 19:09:36 -070012650 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012651 *
12652 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012653 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012654 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012655 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012656 return mDisplayList;
12657 }
12658
Romain Guy38c2ece2012-05-24 14:20:56 -070012659 private void clearDisplayList() {
12660 if (mDisplayList != null) {
12661 mDisplayList.invalidate();
12662 mDisplayList.clear();
12663 }
12664 }
12665
Romain Guyb051e892010-09-28 19:09:36 -070012666 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012667 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012668 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012669 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012670 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012671 * @see #getDrawingCache(boolean)
12672 */
12673 public Bitmap getDrawingCache() {
12674 return getDrawingCache(false);
12675 }
12676
12677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012678 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12679 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12680 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12681 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12682 * request the drawing cache by calling this method and draw it on screen if the
12683 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012684 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012685 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12686 * this method will create a bitmap of the same size as this view. Because this bitmap
12687 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12688 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12689 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12690 * size than the view. This implies that your application must be able to handle this
12691 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012692 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012693 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12694 * the current density of the screen when the application is in compatibility
12695 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012697 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012699 * @see #setDrawingCacheEnabled(boolean)
12700 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012701 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012702 * @see #destroyDrawingCache()
12703 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012704 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012705 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12706 return null;
12707 }
12708 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012709 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 }
Romain Guy02890fd2010-08-06 17:58:44 -070012711 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 }
12713
12714 /**
12715 * <p>Frees the resources used by the drawing cache. If you call
12716 * {@link #buildDrawingCache()} manually without calling
12717 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12718 * should cleanup the cache with this method afterwards.</p>
12719 *
12720 * @see #setDrawingCacheEnabled(boolean)
12721 * @see #buildDrawingCache()
12722 * @see #getDrawingCache()
12723 */
12724 public void destroyDrawingCache() {
12725 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012726 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012727 mDrawingCache = null;
12728 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012729 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012730 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012731 mUnscaledDrawingCache = null;
12732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 }
12734
12735 /**
12736 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012737 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012738 * view will always be drawn on top of a solid color.
12739 *
12740 * @param color The background color to use for the drawing cache's bitmap
12741 *
12742 * @see #setDrawingCacheEnabled(boolean)
12743 * @see #buildDrawingCache()
12744 * @see #getDrawingCache()
12745 */
12746 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012747 if (color != mDrawingCacheBackgroundColor) {
12748 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012749 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 }
12752
12753 /**
12754 * @see #setDrawingCacheBackgroundColor(int)
12755 *
12756 * @return The background color to used for the drawing cache's bitmap
12757 */
12758 public int getDrawingCacheBackgroundColor() {
12759 return mDrawingCacheBackgroundColor;
12760 }
12761
12762 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012763 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012764 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012765 * @see #buildDrawingCache(boolean)
12766 */
12767 public void buildDrawingCache() {
12768 buildDrawingCache(false);
12769 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012770
Romain Guyfbd8f692009-06-26 14:51:58 -070012771 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012772 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12773 *
12774 * <p>If you call {@link #buildDrawingCache()} manually without calling
12775 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12776 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012777 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012778 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12779 * this method will create a bitmap of the same size as this view. Because this bitmap
12780 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12781 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12782 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12783 * size than the view. This implies that your application must be able to handle this
12784 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012785 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012786 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12787 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012788 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012789 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 *
12791 * @see #getDrawingCache()
12792 * @see #destroyDrawingCache()
12793 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012794 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012795 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012796 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012797 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012798
Romain Guy8506ab42009-06-11 17:35:47 -070012799 int width = mRight - mLeft;
12800 int height = mBottom - mTop;
12801
12802 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012803 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012804
Romain Guye1123222009-06-29 14:24:56 -070012805 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012806 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12807 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012809
12810 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012811 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012812 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012813
Chris Craik10e9d1d2012-09-06 14:42:44 -070012814 final int projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12815 final int drawingCacheSize =
12816 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12817 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012818 if (width > 0 && height > 0) {
12819 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12820 + projectedBitmapSize + " bytes, only "
12821 + drawingCacheSize + " available");
12822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012823 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012824 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012825 return;
12826 }
12827
12828 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012829 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012830
12831 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012832 Bitmap.Config quality;
12833 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012834 // Never pick ARGB_4444 because it looks awful
12835 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12837 case DRAWING_CACHE_QUALITY_AUTO:
12838 quality = Bitmap.Config.ARGB_8888;
12839 break;
12840 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012841 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842 break;
12843 case DRAWING_CACHE_QUALITY_HIGH:
12844 quality = Bitmap.Config.ARGB_8888;
12845 break;
12846 default:
12847 quality = Bitmap.Config.ARGB_8888;
12848 break;
12849 }
12850 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012851 // Optimization for translucent windows
12852 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012853 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 }
12855
12856 // Try to cleanup memory
12857 if (bitmap != null) bitmap.recycle();
12858
12859 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012860 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12861 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012862 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012863 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012864 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012865 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012866 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012867 }
Adam Powell26153a32010-11-08 15:22:27 -080012868 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012869 } catch (OutOfMemoryError e) {
12870 // If there is not enough memory to create the bitmap cache, just
12871 // ignore the issue as bitmap caches are not required to draw the
12872 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012873 if (autoScale) {
12874 mDrawingCache = null;
12875 } else {
12876 mUnscaledDrawingCache = null;
12877 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012878 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012879 return;
12880 }
12881
12882 clear = drawingCacheBackgroundColor != 0;
12883 }
12884
12885 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012886 if (attachInfo != null) {
12887 canvas = attachInfo.mCanvas;
12888 if (canvas == null) {
12889 canvas = new Canvas();
12890 }
12891 canvas.setBitmap(bitmap);
12892 // Temporarily clobber the cached Canvas in case one of our children
12893 // is also using a drawing cache. Without this, the children would
12894 // steal the canvas by attaching their own bitmap to it and bad, bad
12895 // thing would happen (invisible views, corrupted drawings, etc.)
12896 attachInfo.mCanvas = null;
12897 } else {
12898 // This case should hopefully never or seldom happen
12899 canvas = new Canvas(bitmap);
12900 }
12901
12902 if (clear) {
12903 bitmap.eraseColor(drawingCacheBackgroundColor);
12904 }
12905
12906 computeScroll();
12907 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012908
Romain Guye1123222009-06-29 14:24:56 -070012909 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012910 final float scale = attachInfo.mApplicationScale;
12911 canvas.scale(scale, scale);
12912 }
Joe Malin32736f02011-01-19 16:14:20 -080012913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012914 canvas.translate(-mScrollX, -mScrollY);
12915
Dianne Hackborn4702a852012-08-17 15:18:29 -070012916 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012917 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12918 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012919 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012921
12922 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012923 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12924 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012925 dispatchDraw(canvas);
12926 } else {
12927 draw(canvas);
12928 }
12929
12930 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012931 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012932
12933 if (attachInfo != null) {
12934 // Restore the cached Canvas for our siblings
12935 attachInfo.mCanvas = canvas;
12936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 }
12938 }
12939
12940 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012941 * Create a snapshot of the view into a bitmap. We should probably make
12942 * some form of this public, but should think about the API.
12943 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012944 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012945 int width = mRight - mLeft;
12946 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012947
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012948 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012949 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012950 width = (int) ((width * scale) + 0.5f);
12951 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012952
Dianne Hackborndde331c2012-08-03 14:01:57 -070012953 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12954 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012955 if (bitmap == null) {
12956 throw new OutOfMemoryError();
12957 }
12958
Romain Guyc529d8d2011-09-06 15:01:39 -070012959 Resources resources = getResources();
12960 if (resources != null) {
12961 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12962 }
Joe Malin32736f02011-01-19 16:14:20 -080012963
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012964 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012965 if (attachInfo != null) {
12966 canvas = attachInfo.mCanvas;
12967 if (canvas == null) {
12968 canvas = new Canvas();
12969 }
12970 canvas.setBitmap(bitmap);
12971 // Temporarily clobber the cached Canvas in case one of our children
12972 // is also using a drawing cache. Without this, the children would
12973 // steal the canvas by attaching their own bitmap to it and bad, bad
12974 // things would happen (invisible views, corrupted drawings, etc.)
12975 attachInfo.mCanvas = null;
12976 } else {
12977 // This case should hopefully never or seldom happen
12978 canvas = new Canvas(bitmap);
12979 }
12980
Romain Guy5bcdff42009-05-14 21:27:18 -070012981 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012982 bitmap.eraseColor(backgroundColor);
12983 }
12984
12985 computeScroll();
12986 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012987 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012988 canvas.translate(-mScrollX, -mScrollY);
12989
Romain Guy5bcdff42009-05-14 21:27:18 -070012990 // Temporarily remove the dirty mask
12991 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012992 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070012993
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012994 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012995 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012996 dispatchDraw(canvas);
12997 } else {
12998 draw(canvas);
12999 }
13000
Romain Guy5bcdff42009-05-14 21:27:18 -070013001 mPrivateFlags = flags;
13002
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013003 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013004 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013005
13006 if (attachInfo != null) {
13007 // Restore the cached Canvas for our siblings
13008 attachInfo.mCanvas = canvas;
13009 }
Romain Guy8506ab42009-06-11 17:35:47 -070013010
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013011 return bitmap;
13012 }
13013
13014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013015 * Indicates whether this View is currently in edit mode. A View is usually
13016 * in edit mode when displayed within a developer tool. For instance, if
13017 * this View is being drawn by a visual user interface builder, this method
13018 * should return true.
13019 *
13020 * Subclasses should check the return value of this method to provide
13021 * different behaviors if their normal behavior might interfere with the
13022 * host environment. For instance: the class spawns a thread in its
13023 * constructor, the drawing code relies on device-specific features, etc.
13024 *
13025 * This method is usually checked in the drawing code of custom widgets.
13026 *
13027 * @return True if this View is in edit mode, false otherwise.
13028 */
13029 public boolean isInEditMode() {
13030 return false;
13031 }
13032
13033 /**
13034 * If the View draws content inside its padding and enables fading edges,
13035 * it needs to support padding offsets. Padding offsets are added to the
13036 * fading edges to extend the length of the fade so that it covers pixels
13037 * drawn inside the padding.
13038 *
13039 * Subclasses of this class should override this method if they need
13040 * to draw content inside the padding.
13041 *
13042 * @return True if padding offset must be applied, false otherwise.
13043 *
13044 * @see #getLeftPaddingOffset()
13045 * @see #getRightPaddingOffset()
13046 * @see #getTopPaddingOffset()
13047 * @see #getBottomPaddingOffset()
13048 *
13049 * @since CURRENT
13050 */
13051 protected boolean isPaddingOffsetRequired() {
13052 return false;
13053 }
13054
13055 /**
13056 * Amount by which to extend the left fading region. Called only when
13057 * {@link #isPaddingOffsetRequired()} returns true.
13058 *
13059 * @return The left padding offset in pixels.
13060 *
13061 * @see #isPaddingOffsetRequired()
13062 *
13063 * @since CURRENT
13064 */
13065 protected int getLeftPaddingOffset() {
13066 return 0;
13067 }
13068
13069 /**
13070 * Amount by which to extend the right fading region. Called only when
13071 * {@link #isPaddingOffsetRequired()} returns true.
13072 *
13073 * @return The right padding offset in pixels.
13074 *
13075 * @see #isPaddingOffsetRequired()
13076 *
13077 * @since CURRENT
13078 */
13079 protected int getRightPaddingOffset() {
13080 return 0;
13081 }
13082
13083 /**
13084 * Amount by which to extend the top fading region. Called only when
13085 * {@link #isPaddingOffsetRequired()} returns true.
13086 *
13087 * @return The top padding offset in pixels.
13088 *
13089 * @see #isPaddingOffsetRequired()
13090 *
13091 * @since CURRENT
13092 */
13093 protected int getTopPaddingOffset() {
13094 return 0;
13095 }
13096
13097 /**
13098 * Amount by which to extend the bottom fading region. Called only when
13099 * {@link #isPaddingOffsetRequired()} returns true.
13100 *
13101 * @return The bottom padding offset in pixels.
13102 *
13103 * @see #isPaddingOffsetRequired()
13104 *
13105 * @since CURRENT
13106 */
13107 protected int getBottomPaddingOffset() {
13108 return 0;
13109 }
13110
13111 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013112 * @hide
13113 * @param offsetRequired
13114 */
13115 protected int getFadeTop(boolean offsetRequired) {
13116 int top = mPaddingTop;
13117 if (offsetRequired) top += getTopPaddingOffset();
13118 return top;
13119 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013120
Romain Guyf2fc4602011-07-19 15:20:03 -070013121 /**
13122 * @hide
13123 * @param offsetRequired
13124 */
13125 protected int getFadeHeight(boolean offsetRequired) {
13126 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013127 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013128 return mBottom - mTop - mPaddingBottom - padding;
13129 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013130
Romain Guyf2fc4602011-07-19 15:20:03 -070013131 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013132 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013133 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013134 *
Romain Guy2bffd262010-09-12 17:40:02 -070013135 * <p>Even if this method returns true, it does not mean that every call
13136 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13137 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013138 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013139 * window is hardware accelerated,
13140 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13141 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013142 *
Romain Guy2bffd262010-09-12 17:40:02 -070013143 * @return True if the view is attached to a window and the window is
13144 * hardware accelerated; false in any other case.
13145 */
13146 public boolean isHardwareAccelerated() {
13147 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13148 }
Joe Malin32736f02011-01-19 16:14:20 -080013149
Romain Guy2bffd262010-09-12 17:40:02 -070013150 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013151 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13152 * case of an active Animation being run on the view.
13153 */
13154 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13155 Animation a, boolean scalingRequired) {
13156 Transformation invalidationTransform;
13157 final int flags = parent.mGroupFlags;
13158 final boolean initialized = a.isInitialized();
13159 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013160 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013161 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013162 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013163 onAnimationStart();
13164 }
13165
13166 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13167 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13168 if (parent.mInvalidationTransformation == null) {
13169 parent.mInvalidationTransformation = new Transformation();
13170 }
13171 invalidationTransform = parent.mInvalidationTransformation;
13172 a.getTransformation(drawingTime, invalidationTransform, 1f);
13173 } else {
13174 invalidationTransform = parent.mChildTransformation;
13175 }
Romain Guy393a52c2012-05-22 20:21:08 -070013176
Chet Haasebcca79a2012-02-14 08:45:14 -080013177 if (more) {
13178 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013179 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13180 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13181 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13182 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013183 // The child need to draw an animation, potentially offscreen, so
13184 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013185 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013186 parent.invalidate(mLeft, mTop, mRight, mBottom);
13187 }
13188 } else {
13189 if (parent.mInvalidateRegion == null) {
13190 parent.mInvalidateRegion = new RectF();
13191 }
13192 final RectF region = parent.mInvalidateRegion;
13193 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13194 invalidationTransform);
13195
13196 // The child need to draw an animation, potentially offscreen, so
13197 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013198 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013199
13200 final int left = mLeft + (int) region.left;
13201 final int top = mTop + (int) region.top;
13202 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13203 top + (int) (region.height() + .5f));
13204 }
13205 }
13206 return more;
13207 }
13208
Chet Haasea1cff502012-02-21 13:43:44 -080013209 /**
13210 * This method is called by getDisplayList() when a display list is created or re-rendered.
13211 * It sets or resets the current value of all properties on that display list (resetting is
13212 * necessary when a display list is being re-created, because we need to make sure that
13213 * previously-set transform values
13214 */
13215 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013216 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013217 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013218 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013219 if (mParent instanceof ViewGroup) {
13220 displayList.setClipChildren(
13221 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13222 }
Chet Haase9420abd2012-03-29 16:28:32 -070013223 float alpha = 1;
13224 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13225 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13226 ViewGroup parentVG = (ViewGroup) mParent;
13227 final boolean hasTransform =
13228 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13229 if (hasTransform) {
13230 Transformation transform = parentVG.mChildTransformation;
13231 final int transformType = parentVG.mChildTransformation.getTransformationType();
13232 if (transformType != Transformation.TYPE_IDENTITY) {
13233 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13234 alpha = transform.getAlpha();
13235 }
13236 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13237 displayList.setStaticMatrix(transform.getMatrix());
13238 }
13239 }
13240 }
Chet Haasea1cff502012-02-21 13:43:44 -080013241 }
13242 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013243 alpha *= mTransformationInfo.mAlpha;
13244 if (alpha < 1) {
13245 final int multipliedAlpha = (int) (255 * alpha);
13246 if (onSetAlpha(multipliedAlpha)) {
13247 alpha = 1;
13248 }
13249 }
13250 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013251 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13252 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13253 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13254 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013255 if (mTransformationInfo.mCamera == null) {
13256 mTransformationInfo.mCamera = new Camera();
13257 mTransformationInfo.matrix3D = new Matrix();
13258 }
13259 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013260 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013261 displayList.setPivotX(getPivotX());
13262 displayList.setPivotY(getPivotY());
13263 }
Chet Haase9420abd2012-03-29 16:28:32 -070013264 } else if (alpha < 1) {
13265 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013266 }
13267 }
13268 }
13269
Chet Haasebcca79a2012-02-14 08:45:14 -080013270 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013271 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13272 * This draw() method is an implementation detail and is not intended to be overridden or
13273 * to be called from anywhere else other than ViewGroup.drawChild().
13274 */
13275 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013276 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013277 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013278 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013279 final int flags = parent.mGroupFlags;
13280
Chet Haasea1cff502012-02-21 13:43:44 -080013281 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013282 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013283 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013284 }
13285
13286 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013287 boolean concatMatrix = false;
13288
13289 boolean scalingRequired = false;
13290 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013291 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013292
13293 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013294 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13295 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013296 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013297 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013298 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13299 } else {
13300 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13301 }
13302
Chet Haasebcca79a2012-02-14 08:45:14 -080013303 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013304 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013305 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013306 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013307 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013308 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013309 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013310 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013311 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013312 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013313 mDisplayList != null) {
13314 // No longer animating: clear out old animation matrix
13315 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013316 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013317 }
13318 if (!useDisplayListProperties &&
13319 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13320 final boolean hasTransform =
13321 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13322 if (hasTransform) {
13323 final int transformType = parent.mChildTransformation.getTransformationType();
13324 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13325 parent.mChildTransformation : null;
13326 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13327 }
Chet Haase64a48c12012-02-13 16:33:29 -080013328 }
13329 }
13330
13331 concatMatrix |= !childHasIdentityMatrix;
13332
13333 // Sets the flag as early as possible to allow draw() implementations
13334 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013335 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013336
Romain Guyfbb43212012-08-30 15:19:27 -070013337 if (!concatMatrix &&
13338 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13339 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013340 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013341 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13342 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013343 return more;
13344 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013345 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013346
13347 if (hardwareAccelerated) {
13348 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13349 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013350 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13351 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013352 }
13353
Chet Haase64a48c12012-02-13 16:33:29 -080013354 DisplayList displayList = null;
13355 Bitmap cache = null;
13356 boolean hasDisplayList = false;
13357 if (caching) {
13358 if (!hardwareAccelerated) {
13359 if (layerType != LAYER_TYPE_NONE) {
13360 layerType = LAYER_TYPE_SOFTWARE;
13361 buildDrawingCache(true);
13362 }
13363 cache = getDrawingCache(true);
13364 } else {
13365 switch (layerType) {
13366 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013367 if (useDisplayListProperties) {
13368 hasDisplayList = canHaveDisplayList();
13369 } else {
13370 buildDrawingCache(true);
13371 cache = getDrawingCache(true);
13372 }
Chet Haase64a48c12012-02-13 16:33:29 -080013373 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013374 case LAYER_TYPE_HARDWARE:
13375 if (useDisplayListProperties) {
13376 hasDisplayList = canHaveDisplayList();
13377 }
13378 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013379 case LAYER_TYPE_NONE:
13380 // Delay getting the display list until animation-driven alpha values are
13381 // set up and possibly passed on to the view
13382 hasDisplayList = canHaveDisplayList();
13383 break;
13384 }
13385 }
13386 }
Chet Haasea1cff502012-02-21 13:43:44 -080013387 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013388 if (useDisplayListProperties) {
13389 displayList = getDisplayList();
13390 if (!displayList.isValid()) {
13391 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13392 // to getDisplayList(), the display list will be marked invalid and we should not
13393 // try to use it again.
13394 displayList = null;
13395 hasDisplayList = false;
13396 useDisplayListProperties = false;
13397 }
13398 }
Chet Haase64a48c12012-02-13 16:33:29 -080013399
Chet Haase526057b2012-07-12 17:50:41 -070013400 int sx = 0;
13401 int sy = 0;
13402 if (!hasDisplayList) {
13403 computeScroll();
13404 sx = mScrollX;
13405 sy = mScrollY;
13406 }
13407
Chet Haase64a48c12012-02-13 16:33:29 -080013408 final boolean hasNoCache = cache == null || hasDisplayList;
13409 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13410 layerType != LAYER_TYPE_HARDWARE;
13411
Chet Haasea1cff502012-02-21 13:43:44 -080013412 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013413 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013414 restoreTo = canvas.save();
13415 }
Chet Haase64a48c12012-02-13 16:33:29 -080013416 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013417 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013418 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013419 if (!useDisplayListProperties) {
13420 canvas.translate(mLeft, mTop);
13421 }
Chet Haase64a48c12012-02-13 16:33:29 -080013422 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013423 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013424 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013425 restoreTo = canvas.save();
13426 }
Chet Haase64a48c12012-02-13 16:33:29 -080013427 // mAttachInfo cannot be null, otherwise scalingRequired == false
13428 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13429 canvas.scale(scale, scale);
13430 }
13431 }
13432
Chet Haasea1cff502012-02-21 13:43:44 -080013433 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013434 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013435 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013436 if (transformToApply != null || !childHasIdentityMatrix) {
13437 int transX = 0;
13438 int transY = 0;
13439
13440 if (offsetForScroll) {
13441 transX = -sx;
13442 transY = -sy;
13443 }
13444
13445 if (transformToApply != null) {
13446 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013447 if (useDisplayListProperties) {
13448 displayList.setAnimationMatrix(transformToApply.getMatrix());
13449 } else {
13450 // Undo the scroll translation, apply the transformation matrix,
13451 // then redo the scroll translate to get the correct result.
13452 canvas.translate(-transX, -transY);
13453 canvas.concat(transformToApply.getMatrix());
13454 canvas.translate(transX, transY);
13455 }
Chet Haasea1cff502012-02-21 13:43:44 -080013456 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013457 }
13458
13459 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013460 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013461 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013462 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013463 }
13464 }
13465
Chet Haasea1cff502012-02-21 13:43:44 -080013466 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013467 canvas.translate(-transX, -transY);
13468 canvas.concat(getMatrix());
13469 canvas.translate(transX, transY);
13470 }
13471 }
13472
Chet Haase21433372012-06-05 07:54:09 -070013473 // Deal with alpha if it is or used to be <1
13474 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013475 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013476 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013477 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013478 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013479 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013480 }
Chet Haasea1cff502012-02-21 13:43:44 -080013481 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013482 if (hasNoCache) {
13483 final int multipliedAlpha = (int) (255 * alpha);
13484 if (!onSetAlpha(multipliedAlpha)) {
13485 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013486 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013487 layerType != LAYER_TYPE_NONE) {
13488 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13489 }
Chet Haase9420abd2012-03-29 16:28:32 -070013490 if (useDisplayListProperties) {
13491 displayList.setAlpha(alpha * getAlpha());
13492 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013493 final int scrollX = hasDisplayList ? 0 : sx;
13494 final int scrollY = hasDisplayList ? 0 : sy;
13495 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13496 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013497 }
13498 } else {
13499 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013500 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013501 }
13502 }
13503 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013504 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013505 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013506 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013507 }
13508
Chet Haasea1cff502012-02-21 13:43:44 -080013509 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13510 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013511 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013512 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013513 } else {
13514 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013515 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013516 } else {
13517 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13518 }
13519 }
13520 }
13521
Chet Haase9420abd2012-03-29 16:28:32 -070013522 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013523 displayList = getDisplayList();
13524 if (!displayList.isValid()) {
13525 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13526 // to getDisplayList(), the display list will be marked invalid and we should not
13527 // try to use it again.
13528 displayList = null;
13529 hasDisplayList = false;
13530 }
13531 }
13532
13533 if (hasNoCache) {
13534 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013535 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013536 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013537 if (layer != null && layer.isValid()) {
13538 mLayerPaint.setAlpha((int) (alpha * 255));
13539 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13540 layerRendered = true;
13541 } else {
13542 final int scrollX = hasDisplayList ? 0 : sx;
13543 final int scrollY = hasDisplayList ? 0 : sy;
13544 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013545 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013546 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13547 }
13548 }
13549
13550 if (!layerRendered) {
13551 if (!hasDisplayList) {
13552 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013553 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13554 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013555 dispatchDraw(canvas);
13556 } else {
13557 draw(canvas);
13558 }
13559 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013560 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013561 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013562 }
13563 }
13564 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013565 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013566 Paint cachePaint;
13567
13568 if (layerType == LAYER_TYPE_NONE) {
13569 cachePaint = parent.mCachePaint;
13570 if (cachePaint == null) {
13571 cachePaint = new Paint();
13572 cachePaint.setDither(false);
13573 parent.mCachePaint = cachePaint;
13574 }
Chet Haase9420abd2012-03-29 16:28:32 -070013575 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013576 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013577 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13578 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013579 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013580 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013581 }
13582 } else {
13583 cachePaint = mLayerPaint;
13584 cachePaint.setAlpha((int) (alpha * 255));
13585 }
13586 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13587 }
13588
Chet Haasea1cff502012-02-21 13:43:44 -080013589 if (restoreTo >= 0) {
13590 canvas.restoreToCount(restoreTo);
13591 }
Chet Haase64a48c12012-02-13 16:33:29 -080013592
13593 if (a != null && !more) {
13594 if (!hardwareAccelerated && !a.getFillAfter()) {
13595 onSetAlpha(255);
13596 }
13597 parent.finishAnimatingView(this, a);
13598 }
13599
13600 if (more && hardwareAccelerated) {
13601 // invalidation is the trigger to recreate display lists, so if we're using
13602 // display lists to render, force an invalidate to allow the animation to
13603 // continue drawing another frame
13604 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013605 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013606 // alpha animations should cause the child to recreate its display list
13607 invalidate(true);
13608 }
13609 }
13610
13611 mRecreateDisplayList = false;
13612
13613 return more;
13614 }
13615
13616 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013617 * Manually render this view (and all of its children) to the given Canvas.
13618 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013619 * called. When implementing a view, implement
13620 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13621 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013622 *
13623 * @param canvas The Canvas to which the View is rendered.
13624 */
13625 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013626 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013627 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013628 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013629 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013631 /*
13632 * Draw traversal performs several drawing steps which must be executed
13633 * in the appropriate order:
13634 *
13635 * 1. Draw the background
13636 * 2. If necessary, save the canvas' layers to prepare for fading
13637 * 3. Draw view's content
13638 * 4. Draw children
13639 * 5. If necessary, draw the fading edges and restore layers
13640 * 6. Draw decorations (scrollbars for instance)
13641 */
13642
13643 // Step 1, draw the background, if needed
13644 int saveCount;
13645
Romain Guy24443ea2009-05-11 11:56:30 -070013646 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013647 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013648 if (background != null) {
13649 final int scrollX = mScrollX;
13650 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013651
Romain Guy24443ea2009-05-11 11:56:30 -070013652 if (mBackgroundSizeChanged) {
13653 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13654 mBackgroundSizeChanged = false;
13655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656
Romain Guy24443ea2009-05-11 11:56:30 -070013657 if ((scrollX | scrollY) == 0) {
13658 background.draw(canvas);
13659 } else {
13660 canvas.translate(scrollX, scrollY);
13661 background.draw(canvas);
13662 canvas.translate(-scrollX, -scrollY);
13663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 }
13665 }
13666
13667 // skip step 2 & 5 if possible (common case)
13668 final int viewFlags = mViewFlags;
13669 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13670 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13671 if (!verticalEdges && !horizontalEdges) {
13672 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013673 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674
13675 // Step 4, draw the children
13676 dispatchDraw(canvas);
13677
13678 // Step 6, draw decorations (scrollbars)
13679 onDrawScrollBars(canvas);
13680
13681 // we're done...
13682 return;
13683 }
13684
13685 /*
13686 * Here we do the full fledged routine...
13687 * (this is an uncommon case where speed matters less,
13688 * this is why we repeat some of the tests that have been
13689 * done above)
13690 */
13691
13692 boolean drawTop = false;
13693 boolean drawBottom = false;
13694 boolean drawLeft = false;
13695 boolean drawRight = false;
13696
13697 float topFadeStrength = 0.0f;
13698 float bottomFadeStrength = 0.0f;
13699 float leftFadeStrength = 0.0f;
13700 float rightFadeStrength = 0.0f;
13701
13702 // Step 2, save the canvas' layers
13703 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013704
13705 final boolean offsetRequired = isPaddingOffsetRequired();
13706 if (offsetRequired) {
13707 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013708 }
13709
13710 int left = mScrollX + paddingLeft;
13711 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013712 int top = mScrollY + getFadeTop(offsetRequired);
13713 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714
13715 if (offsetRequired) {
13716 right += getRightPaddingOffset();
13717 bottom += getBottomPaddingOffset();
13718 }
13719
13720 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013721 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013722 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013723
13724 // clip the fade length if top and bottom fades overlap
13725 // overlapping fades produce odd-looking artifacts
13726 if (verticalEdges && (top + length > bottom - length)) {
13727 length = (bottom - top) / 2;
13728 }
13729
13730 // also clip horizontal fades if necessary
13731 if (horizontalEdges && (left + length > right - length)) {
13732 length = (right - left) / 2;
13733 }
13734
13735 if (verticalEdges) {
13736 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013737 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013739 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740 }
13741
13742 if (horizontalEdges) {
13743 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013744 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013746 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 }
13748
13749 saveCount = canvas.getSaveCount();
13750
13751 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013752 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013753 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13754
13755 if (drawTop) {
13756 canvas.saveLayer(left, top, right, top + length, null, flags);
13757 }
13758
13759 if (drawBottom) {
13760 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13761 }
13762
13763 if (drawLeft) {
13764 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13765 }
13766
13767 if (drawRight) {
13768 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13769 }
13770 } else {
13771 scrollabilityCache.setFadeColor(solidColor);
13772 }
13773
13774 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013775 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776
13777 // Step 4, draw the children
13778 dispatchDraw(canvas);
13779
13780 // Step 5, draw the fade effect and restore layers
13781 final Paint p = scrollabilityCache.paint;
13782 final Matrix matrix = scrollabilityCache.matrix;
13783 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784
13785 if (drawTop) {
13786 matrix.setScale(1, fadeHeight * topFadeStrength);
13787 matrix.postTranslate(left, top);
13788 fade.setLocalMatrix(matrix);
13789 canvas.drawRect(left, top, right, top + length, p);
13790 }
13791
13792 if (drawBottom) {
13793 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13794 matrix.postRotate(180);
13795 matrix.postTranslate(left, bottom);
13796 fade.setLocalMatrix(matrix);
13797 canvas.drawRect(left, bottom - length, right, bottom, p);
13798 }
13799
13800 if (drawLeft) {
13801 matrix.setScale(1, fadeHeight * leftFadeStrength);
13802 matrix.postRotate(-90);
13803 matrix.postTranslate(left, top);
13804 fade.setLocalMatrix(matrix);
13805 canvas.drawRect(left, top, left + length, bottom, p);
13806 }
13807
13808 if (drawRight) {
13809 matrix.setScale(1, fadeHeight * rightFadeStrength);
13810 matrix.postRotate(90);
13811 matrix.postTranslate(right, top);
13812 fade.setLocalMatrix(matrix);
13813 canvas.drawRect(right - length, top, right, bottom, p);
13814 }
13815
13816 canvas.restoreToCount(saveCount);
13817
13818 // Step 6, draw decorations (scrollbars)
13819 onDrawScrollBars(canvas);
13820 }
13821
13822 /**
13823 * Override this if your view is known to always be drawn on top of a solid color background,
13824 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13825 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13826 * should be set to 0xFF.
13827 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013828 * @see #setVerticalFadingEdgeEnabled(boolean)
13829 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013830 *
13831 * @return The known solid color background for this view, or 0 if the color may vary
13832 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013833 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013834 public int getSolidColor() {
13835 return 0;
13836 }
13837
13838 /**
13839 * Build a human readable string representation of the specified view flags.
13840 *
13841 * @param flags the view flags to convert to a string
13842 * @return a String representing the supplied flags
13843 */
13844 private static String printFlags(int flags) {
13845 String output = "";
13846 int numFlags = 0;
13847 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13848 output += "TAKES_FOCUS";
13849 numFlags++;
13850 }
13851
13852 switch (flags & VISIBILITY_MASK) {
13853 case INVISIBLE:
13854 if (numFlags > 0) {
13855 output += " ";
13856 }
13857 output += "INVISIBLE";
13858 // USELESS HERE numFlags++;
13859 break;
13860 case GONE:
13861 if (numFlags > 0) {
13862 output += " ";
13863 }
13864 output += "GONE";
13865 // USELESS HERE numFlags++;
13866 break;
13867 default:
13868 break;
13869 }
13870 return output;
13871 }
13872
13873 /**
13874 * Build a human readable string representation of the specified private
13875 * view flags.
13876 *
13877 * @param privateFlags the private view flags to convert to a string
13878 * @return a String representing the supplied flags
13879 */
13880 private static String printPrivateFlags(int privateFlags) {
13881 String output = "";
13882 int numFlags = 0;
13883
Dianne Hackborn4702a852012-08-17 15:18:29 -070013884 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013885 output += "WANTS_FOCUS";
13886 numFlags++;
13887 }
13888
Dianne Hackborn4702a852012-08-17 15:18:29 -070013889 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 if (numFlags > 0) {
13891 output += " ";
13892 }
13893 output += "FOCUSED";
13894 numFlags++;
13895 }
13896
Dianne Hackborn4702a852012-08-17 15:18:29 -070013897 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013898 if (numFlags > 0) {
13899 output += " ";
13900 }
13901 output += "SELECTED";
13902 numFlags++;
13903 }
13904
Dianne Hackborn4702a852012-08-17 15:18:29 -070013905 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 if (numFlags > 0) {
13907 output += " ";
13908 }
13909 output += "IS_ROOT_NAMESPACE";
13910 numFlags++;
13911 }
13912
Dianne Hackborn4702a852012-08-17 15:18:29 -070013913 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013914 if (numFlags > 0) {
13915 output += " ";
13916 }
13917 output += "HAS_BOUNDS";
13918 numFlags++;
13919 }
13920
Dianne Hackborn4702a852012-08-17 15:18:29 -070013921 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013922 if (numFlags > 0) {
13923 output += " ";
13924 }
13925 output += "DRAWN";
13926 // USELESS HERE numFlags++;
13927 }
13928 return output;
13929 }
13930
13931 /**
13932 * <p>Indicates whether or not this view's layout will be requested during
13933 * the next hierarchy layout pass.</p>
13934 *
13935 * @return true if the layout will be forced during next layout pass
13936 */
13937 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013938 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939 }
13940
13941 /**
13942 * Assign a size and position to a view and all of its
13943 * descendants
13944 *
13945 * <p>This is the second phase of the layout mechanism.
13946 * (The first is measuring). In this phase, each parent calls
13947 * layout on all of its children to position them.
13948 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013949 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013950 *
Chet Haase9c087442011-01-12 16:20:16 -080013951 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013952 * Derived classes with children should override
13953 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013954 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013955 *
13956 * @param l Left position, relative to parent
13957 * @param t Top position, relative to parent
13958 * @param r Right position, relative to parent
13959 * @param b Bottom position, relative to parent
13960 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013961 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013962 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013963 int oldL = mLeft;
13964 int oldT = mTop;
13965 int oldB = mBottom;
13966 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013968 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013969 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013970 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013971
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013972 ListenerInfo li = mListenerInfo;
13973 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013974 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013975 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013976 int numListeners = listenersCopy.size();
13977 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013978 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013979 }
13980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013982 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983 }
13984
13985 /**
13986 * Called from layout when this view should
13987 * assign a size and position to each of its children.
13988 *
13989 * Derived classes with children should override
13990 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013991 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 * @param changed This is a new size or position for this view
13993 * @param left Left position, relative to parent
13994 * @param top Top position, relative to parent
13995 * @param right Right position, relative to parent
13996 * @param bottom Bottom position, relative to parent
13997 */
13998 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13999 }
14000
14001 /**
14002 * Assign a size and position to this view.
14003 *
14004 * This is called from layout.
14005 *
14006 * @param left Left position, relative to parent
14007 * @param top Top position, relative to parent
14008 * @param right Right position, relative to parent
14009 * @param bottom Bottom position, relative to parent
14010 * @return true if the new size and position are different than the
14011 * previous ones
14012 * {@hide}
14013 */
14014 protected boolean setFrame(int left, int top, int right, int bottom) {
14015 boolean changed = false;
14016
14017 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014018 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014019 + right + "," + bottom + ")");
14020 }
14021
14022 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14023 changed = true;
14024
14025 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014026 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014028 int oldWidth = mRight - mLeft;
14029 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014030 int newWidth = right - left;
14031 int newHeight = bottom - top;
14032 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14033
14034 // Invalidate our old position
14035 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014036
14037 mLeft = left;
14038 mTop = top;
14039 mRight = right;
14040 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014041 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014042 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014044
Dianne Hackborn4702a852012-08-17 15:18:29 -070014045 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014047
Chet Haase75755e22011-07-18 17:48:25 -070014048 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014049 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014050 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014051 if (mTransformationInfo != null) {
14052 mTransformationInfo.mMatrixDirty = true;
14053 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014055 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14056 }
14057
14058 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14059 // If we are visible, force the DRAWN bit to on so that
14060 // this invalidate will go through (at least to our parent).
14061 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014062 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014063 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014064 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014065 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014066 // parent display list may need to be recreated based on a change in the bounds
14067 // of any child
14068 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069 }
14070
14071 // Reset drawn bit to original value (invalidate turns it off)
14072 mPrivateFlags |= drawn;
14073
14074 mBackgroundSizeChanged = true;
14075 }
14076 return changed;
14077 }
14078
14079 /**
14080 * Finalize inflating a view from XML. This is called as the last phase
14081 * of inflation, after all child views have been added.
14082 *
14083 * <p>Even if the subclass overrides onFinishInflate, they should always be
14084 * sure to call the super method, so that we get called.
14085 */
14086 protected void onFinishInflate() {
14087 }
14088
14089 /**
14090 * Returns the resources associated with this view.
14091 *
14092 * @return Resources object.
14093 */
14094 public Resources getResources() {
14095 return mResources;
14096 }
14097
14098 /**
14099 * Invalidates the specified Drawable.
14100 *
14101 * @param drawable the drawable to invalidate
14102 */
14103 public void invalidateDrawable(Drawable drawable) {
14104 if (verifyDrawable(drawable)) {
14105 final Rect dirty = drawable.getBounds();
14106 final int scrollX = mScrollX;
14107 final int scrollY = mScrollY;
14108
14109 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14110 dirty.right + scrollX, dirty.bottom + scrollY);
14111 }
14112 }
14113
14114 /**
14115 * Schedules an action on a drawable to occur at a specified time.
14116 *
14117 * @param who the recipient of the action
14118 * @param what the action to run on the drawable
14119 * @param when the time at which the action must occur. Uses the
14120 * {@link SystemClock#uptimeMillis} timebase.
14121 */
14122 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014123 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014124 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014125 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014126 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14127 Choreographer.CALLBACK_ANIMATION, what, who,
14128 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014129 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014130 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014132 }
14133 }
14134
14135 /**
14136 * Cancels a scheduled action on a drawable.
14137 *
14138 * @param who the recipient of the action
14139 * @param what the action to cancel
14140 */
14141 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014142 if (verifyDrawable(who) && what != null) {
14143 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014144 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14145 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014146 } else {
14147 ViewRootImpl.getRunQueue().removeCallbacks(what);
14148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014149 }
14150 }
14151
14152 /**
14153 * Unschedule any events associated with the given Drawable. This can be
14154 * used when selecting a new Drawable into a view, so that the previous
14155 * one is completely unscheduled.
14156 *
14157 * @param who The Drawable to unschedule.
14158 *
14159 * @see #drawableStateChanged
14160 */
14161 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014162 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014163 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14164 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014165 }
14166 }
14167
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014168 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014169 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14170 * that the View directionality can and will be resolved before its Drawables.
14171 *
14172 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014173 *
14174 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014175 */
14176 public void resolveDrawables() {
14177 if (mBackground != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014178 mBackground.setLayoutDirection(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014179 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014180 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014181 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014182 }
14183
14184 /**
14185 * Called when layout direction has been resolved.
14186 *
14187 * The default implementation does nothing.
14188 *
14189 * @param layoutDirection The resolved layout direction.
14190 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014191 * @see #LAYOUT_DIRECTION_LTR
14192 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014193 *
14194 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014195 */
14196 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014197 }
14198
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014199 private void resetResolvedDrawables() {
14200 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14201 }
14202
14203 private boolean isDrawablesResolved() {
14204 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14205 }
14206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014207 /**
14208 * If your view subclass is displaying its own Drawable objects, it should
14209 * override this function and return true for any Drawable it is
14210 * displaying. This allows animations for those drawables to be
14211 * scheduled.
14212 *
14213 * <p>Be sure to call through to the super class when overriding this
14214 * function.
14215 *
14216 * @param who The Drawable to verify. Return true if it is one you are
14217 * displaying, else return the result of calling through to the
14218 * super class.
14219 *
14220 * @return boolean If true than the Drawable is being displayed in the
14221 * view; else false and it is not allowed to animate.
14222 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014223 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14224 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014225 */
14226 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014227 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014228 }
14229
14230 /**
14231 * This function is called whenever the state of the view changes in such
14232 * a way that it impacts the state of drawables being shown.
14233 *
14234 * <p>Be sure to call through to the superclass when overriding this
14235 * function.
14236 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014237 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014238 */
14239 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014240 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014241 if (d != null && d.isStateful()) {
14242 d.setState(getDrawableState());
14243 }
14244 }
14245
14246 /**
14247 * Call this to force a view to update its drawable state. This will cause
14248 * drawableStateChanged to be called on this view. Views that are interested
14249 * in the new state should call getDrawableState.
14250 *
14251 * @see #drawableStateChanged
14252 * @see #getDrawableState
14253 */
14254 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014255 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 drawableStateChanged();
14257
14258 ViewParent parent = mParent;
14259 if (parent != null) {
14260 parent.childDrawableStateChanged(this);
14261 }
14262 }
14263
14264 /**
14265 * Return an array of resource IDs of the drawable states representing the
14266 * current state of the view.
14267 *
14268 * @return The current drawable state
14269 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014270 * @see Drawable#setState(int[])
14271 * @see #drawableStateChanged()
14272 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014273 */
14274 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014275 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014276 return mDrawableState;
14277 } else {
14278 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014279 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014280 return mDrawableState;
14281 }
14282 }
14283
14284 /**
14285 * Generate the new {@link android.graphics.drawable.Drawable} state for
14286 * this view. This is called by the view
14287 * system when the cached Drawable state is determined to be invalid. To
14288 * retrieve the current state, you should use {@link #getDrawableState}.
14289 *
14290 * @param extraSpace if non-zero, this is the number of extra entries you
14291 * would like in the returned array in which you can place your own
14292 * states.
14293 *
14294 * @return Returns an array holding the current {@link Drawable} state of
14295 * the view.
14296 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014297 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 */
14299 protected int[] onCreateDrawableState(int extraSpace) {
14300 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14301 mParent instanceof View) {
14302 return ((View) mParent).onCreateDrawableState(extraSpace);
14303 }
14304
14305 int[] drawableState;
14306
14307 int privateFlags = mPrivateFlags;
14308
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014309 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014310 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014311 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14312 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014313 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014314 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014315 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014316 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14317 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014318 // This is set if HW acceleration is requested, even if the current
14319 // process doesn't allow it. This is just to allow app preview
14320 // windows to better match their app.
14321 viewStateIndex |= VIEW_STATE_ACCELERATED;
14322 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014323 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014324
Christopher Tate3d4bf172011-03-28 16:16:46 -070014325 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014326 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14327 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014329 drawableState = VIEW_STATE_SETS[viewStateIndex];
14330
14331 //noinspection ConstantIfStatement
14332 if (false) {
14333 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14334 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014335 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014336 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14337 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014338 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014339 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014340 + ": " + Arrays.toString(drawableState));
14341 }
14342
14343 if (extraSpace == 0) {
14344 return drawableState;
14345 }
14346
14347 final int[] fullState;
14348 if (drawableState != null) {
14349 fullState = new int[drawableState.length + extraSpace];
14350 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14351 } else {
14352 fullState = new int[extraSpace];
14353 }
14354
14355 return fullState;
14356 }
14357
14358 /**
14359 * Merge your own state values in <var>additionalState</var> into the base
14360 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014361 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 *
14363 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014364 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014365 * own additional state values.
14366 *
14367 * @param additionalState The additional state values you would like
14368 * added to <var>baseState</var>; this array is not modified.
14369 *
14370 * @return As a convenience, the <var>baseState</var> array you originally
14371 * passed into the function is returned.
14372 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014373 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014374 */
14375 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14376 final int N = baseState.length;
14377 int i = N - 1;
14378 while (i >= 0 && baseState[i] == 0) {
14379 i--;
14380 }
14381 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14382 return baseState;
14383 }
14384
14385 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014386 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14387 * on all Drawable objects associated with this view.
14388 */
14389 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014390 if (mBackground != null) {
14391 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014392 }
14393 }
14394
14395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014396 * Sets the background color for this view.
14397 * @param color the color of the background
14398 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014399 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014400 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014401 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014402 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014403 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014404 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014405 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014407 }
14408
14409 /**
14410 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014411 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014412 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014413 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014414 * @attr ref android.R.styleable#View_background
14415 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014416 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014417 public void setBackgroundResource(int resid) {
14418 if (resid != 0 && resid == mBackgroundResource) {
14419 return;
14420 }
14421
14422 Drawable d= null;
14423 if (resid != 0) {
14424 d = mResources.getDrawable(resid);
14425 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014426 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014427
14428 mBackgroundResource = resid;
14429 }
14430
14431 /**
14432 * Set the background to a given Drawable, or remove the background. If the
14433 * background has padding, this View's padding is set to the background's
14434 * padding. However, when a background is removed, this View's padding isn't
14435 * touched. If setting the padding is desired, please use
14436 * {@link #setPadding(int, int, int, int)}.
14437 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014438 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014439 * background
14440 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014441 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014442 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014443 setBackgroundDrawable(background);
14444 }
14445
14446 /**
14447 * @deprecated use {@link #setBackground(Drawable)} instead
14448 */
14449 @Deprecated
14450 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014451 computeOpaqueFlags();
14452
Philip Milne6c8ea062012-04-03 17:38:43 -070014453 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014454 return;
14455 }
14456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014457 boolean requestLayout = false;
14458
14459 mBackgroundResource = 0;
14460
14461 /*
14462 * Regardless of whether we're setting a new background or not, we want
14463 * to clear the previous drawable.
14464 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014465 if (mBackground != null) {
14466 mBackground.setCallback(null);
14467 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014468 }
14469
Philip Milne6c8ea062012-04-03 17:38:43 -070014470 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014471 Rect padding = sThreadLocal.get();
14472 if (padding == null) {
14473 padding = new Rect();
14474 sThreadLocal.set(padding);
14475 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014476 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014477 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014478 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014479 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014480 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014481 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014482 mUserPaddingLeftInitial = padding.right;
14483 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014484 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014485 break;
14486 case LAYOUT_DIRECTION_LTR:
14487 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014488 mUserPaddingLeftInitial = padding.left;
14489 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014490 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014492 }
14493
14494 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14495 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014496 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14497 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014498 requestLayout = true;
14499 }
14500
Philip Milne6c8ea062012-04-03 17:38:43 -070014501 background.setCallback(this);
14502 if (background.isStateful()) {
14503 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014504 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014505 background.setVisible(getVisibility() == VISIBLE, false);
14506 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014507
Dianne Hackborn4702a852012-08-17 15:18:29 -070014508 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14509 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14510 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014511 requestLayout = true;
14512 }
14513 } else {
14514 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014515 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014516
Dianne Hackborn4702a852012-08-17 15:18:29 -070014517 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014518 /*
14519 * This view ONLY drew the background before and we're removing
14520 * the background, so now it won't draw anything
14521 * (hence we SKIP_DRAW)
14522 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014523 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14524 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014525 }
14526
14527 /*
14528 * When the background is set, we try to apply its padding to this
14529 * View. When the background is removed, we don't touch this View's
14530 * padding. This is noted in the Javadocs. Hence, we don't need to
14531 * requestLayout(), the invalidate() below is sufficient.
14532 */
14533
14534 // The old background's minimum size could have affected this
14535 // View's layout, so let's requestLayout
14536 requestLayout = true;
14537 }
14538
Romain Guy8f1344f52009-05-15 16:03:59 -070014539 computeOpaqueFlags();
14540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014541 if (requestLayout) {
14542 requestLayout();
14543 }
14544
14545 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014546 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 }
14548
14549 /**
14550 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014552 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014553 *
14554 * @see #setBackground(Drawable)
14555 *
14556 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014557 */
14558 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014559 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014560 }
14561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 /**
14563 * Sets the padding. The view may add on the space required to display
14564 * the scrollbars, depending on the style and visibility of the scrollbars.
14565 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14566 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14567 * from the values set in this call.
14568 *
14569 * @attr ref android.R.styleable#View_padding
14570 * @attr ref android.R.styleable#View_paddingBottom
14571 * @attr ref android.R.styleable#View_paddingLeft
14572 * @attr ref android.R.styleable#View_paddingRight
14573 * @attr ref android.R.styleable#View_paddingTop
14574 * @param left the left padding in pixels
14575 * @param top the top padding in pixels
14576 * @param right the right padding in pixels
14577 * @param bottom the bottom padding in pixels
14578 */
14579 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014580 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014581
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014582 mUserPaddingStart = UNDEFINED_PADDING;
14583 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014584
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014585 mUserPaddingLeftInitial = left;
14586 mUserPaddingRightInitial = right;
14587
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014588 internalSetPadding(left, top, right, bottom);
14589 }
14590
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014591 /**
14592 * @hide
14593 */
14594 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014595 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014596 mUserPaddingRight = right;
14597 mUserPaddingBottom = bottom;
14598
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014599 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014600 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014601
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014602 // Common case is there are no scroll bars.
14603 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014604 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014605 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014606 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014607 switch (mVerticalScrollbarPosition) {
14608 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014609 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014610 left += offset;
14611 } else {
14612 right += offset;
14613 }
14614 break;
Adam Powell20232d02010-12-08 21:08:53 -080014615 case SCROLLBAR_POSITION_RIGHT:
14616 right += offset;
14617 break;
14618 case SCROLLBAR_POSITION_LEFT:
14619 left += offset;
14620 break;
14621 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014622 }
Adam Powell20232d02010-12-08 21:08:53 -080014623 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014624 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14625 ? 0 : getHorizontalScrollbarHeight();
14626 }
14627 }
Romain Guy8506ab42009-06-11 17:35:47 -070014628
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014629 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014630 changed = true;
14631 mPaddingLeft = left;
14632 }
14633 if (mPaddingTop != top) {
14634 changed = true;
14635 mPaddingTop = top;
14636 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014637 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014638 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014639 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014640 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014641 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014642 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014643 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014644 }
14645
14646 if (changed) {
14647 requestLayout();
14648 }
14649 }
14650
14651 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014652 * Sets the relative padding. The view may add on the space required to display
14653 * the scrollbars, depending on the style and visibility of the scrollbars.
14654 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14655 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14656 * from the values set in this call.
14657 *
14658 * @attr ref android.R.styleable#View_padding
14659 * @attr ref android.R.styleable#View_paddingBottom
14660 * @attr ref android.R.styleable#View_paddingStart
14661 * @attr ref android.R.styleable#View_paddingEnd
14662 * @attr ref android.R.styleable#View_paddingTop
14663 * @param start the start padding in pixels
14664 * @param top the top padding in pixels
14665 * @param end the end padding in pixels
14666 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014667 */
14668 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014669 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014670
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014671 mUserPaddingStart = start;
14672 mUserPaddingEnd = end;
14673
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014674 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014675 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014676 mUserPaddingLeftInitial = end;
14677 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014678 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014679 break;
14680 case LAYOUT_DIRECTION_LTR:
14681 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014682 mUserPaddingLeftInitial = start;
14683 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014684 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014685 }
14686 }
14687
14688 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014689 * Returns the top padding of this view.
14690 *
14691 * @return the top padding in pixels
14692 */
14693 public int getPaddingTop() {
14694 return mPaddingTop;
14695 }
14696
14697 /**
14698 * Returns the bottom padding of this view. If there are inset and enabled
14699 * scrollbars, this value may include the space required to display the
14700 * scrollbars as well.
14701 *
14702 * @return the bottom padding in pixels
14703 */
14704 public int getPaddingBottom() {
14705 return mPaddingBottom;
14706 }
14707
14708 /**
14709 * Returns the left padding of this view. If there are inset and enabled
14710 * scrollbars, this value may include the space required to display the
14711 * scrollbars as well.
14712 *
14713 * @return the left padding in pixels
14714 */
14715 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014716 if (!isPaddingResolved()) {
14717 resolvePadding();
14718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014719 return mPaddingLeft;
14720 }
14721
14722 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014723 * Returns the start padding of this view depending on its resolved layout direction.
14724 * If there are inset and enabled scrollbars, this value may include the space
14725 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014726 *
14727 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014728 */
14729 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014730 if (!isPaddingResolved()) {
14731 resolvePadding();
14732 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014733 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014734 mPaddingRight : mPaddingLeft;
14735 }
14736
14737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014738 * Returns the right padding of this view. If there are inset and enabled
14739 * scrollbars, this value may include the space required to display the
14740 * scrollbars as well.
14741 *
14742 * @return the right padding in pixels
14743 */
14744 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014745 if (!isPaddingResolved()) {
14746 resolvePadding();
14747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014748 return mPaddingRight;
14749 }
14750
14751 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014752 * Returns the end padding of this view depending on its resolved layout direction.
14753 * If there are inset and enabled scrollbars, this value may include the space
14754 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014755 *
14756 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014757 */
14758 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014759 if (!isPaddingResolved()) {
14760 resolvePadding();
14761 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014762 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014763 mPaddingLeft : mPaddingRight;
14764 }
14765
14766 /**
14767 * Return if the padding as been set thru relative values
14768 * {@link #setPaddingRelative(int, int, int, int)} or thru
14769 * @attr ref android.R.styleable#View_paddingStart or
14770 * @attr ref android.R.styleable#View_paddingEnd
14771 *
14772 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014773 */
14774 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014775 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014776 }
14777
14778 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014779 * @hide
14780 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014781 public void resetPaddingToInitialValues() {
14782 mPaddingLeft = mUserPaddingLeftInitial;
14783 mPaddingRight = mUserPaddingRightInitial;
14784 }
14785
14786 /**
14787 * @hide
14788 */
Philip Milne7a23b492012-04-24 22:12:36 -070014789 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014790 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014791 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014792 }
14793 return mLayoutInsets;
14794 }
14795
14796 /**
14797 * @hide
14798 */
14799 public void setLayoutInsets(Insets layoutInsets) {
14800 mLayoutInsets = layoutInsets;
14801 }
14802
14803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014804 * Changes the selection state of this view. A view can be selected or not.
14805 * Note that selection is not the same as focus. Views are typically
14806 * selected in the context of an AdapterView like ListView or GridView;
14807 * the selected view is the view that is highlighted.
14808 *
14809 * @param selected true if the view must be selected, false otherwise
14810 */
14811 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014812 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14813 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014814 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014815 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014816 refreshDrawableState();
14817 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014818 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14819 notifyAccessibilityStateChanged();
14820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014821 }
14822 }
14823
14824 /**
14825 * Dispatch setSelected to all of this View's children.
14826 *
14827 * @see #setSelected(boolean)
14828 *
14829 * @param selected The new selected state
14830 */
14831 protected void dispatchSetSelected(boolean selected) {
14832 }
14833
14834 /**
14835 * Indicates the selection state of this view.
14836 *
14837 * @return true if the view is selected, false otherwise
14838 */
14839 @ViewDebug.ExportedProperty
14840 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014841 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014842 }
14843
14844 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014845 * Changes the activated state of this view. A view can be activated or not.
14846 * Note that activation is not the same as selection. Selection is
14847 * a transient property, representing the view (hierarchy) the user is
14848 * currently interacting with. Activation is a longer-term state that the
14849 * user can move views in and out of. For example, in a list view with
14850 * single or multiple selection enabled, the views in the current selection
14851 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14852 * here.) The activated state is propagated down to children of the view it
14853 * is set on.
14854 *
14855 * @param activated true if the view must be activated, false otherwise
14856 */
14857 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014858 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14859 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014860 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014861 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014862 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014863 }
14864 }
14865
14866 /**
14867 * Dispatch setActivated to all of this View's children.
14868 *
14869 * @see #setActivated(boolean)
14870 *
14871 * @param activated The new activated state
14872 */
14873 protected void dispatchSetActivated(boolean activated) {
14874 }
14875
14876 /**
14877 * Indicates the activation state of this view.
14878 *
14879 * @return true if the view is activated, false otherwise
14880 */
14881 @ViewDebug.ExportedProperty
14882 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014883 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014884 }
14885
14886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014887 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14888 * observer can be used to get notifications when global events, like
14889 * layout, happen.
14890 *
14891 * The returned ViewTreeObserver observer is not guaranteed to remain
14892 * valid for the lifetime of this View. If the caller of this method keeps
14893 * a long-lived reference to ViewTreeObserver, it should always check for
14894 * the return value of {@link ViewTreeObserver#isAlive()}.
14895 *
14896 * @return The ViewTreeObserver for this view's hierarchy.
14897 */
14898 public ViewTreeObserver getViewTreeObserver() {
14899 if (mAttachInfo != null) {
14900 return mAttachInfo.mTreeObserver;
14901 }
14902 if (mFloatingTreeObserver == null) {
14903 mFloatingTreeObserver = new ViewTreeObserver();
14904 }
14905 return mFloatingTreeObserver;
14906 }
14907
14908 /**
14909 * <p>Finds the topmost view in the current view hierarchy.</p>
14910 *
14911 * @return the topmost view containing this view
14912 */
14913 public View getRootView() {
14914 if (mAttachInfo != null) {
14915 final View v = mAttachInfo.mRootView;
14916 if (v != null) {
14917 return v;
14918 }
14919 }
Romain Guy8506ab42009-06-11 17:35:47 -070014920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014921 View parent = this;
14922
14923 while (parent.mParent != null && parent.mParent instanceof View) {
14924 parent = (View) parent.mParent;
14925 }
14926
14927 return parent;
14928 }
14929
14930 /**
14931 * <p>Computes the coordinates of this view on the screen. The argument
14932 * must be an array of two integers. After the method returns, the array
14933 * contains the x and y location in that order.</p>
14934 *
14935 * @param location an array of two integers in which to hold the coordinates
14936 */
14937 public void getLocationOnScreen(int[] location) {
14938 getLocationInWindow(location);
14939
14940 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014941 if (info != null) {
14942 location[0] += info.mWindowLeft;
14943 location[1] += info.mWindowTop;
14944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014945 }
14946
14947 /**
14948 * <p>Computes the coordinates of this view in its window. The argument
14949 * must be an array of two integers. After the method returns, the array
14950 * contains the x and y location in that order.</p>
14951 *
14952 * @param location an array of two integers in which to hold the coordinates
14953 */
14954 public void getLocationInWindow(int[] location) {
14955 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014956 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014957 }
14958
Gilles Debunne6583ce52011-12-06 18:09:02 -080014959 if (mAttachInfo == null) {
14960 // When the view is not attached to a window, this method does not make sense
14961 location[0] = location[1] = 0;
14962 return;
14963 }
14964
Gilles Debunnecea45132011-11-24 02:19:27 +010014965 float[] position = mAttachInfo.mTmpTransformLocation;
14966 position[0] = position[1] = 0.0f;
14967
14968 if (!hasIdentityMatrix()) {
14969 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014971
Gilles Debunnecea45132011-11-24 02:19:27 +010014972 position[0] += mLeft;
14973 position[1] += mTop;
14974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014975 ViewParent viewParent = mParent;
14976 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014977 final View view = (View) viewParent;
14978
14979 position[0] -= view.mScrollX;
14980 position[1] -= view.mScrollY;
14981
14982 if (!view.hasIdentityMatrix()) {
14983 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014984 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014985
14986 position[0] += view.mLeft;
14987 position[1] += view.mTop;
14988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014989 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014990 }
Romain Guy8506ab42009-06-11 17:35:47 -070014991
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014992 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014993 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014994 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14995 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014997
14998 location[0] = (int) (position[0] + 0.5f);
14999 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015000 }
15001
15002 /**
15003 * {@hide}
15004 * @param id the id of the view to be found
15005 * @return the view of the specified id, null if cannot be found
15006 */
15007 protected View findViewTraversal(int id) {
15008 if (id == mID) {
15009 return this;
15010 }
15011 return null;
15012 }
15013
15014 /**
15015 * {@hide}
15016 * @param tag the tag of the view to be found
15017 * @return the view of specified tag, null if cannot be found
15018 */
15019 protected View findViewWithTagTraversal(Object tag) {
15020 if (tag != null && tag.equals(mTag)) {
15021 return this;
15022 }
15023 return null;
15024 }
15025
15026 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015027 * {@hide}
15028 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015029 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015030 * @return The first view that matches the predicate or null.
15031 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015032 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015033 if (predicate.apply(this)) {
15034 return this;
15035 }
15036 return null;
15037 }
15038
15039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015040 * Look for a child view with the given id. If this view has the given
15041 * id, return this view.
15042 *
15043 * @param id The id to search for.
15044 * @return The view that has the given id in the hierarchy or null
15045 */
15046 public final View findViewById(int id) {
15047 if (id < 0) {
15048 return null;
15049 }
15050 return findViewTraversal(id);
15051 }
15052
15053 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015054 * Finds a view by its unuque and stable accessibility id.
15055 *
15056 * @param accessibilityId The searched accessibility id.
15057 * @return The found view.
15058 */
15059 final View findViewByAccessibilityId(int accessibilityId) {
15060 if (accessibilityId < 0) {
15061 return null;
15062 }
15063 return findViewByAccessibilityIdTraversal(accessibilityId);
15064 }
15065
15066 /**
15067 * Performs the traversal to find a view by its unuque and stable accessibility id.
15068 *
15069 * <strong>Note:</strong>This method does not stop at the root namespace
15070 * boundary since the user can touch the screen at an arbitrary location
15071 * potentially crossing the root namespace bounday which will send an
15072 * accessibility event to accessibility services and they should be able
15073 * to obtain the event source. Also accessibility ids are guaranteed to be
15074 * unique in the window.
15075 *
15076 * @param accessibilityId The accessibility id.
15077 * @return The found view.
15078 */
15079 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15080 if (getAccessibilityViewId() == accessibilityId) {
15081 return this;
15082 }
15083 return null;
15084 }
15085
15086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015087 * Look for a child view with the given tag. If this view has the given
15088 * tag, return this view.
15089 *
15090 * @param tag The tag to search for, using "tag.equals(getTag())".
15091 * @return The View that has the given tag in the hierarchy or null
15092 */
15093 public final View findViewWithTag(Object tag) {
15094 if (tag == null) {
15095 return null;
15096 }
15097 return findViewWithTagTraversal(tag);
15098 }
15099
15100 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015101 * {@hide}
15102 * Look for a child view that matches the specified predicate.
15103 * If this view matches the predicate, return this view.
15104 *
15105 * @param predicate The predicate to evaluate.
15106 * @return The first view that matches the predicate or null.
15107 */
15108 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015109 return findViewByPredicateTraversal(predicate, null);
15110 }
15111
15112 /**
15113 * {@hide}
15114 * Look for a child view that matches the specified predicate,
15115 * starting with the specified view and its descendents and then
15116 * recusively searching the ancestors and siblings of that view
15117 * until this view is reached.
15118 *
15119 * This method is useful in cases where the predicate does not match
15120 * a single unique view (perhaps multiple views use the same id)
15121 * and we are trying to find the view that is "closest" in scope to the
15122 * starting view.
15123 *
15124 * @param start The view to start from.
15125 * @param predicate The predicate to evaluate.
15126 * @return The first view that matches the predicate or null.
15127 */
15128 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15129 View childToSkip = null;
15130 for (;;) {
15131 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15132 if (view != null || start == this) {
15133 return view;
15134 }
15135
15136 ViewParent parent = start.getParent();
15137 if (parent == null || !(parent instanceof View)) {
15138 return null;
15139 }
15140
15141 childToSkip = start;
15142 start = (View) parent;
15143 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015144 }
15145
15146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015147 * Sets the identifier for this view. The identifier does not have to be
15148 * unique in this view's hierarchy. The identifier should be a positive
15149 * number.
15150 *
15151 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015152 * @see #getId()
15153 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015154 *
15155 * @param id a number used to identify the view
15156 *
15157 * @attr ref android.R.styleable#View_id
15158 */
15159 public void setId(int id) {
15160 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015161 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15162 mID = generateViewId();
15163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015164 }
15165
15166 /**
15167 * {@hide}
15168 *
15169 * @param isRoot true if the view belongs to the root namespace, false
15170 * otherwise
15171 */
15172 public void setIsRootNamespace(boolean isRoot) {
15173 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015174 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015175 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015176 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015177 }
15178 }
15179
15180 /**
15181 * {@hide}
15182 *
15183 * @return true if the view belongs to the root namespace, false otherwise
15184 */
15185 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015186 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015187 }
15188
15189 /**
15190 * Returns this view's identifier.
15191 *
15192 * @return a positive integer used to identify the view or {@link #NO_ID}
15193 * if the view has no ID
15194 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015195 * @see #setId(int)
15196 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015197 * @attr ref android.R.styleable#View_id
15198 */
15199 @ViewDebug.CapturedViewProperty
15200 public int getId() {
15201 return mID;
15202 }
15203
15204 /**
15205 * Returns this view's tag.
15206 *
15207 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015208 *
15209 * @see #setTag(Object)
15210 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015211 */
15212 @ViewDebug.ExportedProperty
15213 public Object getTag() {
15214 return mTag;
15215 }
15216
15217 /**
15218 * Sets the tag associated with this view. A tag can be used to mark
15219 * a view in its hierarchy and does not have to be unique within the
15220 * hierarchy. Tags can also be used to store data within a view without
15221 * resorting to another data structure.
15222 *
15223 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015224 *
15225 * @see #getTag()
15226 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015227 */
15228 public void setTag(final Object tag) {
15229 mTag = tag;
15230 }
15231
15232 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015233 * Returns the tag associated with this view and the specified key.
15234 *
15235 * @param key The key identifying the tag
15236 *
15237 * @return the Object stored in this view as a tag
15238 *
15239 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015240 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015241 */
15242 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015243 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015244 return null;
15245 }
15246
15247 /**
15248 * Sets a tag associated with this view and a key. A tag can be used
15249 * to mark a view in its hierarchy and does not have to be unique within
15250 * the hierarchy. Tags can also be used to store data within a view
15251 * without resorting to another data structure.
15252 *
15253 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015254 * application to ensure it is unique (see the <a
15255 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15256 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015257 * the Android framework or not associated with any package will cause
15258 * an {@link IllegalArgumentException} to be thrown.
15259 *
15260 * @param key The key identifying the tag
15261 * @param tag An Object to tag the view with
15262 *
15263 * @throws IllegalArgumentException If they specified key is not valid
15264 *
15265 * @see #setTag(Object)
15266 * @see #getTag(int)
15267 */
15268 public void setTag(int key, final Object tag) {
15269 // If the package id is 0x00 or 0x01, it's either an undefined package
15270 // or a framework id
15271 if ((key >>> 24) < 2) {
15272 throw new IllegalArgumentException("The key must be an application-specific "
15273 + "resource id.");
15274 }
15275
Adam Powell2b2f6d62011-09-23 11:15:39 -070015276 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015277 }
15278
15279 /**
15280 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15281 * framework id.
15282 *
15283 * @hide
15284 */
15285 public void setTagInternal(int key, Object tag) {
15286 if ((key >>> 24) != 0x1) {
15287 throw new IllegalArgumentException("The key must be a framework-specific "
15288 + "resource id.");
15289 }
15290
Adam Powell2b2f6d62011-09-23 11:15:39 -070015291 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015292 }
15293
Adam Powell2b2f6d62011-09-23 11:15:39 -070015294 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015295 if (mKeyedTags == null) {
15296 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015297 }
15298
Adam Powell7db82ac2011-09-22 19:44:04 -070015299 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015300 }
15301
15302 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015303 * Prints information about this view in the log output, with the tag
15304 * {@link #VIEW_LOG_TAG}.
15305 *
15306 * @hide
15307 */
15308 public void debug() {
15309 debug(0);
15310 }
15311
15312 /**
15313 * Prints information about this view in the log output, with the tag
15314 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15315 * indentation defined by the <code>depth</code>.
15316 *
15317 * @param depth the indentation level
15318 *
15319 * @hide
15320 */
15321 protected void debug(int depth) {
15322 String output = debugIndent(depth - 1);
15323
15324 output += "+ " + this;
15325 int id = getId();
15326 if (id != -1) {
15327 output += " (id=" + id + ")";
15328 }
15329 Object tag = getTag();
15330 if (tag != null) {
15331 output += " (tag=" + tag + ")";
15332 }
15333 Log.d(VIEW_LOG_TAG, output);
15334
Dianne Hackborn4702a852012-08-17 15:18:29 -070015335 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015336 output = debugIndent(depth) + " FOCUSED";
15337 Log.d(VIEW_LOG_TAG, output);
15338 }
15339
15340 output = debugIndent(depth);
15341 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15342 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15343 + "} ";
15344 Log.d(VIEW_LOG_TAG, output);
15345
15346 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15347 || mPaddingBottom != 0) {
15348 output = debugIndent(depth);
15349 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15350 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15351 Log.d(VIEW_LOG_TAG, output);
15352 }
15353
15354 output = debugIndent(depth);
15355 output += "mMeasureWidth=" + mMeasuredWidth +
15356 " mMeasureHeight=" + mMeasuredHeight;
15357 Log.d(VIEW_LOG_TAG, output);
15358
15359 output = debugIndent(depth);
15360 if (mLayoutParams == null) {
15361 output += "BAD! no layout params";
15362 } else {
15363 output = mLayoutParams.debug(output);
15364 }
15365 Log.d(VIEW_LOG_TAG, output);
15366
15367 output = debugIndent(depth);
15368 output += "flags={";
15369 output += View.printFlags(mViewFlags);
15370 output += "}";
15371 Log.d(VIEW_LOG_TAG, output);
15372
15373 output = debugIndent(depth);
15374 output += "privateFlags={";
15375 output += View.printPrivateFlags(mPrivateFlags);
15376 output += "}";
15377 Log.d(VIEW_LOG_TAG, output);
15378 }
15379
15380 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015381 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015382 *
15383 * @param depth the indentation level
15384 * @return a String containing (depth * 2 + 3) * 2 white spaces
15385 *
15386 * @hide
15387 */
15388 protected static String debugIndent(int depth) {
15389 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15390 for (int i = 0; i < (depth * 2) + 3; i++) {
15391 spaces.append(' ').append(' ');
15392 }
15393 return spaces.toString();
15394 }
15395
15396 /**
15397 * <p>Return the offset of the widget's text baseline from the widget's top
15398 * boundary. If this widget does not support baseline alignment, this
15399 * method returns -1. </p>
15400 *
15401 * @return the offset of the baseline within the widget's bounds or -1
15402 * if baseline alignment is not supported
15403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015404 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015405 public int getBaseline() {
15406 return -1;
15407 }
15408
15409 /**
15410 * Call this when something has changed which has invalidated the
15411 * layout of this view. This will schedule a layout pass of the view
15412 * tree.
15413 */
15414 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015415 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15416 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015417
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015418 if (mParent != null && !mParent.isLayoutRequested()) {
15419 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015420 }
15421 }
15422
15423 /**
15424 * Forces this view to be laid out during the next layout pass.
15425 * This method does not call requestLayout() or forceLayout()
15426 * on the parent.
15427 */
15428 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015429 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15430 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015431 }
15432
15433 /**
15434 * <p>
15435 * This is called to find out how big a view should be. The parent
15436 * supplies constraint information in the width and height parameters.
15437 * </p>
15438 *
15439 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015440 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015441 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015442 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015443 * </p>
15444 *
15445 *
15446 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15447 * parent
15448 * @param heightMeasureSpec Vertical space requirements as imposed by the
15449 * parent
15450 *
15451 * @see #onMeasure(int, int)
15452 */
15453 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015454 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015455 widthMeasureSpec != mOldWidthMeasureSpec ||
15456 heightMeasureSpec != mOldHeightMeasureSpec) {
15457
15458 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015459 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015460
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015461 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015463 // measure ourselves, this should set the measured dimension flag back
15464 onMeasure(widthMeasureSpec, heightMeasureSpec);
15465
15466 // flag not set, setMeasuredDimension() was not invoked, we raise
15467 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015468 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015469 throw new IllegalStateException("onMeasure() did not set the"
15470 + " measured dimension by calling"
15471 + " setMeasuredDimension()");
15472 }
15473
Dianne Hackborn4702a852012-08-17 15:18:29 -070015474 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015475 }
15476
15477 mOldWidthMeasureSpec = widthMeasureSpec;
15478 mOldHeightMeasureSpec = heightMeasureSpec;
15479 }
15480
15481 /**
15482 * <p>
15483 * Measure the view and its content to determine the measured width and the
15484 * measured height. This method is invoked by {@link #measure(int, int)} and
15485 * should be overriden by subclasses to provide accurate and efficient
15486 * measurement of their contents.
15487 * </p>
15488 *
15489 * <p>
15490 * <strong>CONTRACT:</strong> When overriding this method, you
15491 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15492 * measured width and height of this view. Failure to do so will trigger an
15493 * <code>IllegalStateException</code>, thrown by
15494 * {@link #measure(int, int)}. Calling the superclass'
15495 * {@link #onMeasure(int, int)} is a valid use.
15496 * </p>
15497 *
15498 * <p>
15499 * The base class implementation of measure defaults to the background size,
15500 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15501 * override {@link #onMeasure(int, int)} to provide better measurements of
15502 * their content.
15503 * </p>
15504 *
15505 * <p>
15506 * If this method is overridden, it is the subclass's responsibility to make
15507 * sure the measured height and width are at least the view's minimum height
15508 * and width ({@link #getSuggestedMinimumHeight()} and
15509 * {@link #getSuggestedMinimumWidth()}).
15510 * </p>
15511 *
15512 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15513 * The requirements are encoded with
15514 * {@link android.view.View.MeasureSpec}.
15515 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15516 * The requirements are encoded with
15517 * {@link android.view.View.MeasureSpec}.
15518 *
15519 * @see #getMeasuredWidth()
15520 * @see #getMeasuredHeight()
15521 * @see #setMeasuredDimension(int, int)
15522 * @see #getSuggestedMinimumHeight()
15523 * @see #getSuggestedMinimumWidth()
15524 * @see android.view.View.MeasureSpec#getMode(int)
15525 * @see android.view.View.MeasureSpec#getSize(int)
15526 */
15527 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15528 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15529 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15530 }
15531
15532 /**
15533 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15534 * measured width and measured height. Failing to do so will trigger an
15535 * exception at measurement time.</p>
15536 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015537 * @param measuredWidth The measured width of this view. May be a complex
15538 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15539 * {@link #MEASURED_STATE_TOO_SMALL}.
15540 * @param measuredHeight The measured height of this view. May be a complex
15541 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15542 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015543 */
15544 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15545 mMeasuredWidth = measuredWidth;
15546 mMeasuredHeight = measuredHeight;
15547
Dianne Hackborn4702a852012-08-17 15:18:29 -070015548 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015549 }
15550
15551 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015552 * Merge two states as returned by {@link #getMeasuredState()}.
15553 * @param curState The current state as returned from a view or the result
15554 * of combining multiple views.
15555 * @param newState The new view state to combine.
15556 * @return Returns a new integer reflecting the combination of the two
15557 * states.
15558 */
15559 public static int combineMeasuredStates(int curState, int newState) {
15560 return curState | newState;
15561 }
15562
15563 /**
15564 * Version of {@link #resolveSizeAndState(int, int, int)}
15565 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15566 */
15567 public static int resolveSize(int size, int measureSpec) {
15568 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15569 }
15570
15571 /**
15572 * Utility to reconcile a desired size and state, with constraints imposed
15573 * by a MeasureSpec. Will take the desired size, unless a different size
15574 * is imposed by the constraints. The returned value is a compound integer,
15575 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15576 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15577 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015578 *
15579 * @param size How big the view wants to be
15580 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015581 * @return Size information bit mask as defined by
15582 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015583 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015584 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015585 int result = size;
15586 int specMode = MeasureSpec.getMode(measureSpec);
15587 int specSize = MeasureSpec.getSize(measureSpec);
15588 switch (specMode) {
15589 case MeasureSpec.UNSPECIFIED:
15590 result = size;
15591 break;
15592 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015593 if (specSize < size) {
15594 result = specSize | MEASURED_STATE_TOO_SMALL;
15595 } else {
15596 result = size;
15597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015598 break;
15599 case MeasureSpec.EXACTLY:
15600 result = specSize;
15601 break;
15602 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015603 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015604 }
15605
15606 /**
15607 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015608 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015609 * by the MeasureSpec.
15610 *
15611 * @param size Default size for this view
15612 * @param measureSpec Constraints imposed by the parent
15613 * @return The size this view should be.
15614 */
15615 public static int getDefaultSize(int size, int measureSpec) {
15616 int result = size;
15617 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015618 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015619
15620 switch (specMode) {
15621 case MeasureSpec.UNSPECIFIED:
15622 result = size;
15623 break;
15624 case MeasureSpec.AT_MOST:
15625 case MeasureSpec.EXACTLY:
15626 result = specSize;
15627 break;
15628 }
15629 return result;
15630 }
15631
15632 /**
15633 * Returns the suggested minimum height that the view should use. This
15634 * returns the maximum of the view's minimum height
15635 * and the background's minimum height
15636 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15637 * <p>
15638 * When being used in {@link #onMeasure(int, int)}, the caller should still
15639 * ensure the returned height is within the requirements of the parent.
15640 *
15641 * @return The suggested minimum height of the view.
15642 */
15643 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015644 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015646 }
15647
15648 /**
15649 * Returns the suggested minimum width that the view should use. This
15650 * returns the maximum of the view's minimum width)
15651 * and the background's minimum width
15652 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15653 * <p>
15654 * When being used in {@link #onMeasure(int, int)}, the caller should still
15655 * ensure the returned width is within the requirements of the parent.
15656 *
15657 * @return The suggested minimum width of the view.
15658 */
15659 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015660 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015662
Philip Milne6c8ea062012-04-03 17:38:43 -070015663 /**
15664 * Returns the minimum height of the view.
15665 *
15666 * @return the minimum height the view will try to be.
15667 *
15668 * @see #setMinimumHeight(int)
15669 *
15670 * @attr ref android.R.styleable#View_minHeight
15671 */
15672 public int getMinimumHeight() {
15673 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015674 }
15675
15676 /**
15677 * Sets the minimum height of the view. It is not guaranteed the view will
15678 * be able to achieve this minimum height (for example, if its parent layout
15679 * constrains it with less available height).
15680 *
15681 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015682 *
15683 * @see #getMinimumHeight()
15684 *
15685 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015686 */
15687 public void setMinimumHeight(int minHeight) {
15688 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015689 requestLayout();
15690 }
15691
15692 /**
15693 * Returns the minimum width of the view.
15694 *
15695 * @return the minimum width the view will try to be.
15696 *
15697 * @see #setMinimumWidth(int)
15698 *
15699 * @attr ref android.R.styleable#View_minWidth
15700 */
15701 public int getMinimumWidth() {
15702 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015703 }
15704
15705 /**
15706 * Sets the minimum width of the view. It is not guaranteed the view will
15707 * be able to achieve this minimum width (for example, if its parent layout
15708 * constrains it with less available width).
15709 *
15710 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015711 *
15712 * @see #getMinimumWidth()
15713 *
15714 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015715 */
15716 public void setMinimumWidth(int minWidth) {
15717 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015718 requestLayout();
15719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015720 }
15721
15722 /**
15723 * Get the animation currently associated with this view.
15724 *
15725 * @return The animation that is currently playing or
15726 * scheduled to play for this view.
15727 */
15728 public Animation getAnimation() {
15729 return mCurrentAnimation;
15730 }
15731
15732 /**
15733 * Start the specified animation now.
15734 *
15735 * @param animation the animation to start now
15736 */
15737 public void startAnimation(Animation animation) {
15738 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15739 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015740 invalidateParentCaches();
15741 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015742 }
15743
15744 /**
15745 * Cancels any animations for this view.
15746 */
15747 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015748 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015749 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015751 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015752 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015753 }
15754
15755 /**
15756 * Sets the next animation to play for this view.
15757 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015758 * {@link #startAnimation(android.view.animation.Animation)} instead.
15759 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015760 * control over the start time and invalidation, but you
15761 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015762 * 2) the view's parent (which controls animations on its children)
15763 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015764 * start.
15765 *
15766 * @param animation The next animation, or null.
15767 */
15768 public void setAnimation(Animation animation) {
15769 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015771 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015772 // If the screen is off assume the animation start time is now instead of
15773 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15774 // would cause the animation to start when the screen turns back on
15775 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15776 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15777 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015779 animation.reset();
15780 }
15781 }
15782
15783 /**
15784 * Invoked by a parent ViewGroup to notify the start of the animation
15785 * currently associated with this view. If you override this method,
15786 * always call super.onAnimationStart();
15787 *
15788 * @see #setAnimation(android.view.animation.Animation)
15789 * @see #getAnimation()
15790 */
15791 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015792 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015793 }
15794
15795 /**
15796 * Invoked by a parent ViewGroup to notify the end of the animation
15797 * currently associated with this view. If you override this method,
15798 * always call super.onAnimationEnd();
15799 *
15800 * @see #setAnimation(android.view.animation.Animation)
15801 * @see #getAnimation()
15802 */
15803 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015804 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015805 }
15806
15807 /**
15808 * Invoked if there is a Transform that involves alpha. Subclass that can
15809 * draw themselves with the specified alpha should return true, and then
15810 * respect that alpha when their onDraw() is called. If this returns false
15811 * then the view may be redirected to draw into an offscreen buffer to
15812 * fulfill the request, which will look fine, but may be slower than if the
15813 * subclass handles it internally. The default implementation returns false.
15814 *
15815 * @param alpha The alpha (0..255) to apply to the view's drawing
15816 * @return true if the view can draw with the specified alpha.
15817 */
15818 protected boolean onSetAlpha(int alpha) {
15819 return false;
15820 }
15821
15822 /**
15823 * This is used by the RootView to perform an optimization when
15824 * the view hierarchy contains one or several SurfaceView.
15825 * SurfaceView is always considered transparent, but its children are not,
15826 * therefore all View objects remove themselves from the global transparent
15827 * region (passed as a parameter to this function).
15828 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015829 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 *
15831 * @return Returns true if the effective visibility of the view at this
15832 * point is opaque, regardless of the transparent region; returns false
15833 * if it is possible for underlying windows to be seen behind the view.
15834 *
15835 * {@hide}
15836 */
15837 public boolean gatherTransparentRegion(Region region) {
15838 final AttachInfo attachInfo = mAttachInfo;
15839 if (region != null && attachInfo != null) {
15840 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015841 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015842 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15843 // remove it from the transparent region.
15844 final int[] location = attachInfo.mTransparentLocation;
15845 getLocationInWindow(location);
15846 region.op(location[0], location[1], location[0] + mRight - mLeft,
15847 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015848 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015849 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15850 // exists, so we remove the background drawable's non-transparent
15851 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015852 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015853 }
15854 }
15855 return true;
15856 }
15857
15858 /**
15859 * Play a sound effect for this view.
15860 *
15861 * <p>The framework will play sound effects for some built in actions, such as
15862 * clicking, but you may wish to play these effects in your widget,
15863 * for instance, for internal navigation.
15864 *
15865 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15866 * {@link #isSoundEffectsEnabled()} is true.
15867 *
15868 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15869 */
15870 public void playSoundEffect(int soundConstant) {
15871 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15872 return;
15873 }
15874 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15875 }
15876
15877 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015878 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015879 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015880 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 *
15882 * <p>The framework will provide haptic feedback for some built in actions,
15883 * such as long presses, but you may wish to provide feedback for your
15884 * own widget.
15885 *
15886 * <p>The feedback will only be performed if
15887 * {@link #isHapticFeedbackEnabled()} is true.
15888 *
15889 * @param feedbackConstant One of the constants defined in
15890 * {@link HapticFeedbackConstants}
15891 */
15892 public boolean performHapticFeedback(int feedbackConstant) {
15893 return performHapticFeedback(feedbackConstant, 0);
15894 }
15895
15896 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015897 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015898 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015899 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015900 *
15901 * @param feedbackConstant One of the constants defined in
15902 * {@link HapticFeedbackConstants}
15903 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15904 */
15905 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15906 if (mAttachInfo == null) {
15907 return false;
15908 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015909 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015910 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015911 && !isHapticFeedbackEnabled()) {
15912 return false;
15913 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015914 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15915 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015916 }
15917
15918 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015919 * Request that the visibility of the status bar or other screen/window
15920 * decorations be changed.
15921 *
15922 * <p>This method is used to put the over device UI into temporary modes
15923 * where the user's attention is focused more on the application content,
15924 * by dimming or hiding surrounding system affordances. This is typically
15925 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15926 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15927 * to be placed behind the action bar (and with these flags other system
15928 * affordances) so that smooth transitions between hiding and showing them
15929 * can be done.
15930 *
15931 * <p>Two representative examples of the use of system UI visibility is
15932 * implementing a content browsing application (like a magazine reader)
15933 * and a video playing application.
15934 *
15935 * <p>The first code shows a typical implementation of a View in a content
15936 * browsing application. In this implementation, the application goes
15937 * into a content-oriented mode by hiding the status bar and action bar,
15938 * and putting the navigation elements into lights out mode. The user can
15939 * then interact with content while in this mode. Such an application should
15940 * provide an easy way for the user to toggle out of the mode (such as to
15941 * check information in the status bar or access notifications). In the
15942 * implementation here, this is done simply by tapping on the content.
15943 *
15944 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15945 * content}
15946 *
15947 * <p>This second code sample shows a typical implementation of a View
15948 * in a video playing application. In this situation, while the video is
15949 * playing the application would like to go into a complete full-screen mode,
15950 * to use as much of the display as possible for the video. When in this state
15951 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015952 * touching on the screen to pop the UI out of full screen mode. See
15953 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015954 *
15955 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15956 * content}
15957 *
15958 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15959 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15960 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15961 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015962 */
15963 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015964 if (visibility != mSystemUiVisibility) {
15965 mSystemUiVisibility = visibility;
15966 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15967 mParent.recomputeViewAttributes(this);
15968 }
Joe Onorato664644d2011-01-23 17:53:23 -080015969 }
15970 }
15971
15972 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015973 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15974 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15975 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15976 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15977 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015978 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015979 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015980 return mSystemUiVisibility;
15981 }
15982
Scott Mainec6331b2011-05-24 16:55:56 -070015983 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015984 * Returns the current system UI visibility that is currently set for
15985 * the entire window. This is the combination of the
15986 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15987 * views in the window.
15988 */
15989 public int getWindowSystemUiVisibility() {
15990 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15991 }
15992
15993 /**
15994 * Override to find out when the window's requested system UI visibility
15995 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15996 * This is different from the callbacks recieved through
15997 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15998 * in that this is only telling you about the local request of the window,
15999 * not the actual values applied by the system.
16000 */
16001 public void onWindowSystemUiVisibilityChanged(int visible) {
16002 }
16003
16004 /**
16005 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16006 * the view hierarchy.
16007 */
16008 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16009 onWindowSystemUiVisibilityChanged(visible);
16010 }
16011
16012 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016013 * Set a listener to receive callbacks when the visibility of the system bar changes.
16014 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16015 */
Joe Onorato664644d2011-01-23 17:53:23 -080016016 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016017 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016018 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16019 mParent.recomputeViewAttributes(this);
16020 }
16021 }
16022
16023 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016024 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16025 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016026 */
16027 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016028 ListenerInfo li = mListenerInfo;
16029 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16030 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016031 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016032 }
16033 }
16034
Dianne Hackborncf675782012-05-10 15:07:24 -070016035 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016036 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16037 if (val != mSystemUiVisibility) {
16038 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016039 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016040 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016041 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016042 }
16043
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016044 /** @hide */
16045 public void setDisabledSystemUiVisibility(int flags) {
16046 if (mAttachInfo != null) {
16047 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16048 mAttachInfo.mDisabledSystemUiVisibility = flags;
16049 if (mParent != null) {
16050 mParent.recomputeViewAttributes(this);
16051 }
16052 }
16053 }
16054 }
16055
Joe Onorato664644d2011-01-23 17:53:23 -080016056 /**
Joe Malin32736f02011-01-19 16:14:20 -080016057 * Creates an image that the system displays during the drag and drop
16058 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16059 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16060 * appearance as the given View. The default also positions the center of the drag shadow
16061 * directly under the touch point. If no View is provided (the constructor with no parameters
16062 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16063 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16064 * default is an invisible drag shadow.
16065 * <p>
16066 * You are not required to use the View you provide to the constructor as the basis of the
16067 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16068 * anything you want as the drag shadow.
16069 * </p>
16070 * <p>
16071 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16072 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16073 * size and position of the drag shadow. It uses this data to construct a
16074 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16075 * so that your application can draw the shadow image in the Canvas.
16076 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016077 *
16078 * <div class="special reference">
16079 * <h3>Developer Guides</h3>
16080 * <p>For a guide to implementing drag and drop features, read the
16081 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16082 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016083 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016084 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016085 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016086
16087 /**
Joe Malin32736f02011-01-19 16:14:20 -080016088 * Constructs a shadow image builder based on a View. By default, the resulting drag
16089 * shadow will have the same appearance and dimensions as the View, with the touch point
16090 * over the center of the View.
16091 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016092 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016093 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016094 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016095 }
16096
Christopher Tate17ed60c2011-01-18 12:50:26 -080016097 /**
16098 * Construct a shadow builder object with no associated View. This
16099 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16100 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16101 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016102 * reference to any View object. If they are not overridden, then the result is an
16103 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016104 */
16105 public DragShadowBuilder() {
16106 mView = new WeakReference<View>(null);
16107 }
16108
16109 /**
16110 * Returns the View object that had been passed to the
16111 * {@link #View.DragShadowBuilder(View)}
16112 * constructor. If that View parameter was {@code null} or if the
16113 * {@link #View.DragShadowBuilder()}
16114 * constructor was used to instantiate the builder object, this method will return
16115 * null.
16116 *
16117 * @return The View object associate with this builder object.
16118 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016119 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016120 final public View getView() {
16121 return mView.get();
16122 }
16123
Christopher Tate2c095f32010-10-04 14:13:40 -070016124 /**
Joe Malin32736f02011-01-19 16:14:20 -080016125 * Provides the metrics for the shadow image. These include the dimensions of
16126 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016127 * be centered under the touch location while dragging.
16128 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016129 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016130 * same as the dimensions of the View itself and centers the shadow under
16131 * the touch point.
16132 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016133 *
Joe Malin32736f02011-01-19 16:14:20 -080016134 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16135 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16136 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16137 * image.
16138 *
16139 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16140 * shadow image that should be underneath the touch point during the drag and drop
16141 * operation. Your application must set {@link android.graphics.Point#x} to the
16142 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016143 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016144 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016145 final View view = mView.get();
16146 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016147 shadowSize.set(view.getWidth(), view.getHeight());
16148 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016149 } else {
16150 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16151 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016152 }
16153
16154 /**
Joe Malin32736f02011-01-19 16:14:20 -080016155 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16156 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016157 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016158 *
Joe Malin32736f02011-01-19 16:14:20 -080016159 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016160 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016161 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016162 final View view = mView.get();
16163 if (view != null) {
16164 view.draw(canvas);
16165 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016166 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016167 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016168 }
16169 }
16170
16171 /**
Joe Malin32736f02011-01-19 16:14:20 -080016172 * Starts a drag and drop operation. When your application calls this method, it passes a
16173 * {@link android.view.View.DragShadowBuilder} object to the system. The
16174 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16175 * to get metrics for the drag shadow, and then calls the object's
16176 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16177 * <p>
16178 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16179 * drag events to all the View objects in your application that are currently visible. It does
16180 * this either by calling the View object's drag listener (an implementation of
16181 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16182 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16183 * Both are passed a {@link android.view.DragEvent} object that has a
16184 * {@link android.view.DragEvent#getAction()} value of
16185 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16186 * </p>
16187 * <p>
16188 * Your application can invoke startDrag() on any attached View object. The View object does not
16189 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16190 * be related to the View the user selected for dragging.
16191 * </p>
16192 * @param data A {@link android.content.ClipData} object pointing to the data to be
16193 * transferred by the drag and drop operation.
16194 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16195 * drag shadow.
16196 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16197 * drop operation. This Object is put into every DragEvent object sent by the system during the
16198 * current drag.
16199 * <p>
16200 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16201 * to the target Views. For example, it can contain flags that differentiate between a
16202 * a copy operation and a move operation.
16203 * </p>
16204 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16205 * so the parameter should be set to 0.
16206 * @return {@code true} if the method completes successfully, or
16207 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16208 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016209 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016210 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016211 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016212 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016213 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016214 }
16215 boolean okay = false;
16216
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016217 Point shadowSize = new Point();
16218 Point shadowTouchPoint = new Point();
16219 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016220
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016221 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16222 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16223 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016224 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016225
Chris Tatea32dcf72010-10-14 12:13:50 -070016226 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016227 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16228 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016229 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016230 Surface surface = new Surface();
16231 try {
16232 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016233 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016234 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016235 + " surface=" + surface);
16236 if (token != null) {
16237 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016238 try {
Chris Tate6b391282010-10-14 15:48:59 -070016239 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016240 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016241 } finally {
16242 surface.unlockCanvasAndPost(canvas);
16243 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016244
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016245 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016246
16247 // Cache the local state object for delivery with DragEvents
16248 root.setLocalDragState(myLocalState);
16249
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016250 // repurpose 'shadowSize' for the last touch point
16251 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016252
Christopher Tatea53146c2010-09-07 11:57:52 -070016253 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016254 shadowSize.x, shadowSize.y,
16255 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016256 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016257
16258 // Off and running! Release our local surface instance; the drag
16259 // shadow surface is now managed by the system process.
16260 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016261 }
16262 } catch (Exception e) {
16263 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16264 surface.destroy();
16265 }
16266
16267 return okay;
16268 }
16269
Christopher Tatea53146c2010-09-07 11:57:52 -070016270 /**
Joe Malin32736f02011-01-19 16:14:20 -080016271 * Handles drag events sent by the system following a call to
16272 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16273 *<p>
16274 * When the system calls this method, it passes a
16275 * {@link android.view.DragEvent} object. A call to
16276 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16277 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16278 * operation.
16279 * @param event The {@link android.view.DragEvent} sent by the system.
16280 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16281 * in DragEvent, indicating the type of drag event represented by this object.
16282 * @return {@code true} if the method was successful, otherwise {@code false}.
16283 * <p>
16284 * The method should return {@code true} in response to an action type of
16285 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16286 * operation.
16287 * </p>
16288 * <p>
16289 * The method should also return {@code true} in response to an action type of
16290 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16291 * {@code false} if it didn't.
16292 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016293 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016294 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016295 return false;
16296 }
16297
16298 /**
Joe Malin32736f02011-01-19 16:14:20 -080016299 * Detects if this View is enabled and has a drag event listener.
16300 * If both are true, then it calls the drag event listener with the
16301 * {@link android.view.DragEvent} it received. If the drag event listener returns
16302 * {@code true}, then dispatchDragEvent() returns {@code true}.
16303 * <p>
16304 * For all other cases, the method calls the
16305 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16306 * method and returns its result.
16307 * </p>
16308 * <p>
16309 * This ensures that a drag event is always consumed, even if the View does not have a drag
16310 * event listener. However, if the View has a listener and the listener returns true, then
16311 * onDragEvent() is not called.
16312 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016313 */
16314 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016315 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016316 ListenerInfo li = mListenerInfo;
16317 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16318 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016319 return true;
16320 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016321 return onDragEvent(event);
16322 }
16323
Christopher Tate3d4bf172011-03-28 16:16:46 -070016324 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016325 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016326 }
16327
Christopher Tatea53146c2010-09-07 11:57:52 -070016328 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016329 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16330 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016331 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016332 */
16333 public void onCloseSystemDialogs(String reason) {
16334 }
Joe Malin32736f02011-01-19 16:14:20 -080016335
Dianne Hackbornffa42482009-09-23 22:20:11 -070016336 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016337 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016338 * update a Region being computed for
16339 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016340 * that any non-transparent parts of the Drawable are removed from the
16341 * given transparent region.
16342 *
16343 * @param dr The Drawable whose transparency is to be applied to the region.
16344 * @param region A Region holding the current transparency information,
16345 * where any parts of the region that are set are considered to be
16346 * transparent. On return, this region will be modified to have the
16347 * transparency information reduced by the corresponding parts of the
16348 * Drawable that are not transparent.
16349 * {@hide}
16350 */
16351 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16352 if (DBG) {
16353 Log.i("View", "Getting transparent region for: " + this);
16354 }
16355 final Region r = dr.getTransparentRegion();
16356 final Rect db = dr.getBounds();
16357 final AttachInfo attachInfo = mAttachInfo;
16358 if (r != null && attachInfo != null) {
16359 final int w = getRight()-getLeft();
16360 final int h = getBottom()-getTop();
16361 if (db.left > 0) {
16362 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16363 r.op(0, 0, db.left, h, Region.Op.UNION);
16364 }
16365 if (db.right < w) {
16366 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16367 r.op(db.right, 0, w, h, Region.Op.UNION);
16368 }
16369 if (db.top > 0) {
16370 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16371 r.op(0, 0, w, db.top, Region.Op.UNION);
16372 }
16373 if (db.bottom < h) {
16374 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16375 r.op(0, db.bottom, w, h, Region.Op.UNION);
16376 }
16377 final int[] location = attachInfo.mTransparentLocation;
16378 getLocationInWindow(location);
16379 r.translate(location[0], location[1]);
16380 region.op(r, Region.Op.INTERSECT);
16381 } else {
16382 region.op(db, Region.Op.DIFFERENCE);
16383 }
16384 }
16385
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016386 private void checkForLongClick(int delayOffset) {
16387 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16388 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016389
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016390 if (mPendingCheckForLongPress == null) {
16391 mPendingCheckForLongPress = new CheckForLongPress();
16392 }
16393 mPendingCheckForLongPress.rememberWindowAttachCount();
16394 postDelayed(mPendingCheckForLongPress,
16395 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016397 }
16398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016399 /**
16400 * Inflate a view from an XML resource. This convenience method wraps the {@link
16401 * LayoutInflater} class, which provides a full range of options for view inflation.
16402 *
16403 * @param context The Context object for your activity or application.
16404 * @param resource The resource ID to inflate
16405 * @param root A view group that will be the parent. Used to properly inflate the
16406 * layout_* parameters.
16407 * @see LayoutInflater
16408 */
16409 public static View inflate(Context context, int resource, ViewGroup root) {
16410 LayoutInflater factory = LayoutInflater.from(context);
16411 return factory.inflate(resource, root);
16412 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016414 /**
Adam Powell637d3372010-08-25 14:37:03 -070016415 * Scroll the view with standard behavior for scrolling beyond the normal
16416 * content boundaries. Views that call this method should override
16417 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16418 * results of an over-scroll operation.
16419 *
16420 * Views can use this method to handle any touch or fling-based scrolling.
16421 *
16422 * @param deltaX Change in X in pixels
16423 * @param deltaY Change in Y in pixels
16424 * @param scrollX Current X scroll value in pixels before applying deltaX
16425 * @param scrollY Current Y scroll value in pixels before applying deltaY
16426 * @param scrollRangeX Maximum content scroll range along the X axis
16427 * @param scrollRangeY Maximum content scroll range along the Y axis
16428 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16429 * along the X axis.
16430 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16431 * along the Y axis.
16432 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16433 * @return true if scrolling was clamped to an over-scroll boundary along either
16434 * axis, false otherwise.
16435 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016436 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016437 protected boolean overScrollBy(int deltaX, int deltaY,
16438 int scrollX, int scrollY,
16439 int scrollRangeX, int scrollRangeY,
16440 int maxOverScrollX, int maxOverScrollY,
16441 boolean isTouchEvent) {
16442 final int overScrollMode = mOverScrollMode;
16443 final boolean canScrollHorizontal =
16444 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16445 final boolean canScrollVertical =
16446 computeVerticalScrollRange() > computeVerticalScrollExtent();
16447 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16448 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16449 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16450 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16451
16452 int newScrollX = scrollX + deltaX;
16453 if (!overScrollHorizontal) {
16454 maxOverScrollX = 0;
16455 }
16456
16457 int newScrollY = scrollY + deltaY;
16458 if (!overScrollVertical) {
16459 maxOverScrollY = 0;
16460 }
16461
16462 // Clamp values if at the limits and record
16463 final int left = -maxOverScrollX;
16464 final int right = maxOverScrollX + scrollRangeX;
16465 final int top = -maxOverScrollY;
16466 final int bottom = maxOverScrollY + scrollRangeY;
16467
16468 boolean clampedX = false;
16469 if (newScrollX > right) {
16470 newScrollX = right;
16471 clampedX = true;
16472 } else if (newScrollX < left) {
16473 newScrollX = left;
16474 clampedX = true;
16475 }
16476
16477 boolean clampedY = false;
16478 if (newScrollY > bottom) {
16479 newScrollY = bottom;
16480 clampedY = true;
16481 } else if (newScrollY < top) {
16482 newScrollY = top;
16483 clampedY = true;
16484 }
16485
16486 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16487
16488 return clampedX || clampedY;
16489 }
16490
16491 /**
16492 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16493 * respond to the results of an over-scroll operation.
16494 *
16495 * @param scrollX New X scroll value in pixels
16496 * @param scrollY New Y scroll value in pixels
16497 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16498 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16499 */
16500 protected void onOverScrolled(int scrollX, int scrollY,
16501 boolean clampedX, boolean clampedY) {
16502 // Intentionally empty.
16503 }
16504
16505 /**
16506 * Returns the over-scroll mode for this view. The result will be
16507 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16508 * (allow over-scrolling only if the view content is larger than the container),
16509 * or {@link #OVER_SCROLL_NEVER}.
16510 *
16511 * @return This view's over-scroll mode.
16512 */
16513 public int getOverScrollMode() {
16514 return mOverScrollMode;
16515 }
16516
16517 /**
16518 * Set the over-scroll mode for this view. Valid over-scroll modes are
16519 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16520 * (allow over-scrolling only if the view content is larger than the container),
16521 * or {@link #OVER_SCROLL_NEVER}.
16522 *
16523 * Setting the over-scroll mode of a view will have an effect only if the
16524 * view is capable of scrolling.
16525 *
16526 * @param overScrollMode The new over-scroll mode for this view.
16527 */
16528 public void setOverScrollMode(int overScrollMode) {
16529 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16530 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16531 overScrollMode != OVER_SCROLL_NEVER) {
16532 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16533 }
16534 mOverScrollMode = overScrollMode;
16535 }
16536
16537 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016538 * Gets a scale factor that determines the distance the view should scroll
16539 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16540 * @return The vertical scroll scale factor.
16541 * @hide
16542 */
16543 protected float getVerticalScrollFactor() {
16544 if (mVerticalScrollFactor == 0) {
16545 TypedValue outValue = new TypedValue();
16546 if (!mContext.getTheme().resolveAttribute(
16547 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16548 throw new IllegalStateException(
16549 "Expected theme to define listPreferredItemHeight.");
16550 }
16551 mVerticalScrollFactor = outValue.getDimension(
16552 mContext.getResources().getDisplayMetrics());
16553 }
16554 return mVerticalScrollFactor;
16555 }
16556
16557 /**
16558 * Gets a scale factor that determines the distance the view should scroll
16559 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16560 * @return The horizontal scroll scale factor.
16561 * @hide
16562 */
16563 protected float getHorizontalScrollFactor() {
16564 // TODO: Should use something else.
16565 return getVerticalScrollFactor();
16566 }
16567
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016568 /**
16569 * Return the value specifying the text direction or policy that was set with
16570 * {@link #setTextDirection(int)}.
16571 *
16572 * @return the defined text direction. It can be one of:
16573 *
16574 * {@link #TEXT_DIRECTION_INHERIT},
16575 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16576 * {@link #TEXT_DIRECTION_ANY_RTL},
16577 * {@link #TEXT_DIRECTION_LTR},
16578 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016579 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016580 *
16581 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016582 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016583 @ViewDebug.ExportedProperty(category = "text", mapping = {
16584 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16585 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16586 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16587 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16588 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16589 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16590 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016591 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016592 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016593 }
16594
16595 /**
16596 * Set the text direction.
16597 *
16598 * @param textDirection the direction to set. Should be one of:
16599 *
16600 * {@link #TEXT_DIRECTION_INHERIT},
16601 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16602 * {@link #TEXT_DIRECTION_ANY_RTL},
16603 * {@link #TEXT_DIRECTION_LTR},
16604 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016605 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016606 *
16607 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16608 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16609 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016610 */
16611 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016612 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016613 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016614 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016615 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016616 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016617 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016618 // Do resolution
16619 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016620 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016621 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016622 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016623 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016624 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016625 }
16626 }
16627
16628 /**
16629 * Return the resolved text direction.
16630 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016631 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016632 *
Doug Feltcb3791202011-07-07 11:57:48 -070016633 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16634 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016635 * {@link #TEXT_DIRECTION_LTR},
16636 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016637 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016638 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016639 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016640 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016641 }
16642
16643 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016644 * Resolve the text direction.
16645 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016646 * @return true if resolution has been done, false otherwise.
16647 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016648 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016649 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016650 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016651 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016652 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016653
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016654 if (hasRtlSupport()) {
16655 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016656 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016657 switch(textDirection) {
16658 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016659 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016660 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016661 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016662 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016663 return false;
16664 }
16665
16666 View parent = ((View) mParent);
16667 // Parent has not yet resolved, so we still return the default
16668 if (!parent.isTextDirectionResolved()) {
16669 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16670 // Resolution will need to happen again later
16671 return false;
16672 }
16673
16674 // Set current resolved direction to the same value as the parent's one
16675 final int parentResolvedDirection = parent.getTextDirection();
16676 switch (parentResolvedDirection) {
16677 case TEXT_DIRECTION_FIRST_STRONG:
16678 case TEXT_DIRECTION_ANY_RTL:
16679 case TEXT_DIRECTION_LTR:
16680 case TEXT_DIRECTION_RTL:
16681 case TEXT_DIRECTION_LOCALE:
16682 mPrivateFlags2 |=
16683 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16684 break;
16685 default:
16686 // Default resolved direction is "first strong" heuristic
16687 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016688 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016689 break;
16690 case TEXT_DIRECTION_FIRST_STRONG:
16691 case TEXT_DIRECTION_ANY_RTL:
16692 case TEXT_DIRECTION_LTR:
16693 case TEXT_DIRECTION_RTL:
16694 case TEXT_DIRECTION_LOCALE:
16695 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016696 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016697 break;
16698 default:
16699 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016700 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016701 }
16702 } else {
16703 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016704 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016705 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016706
16707 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016708 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016709 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016710 }
16711
16712 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016713 * Check if text direction resolution can be done.
16714 *
16715 * @return true if text direction resolution can be done otherwise return false.
16716 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016717 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016718 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016719 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016720 return (mParent != null) && (mParent instanceof View) &&
16721 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016722 default:
16723 return true;
16724 }
16725 }
16726
16727 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016728 * Reset resolved text direction. Text direction will be resolved during a call to
16729 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016730 *
16731 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016732 */
16733 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016734 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016735 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016736 // Set to default value
16737 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016738 }
16739
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016740 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016741 * @return true if text direction is inherited.
16742 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016743 * @hide
16744 */
16745 public boolean isTextDirectionInherited() {
16746 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16747 }
16748
16749 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016750 * @return true if text direction is resolved.
16751 */
16752 private boolean isTextDirectionResolved() {
16753 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16754 }
16755
16756 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016757 * Return the value specifying the text alignment or policy that was set with
16758 * {@link #setTextAlignment(int)}.
16759 *
16760 * @return the defined text alignment. It can be one of:
16761 *
16762 * {@link #TEXT_ALIGNMENT_INHERIT},
16763 * {@link #TEXT_ALIGNMENT_GRAVITY},
16764 * {@link #TEXT_ALIGNMENT_CENTER},
16765 * {@link #TEXT_ALIGNMENT_TEXT_START},
16766 * {@link #TEXT_ALIGNMENT_TEXT_END},
16767 * {@link #TEXT_ALIGNMENT_VIEW_START},
16768 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016769 *
16770 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016771 */
16772 @ViewDebug.ExportedProperty(category = "text", mapping = {
16773 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16774 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16775 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16776 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16777 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16778 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16779 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16780 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016781 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016782 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016783 }
16784
16785 /**
16786 * Set the text alignment.
16787 *
16788 * @param textAlignment The text alignment to set. Should be one of
16789 *
16790 * {@link #TEXT_ALIGNMENT_INHERIT},
16791 * {@link #TEXT_ALIGNMENT_GRAVITY},
16792 * {@link #TEXT_ALIGNMENT_CENTER},
16793 * {@link #TEXT_ALIGNMENT_TEXT_START},
16794 * {@link #TEXT_ALIGNMENT_TEXT_END},
16795 * {@link #TEXT_ALIGNMENT_VIEW_START},
16796 * {@link #TEXT_ALIGNMENT_VIEW_END}
16797 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016798 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16799 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16800 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16801 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016802 * @attr ref android.R.styleable#View_textAlignment
16803 */
16804 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016805 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016806 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016807 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016808 resetResolvedTextAlignment();
16809 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016810 mPrivateFlags2 |=
16811 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16812 // Do resolution
16813 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016814 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016815 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016816 // Refresh
16817 requestLayout();
16818 invalidate(true);
16819 }
16820 }
16821
16822 /**
16823 * Return the resolved text alignment.
16824 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016825 * @return the resolved text alignment. Returns one of:
16826 *
16827 * {@link #TEXT_ALIGNMENT_GRAVITY},
16828 * {@link #TEXT_ALIGNMENT_CENTER},
16829 * {@link #TEXT_ALIGNMENT_TEXT_START},
16830 * {@link #TEXT_ALIGNMENT_TEXT_END},
16831 * {@link #TEXT_ALIGNMENT_VIEW_START},
16832 * {@link #TEXT_ALIGNMENT_VIEW_END}
16833 */
16834 @ViewDebug.ExportedProperty(category = "text", mapping = {
16835 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16836 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16837 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16838 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16839 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16840 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16841 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16842 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016843 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016844 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16845 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016846 }
16847
16848 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016849 * Resolve the text alignment.
16850 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016851 * @return true if resolution has been done, false otherwise.
16852 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016853 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016854 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016855 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016856 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016857 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016858
16859 if (hasRtlSupport()) {
16860 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016861 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016862 switch (textAlignment) {
16863 case TEXT_ALIGNMENT_INHERIT:
16864 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016865 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016866 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016867 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016868 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016869 return false;
16870 }
16871 View parent = (View) mParent;
16872
16873 // Parent has not yet resolved, so we still return the default
16874 if (!parent.isTextAlignmentResolved()) {
16875 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16876 // Resolution will need to happen again later
16877 return false;
16878 }
16879
16880 final int parentResolvedTextAlignment = parent.getTextAlignment();
16881 switch (parentResolvedTextAlignment) {
16882 case TEXT_ALIGNMENT_GRAVITY:
16883 case TEXT_ALIGNMENT_TEXT_START:
16884 case TEXT_ALIGNMENT_TEXT_END:
16885 case TEXT_ALIGNMENT_CENTER:
16886 case TEXT_ALIGNMENT_VIEW_START:
16887 case TEXT_ALIGNMENT_VIEW_END:
16888 // Resolved text alignment is the same as the parent resolved
16889 // text alignment
16890 mPrivateFlags2 |=
16891 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16892 break;
16893 default:
16894 // Use default resolved text alignment
16895 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016896 }
16897 break;
16898 case TEXT_ALIGNMENT_GRAVITY:
16899 case TEXT_ALIGNMENT_TEXT_START:
16900 case TEXT_ALIGNMENT_TEXT_END:
16901 case TEXT_ALIGNMENT_CENTER:
16902 case TEXT_ALIGNMENT_VIEW_START:
16903 case TEXT_ALIGNMENT_VIEW_END:
16904 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016905 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016906 break;
16907 default:
16908 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016909 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016910 }
16911 } else {
16912 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016913 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016914 }
16915
16916 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016917 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016918 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016919 }
16920
16921 /**
16922 * Check if text alignment resolution can be done.
16923 *
16924 * @return true if text alignment resolution can be done otherwise return false.
16925 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016926 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016927 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016928 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016929 return (mParent != null) && (mParent instanceof View) &&
16930 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016931 default:
16932 return true;
16933 }
16934 }
16935
16936 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016937 * Reset resolved text alignment. Text alignment will be resolved during a call to
16938 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016939 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016940 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016941 */
16942 public void resetResolvedTextAlignment() {
16943 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016944 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016945 // Set to default
16946 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016947 }
16948
Adam Powella9108a22012-07-18 11:18:09 -070016949 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016950 * @return true if text alignment is inherited.
16951 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016952 * @hide
16953 */
16954 public boolean isTextAlignmentInherited() {
16955 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16956 }
16957
16958 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016959 * @return true if text alignment is resolved.
16960 */
16961 private boolean isTextAlignmentResolved() {
16962 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
16963 }
16964
16965 /**
Adam Powella9108a22012-07-18 11:18:09 -070016966 * Generate a value suitable for use in {@link #setId(int)}.
16967 * This value will not collide with ID values generated at build time by aapt for R.id.
16968 *
16969 * @return a generated ID value
16970 */
16971 public static int generateViewId() {
16972 for (;;) {
16973 final int result = sNextGeneratedId.get();
16974 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
16975 int newValue = result + 1;
16976 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
16977 if (sNextGeneratedId.compareAndSet(result, newValue)) {
16978 return result;
16979 }
16980 }
16981 }
16982
Chet Haaseb39f0512011-05-24 14:36:40 -070016983 //
16984 // Properties
16985 //
16986 /**
16987 * A Property wrapper around the <code>alpha</code> functionality handled by the
16988 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16989 */
Chet Haased47f1532011-12-16 11:18:52 -080016990 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016991 @Override
16992 public void setValue(View object, float value) {
16993 object.setAlpha(value);
16994 }
16995
16996 @Override
16997 public Float get(View object) {
16998 return object.getAlpha();
16999 }
17000 };
17001
17002 /**
17003 * A Property wrapper around the <code>translationX</code> functionality handled by the
17004 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17005 */
Chet Haased47f1532011-12-16 11:18:52 -080017006 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017007 @Override
17008 public void setValue(View object, float value) {
17009 object.setTranslationX(value);
17010 }
17011
17012 @Override
17013 public Float get(View object) {
17014 return object.getTranslationX();
17015 }
17016 };
17017
17018 /**
17019 * A Property wrapper around the <code>translationY</code> functionality handled by the
17020 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17021 */
Chet Haased47f1532011-12-16 11:18:52 -080017022 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017023 @Override
17024 public void setValue(View object, float value) {
17025 object.setTranslationY(value);
17026 }
17027
17028 @Override
17029 public Float get(View object) {
17030 return object.getTranslationY();
17031 }
17032 };
17033
17034 /**
17035 * A Property wrapper around the <code>x</code> functionality handled by the
17036 * {@link View#setX(float)} and {@link View#getX()} methods.
17037 */
Chet Haased47f1532011-12-16 11:18:52 -080017038 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017039 @Override
17040 public void setValue(View object, float value) {
17041 object.setX(value);
17042 }
17043
17044 @Override
17045 public Float get(View object) {
17046 return object.getX();
17047 }
17048 };
17049
17050 /**
17051 * A Property wrapper around the <code>y</code> functionality handled by the
17052 * {@link View#setY(float)} and {@link View#getY()} methods.
17053 */
Chet Haased47f1532011-12-16 11:18:52 -080017054 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017055 @Override
17056 public void setValue(View object, float value) {
17057 object.setY(value);
17058 }
17059
17060 @Override
17061 public Float get(View object) {
17062 return object.getY();
17063 }
17064 };
17065
17066 /**
17067 * A Property wrapper around the <code>rotation</code> functionality handled by the
17068 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17069 */
Chet Haased47f1532011-12-16 11:18:52 -080017070 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017071 @Override
17072 public void setValue(View object, float value) {
17073 object.setRotation(value);
17074 }
17075
17076 @Override
17077 public Float get(View object) {
17078 return object.getRotation();
17079 }
17080 };
17081
17082 /**
17083 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17084 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17085 */
Chet Haased47f1532011-12-16 11:18:52 -080017086 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017087 @Override
17088 public void setValue(View object, float value) {
17089 object.setRotationX(value);
17090 }
17091
17092 @Override
17093 public Float get(View object) {
17094 return object.getRotationX();
17095 }
17096 };
17097
17098 /**
17099 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17100 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17101 */
Chet Haased47f1532011-12-16 11:18:52 -080017102 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017103 @Override
17104 public void setValue(View object, float value) {
17105 object.setRotationY(value);
17106 }
17107
17108 @Override
17109 public Float get(View object) {
17110 return object.getRotationY();
17111 }
17112 };
17113
17114 /**
17115 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17116 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17117 */
Chet Haased47f1532011-12-16 11:18:52 -080017118 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017119 @Override
17120 public void setValue(View object, float value) {
17121 object.setScaleX(value);
17122 }
17123
17124 @Override
17125 public Float get(View object) {
17126 return object.getScaleX();
17127 }
17128 };
17129
17130 /**
17131 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17132 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17133 */
Chet Haased47f1532011-12-16 11:18:52 -080017134 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017135 @Override
17136 public void setValue(View object, float value) {
17137 object.setScaleY(value);
17138 }
17139
17140 @Override
17141 public Float get(View object) {
17142 return object.getScaleY();
17143 }
17144 };
17145
Jeff Brown33bbfd22011-02-24 20:55:35 -080017146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017147 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17148 * Each MeasureSpec represents a requirement for either the width or the height.
17149 * A MeasureSpec is comprised of a size and a mode. There are three possible
17150 * modes:
17151 * <dl>
17152 * <dt>UNSPECIFIED</dt>
17153 * <dd>
17154 * The parent has not imposed any constraint on the child. It can be whatever size
17155 * it wants.
17156 * </dd>
17157 *
17158 * <dt>EXACTLY</dt>
17159 * <dd>
17160 * The parent has determined an exact size for the child. The child is going to be
17161 * given those bounds regardless of how big it wants to be.
17162 * </dd>
17163 *
17164 * <dt>AT_MOST</dt>
17165 * <dd>
17166 * The child can be as large as it wants up to the specified size.
17167 * </dd>
17168 * </dl>
17169 *
17170 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17171 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17172 */
17173 public static class MeasureSpec {
17174 private static final int MODE_SHIFT = 30;
17175 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17176
17177 /**
17178 * Measure specification mode: The parent has not imposed any constraint
17179 * on the child. It can be whatever size it wants.
17180 */
17181 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17182
17183 /**
17184 * Measure specification mode: The parent has determined an exact size
17185 * for the child. The child is going to be given those bounds regardless
17186 * of how big it wants to be.
17187 */
17188 public static final int EXACTLY = 1 << MODE_SHIFT;
17189
17190 /**
17191 * Measure specification mode: The child can be as large as it wants up
17192 * to the specified size.
17193 */
17194 public static final int AT_MOST = 2 << MODE_SHIFT;
17195
17196 /**
17197 * Creates a measure specification based on the supplied size and mode.
17198 *
17199 * The mode must always be one of the following:
17200 * <ul>
17201 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17202 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17203 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17204 * </ul>
17205 *
17206 * @param size the size of the measure specification
17207 * @param mode the mode of the measure specification
17208 * @return the measure specification based on size and mode
17209 */
17210 public static int makeMeasureSpec(int size, int mode) {
17211 return size + mode;
17212 }
17213
17214 /**
17215 * Extracts the mode from the supplied measure specification.
17216 *
17217 * @param measureSpec the measure specification to extract the mode from
17218 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17219 * {@link android.view.View.MeasureSpec#AT_MOST} or
17220 * {@link android.view.View.MeasureSpec#EXACTLY}
17221 */
17222 public static int getMode(int measureSpec) {
17223 return (measureSpec & MODE_MASK);
17224 }
17225
17226 /**
17227 * Extracts the size from the supplied measure specification.
17228 *
17229 * @param measureSpec the measure specification to extract the size from
17230 * @return the size in pixels defined in the supplied measure specification
17231 */
17232 public static int getSize(int measureSpec) {
17233 return (measureSpec & ~MODE_MASK);
17234 }
17235
17236 /**
17237 * Returns a String representation of the specified measure
17238 * specification.
17239 *
17240 * @param measureSpec the measure specification to convert to a String
17241 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17242 */
17243 public static String toString(int measureSpec) {
17244 int mode = getMode(measureSpec);
17245 int size = getSize(measureSpec);
17246
17247 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17248
17249 if (mode == UNSPECIFIED)
17250 sb.append("UNSPECIFIED ");
17251 else if (mode == EXACTLY)
17252 sb.append("EXACTLY ");
17253 else if (mode == AT_MOST)
17254 sb.append("AT_MOST ");
17255 else
17256 sb.append(mode).append(" ");
17257
17258 sb.append(size);
17259 return sb.toString();
17260 }
17261 }
17262
17263 class CheckForLongPress implements Runnable {
17264
17265 private int mOriginalWindowAttachCount;
17266
17267 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017268 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017269 && mOriginalWindowAttachCount == mWindowAttachCount) {
17270 if (performLongClick()) {
17271 mHasPerformedLongPress = true;
17272 }
17273 }
17274 }
17275
17276 public void rememberWindowAttachCount() {
17277 mOriginalWindowAttachCount = mWindowAttachCount;
17278 }
17279 }
Joe Malin32736f02011-01-19 16:14:20 -080017280
Adam Powelle14579b2009-12-16 18:39:52 -080017281 private final class CheckForTap implements Runnable {
17282 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017283 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017284 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017285 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017286 }
17287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017288
Adam Powella35d7682010-03-12 14:48:13 -080017289 private final class PerformClick implements Runnable {
17290 public void run() {
17291 performClick();
17292 }
17293 }
17294
Dianne Hackborn63042d62011-01-26 18:56:29 -080017295 /** @hide */
17296 public void hackTurnOffWindowResizeAnim(boolean off) {
17297 mAttachInfo.mTurnOffWindowResizeAnim = off;
17298 }
Joe Malin32736f02011-01-19 16:14:20 -080017299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017300 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017301 * This method returns a ViewPropertyAnimator object, which can be used to animate
17302 * specific properties on this View.
17303 *
17304 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17305 */
17306 public ViewPropertyAnimator animate() {
17307 if (mAnimator == null) {
17308 mAnimator = new ViewPropertyAnimator(this);
17309 }
17310 return mAnimator;
17311 }
17312
17313 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017314 * Interface definition for a callback to be invoked when a hardware key event is
17315 * dispatched to this view. The callback will be invoked before the key event is
17316 * given to the view. This is only useful for hardware keyboards; a software input
17317 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017318 */
17319 public interface OnKeyListener {
17320 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017321 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017322 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017323 * <p>Key presses in software keyboards will generally NOT trigger this method,
17324 * although some may elect to do so in some situations. Do not assume a
17325 * software input method has to be key-based; even if it is, it may use key presses
17326 * in a different way than you expect, so there is no way to reliably catch soft
17327 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017328 *
17329 * @param v The view the key has been dispatched to.
17330 * @param keyCode The code for the physical key that was pressed
17331 * @param event The KeyEvent object containing full information about
17332 * the event.
17333 * @return True if the listener has consumed the event, false otherwise.
17334 */
17335 boolean onKey(View v, int keyCode, KeyEvent event);
17336 }
17337
17338 /**
17339 * Interface definition for a callback to be invoked when a touch event is
17340 * dispatched to this view. The callback will be invoked before the touch
17341 * event is given to the view.
17342 */
17343 public interface OnTouchListener {
17344 /**
17345 * Called when a touch event is dispatched to a view. This allows listeners to
17346 * get a chance to respond before the target view.
17347 *
17348 * @param v The view the touch event has been dispatched to.
17349 * @param event The MotionEvent object containing full information about
17350 * the event.
17351 * @return True if the listener has consumed the event, false otherwise.
17352 */
17353 boolean onTouch(View v, MotionEvent event);
17354 }
17355
17356 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017357 * Interface definition for a callback to be invoked when a hover event is
17358 * dispatched to this view. The callback will be invoked before the hover
17359 * event is given to the view.
17360 */
17361 public interface OnHoverListener {
17362 /**
17363 * Called when a hover event is dispatched to a view. This allows listeners to
17364 * get a chance to respond before the target view.
17365 *
17366 * @param v The view the hover event has been dispatched to.
17367 * @param event The MotionEvent object containing full information about
17368 * the event.
17369 * @return True if the listener has consumed the event, false otherwise.
17370 */
17371 boolean onHover(View v, MotionEvent event);
17372 }
17373
17374 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017375 * Interface definition for a callback to be invoked when a generic motion event is
17376 * dispatched to this view. The callback will be invoked before the generic motion
17377 * event is given to the view.
17378 */
17379 public interface OnGenericMotionListener {
17380 /**
17381 * Called when a generic motion event is dispatched to a view. This allows listeners to
17382 * get a chance to respond before the target view.
17383 *
17384 * @param v The view the generic motion event has been dispatched to.
17385 * @param event The MotionEvent object containing full information about
17386 * the event.
17387 * @return True if the listener has consumed the event, false otherwise.
17388 */
17389 boolean onGenericMotion(View v, MotionEvent event);
17390 }
17391
17392 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017393 * Interface definition for a callback to be invoked when a view has been clicked and held.
17394 */
17395 public interface OnLongClickListener {
17396 /**
17397 * Called when a view has been clicked and held.
17398 *
17399 * @param v The view that was clicked and held.
17400 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017401 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017402 */
17403 boolean onLongClick(View v);
17404 }
17405
17406 /**
Chris Tate32affef2010-10-18 15:29:21 -070017407 * Interface definition for a callback to be invoked when a drag is being dispatched
17408 * to this view. The callback will be invoked before the hosting view's own
17409 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17410 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017411 *
17412 * <div class="special reference">
17413 * <h3>Developer Guides</h3>
17414 * <p>For a guide to implementing drag and drop features, read the
17415 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17416 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017417 */
17418 public interface OnDragListener {
17419 /**
17420 * Called when a drag event is dispatched to a view. This allows listeners
17421 * to get a chance to override base View behavior.
17422 *
Joe Malin32736f02011-01-19 16:14:20 -080017423 * @param v The View that received the drag event.
17424 * @param event The {@link android.view.DragEvent} object for the drag event.
17425 * @return {@code true} if the drag event was handled successfully, or {@code false}
17426 * if the drag event was not handled. Note that {@code false} will trigger the View
17427 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017428 */
17429 boolean onDrag(View v, DragEvent event);
17430 }
17431
17432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017433 * Interface definition for a callback to be invoked when the focus state of
17434 * a view changed.
17435 */
17436 public interface OnFocusChangeListener {
17437 /**
17438 * Called when the focus state of a view has changed.
17439 *
17440 * @param v The view whose state has changed.
17441 * @param hasFocus The new focus state of v.
17442 */
17443 void onFocusChange(View v, boolean hasFocus);
17444 }
17445
17446 /**
17447 * Interface definition for a callback to be invoked when a view is clicked.
17448 */
17449 public interface OnClickListener {
17450 /**
17451 * Called when a view has been clicked.
17452 *
17453 * @param v The view that was clicked.
17454 */
17455 void onClick(View v);
17456 }
17457
17458 /**
17459 * Interface definition for a callback to be invoked when the context menu
17460 * for this view is being built.
17461 */
17462 public interface OnCreateContextMenuListener {
17463 /**
17464 * Called when the context menu for this view is being built. It is not
17465 * safe to hold onto the menu after this method returns.
17466 *
17467 * @param menu The context menu that is being built
17468 * @param v The view for which the context menu is being built
17469 * @param menuInfo Extra information about the item for which the
17470 * context menu should be shown. This information will vary
17471 * depending on the class of v.
17472 */
17473 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17474 }
17475
Joe Onorato664644d2011-01-23 17:53:23 -080017476 /**
17477 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017478 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017479 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017480 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017481 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017482 */
17483 public interface OnSystemUiVisibilityChangeListener {
17484 /**
17485 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017486 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017487 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017488 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17489 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17490 * This tells you the <strong>global</strong> state of these UI visibility
17491 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017492 */
17493 public void onSystemUiVisibilityChange(int visibility);
17494 }
17495
Adam Powell4afd62b2011-02-18 15:02:18 -080017496 /**
17497 * Interface definition for a callback to be invoked when this view is attached
17498 * or detached from its window.
17499 */
17500 public interface OnAttachStateChangeListener {
17501 /**
17502 * Called when the view is attached to a window.
17503 * @param v The view that was attached
17504 */
17505 public void onViewAttachedToWindow(View v);
17506 /**
17507 * Called when the view is detached from a window.
17508 * @param v The view that was detached
17509 */
17510 public void onViewDetachedFromWindow(View v);
17511 }
17512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017513 private final class UnsetPressedState implements Runnable {
17514 public void run() {
17515 setPressed(false);
17516 }
17517 }
17518
17519 /**
17520 * Base class for derived classes that want to save and restore their own
17521 * state in {@link android.view.View#onSaveInstanceState()}.
17522 */
17523 public static class BaseSavedState extends AbsSavedState {
17524 /**
17525 * Constructor used when reading from a parcel. Reads the state of the superclass.
17526 *
17527 * @param source
17528 */
17529 public BaseSavedState(Parcel source) {
17530 super(source);
17531 }
17532
17533 /**
17534 * Constructor called by derived classes when creating their SavedState objects
17535 *
17536 * @param superState The state of the superclass of this view
17537 */
17538 public BaseSavedState(Parcelable superState) {
17539 super(superState);
17540 }
17541
17542 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17543 new Parcelable.Creator<BaseSavedState>() {
17544 public BaseSavedState createFromParcel(Parcel in) {
17545 return new BaseSavedState(in);
17546 }
17547
17548 public BaseSavedState[] newArray(int size) {
17549 return new BaseSavedState[size];
17550 }
17551 };
17552 }
17553
17554 /**
17555 * A set of information given to a view when it is attached to its parent
17556 * window.
17557 */
17558 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017559 interface Callbacks {
17560 void playSoundEffect(int effectId);
17561 boolean performHapticFeedback(int effectId, boolean always);
17562 }
17563
17564 /**
17565 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17566 * to a Handler. This class contains the target (View) to invalidate and
17567 * the coordinates of the dirty rectangle.
17568 *
17569 * For performance purposes, this class also implements a pool of up to
17570 * POOL_LIMIT objects that get reused. This reduces memory allocations
17571 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017572 */
Romain Guyd928d682009-03-31 17:52:16 -070017573 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017574 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017575 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17576 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017577 public InvalidateInfo newInstance() {
17578 return new InvalidateInfo();
17579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017580
Romain Guyd928d682009-03-31 17:52:16 -070017581 public void onAcquired(InvalidateInfo element) {
17582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017583
Romain Guyd928d682009-03-31 17:52:16 -070017584 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017585 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017586 }
17587 }, POOL_LIMIT)
17588 );
17589
17590 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017591 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017592
17593 View target;
17594
17595 int left;
17596 int top;
17597 int right;
17598 int bottom;
17599
Romain Guyd928d682009-03-31 17:52:16 -070017600 public void setNextPoolable(InvalidateInfo element) {
17601 mNext = element;
17602 }
17603
17604 public InvalidateInfo getNextPoolable() {
17605 return mNext;
17606 }
17607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017608 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017609 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017610 }
17611
17612 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017613 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017614 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017615
17616 public boolean isPooled() {
17617 return mIsPooled;
17618 }
17619
17620 public void setPooled(boolean isPooled) {
17621 mIsPooled = isPooled;
17622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017623 }
17624
17625 final IWindowSession mSession;
17626
17627 final IWindow mWindow;
17628
17629 final IBinder mWindowToken;
17630
Jeff Brown98365d72012-08-19 20:30:52 -070017631 final Display mDisplay;
17632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017633 final Callbacks mRootCallbacks;
17634
Romain Guy59a12ca2011-06-09 17:48:21 -070017635 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017637 /**
17638 * The top view of the hierarchy.
17639 */
17640 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017642 IBinder mPanelParentWindowToken;
17643 Surface mSurface;
17644
Romain Guyb051e892010-09-28 19:09:36 -070017645 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017646 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017647 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017648
Romain Guy7e4e5612012-03-05 14:37:29 -080017649 boolean mScreenOn;
17650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017651 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017652 * Scale factor used by the compatibility mode
17653 */
17654 float mApplicationScale;
17655
17656 /**
17657 * Indicates whether the application is in compatibility mode
17658 */
17659 boolean mScalingRequired;
17660
17661 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017662 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017663 */
17664 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017665
Dianne Hackborn63042d62011-01-26 18:56:29 -080017666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017667 * Left position of this view's window
17668 */
17669 int mWindowLeft;
17670
17671 /**
17672 * Top position of this view's window
17673 */
17674 int mWindowTop;
17675
17676 /**
Adam Powell26153a32010-11-08 15:22:27 -080017677 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017678 */
Adam Powell26153a32010-11-08 15:22:27 -080017679 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017680
17681 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017682 * For windows that are full-screen but using insets to layout inside
17683 * of the screen decorations, these are the current insets for the
17684 * content of the window.
17685 */
17686 final Rect mContentInsets = new Rect();
17687
17688 /**
17689 * For windows that are full-screen but using insets to layout inside
17690 * of the screen decorations, these are the current insets for the
17691 * actual visible parts of the window.
17692 */
17693 final Rect mVisibleInsets = new Rect();
17694
17695 /**
17696 * The internal insets given by this window. This value is
17697 * supplied by the client (through
17698 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17699 * be given to the window manager when changed to be used in laying
17700 * out windows behind it.
17701 */
17702 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17703 = new ViewTreeObserver.InternalInsetsInfo();
17704
17705 /**
17706 * All views in the window's hierarchy that serve as scroll containers,
17707 * used to determine if the window can be resized or must be panned
17708 * to adjust for a soft input area.
17709 */
17710 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17711
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017712 final KeyEvent.DispatcherState mKeyDispatchState
17713 = new KeyEvent.DispatcherState();
17714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017715 /**
17716 * Indicates whether the view's window currently has the focus.
17717 */
17718 boolean mHasWindowFocus;
17719
17720 /**
17721 * The current visibility of the window.
17722 */
17723 int mWindowVisibility;
17724
17725 /**
17726 * Indicates the time at which drawing started to occur.
17727 */
17728 long mDrawingTime;
17729
17730 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017731 * Indicates whether or not ignoring the DIRTY_MASK flags.
17732 */
17733 boolean mIgnoreDirtyState;
17734
17735 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017736 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17737 * to avoid clearing that flag prematurely.
17738 */
17739 boolean mSetIgnoreDirtyState = false;
17740
17741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017742 * Indicates whether the view's window is currently in touch mode.
17743 */
17744 boolean mInTouchMode;
17745
17746 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017747 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017748 * the next time it performs a traversal
17749 */
17750 boolean mRecomputeGlobalAttributes;
17751
17752 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017753 * Always report new attributes at next traversal.
17754 */
17755 boolean mForceReportNewAttributes;
17756
17757 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017758 * Set during a traveral if any views want to keep the screen on.
17759 */
17760 boolean mKeepScreenOn;
17761
17762 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017763 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17764 */
17765 int mSystemUiVisibility;
17766
17767 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017768 * Hack to force certain system UI visibility flags to be cleared.
17769 */
17770 int mDisabledSystemUiVisibility;
17771
17772 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017773 * Last global system UI visibility reported by the window manager.
17774 */
17775 int mGlobalSystemUiVisibility;
17776
17777 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017778 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17779 * attached.
17780 */
17781 boolean mHasSystemUiListeners;
17782
17783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017784 * Set if the visibility of any views has changed.
17785 */
17786 boolean mViewVisibilityChanged;
17787
17788 /**
17789 * Set to true if a view has been scrolled.
17790 */
17791 boolean mViewScrollChanged;
17792
17793 /**
17794 * Global to the view hierarchy used as a temporary for dealing with
17795 * x/y points in the transparent region computations.
17796 */
17797 final int[] mTransparentLocation = new int[2];
17798
17799 /**
17800 * Global to the view hierarchy used as a temporary for dealing with
17801 * x/y points in the ViewGroup.invalidateChild implementation.
17802 */
17803 final int[] mInvalidateChildLocation = new int[2];
17804
Chet Haasec3aa3612010-06-17 08:50:37 -070017805
17806 /**
17807 * Global to the view hierarchy used as a temporary for dealing with
17808 * x/y location when view is transformed.
17809 */
17810 final float[] mTmpTransformLocation = new float[2];
17811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017812 /**
17813 * The view tree observer used to dispatch global events like
17814 * layout, pre-draw, touch mode change, etc.
17815 */
17816 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17817
17818 /**
17819 * A Canvas used by the view hierarchy to perform bitmap caching.
17820 */
17821 Canvas mCanvas;
17822
17823 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017824 * The view root impl.
17825 */
17826 final ViewRootImpl mViewRootImpl;
17827
17828 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017829 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017830 * handler can be used to pump events in the UI events queue.
17831 */
17832 final Handler mHandler;
17833
17834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017835 * Temporary for use in computing invalidate rectangles while
17836 * calling up the hierarchy.
17837 */
17838 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017839
17840 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017841 * Temporary for use in computing hit areas with transformed views
17842 */
17843 final RectF mTmpTransformRect = new RectF();
17844
17845 /**
Chet Haase599913d2012-07-23 16:22:05 -070017846 * Temporary for use in transforming invalidation rect
17847 */
17848 final Matrix mTmpMatrix = new Matrix();
17849
17850 /**
17851 * Temporary for use in transforming invalidation rect
17852 */
17853 final Transformation mTmpTransformation = new Transformation();
17854
17855 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017856 * Temporary list for use in collecting focusable descendents of a view.
17857 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017858 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017860 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017861 * The id of the window for accessibility purposes.
17862 */
17863 int mAccessibilityWindowId = View.NO_ID;
17864
17865 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017866 * Whether to ingore not exposed for accessibility Views when
17867 * reporting the view tree to accessibility services.
17868 */
17869 boolean mIncludeNotImportantViews;
17870
17871 /**
17872 * The drawable for highlighting accessibility focus.
17873 */
17874 Drawable mAccessibilityFocusDrawable;
17875
17876 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017877 * Show where the margins, bounds and layout bounds are for each view.
17878 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017879 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017880
17881 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017882 * Point used to compute visible regions.
17883 */
17884 final Point mPoint = new Point();
17885
17886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017887 * Creates a new set of attachment information with the specified
17888 * events handler and thread.
17889 *
17890 * @param handler the events handler the view must use
17891 */
Jeff Brown98365d72012-08-19 20:30:52 -070017892 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017893 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017894 mSession = session;
17895 mWindow = window;
17896 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017897 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017898 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017899 mHandler = handler;
17900 mRootCallbacks = effectPlayer;
17901 }
17902 }
17903
17904 /**
17905 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17906 * is supported. This avoids keeping too many unused fields in most
17907 * instances of View.</p>
17908 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017909 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017910
Mike Cleronf116bf82009-09-27 19:14:12 -070017911 /**
17912 * Scrollbars are not visible
17913 */
17914 public static final int OFF = 0;
17915
17916 /**
17917 * Scrollbars are visible
17918 */
17919 public static final int ON = 1;
17920
17921 /**
17922 * Scrollbars are fading away
17923 */
17924 public static final int FADING = 2;
17925
17926 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017928 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017929 public int scrollBarDefaultDelayBeforeFade;
17930 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017931
17932 public int scrollBarSize;
17933 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017934 public float[] interpolatorValues;
17935 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017936
17937 public final Paint paint;
17938 public final Matrix matrix;
17939 public Shader shader;
17940
Mike Cleronf116bf82009-09-27 19:14:12 -070017941 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17942
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017943 private static final float[] OPAQUE = { 255 };
17944 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017945
Mike Cleronf116bf82009-09-27 19:14:12 -070017946 /**
17947 * When fading should start. This time moves into the future every time
17948 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17949 */
17950 public long fadeStartTime;
17951
17952
17953 /**
17954 * The current state of the scrollbars: ON, OFF, or FADING
17955 */
17956 public int state = OFF;
17957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017958 private int mLastColor;
17959
Mike Cleronf116bf82009-09-27 19:14:12 -070017960 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017961 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17962 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017963 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17964 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017965
17966 paint = new Paint();
17967 matrix = new Matrix();
17968 // use use a height of 1, and then wack the matrix each time we
17969 // actually use it.
17970 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017971 paint.setShader(shader);
17972 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017973
Mike Cleronf116bf82009-09-27 19:14:12 -070017974 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017975 }
Romain Guy8506ab42009-06-11 17:35:47 -070017976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017977 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070017978 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017979 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070017980
Romain Guyd679b572012-08-29 21:49:00 -070017981 if (color != 0) {
17982 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17983 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17984 paint.setShader(shader);
17985 // Restore the default transfer mode (src_over)
17986 paint.setXfermode(null);
17987 } else {
17988 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17989 paint.setShader(shader);
17990 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017992 }
17993 }
Joe Malin32736f02011-01-19 16:14:20 -080017994
Mike Cleronf116bf82009-09-27 19:14:12 -070017995 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017996 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017997 if (now >= fadeStartTime) {
17998
17999 // the animation fades the scrollbars out by changing
18000 // the opacity (alpha) from fully opaque to fully
18001 // transparent
18002 int nextFrame = (int) now;
18003 int framesCount = 0;
18004
18005 Interpolator interpolator = scrollBarInterpolator;
18006
18007 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018008 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018009
18010 // End transparent
18011 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018012 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018013
18014 state = FADING;
18015
18016 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018017 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018018 }
18019 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018020 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018021
Svetoslav Ganova0156172011-06-26 17:55:44 -070018022 /**
18023 * Resuable callback for sending
18024 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18025 */
18026 private class SendViewScrolledAccessibilityEvent implements Runnable {
18027 public volatile boolean mIsPending;
18028
18029 public void run() {
18030 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18031 mIsPending = false;
18032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018033 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018034
18035 /**
18036 * <p>
18037 * This class represents a delegate that can be registered in a {@link View}
18038 * to enhance accessibility support via composition rather via inheritance.
18039 * It is specifically targeted to widget developers that extend basic View
18040 * classes i.e. classes in package android.view, that would like their
18041 * applications to be backwards compatible.
18042 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018043 * <div class="special reference">
18044 * <h3>Developer Guides</h3>
18045 * <p>For more information about making applications accessible, read the
18046 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18047 * developer guide.</p>
18048 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018049 * <p>
18050 * A scenario in which a developer would like to use an accessibility delegate
18051 * is overriding a method introduced in a later API version then the minimal API
18052 * version supported by the application. For example, the method
18053 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18054 * in API version 4 when the accessibility APIs were first introduced. If a
18055 * developer would like his application to run on API version 4 devices (assuming
18056 * all other APIs used by the application are version 4 or lower) and take advantage
18057 * of this method, instead of overriding the method which would break the application's
18058 * backwards compatibility, he can override the corresponding method in this
18059 * delegate and register the delegate in the target View if the API version of
18060 * the system is high enough i.e. the API version is same or higher to the API
18061 * version that introduced
18062 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18063 * </p>
18064 * <p>
18065 * Here is an example implementation:
18066 * </p>
18067 * <code><pre><p>
18068 * if (Build.VERSION.SDK_INT >= 14) {
18069 * // If the API version is equal of higher than the version in
18070 * // which onInitializeAccessibilityNodeInfo was introduced we
18071 * // register a delegate with a customized implementation.
18072 * View view = findViewById(R.id.view_id);
18073 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18074 * public void onInitializeAccessibilityNodeInfo(View host,
18075 * AccessibilityNodeInfo info) {
18076 * // Let the default implementation populate the info.
18077 * super.onInitializeAccessibilityNodeInfo(host, info);
18078 * // Set some other information.
18079 * info.setEnabled(host.isEnabled());
18080 * }
18081 * });
18082 * }
18083 * </code></pre></p>
18084 * <p>
18085 * This delegate contains methods that correspond to the accessibility methods
18086 * in View. If a delegate has been specified the implementation in View hands
18087 * off handling to the corresponding method in this delegate. The default
18088 * implementation the delegate methods behaves exactly as the corresponding
18089 * method in View for the case of no accessibility delegate been set. Hence,
18090 * to customize the behavior of a View method, clients can override only the
18091 * corresponding delegate method without altering the behavior of the rest
18092 * accessibility related methods of the host view.
18093 * </p>
18094 */
18095 public static class AccessibilityDelegate {
18096
18097 /**
18098 * Sends an accessibility event of the given type. If accessibility is not
18099 * enabled this method has no effect.
18100 * <p>
18101 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18102 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18103 * been set.
18104 * </p>
18105 *
18106 * @param host The View hosting the delegate.
18107 * @param eventType The type of the event to send.
18108 *
18109 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18110 */
18111 public void sendAccessibilityEvent(View host, int eventType) {
18112 host.sendAccessibilityEventInternal(eventType);
18113 }
18114
18115 /**
alanv8eeefef2012-05-07 16:57:53 -070018116 * Performs the specified accessibility action on the view. For
18117 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18118 * <p>
18119 * The default implementation behaves as
18120 * {@link View#performAccessibilityAction(int, Bundle)
18121 * View#performAccessibilityAction(int, Bundle)} for the case of
18122 * no accessibility delegate been set.
18123 * </p>
18124 *
18125 * @param action The action to perform.
18126 * @return Whether the action was performed.
18127 *
18128 * @see View#performAccessibilityAction(int, Bundle)
18129 * View#performAccessibilityAction(int, Bundle)
18130 */
18131 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18132 return host.performAccessibilityActionInternal(action, args);
18133 }
18134
18135 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018136 * Sends an accessibility event. This method behaves exactly as
18137 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18138 * empty {@link AccessibilityEvent} and does not perform a check whether
18139 * accessibility is enabled.
18140 * <p>
18141 * The default implementation behaves as
18142 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18143 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18144 * the case of no accessibility delegate been set.
18145 * </p>
18146 *
18147 * @param host The View hosting the delegate.
18148 * @param event The event to send.
18149 *
18150 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18151 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18152 */
18153 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18154 host.sendAccessibilityEventUncheckedInternal(event);
18155 }
18156
18157 /**
18158 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18159 * to its children for adding their text content to the event.
18160 * <p>
18161 * The default implementation behaves as
18162 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18163 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18164 * the case of no accessibility delegate been set.
18165 * </p>
18166 *
18167 * @param host The View hosting the delegate.
18168 * @param event The event.
18169 * @return True if the event population was completed.
18170 *
18171 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18172 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18173 */
18174 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18175 return host.dispatchPopulateAccessibilityEventInternal(event);
18176 }
18177
18178 /**
18179 * Gives a chance to the host View to populate the accessibility event with its
18180 * text content.
18181 * <p>
18182 * The default implementation behaves as
18183 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18184 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18185 * the case of no accessibility delegate been set.
18186 * </p>
18187 *
18188 * @param host The View hosting the delegate.
18189 * @param event The accessibility event which to populate.
18190 *
18191 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18192 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18193 */
18194 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18195 host.onPopulateAccessibilityEventInternal(event);
18196 }
18197
18198 /**
18199 * Initializes an {@link AccessibilityEvent} with information about the
18200 * the host View which is the event source.
18201 * <p>
18202 * The default implementation behaves as
18203 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18204 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18205 * the case of no accessibility delegate been set.
18206 * </p>
18207 *
18208 * @param host The View hosting the delegate.
18209 * @param event The event to initialize.
18210 *
18211 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18212 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18213 */
18214 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18215 host.onInitializeAccessibilityEventInternal(event);
18216 }
18217
18218 /**
18219 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18220 * <p>
18221 * The default implementation behaves as
18222 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18223 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18224 * the case of no accessibility delegate been set.
18225 * </p>
18226 *
18227 * @param host The View hosting the delegate.
18228 * @param info The instance to initialize.
18229 *
18230 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18231 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18232 */
18233 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18234 host.onInitializeAccessibilityNodeInfoInternal(info);
18235 }
18236
18237 /**
18238 * Called when a child of the host View has requested sending an
18239 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18240 * to augment the event.
18241 * <p>
18242 * The default implementation behaves as
18243 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18244 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18245 * the case of no accessibility delegate been set.
18246 * </p>
18247 *
18248 * @param host The View hosting the delegate.
18249 * @param child The child which requests sending the event.
18250 * @param event The event to be sent.
18251 * @return True if the event should be sent
18252 *
18253 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18254 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18255 */
18256 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18257 AccessibilityEvent event) {
18258 return host.onRequestSendAccessibilityEventInternal(child, event);
18259 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018260
18261 /**
18262 * Gets the provider for managing a virtual view hierarchy rooted at this View
18263 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18264 * that explore the window content.
18265 * <p>
18266 * The default implementation behaves as
18267 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18268 * the case of no accessibility delegate been set.
18269 * </p>
18270 *
18271 * @return The provider.
18272 *
18273 * @see AccessibilityNodeProvider
18274 */
18275 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18276 return null;
18277 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018278 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018279
18280 private class MatchIdPredicate implements Predicate<View> {
18281 public int mId;
18282
18283 @Override
18284 public boolean apply(View view) {
18285 return (view.mID == mId);
18286 }
18287 }
18288
18289 private class MatchLabelForPredicate implements Predicate<View> {
18290 private int mLabeledId;
18291
18292 @Override
18293 public boolean apply(View view) {
18294 return (view.mLabelForId == mLabeledId);
18295 }
18296 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018297
18298 /**
18299 * Dump all private flags in readable format, useful for documentation and
18300 * sanity checking.
18301 */
18302 private static void dumpFlags() {
18303 final HashMap<String, String> found = Maps.newHashMap();
18304 try {
18305 for (Field field : View.class.getDeclaredFields()) {
18306 final int modifiers = field.getModifiers();
18307 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18308 if (field.getType().equals(int.class)) {
18309 final int value = field.getInt(null);
18310 dumpFlag(found, field.getName(), value);
18311 } else if (field.getType().equals(int[].class)) {
18312 final int[] values = (int[]) field.get(null);
18313 for (int i = 0; i < values.length; i++) {
18314 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18315 }
18316 }
18317 }
18318 }
18319 } catch (IllegalAccessException e) {
18320 throw new RuntimeException(e);
18321 }
18322
18323 final ArrayList<String> keys = Lists.newArrayList();
18324 keys.addAll(found.keySet());
18325 Collections.sort(keys);
18326 for (String key : keys) {
18327 Log.d(VIEW_LOG_TAG, found.get(key));
18328 }
18329 }
18330
18331 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18332 // Sort flags by prefix, then by bits, always keeping unique keys
18333 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18334 final int prefix = name.indexOf('_');
18335 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18336 final String output = bits + " " + name;
18337 found.put(key, output);
18338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018339}