blob: 6eafc570b39e27991048b0ecb9746a9efa0477ec [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 Meglio80766f12012-10-01 13:13:12 -07002884 int mUserPaddingLeftInitial = 0;
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 Meglio80766f12012-10-01 13:13:12 -07002891 int mUserPaddingRightInitial = 0;
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
Romain Guydfab3632012-10-03 14:53:25 -07002907 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002908 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 private int mBackgroundResource;
2911 private boolean mBackgroundSizeChanged;
2912
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002913 static class ListenerInfo {
2914 /**
2915 * Listener used to dispatch focus change events.
2916 * This field should be made private, so it is hidden from the SDK.
2917 * {@hide}
2918 */
2919 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002921 /**
2922 * Listeners for layout change events.
2923 */
2924 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002925
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002926 /**
2927 * Listeners for attach events.
2928 */
2929 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002930
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002931 /**
2932 * Listener used to dispatch click events.
2933 * This field should be made private, so it is hidden from the SDK.
2934 * {@hide}
2935 */
2936 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002938 /**
2939 * Listener used to dispatch long click events.
2940 * This field should be made private, so it is hidden from the SDK.
2941 * {@hide}
2942 */
2943 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002945 /**
2946 * Listener used to build the context menu.
2947 * This field should be made private, so it is hidden from the SDK.
2948 * {@hide}
2949 */
2950 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002952 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002954 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002956 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002957
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002958 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002961
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002962 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2963 }
2964
2965 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 /**
2968 * The application environment this view lives in.
2969 * This field should be made private, so it is hidden from the SDK.
2970 * {@hide}
2971 */
2972 protected Context mContext;
2973
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002974 private final Resources mResources;
2975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 private ScrollabilityCache mScrollCache;
2977
2978 private int[] mDrawableState = null;
2979
Romain Guy0211a0a2011-02-14 16:34:59 -08002980 /**
2981 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002982 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002983 * @hide
2984 */
2985 public boolean mCachingFailed;
2986
Romain Guy02890fd2010-08-06 17:58:44 -07002987 private Bitmap mDrawingCache;
2988 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002989 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002990 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991
2992 /**
2993 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2994 * the user may specify which view to go to next.
2995 */
2996 private int mNextFocusLeftId = View.NO_ID;
2997
2998 /**
2999 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3000 * the user may specify which view to go to next.
3001 */
3002 private int mNextFocusRightId = View.NO_ID;
3003
3004 /**
3005 * When this view has focus and the next focus is {@link #FOCUS_UP},
3006 * the user may specify which view to go to next.
3007 */
3008 private int mNextFocusUpId = View.NO_ID;
3009
3010 /**
3011 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3012 * the user may specify which view to go to next.
3013 */
3014 private int mNextFocusDownId = View.NO_ID;
3015
Jeff Brown4e6319b2010-12-13 10:36:51 -08003016 /**
3017 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3018 * the user may specify which view to go to next.
3019 */
3020 int mNextFocusForwardId = View.NO_ID;
3021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003023 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003024 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003025 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 private UnsetPressedState mUnsetPressedState;
3028
3029 /**
3030 * Whether the long press's action has been invoked. The tap's action is invoked on the
3031 * up event while a long press is invoked as soon as the long press duration is reached, so
3032 * a long press could be performed before the tap is checked, in which case the tap's action
3033 * should not be invoked.
3034 */
3035 private boolean mHasPerformedLongPress;
3036
3037 /**
3038 * The minimum height of the view. We'll try our best to have the height
3039 * of this view to at least this amount.
3040 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003041 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 private int mMinHeight;
3043
3044 /**
3045 * The minimum width of the view. We'll try our best to have the width
3046 * of this view to at least this amount.
3047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003048 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 private int mMinWidth;
3050
3051 /**
3052 * The delegate to handle touch events that are physically in this view
3053 * but should be handled by another view.
3054 */
3055 private TouchDelegate mTouchDelegate = null;
3056
3057 /**
3058 * Solid color to use as a background when creating the drawing cache. Enables
3059 * the cache to use 16 bit bitmaps instead of 32 bit.
3060 */
3061 private int mDrawingCacheBackgroundColor = 0;
3062
3063 /**
3064 * Special tree observer used when mAttachInfo is null.
3065 */
3066 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003067
Adam Powelle14579b2009-12-16 18:39:52 -08003068 /**
3069 * Cache the touch slop from the context that created the view.
3070 */
3071 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003074 * Object that handles automatic animation of view properties.
3075 */
3076 private ViewPropertyAnimator mAnimator = null;
3077
3078 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003079 * Flag indicating that a drag can cross window boundaries. When
3080 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3081 * with this flag set, all visible applications will be able to participate
3082 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003083 *
3084 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003085 */
3086 public static final int DRAG_FLAG_GLOBAL = 1;
3087
3088 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003089 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3090 */
3091 private float mVerticalScrollFactor;
3092
3093 /**
Adam Powell20232d02010-12-08 21:08:53 -08003094 * Position of the vertical scroll bar.
3095 */
3096 private int mVerticalScrollbarPosition;
3097
3098 /**
3099 * Position the scroll bar at the default position as determined by the system.
3100 */
3101 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3102
3103 /**
3104 * Position the scroll bar along the left edge.
3105 */
3106 public static final int SCROLLBAR_POSITION_LEFT = 1;
3107
3108 /**
3109 * Position the scroll bar along the right edge.
3110 */
3111 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3112
3113 /**
Romain Guy171c5922011-01-06 10:04:23 -08003114 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003115 *
3116 * @see #getLayerType()
3117 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003118 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003119 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003120 */
3121 public static final int LAYER_TYPE_NONE = 0;
3122
3123 /**
3124 * <p>Indicates that the view has a software layer. A software layer is backed
3125 * by a bitmap and causes the view to be rendered using Android's software
3126 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003127 *
Romain Guy171c5922011-01-06 10:04:23 -08003128 * <p>Software layers have various usages:</p>
3129 * <p>When the application is not using hardware acceleration, a software layer
3130 * is useful to apply a specific color filter and/or blending mode and/or
3131 * translucency to a view and all its children.</p>
3132 * <p>When the application is using hardware acceleration, a software layer
3133 * is useful to render drawing primitives not supported by the hardware
3134 * accelerated pipeline. It can also be used to cache a complex view tree
3135 * into a texture and reduce the complexity of drawing operations. For instance,
3136 * when animating a complex view tree with a translation, a software layer can
3137 * be used to render the view tree only once.</p>
3138 * <p>Software layers should be avoided when the affected view tree updates
3139 * often. Every update will require to re-render the software layer, which can
3140 * potentially be slow (particularly when hardware acceleration is turned on
3141 * since the layer will have to be uploaded into a hardware texture after every
3142 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003143 *
3144 * @see #getLayerType()
3145 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003146 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003147 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003148 */
3149 public static final int LAYER_TYPE_SOFTWARE = 1;
3150
3151 /**
3152 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3153 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3154 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3155 * rendering pipeline, but only if hardware acceleration is turned on for the
3156 * view hierarchy. When hardware acceleration is turned off, hardware layers
3157 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003158 *
Romain Guy171c5922011-01-06 10:04:23 -08003159 * <p>A hardware layer is useful to apply a specific color filter and/or
3160 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003161 * <p>A hardware layer can be used to cache a complex view tree into a
3162 * texture and reduce the complexity of drawing operations. For instance,
3163 * when animating a complex view tree with a translation, a hardware layer can
3164 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003165 * <p>A hardware layer can also be used to increase the rendering quality when
3166 * rotation transformations are applied on a view. It can also be used to
3167 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003168 *
3169 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003170 * @see #setLayerType(int, android.graphics.Paint)
3171 * @see #LAYER_TYPE_NONE
3172 * @see #LAYER_TYPE_SOFTWARE
3173 */
3174 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003175
Romain Guy3aaff3a2011-01-12 14:18:47 -08003176 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3177 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3178 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3179 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3180 })
Romain Guy171c5922011-01-06 10:04:23 -08003181 int mLayerType = LAYER_TYPE_NONE;
3182 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003183 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003184
3185 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003186 * Set to true when the view is sending hover accessibility events because it
3187 * is the innermost hovered view.
3188 */
3189 private boolean mSendingHoverAccessibilityEvents;
3190
Dianne Hackborn4702a852012-08-17 15:18:29 -07003191 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003192 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003193 */
3194 AccessibilityDelegate mAccessibilityDelegate;
3195
3196 /**
3197 * Consistency verifier for debugging purposes.
3198 * @hide
3199 */
3200 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3201 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3202 new InputEventConsistencyVerifier(this, 0) : null;
3203
Adam Powella9108a22012-07-18 11:18:09 -07003204 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3205
Jeff Brown87b7f802011-06-21 18:35:45 -07003206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 * Simple constructor to use when creating a view from code.
3208 *
3209 * @param context The Context the view is running in, through which it can
3210 * access the current theme, resources, etc.
3211 */
3212 public View(Context context) {
3213 mContext = context;
3214 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003215 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003216 // Set layout and text direction defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003217 mPrivateFlags2 =
3218 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003219 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003220 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003221 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003222 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003223 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003224 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003225 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003226 mUserPaddingStart = UNDEFINED_PADDING;
3227 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
3229
3230 /**
3231 * Constructor that is called when inflating a view from XML. This is called
3232 * when a view is being constructed from an XML file, supplying attributes
3233 * that were specified in the XML file. This version uses a default style of
3234 * 0, so the only attribute values applied are those in the Context's Theme
3235 * and the given AttributeSet.
3236 *
3237 * <p>
3238 * The method onFinishInflate() will be called after all children have been
3239 * added.
3240 *
3241 * @param context The Context the view is running in, through which it can
3242 * access the current theme, resources, etc.
3243 * @param attrs The attributes of the XML tag that is inflating the view.
3244 * @see #View(Context, AttributeSet, int)
3245 */
3246 public View(Context context, AttributeSet attrs) {
3247 this(context, attrs, 0);
3248 }
3249
3250 /**
3251 * Perform inflation from XML and apply a class-specific base style. This
3252 * constructor of View allows subclasses to use their own base style when
3253 * they are inflating. For example, a Button class's constructor would call
3254 * this version of the super class constructor and supply
3255 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3256 * the theme's button style to modify all of the base view attributes (in
3257 * particular its background) as well as the Button class's attributes.
3258 *
3259 * @param context The Context the view is running in, through which it can
3260 * access the current theme, resources, etc.
3261 * @param attrs The attributes of the XML tag that is inflating the view.
3262 * @param defStyle The default style to apply to this view. If 0, no style
3263 * will be applied (beyond what is included in the theme). This may
3264 * either be an attribute resource, whose value will be retrieved
3265 * from the current theme, or an explicit style resource.
3266 * @see #View(Context, AttributeSet)
3267 */
3268 public View(Context context, AttributeSet attrs, int defStyle) {
3269 this(context);
3270
3271 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3272 defStyle, 0);
3273
3274 Drawable background = null;
3275
3276 int leftPadding = -1;
3277 int topPadding = -1;
3278 int rightPadding = -1;
3279 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003280 int startPadding = UNDEFINED_PADDING;
3281 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282
3283 int padding = -1;
3284
3285 int viewFlagValues = 0;
3286 int viewFlagMasks = 0;
3287
3288 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 int x = 0;
3291 int y = 0;
3292
Chet Haase73066682010-11-29 15:55:32 -08003293 float tx = 0;
3294 float ty = 0;
3295 float rotation = 0;
3296 float rotationX = 0;
3297 float rotationY = 0;
3298 float sx = 1f;
3299 float sy = 1f;
3300 boolean transformSet = false;
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003303 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003304 boolean initializeScrollbars = false;
3305
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003306 boolean leftPaddingDefined = false;
3307 boolean rightPaddingDefined = false;
3308 boolean startPaddingDefined = false;
3309 boolean endPaddingDefined = false;
3310
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003311 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 final int N = a.getIndexCount();
3314 for (int i = 0; i < N; i++) {
3315 int attr = a.getIndex(i);
3316 switch (attr) {
3317 case com.android.internal.R.styleable.View_background:
3318 background = a.getDrawable(attr);
3319 break;
3320 case com.android.internal.R.styleable.View_padding:
3321 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003322 mUserPaddingLeftInitial = padding;
3323 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003324 leftPaddingDefined = true;
3325 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 break;
3327 case com.android.internal.R.styleable.View_paddingLeft:
3328 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003329 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003330 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 break;
3332 case com.android.internal.R.styleable.View_paddingTop:
3333 topPadding = a.getDimensionPixelSize(attr, -1);
3334 break;
3335 case com.android.internal.R.styleable.View_paddingRight:
3336 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003337 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003338 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 break;
3340 case com.android.internal.R.styleable.View_paddingBottom:
3341 bottomPadding = a.getDimensionPixelSize(attr, -1);
3342 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003343 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003344 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003345 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003346 break;
3347 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003348 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003349 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003350 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 case com.android.internal.R.styleable.View_scrollX:
3352 x = a.getDimensionPixelOffset(attr, 0);
3353 break;
3354 case com.android.internal.R.styleable.View_scrollY:
3355 y = a.getDimensionPixelOffset(attr, 0);
3356 break;
Chet Haase73066682010-11-29 15:55:32 -08003357 case com.android.internal.R.styleable.View_alpha:
3358 setAlpha(a.getFloat(attr, 1f));
3359 break;
3360 case com.android.internal.R.styleable.View_transformPivotX:
3361 setPivotX(a.getDimensionPixelOffset(attr, 0));
3362 break;
3363 case com.android.internal.R.styleable.View_transformPivotY:
3364 setPivotY(a.getDimensionPixelOffset(attr, 0));
3365 break;
3366 case com.android.internal.R.styleable.View_translationX:
3367 tx = a.getDimensionPixelOffset(attr, 0);
3368 transformSet = true;
3369 break;
3370 case com.android.internal.R.styleable.View_translationY:
3371 ty = a.getDimensionPixelOffset(attr, 0);
3372 transformSet = true;
3373 break;
3374 case com.android.internal.R.styleable.View_rotation:
3375 rotation = a.getFloat(attr, 0);
3376 transformSet = true;
3377 break;
3378 case com.android.internal.R.styleable.View_rotationX:
3379 rotationX = a.getFloat(attr, 0);
3380 transformSet = true;
3381 break;
3382 case com.android.internal.R.styleable.View_rotationY:
3383 rotationY = a.getFloat(attr, 0);
3384 transformSet = true;
3385 break;
3386 case com.android.internal.R.styleable.View_scaleX:
3387 sx = a.getFloat(attr, 1f);
3388 transformSet = true;
3389 break;
3390 case com.android.internal.R.styleable.View_scaleY:
3391 sy = a.getFloat(attr, 1f);
3392 transformSet = true;
3393 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 case com.android.internal.R.styleable.View_id:
3395 mID = a.getResourceId(attr, NO_ID);
3396 break;
3397 case com.android.internal.R.styleable.View_tag:
3398 mTag = a.getText(attr);
3399 break;
3400 case com.android.internal.R.styleable.View_fitsSystemWindows:
3401 if (a.getBoolean(attr, false)) {
3402 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3403 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_focusable:
3407 if (a.getBoolean(attr, false)) {
3408 viewFlagValues |= FOCUSABLE;
3409 viewFlagMasks |= FOCUSABLE_MASK;
3410 }
3411 break;
3412 case com.android.internal.R.styleable.View_focusableInTouchMode:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3415 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3416 }
3417 break;
3418 case com.android.internal.R.styleable.View_clickable:
3419 if (a.getBoolean(attr, false)) {
3420 viewFlagValues |= CLICKABLE;
3421 viewFlagMasks |= CLICKABLE;
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_longClickable:
3425 if (a.getBoolean(attr, false)) {
3426 viewFlagValues |= LONG_CLICKABLE;
3427 viewFlagMasks |= LONG_CLICKABLE;
3428 }
3429 break;
3430 case com.android.internal.R.styleable.View_saveEnabled:
3431 if (!a.getBoolean(attr, true)) {
3432 viewFlagValues |= SAVE_DISABLED;
3433 viewFlagMasks |= SAVE_DISABLED_MASK;
3434 }
3435 break;
3436 case com.android.internal.R.styleable.View_duplicateParentState:
3437 if (a.getBoolean(attr, false)) {
3438 viewFlagValues |= DUPLICATE_PARENT_STATE;
3439 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3440 }
3441 break;
3442 case com.android.internal.R.styleable.View_visibility:
3443 final int visibility = a.getInt(attr, 0);
3444 if (visibility != 0) {
3445 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3446 viewFlagMasks |= VISIBILITY_MASK;
3447 }
3448 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003449 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003450 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003451 mPrivateFlags2 &=
3452 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003453 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003454 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003455 final int value = (layoutDirection != -1) ?
3456 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003457 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003458 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 case com.android.internal.R.styleable.View_drawingCacheQuality:
3460 final int cacheQuality = a.getInt(attr, 0);
3461 if (cacheQuality != 0) {
3462 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3463 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3464 }
3465 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003466 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003467 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003468 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003469 case com.android.internal.R.styleable.View_labelFor:
3470 setLabelFor(a.getResourceId(attr, NO_ID));
3471 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3473 if (!a.getBoolean(attr, true)) {
3474 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3475 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3476 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003477 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3479 if (!a.getBoolean(attr, true)) {
3480 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3481 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3482 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003483 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 case R.styleable.View_scrollbars:
3485 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3486 if (scrollbars != SCROLLBARS_NONE) {
3487 viewFlagValues |= scrollbars;
3488 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003489 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 }
3491 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003492 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003494 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003495 // Ignore the attribute starting with ICS
3496 break;
3497 }
3498 // With builds < ICS, fall through and apply fading edges
3499 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3501 if (fadingEdge != FADING_EDGE_NONE) {
3502 viewFlagValues |= fadingEdge;
3503 viewFlagMasks |= FADING_EDGE_MASK;
3504 initializeFadingEdge(a);
3505 }
3506 break;
3507 case R.styleable.View_scrollbarStyle:
3508 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3509 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3510 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3511 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3512 }
3513 break;
3514 case R.styleable.View_isScrollContainer:
3515 setScrollContainer = true;
3516 if (a.getBoolean(attr, false)) {
3517 setScrollContainer(true);
3518 }
3519 break;
3520 case com.android.internal.R.styleable.View_keepScreenOn:
3521 if (a.getBoolean(attr, false)) {
3522 viewFlagValues |= KEEP_SCREEN_ON;
3523 viewFlagMasks |= KEEP_SCREEN_ON;
3524 }
3525 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003526 case R.styleable.View_filterTouchesWhenObscured:
3527 if (a.getBoolean(attr, false)) {
3528 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3529 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3530 }
3531 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 case R.styleable.View_nextFocusLeft:
3533 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3534 break;
3535 case R.styleable.View_nextFocusRight:
3536 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3537 break;
3538 case R.styleable.View_nextFocusUp:
3539 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3540 break;
3541 case R.styleable.View_nextFocusDown:
3542 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3543 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003544 case R.styleable.View_nextFocusForward:
3545 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3546 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 case R.styleable.View_minWidth:
3548 mMinWidth = a.getDimensionPixelSize(attr, 0);
3549 break;
3550 case R.styleable.View_minHeight:
3551 mMinHeight = a.getDimensionPixelSize(attr, 0);
3552 break;
Romain Guy9a817362009-05-01 10:57:14 -07003553 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003554 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003555 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003556 + "be used within a restricted context");
3557 }
3558
Romain Guy9a817362009-05-01 10:57:14 -07003559 final String handlerName = a.getString(attr);
3560 if (handlerName != null) {
3561 setOnClickListener(new OnClickListener() {
3562 private Method mHandler;
3563
3564 public void onClick(View v) {
3565 if (mHandler == null) {
3566 try {
3567 mHandler = getContext().getClass().getMethod(handlerName,
3568 View.class);
3569 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003570 int id = getId();
3571 String idText = id == NO_ID ? "" : " with id '"
3572 + getContext().getResources().getResourceEntryName(
3573 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003574 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003575 handlerName + "(View) in the activity "
3576 + getContext().getClass() + " for onClick handler"
3577 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003578 }
3579 }
3580
3581 try {
3582 mHandler.invoke(getContext(), View.this);
3583 } catch (IllegalAccessException e) {
3584 throw new IllegalStateException("Could not execute non "
3585 + "public method of the activity", e);
3586 } catch (InvocationTargetException e) {
3587 throw new IllegalStateException("Could not execute "
3588 + "method of the activity", e);
3589 }
3590 }
3591 });
3592 }
3593 break;
Adam Powell637d3372010-08-25 14:37:03 -07003594 case R.styleable.View_overScrollMode:
3595 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3596 break;
Adam Powell20232d02010-12-08 21:08:53 -08003597 case R.styleable.View_verticalScrollbarPosition:
3598 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3599 break;
Romain Guy171c5922011-01-06 10:04:23 -08003600 case R.styleable.View_layerType:
3601 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3602 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003603 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003604 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003605 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003606 // Set the text direction flags depending on the value of the attribute
3607 final int textDirection = a.getInt(attr, -1);
3608 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003609 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003610 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003611 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003612 case R.styleable.View_textAlignment:
3613 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003614 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003615 // Set the text alignment flag depending on the value of the attribute
3616 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003617 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003618 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003619 case R.styleable.View_importantForAccessibility:
3620 setImportantForAccessibility(a.getInt(attr,
3621 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
3624 }
3625
Adam Powell637d3372010-08-25 14:37:03 -07003626 setOverScrollMode(overScrollMode);
3627
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003628 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3629 // the resolved layout direction). Those cached values will be used later during padding
3630 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003631 mUserPaddingStart = startPadding;
3632 mUserPaddingEnd = endPadding;
3633
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003634 if (background != null) {
3635 setBackground(background);
3636 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 if (padding >= 0) {
3639 leftPadding = padding;
3640 topPadding = padding;
3641 rightPadding = padding;
3642 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003643 mUserPaddingLeftInitial = padding;
3644 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
3646
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003647 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003648 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3649 // left / right padding are used if defined (meaning here nothing to do). If they are not
3650 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3651 // start / end and resolve them as left / right (layout direction is not taken into account).
3652 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3653 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3654 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003655 if (!leftPaddingDefined && startPaddingDefined) {
3656 leftPadding = startPadding;
3657 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003658 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003659 if (!rightPaddingDefined && endPaddingDefined) {
3660 rightPadding = endPadding;
3661 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003662 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003663 } else {
3664 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3665 // values defined. Otherwise, left /right values are used.
3666 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3667 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3668 // defined.
3669 if (startPaddingDefined) {
3670 mUserPaddingLeftInitial = startPadding;
3671 } else if (leftPaddingDefined) {
3672 mUserPaddingLeftInitial = leftPadding;
3673 }
3674 if (endPaddingDefined) {
3675 mUserPaddingRightInitial = endPadding;
3676 }
3677 else if (rightPaddingDefined) {
3678 mUserPaddingRightInitial = rightPadding;
3679 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003680 }
3681
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003682 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003683 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003685 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3687
3688 if (viewFlagMasks != 0) {
3689 setFlags(viewFlagValues, viewFlagMasks);
3690 }
3691
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003692 if (initializeScrollbars) {
3693 initializeScrollbars(a);
3694 }
3695
3696 a.recycle();
3697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 // Needs to be called after mViewFlags is set
3699 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3700 recomputePadding();
3701 }
3702
3703 if (x != 0 || y != 0) {
3704 scrollTo(x, y);
3705 }
3706
Chet Haase73066682010-11-29 15:55:32 -08003707 if (transformSet) {
3708 setTranslationX(tx);
3709 setTranslationY(ty);
3710 setRotation(rotation);
3711 setRotationX(rotationX);
3712 setRotationY(rotationY);
3713 setScaleX(sx);
3714 setScaleY(sy);
3715 }
3716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3718 setScrollContainer(true);
3719 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003720
3721 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723
3724 /**
3725 * Non-public constructor for use in testing
3726 */
3727 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003728 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 }
3730
Dianne Hackborn4702a852012-08-17 15:18:29 -07003731 public String toString() {
3732 StringBuilder out = new StringBuilder(128);
3733 out.append(getClass().getName());
3734 out.append('{');
3735 out.append(Integer.toHexString(System.identityHashCode(this)));
3736 out.append(' ');
3737 switch (mViewFlags&VISIBILITY_MASK) {
3738 case VISIBLE: out.append('V'); break;
3739 case INVISIBLE: out.append('I'); break;
3740 case GONE: out.append('G'); break;
3741 default: out.append('.'); break;
3742 }
3743 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3744 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3745 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3746 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3747 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3748 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3749 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3750 out.append(' ');
3751 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3752 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3753 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3754 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3755 out.append('p');
3756 } else {
3757 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3758 }
3759 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3760 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3761 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3762 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3763 out.append(' ');
3764 out.append(mLeft);
3765 out.append(',');
3766 out.append(mTop);
3767 out.append('-');
3768 out.append(mRight);
3769 out.append(',');
3770 out.append(mBottom);
3771 final int id = getId();
3772 if (id != NO_ID) {
3773 out.append(" #");
3774 out.append(Integer.toHexString(id));
3775 final Resources r = mResources;
3776 if (id != 0 && r != null) {
3777 try {
3778 String pkgname;
3779 switch (id&0xff000000) {
3780 case 0x7f000000:
3781 pkgname="app";
3782 break;
3783 case 0x01000000:
3784 pkgname="android";
3785 break;
3786 default:
3787 pkgname = r.getResourcePackageName(id);
3788 break;
3789 }
3790 String typename = r.getResourceTypeName(id);
3791 String entryname = r.getResourceEntryName(id);
3792 out.append(" ");
3793 out.append(pkgname);
3794 out.append(":");
3795 out.append(typename);
3796 out.append("/");
3797 out.append(entryname);
3798 } catch (Resources.NotFoundException e) {
3799 }
3800 }
3801 }
3802 out.append("}");
3803 return out.toString();
3804 }
3805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 /**
3807 * <p>
3808 * Initializes the fading edges from a given set of styled attributes. This
3809 * method should be called by subclasses that need fading edges and when an
3810 * instance of these subclasses is created programmatically rather than
3811 * being inflated from XML. This method is automatically called when the XML
3812 * is inflated.
3813 * </p>
3814 *
3815 * @param a the styled attributes set to initialize the fading edges from
3816 */
3817 protected void initializeFadingEdge(TypedArray a) {
3818 initScrollCache();
3819
3820 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3821 R.styleable.View_fadingEdgeLength,
3822 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3823 }
3824
3825 /**
3826 * Returns the size of the vertical faded edges used to indicate that more
3827 * content in this view is visible.
3828 *
3829 * @return The size in pixels of the vertical faded edge or 0 if vertical
3830 * faded edges are not enabled for this view.
3831 * @attr ref android.R.styleable#View_fadingEdgeLength
3832 */
3833 public int getVerticalFadingEdgeLength() {
3834 if (isVerticalFadingEdgeEnabled()) {
3835 ScrollabilityCache cache = mScrollCache;
3836 if (cache != null) {
3837 return cache.fadingEdgeLength;
3838 }
3839 }
3840 return 0;
3841 }
3842
3843 /**
3844 * Set the size of the faded edge used to indicate that more content in this
3845 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003846 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3847 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3848 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 *
3850 * @param length The size in pixels of the faded edge used to indicate that more
3851 * content in this view is visible.
3852 */
3853 public void setFadingEdgeLength(int length) {
3854 initScrollCache();
3855 mScrollCache.fadingEdgeLength = length;
3856 }
3857
3858 /**
3859 * Returns the size of the horizontal faded edges used to indicate that more
3860 * content in this view is visible.
3861 *
3862 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3863 * faded edges are not enabled for this view.
3864 * @attr ref android.R.styleable#View_fadingEdgeLength
3865 */
3866 public int getHorizontalFadingEdgeLength() {
3867 if (isHorizontalFadingEdgeEnabled()) {
3868 ScrollabilityCache cache = mScrollCache;
3869 if (cache != null) {
3870 return cache.fadingEdgeLength;
3871 }
3872 }
3873 return 0;
3874 }
3875
3876 /**
3877 * Returns the width of the vertical scrollbar.
3878 *
3879 * @return The width in pixels of the vertical scrollbar or 0 if there
3880 * is no vertical scrollbar.
3881 */
3882 public int getVerticalScrollbarWidth() {
3883 ScrollabilityCache cache = mScrollCache;
3884 if (cache != null) {
3885 ScrollBarDrawable scrollBar = cache.scrollBar;
3886 if (scrollBar != null) {
3887 int size = scrollBar.getSize(true);
3888 if (size <= 0) {
3889 size = cache.scrollBarSize;
3890 }
3891 return size;
3892 }
3893 return 0;
3894 }
3895 return 0;
3896 }
3897
3898 /**
3899 * Returns the height of the horizontal scrollbar.
3900 *
3901 * @return The height in pixels of the horizontal scrollbar or 0 if
3902 * there is no horizontal scrollbar.
3903 */
3904 protected int getHorizontalScrollbarHeight() {
3905 ScrollabilityCache cache = mScrollCache;
3906 if (cache != null) {
3907 ScrollBarDrawable scrollBar = cache.scrollBar;
3908 if (scrollBar != null) {
3909 int size = scrollBar.getSize(false);
3910 if (size <= 0) {
3911 size = cache.scrollBarSize;
3912 }
3913 return size;
3914 }
3915 return 0;
3916 }
3917 return 0;
3918 }
3919
3920 /**
3921 * <p>
3922 * Initializes the scrollbars from a given set of styled attributes. This
3923 * method should be called by subclasses that need scrollbars and when an
3924 * instance of these subclasses is created programmatically rather than
3925 * being inflated from XML. This method is automatically called when the XML
3926 * is inflated.
3927 * </p>
3928 *
3929 * @param a the styled attributes set to initialize the scrollbars from
3930 */
3931 protected void initializeScrollbars(TypedArray a) {
3932 initScrollCache();
3933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003935
Mike Cleronf116bf82009-09-27 19:14:12 -07003936 if (scrollabilityCache.scrollBar == null) {
3937 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3938 }
Joe Malin32736f02011-01-19 16:14:20 -08003939
Romain Guy8bda2482010-03-02 11:42:11 -08003940 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941
Mike Cleronf116bf82009-09-27 19:14:12 -07003942 if (!fadeScrollbars) {
3943 scrollabilityCache.state = ScrollabilityCache.ON;
3944 }
3945 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003946
3947
Mike Cleronf116bf82009-09-27 19:14:12 -07003948 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3949 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3950 .getScrollBarFadeDuration());
3951 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3952 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3953 ViewConfiguration.getScrollDefaultDelay());
3954
Joe Malin32736f02011-01-19 16:14:20 -08003955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3957 com.android.internal.R.styleable.View_scrollbarSize,
3958 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3959
3960 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3961 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3962
3963 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3964 if (thumb != null) {
3965 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3966 }
3967
3968 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3969 false);
3970 if (alwaysDraw) {
3971 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3972 }
3973
3974 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3975 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3976
3977 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3978 if (thumb != null) {
3979 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3980 }
3981
3982 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3983 false);
3984 if (alwaysDraw) {
3985 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3986 }
3987
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003988 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003989 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003990 if (track != null) {
3991 track.setLayoutDirection(layoutDirection);
3992 }
3993 if (thumb != null) {
3994 thumb.setLayoutDirection(layoutDirection);
3995 }
3996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003998 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 }
4000
4001 /**
4002 * <p>
4003 * Initalizes the scrollability cache if necessary.
4004 * </p>
4005 */
4006 private void initScrollCache() {
4007 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004008 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 }
4010 }
4011
Philip Milne6c8ea062012-04-03 17:38:43 -07004012 private ScrollabilityCache getScrollCache() {
4013 initScrollCache();
4014 return mScrollCache;
4015 }
4016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 /**
Adam Powell20232d02010-12-08 21:08:53 -08004018 * Set the position of the vertical scroll bar. Should be one of
4019 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4020 * {@link #SCROLLBAR_POSITION_RIGHT}.
4021 *
4022 * @param position Where the vertical scroll bar should be positioned.
4023 */
4024 public void setVerticalScrollbarPosition(int position) {
4025 if (mVerticalScrollbarPosition != position) {
4026 mVerticalScrollbarPosition = position;
4027 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004028 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004029 }
4030 }
4031
4032 /**
4033 * @return The position where the vertical scroll bar will show, if applicable.
4034 * @see #setVerticalScrollbarPosition(int)
4035 */
4036 public int getVerticalScrollbarPosition() {
4037 return mVerticalScrollbarPosition;
4038 }
4039
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004040 ListenerInfo getListenerInfo() {
4041 if (mListenerInfo != null) {
4042 return mListenerInfo;
4043 }
4044 mListenerInfo = new ListenerInfo();
4045 return mListenerInfo;
4046 }
4047
Adam Powell20232d02010-12-08 21:08:53 -08004048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 * Register a callback to be invoked when focus of this view changed.
4050 *
4051 * @param l The callback that will run.
4052 */
4053 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004054 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 }
4056
4057 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004058 * Add a listener that will be called when the bounds of the view change due to
4059 * layout processing.
4060 *
4061 * @param listener The listener that will be called when layout bounds change.
4062 */
4063 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 ListenerInfo li = getListenerInfo();
4065 if (li.mOnLayoutChangeListeners == null) {
4066 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004067 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004068 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4069 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004070 }
Chet Haase21cd1382010-09-01 17:42:29 -07004071 }
4072
4073 /**
4074 * Remove a listener for layout changes.
4075 *
4076 * @param listener The listener for layout bounds change.
4077 */
4078 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004079 ListenerInfo li = mListenerInfo;
4080 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004081 return;
4082 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004083 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004084 }
4085
4086 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004087 * Add a listener for attach state changes.
4088 *
4089 * This listener will be called whenever this view is attached or detached
4090 * from a window. Remove the listener using
4091 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4092 *
4093 * @param listener Listener to attach
4094 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4095 */
4096 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 ListenerInfo li = getListenerInfo();
4098 if (li.mOnAttachStateChangeListeners == null) {
4099 li.mOnAttachStateChangeListeners
4100 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004101 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004102 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004103 }
4104
4105 /**
4106 * Remove a listener for attach state changes. The listener will receive no further
4107 * notification of window attach/detach events.
4108 *
4109 * @param listener Listener to remove
4110 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4111 */
4112 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004113 ListenerInfo li = mListenerInfo;
4114 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004115 return;
4116 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004117 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004118 }
4119
4120 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 * Returns the focus-change callback registered for this view.
4122 *
4123 * @return The callback, or null if one is not registered.
4124 */
4125 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004126 ListenerInfo li = mListenerInfo;
4127 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 }
4129
4130 /**
4131 * Register a callback to be invoked when this view is clicked. If this view is not
4132 * clickable, it becomes clickable.
4133 *
4134 * @param l The callback that will run
4135 *
4136 * @see #setClickable(boolean)
4137 */
4138 public void setOnClickListener(OnClickListener l) {
4139 if (!isClickable()) {
4140 setClickable(true);
4141 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004142 getListenerInfo().mOnClickListener = l;
4143 }
4144
4145 /**
4146 * Return whether this view has an attached OnClickListener. Returns
4147 * true if there is a listener, false if there is none.
4148 */
4149 public boolean hasOnClickListeners() {
4150 ListenerInfo li = mListenerInfo;
4151 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 }
4153
4154 /**
4155 * Register a callback to be invoked when this view is clicked and held. If this view is not
4156 * long clickable, it becomes long clickable.
4157 *
4158 * @param l The callback that will run
4159 *
4160 * @see #setLongClickable(boolean)
4161 */
4162 public void setOnLongClickListener(OnLongClickListener l) {
4163 if (!isLongClickable()) {
4164 setLongClickable(true);
4165 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004166 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
4168
4169 /**
4170 * Register a callback to be invoked when the context menu for this view is
4171 * being built. If this view is not long clickable, it becomes long clickable.
4172 *
4173 * @param l The callback that will run
4174 *
4175 */
4176 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4177 if (!isLongClickable()) {
4178 setLongClickable(true);
4179 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004180 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
4182
4183 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004184 * Call this view's OnClickListener, if it is defined. Performs all normal
4185 * actions associated with clicking: reporting accessibility event, playing
4186 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 *
4188 * @return True there was an assigned OnClickListener that was called, false
4189 * otherwise is returned.
4190 */
4191 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004192 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4193
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004194 ListenerInfo li = mListenerInfo;
4195 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004197 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 return true;
4199 }
4200
4201 return false;
4202 }
4203
4204 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004205 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4206 * this only calls the listener, and does not do any associated clicking
4207 * actions like reporting an accessibility event.
4208 *
4209 * @return True there was an assigned OnClickListener that was called, false
4210 * otherwise is returned.
4211 */
4212 public boolean callOnClick() {
4213 ListenerInfo li = mListenerInfo;
4214 if (li != null && li.mOnClickListener != null) {
4215 li.mOnClickListener.onClick(this);
4216 return true;
4217 }
4218 return false;
4219 }
4220
4221 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004222 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4223 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004225 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 */
4227 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004228 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004231 ListenerInfo li = mListenerInfo;
4232 if (li != null && li.mOnLongClickListener != null) {
4233 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 }
4235 if (!handled) {
4236 handled = showContextMenu();
4237 }
4238 if (handled) {
4239 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4240 }
4241 return handled;
4242 }
4243
4244 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004245 * Performs button-related actions during a touch down event.
4246 *
4247 * @param event The event.
4248 * @return True if the down was consumed.
4249 *
4250 * @hide
4251 */
4252 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4253 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4254 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4255 return true;
4256 }
4257 }
4258 return false;
4259 }
4260
4261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 * Bring up the context menu for this view.
4263 *
4264 * @return Whether a context menu was displayed.
4265 */
4266 public boolean showContextMenu() {
4267 return getParent().showContextMenuForChild(this);
4268 }
4269
4270 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004271 * Bring up the context menu for this view, referring to the item under the specified point.
4272 *
4273 * @param x The referenced x coordinate.
4274 * @param y The referenced y coordinate.
4275 * @param metaState The keyboard modifiers that were pressed.
4276 * @return Whether a context menu was displayed.
4277 *
4278 * @hide
4279 */
4280 public boolean showContextMenu(float x, float y, int metaState) {
4281 return showContextMenu();
4282 }
4283
4284 /**
Adam Powell6e346362010-07-23 10:18:23 -07004285 * Start an action mode.
4286 *
4287 * @param callback Callback that will control the lifecycle of the action mode
4288 * @return The new action mode if it is started, null otherwise
4289 *
4290 * @see ActionMode
4291 */
4292 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004293 ViewParent parent = getParent();
4294 if (parent == null) return null;
4295 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004296 }
4297
4298 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004299 * Register a callback to be invoked when a hardware key is pressed in this view.
4300 * Key presses in software input methods will generally not trigger the methods of
4301 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 * @param l the key listener to attach to this view
4303 */
4304 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004305 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 }
4307
4308 /**
4309 * Register a callback to be invoked when a touch event is sent to this view.
4310 * @param l the touch listener to attach to this view
4311 */
4312 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004313 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 }
4315
4316 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004317 * Register a callback to be invoked when a generic motion event is sent to this view.
4318 * @param l the generic motion listener to attach to this view
4319 */
4320 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004321 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004322 }
4323
4324 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004325 * Register a callback to be invoked when a hover event is sent to this view.
4326 * @param l the hover listener to attach to this view
4327 */
4328 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004329 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004330 }
4331
4332 /**
Joe Malin32736f02011-01-19 16:14:20 -08004333 * Register a drag event listener callback object for this View. The parameter is
4334 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4335 * View, the system calls the
4336 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4337 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004338 */
4339 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004340 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004341 }
4342
4343 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004344 * Give this view focus. This will cause
4345 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 *
4347 * Note: this does not check whether this {@link View} should get focus, it just
4348 * gives it focus no matter what. It should only be called internally by framework
4349 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4350 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004351 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4352 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 * focus moved when requestFocus() is called. It may not always
4354 * apply, in which case use the default View.FOCUS_DOWN.
4355 * @param previouslyFocusedRect The rectangle of the view that had focus
4356 * prior in this View's coordinate system.
4357 */
4358 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4359 if (DBG) {
4360 System.out.println(this + " requestFocus()");
4361 }
4362
Dianne Hackborn4702a852012-08-17 15:18:29 -07004363 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4364 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365
4366 if (mParent != null) {
4367 mParent.requestChildFocus(this, this);
4368 }
4369
4370 onFocusChanged(true, direction, previouslyFocusedRect);
4371 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004372
4373 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4374 notifyAccessibilityStateChanged();
4375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377 }
4378
4379 /**
4380 * Request that a rectangle of this view be visible on the screen,
4381 * scrolling if necessary just enough.
4382 *
4383 * <p>A View should call this if it maintains some notion of which part
4384 * of its content is interesting. For example, a text editing view
4385 * should call this when its cursor moves.
4386 *
4387 * @param rectangle The rectangle.
4388 * @return Whether any parent scrolled.
4389 */
4390 public boolean requestRectangleOnScreen(Rect rectangle) {
4391 return requestRectangleOnScreen(rectangle, false);
4392 }
4393
4394 /**
4395 * Request that a rectangle of this view be visible on the screen,
4396 * scrolling if necessary just enough.
4397 *
4398 * <p>A View should call this if it maintains some notion of which part
4399 * of its content is interesting. For example, a text editing view
4400 * should call this when its cursor moves.
4401 *
4402 * <p>When <code>immediate</code> is set to true, scrolling will not be
4403 * animated.
4404 *
4405 * @param rectangle The rectangle.
4406 * @param immediate True to forbid animated scrolling, false otherwise
4407 * @return Whether any parent scrolled.
4408 */
4409 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004410 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004411 return false;
4412 }
4413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004415
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004416 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004417 position.set(rectangle);
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 ViewParent parent = mParent;
4420 boolean scrolled = false;
4421 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004422 rectangle.set((int) position.left, (int) position.top,
4423 (int) position.right, (int) position.bottom);
4424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 scrolled |= parent.requestChildRectangleOnScreen(child,
4426 rectangle, immediate);
4427
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004428 if (!child.hasIdentityMatrix()) {
4429 child.getMatrix().mapRect(position);
4430 }
4431
4432 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433
4434 if (!(parent instanceof View)) {
4435 break;
4436 }
Romain Guy8506ab42009-06-11 17:35:47 -07004437
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004438 View parentView = (View) parent;
4439
4440 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4441
4442 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 parent = child.getParent();
4444 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004446 return scrolled;
4447 }
4448
4449 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004450 * Called when this view wants to give up focus. If focus is cleared
4451 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4452 * <p>
4453 * <strong>Note:</strong> When a View clears focus the framework is trying
4454 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004455 * View is the first from the top that can take focus, then all callbacks
4456 * related to clearing focus will be invoked after wich the framework will
4457 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004458 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 */
4460 public void clearFocus() {
4461 if (DBG) {
4462 System.out.println(this + " clearFocus()");
4463 }
4464
Dianne Hackborn4702a852012-08-17 15:18:29 -07004465 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4466 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467
4468 if (mParent != null) {
4469 mParent.clearChildFocus(this);
4470 }
4471
4472 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004475
4476 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004477
4478 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4479 notifyAccessibilityStateChanged();
4480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 }
4482 }
4483
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004484 void ensureInputFocusOnFirstFocusable() {
4485 View root = getRootView();
4486 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004487 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 }
4489 }
4490
4491 /**
4492 * Called internally by the view system when a new view is getting focus.
4493 * This is what clears the old focus.
4494 */
4495 void unFocus() {
4496 if (DBG) {
4497 System.out.println(this + " unFocus()");
4498 }
4499
Dianne Hackborn4702a852012-08-17 15:18:29 -07004500 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4501 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502
4503 onFocusChanged(false, 0, null);
4504 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004505
4506 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4507 notifyAccessibilityStateChanged();
4508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 }
4510 }
4511
4512 /**
4513 * Returns true if this view has focus iteself, or is the ancestor of the
4514 * view that has focus.
4515 *
4516 * @return True if this view has or contains focus, false otherwise.
4517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004518 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004520 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522
4523 /**
4524 * Returns true if this view is focusable or if it contains a reachable View
4525 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4526 * is a View whose parents do not block descendants focus.
4527 *
4528 * Only {@link #VISIBLE} views are considered focusable.
4529 *
4530 * @return True if the view is focusable or if the view contains a focusable
4531 * View, false otherwise.
4532 *
4533 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4534 */
4535 public boolean hasFocusable() {
4536 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4537 }
4538
4539 /**
4540 * Called by the view system when the focus state of this view changes.
4541 * When the focus change event is caused by directional navigation, direction
4542 * and previouslyFocusedRect provide insight into where the focus is coming from.
4543 * When overriding, be sure to call up through to the super class so that
4544 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 * @param gainFocus True if the View has focus; false otherwise.
4547 * @param direction The direction focus has moved when requestFocus()
4548 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004549 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4550 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4551 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4553 * system, of the previously focused view. If applicable, this will be
4554 * passed in as finer grained information about where the focus is coming
4555 * from (in addition to direction). Will be <code>null</code> otherwise.
4556 */
4557 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004558 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004559 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4560 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004561 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004562 }
4563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 InputMethodManager imm = InputMethodManager.peekInstance();
4565 if (!gainFocus) {
4566 if (isPressed()) {
4567 setPressed(false);
4568 }
4569 if (imm != null && mAttachInfo != null
4570 && mAttachInfo.mHasWindowFocus) {
4571 imm.focusOut(this);
4572 }
Romain Guya2431d02009-04-30 16:30:00 -07004573 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 } else if (imm != null && mAttachInfo != null
4575 && mAttachInfo.mHasWindowFocus) {
4576 imm.focusIn(this);
4577 }
Romain Guy8506ab42009-06-11 17:35:47 -07004578
Romain Guy0fd89bf2011-01-26 15:41:30 -08004579 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004580 ListenerInfo li = mListenerInfo;
4581 if (li != null && li.mOnFocusChangeListener != null) {
4582 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 }
Joe Malin32736f02011-01-19 16:14:20 -08004584
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004585 if (mAttachInfo != null) {
4586 mAttachInfo.mKeyDispatchState.reset(this);
4587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 }
4589
4590 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004591 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004592 * not enabled this method has no effect. The default implementation calls
4593 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4594 * to populate information about the event source (this View), then calls
4595 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4596 * populate the text content of the event source including its descendants,
4597 * and last calls
4598 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4599 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004600 * <p>
4601 * If an {@link AccessibilityDelegate} has been specified via calling
4602 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4603 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4604 * responsible for handling this call.
4605 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004606 *
Scott Mainb303d832011-10-12 16:45:18 -07004607 * @param eventType The type of the event to send, as defined by several types from
4608 * {@link android.view.accessibility.AccessibilityEvent}, such as
4609 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4610 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004611 *
4612 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4613 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4614 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004615 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004616 */
4617 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004618 if (mAccessibilityDelegate != null) {
4619 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4620 } else {
4621 sendAccessibilityEventInternal(eventType);
4622 }
4623 }
4624
4625 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004626 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4627 * {@link AccessibilityEvent} to make an announcement which is related to some
4628 * sort of a context change for which none of the events representing UI transitions
4629 * is a good fit. For example, announcing a new page in a book. If accessibility
4630 * is not enabled this method does nothing.
4631 *
4632 * @param text The announcement text.
4633 */
4634 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004635 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004636 AccessibilityEvent event = AccessibilityEvent.obtain(
4637 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004638 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004639 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004640 event.setContentDescription(null);
4641 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004642 }
4643 }
4644
4645 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004646 * @see #sendAccessibilityEvent(int)
4647 *
4648 * Note: Called from the default {@link AccessibilityDelegate}.
4649 */
4650 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004651 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4652 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4653 }
4654 }
4655
4656 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004657 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4658 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004659 * perform a check whether accessibility is enabled.
4660 * <p>
4661 * If an {@link AccessibilityDelegate} has been specified via calling
4662 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4663 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4664 * is responsible for handling this call.
4665 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004666 *
4667 * @param event The event to send.
4668 *
4669 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004670 */
4671 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004672 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004673 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004674 } else {
4675 sendAccessibilityEventUncheckedInternal(event);
4676 }
4677 }
4678
4679 /**
4680 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4681 *
4682 * Note: Called from the default {@link AccessibilityDelegate}.
4683 */
4684 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004685 if (!isShown()) {
4686 return;
4687 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004688 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004689 // Only a subset of accessibility events populates text content.
4690 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4691 dispatchPopulateAccessibilityEvent(event);
4692 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004693 // In the beginning we called #isShown(), so we know that getParent() is not null.
4694 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004695 }
4696
4697 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004698 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4699 * to its children for adding their text content to the event. Note that the
4700 * event text is populated in a separate dispatch path since we add to the
4701 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004702 * A typical implementation will call
4703 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4704 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4705 * on each child. Override this method if custom population of the event text
4706 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004707 * <p>
4708 * If an {@link AccessibilityDelegate} has been specified via calling
4709 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4710 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4711 * is responsible for handling this call.
4712 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004713 * <p>
4714 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4715 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4716 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004717 *
4718 * @param event The event.
4719 *
4720 * @return True if the event population was completed.
4721 */
4722 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004723 if (mAccessibilityDelegate != null) {
4724 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4725 } else {
4726 return dispatchPopulateAccessibilityEventInternal(event);
4727 }
4728 }
4729
4730 /**
4731 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4732 *
4733 * Note: Called from the default {@link AccessibilityDelegate}.
4734 */
4735 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004736 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004737 return false;
4738 }
4739
4740 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004741 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004742 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004743 * text content. While this method is free to modify event
4744 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004745 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4746 * <p>
4747 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004748 * to the text added by the super implementation:
4749 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004750 * super.onPopulateAccessibilityEvent(event);
4751 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4752 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4753 * mCurrentDate.getTimeInMillis(), flags);
4754 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004755 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004756 * <p>
4757 * If an {@link AccessibilityDelegate} has been specified via calling
4758 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4759 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4760 * is responsible for handling this call.
4761 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004762 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4763 * information to the event, in case the default implementation has basic information to add.
4764 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004765 *
4766 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004767 *
4768 * @see #sendAccessibilityEvent(int)
4769 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004770 */
4771 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004772 if (mAccessibilityDelegate != null) {
4773 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4774 } else {
4775 onPopulateAccessibilityEventInternal(event);
4776 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004777 }
4778
4779 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004780 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4781 *
4782 * Note: Called from the default {@link AccessibilityDelegate}.
4783 */
4784 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4785
4786 }
4787
4788 /**
4789 * Initializes an {@link AccessibilityEvent} with information about
4790 * this View which is the event source. In other words, the source of
4791 * an accessibility event is the view whose state change triggered firing
4792 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004793 * <p>
4794 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004795 * to properties set by the super implementation:
4796 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4797 * super.onInitializeAccessibilityEvent(event);
4798 * event.setPassword(true);
4799 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004800 * <p>
4801 * If an {@link AccessibilityDelegate} has been specified via calling
4802 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4803 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4804 * is responsible for handling this call.
4805 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004806 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4807 * information to the event, in case the default implementation has basic information to add.
4808 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004809 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004810 *
4811 * @see #sendAccessibilityEvent(int)
4812 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4813 */
4814 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004815 if (mAccessibilityDelegate != null) {
4816 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4817 } else {
4818 onInitializeAccessibilityEventInternal(event);
4819 }
4820 }
4821
4822 /**
4823 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4824 *
4825 * Note: Called from the default {@link AccessibilityDelegate}.
4826 */
4827 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004828 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004829 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004830 event.setPackageName(getContext().getPackageName());
4831 event.setEnabled(isEnabled());
4832 event.setContentDescription(mContentDescription);
4833
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004834 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004835 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004836 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4837 FOCUSABLES_ALL);
4838 event.setItemCount(focusablesTempList.size());
4839 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4840 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004841 }
4842 }
4843
4844 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004845 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4846 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4847 * This method is responsible for obtaining an accessibility node info from a
4848 * pool of reusable instances and calling
4849 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4850 * initialize the former.
4851 * <p>
4852 * Note: The client is responsible for recycling the obtained instance by calling
4853 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4854 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004855 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004856 * @return A populated {@link AccessibilityNodeInfo}.
4857 *
4858 * @see AccessibilityNodeInfo
4859 */
4860 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004861 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4862 if (provider != null) {
4863 return provider.createAccessibilityNodeInfo(View.NO_ID);
4864 } else {
4865 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4866 onInitializeAccessibilityNodeInfo(info);
4867 return info;
4868 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004869 }
4870
4871 /**
4872 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4873 * The base implementation sets:
4874 * <ul>
4875 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004876 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4877 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004878 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4879 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4880 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4881 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4882 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4883 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4884 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4885 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4886 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4887 * </ul>
4888 * <p>
4889 * Subclasses should override this method, call the super implementation,
4890 * and set additional attributes.
4891 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004892 * <p>
4893 * If an {@link AccessibilityDelegate} has been specified via calling
4894 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4895 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4896 * is responsible for handling this call.
4897 * </p>
4898 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004899 * @param info The instance to initialize.
4900 */
4901 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004902 if (mAccessibilityDelegate != null) {
4903 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4904 } else {
4905 onInitializeAccessibilityNodeInfoInternal(info);
4906 }
4907 }
4908
4909 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004910 * Gets the location of this view in screen coordintates.
4911 *
4912 * @param outRect The output location
4913 */
4914 private void getBoundsOnScreen(Rect outRect) {
4915 if (mAttachInfo == null) {
4916 return;
4917 }
4918
4919 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004920 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004921
4922 if (!hasIdentityMatrix()) {
4923 getMatrix().mapRect(position);
4924 }
4925
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004926 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004927
4928 ViewParent parent = mParent;
4929 while (parent instanceof View) {
4930 View parentView = (View) parent;
4931
4932 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4933
4934 if (!parentView.hasIdentityMatrix()) {
4935 parentView.getMatrix().mapRect(position);
4936 }
4937
4938 position.offset(parentView.mLeft, parentView.mTop);
4939
4940 parent = parentView.mParent;
4941 }
4942
4943 if (parent instanceof ViewRootImpl) {
4944 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4945 position.offset(0, -viewRootImpl.mCurScrollY);
4946 }
4947
4948 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4949
4950 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4951 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4952 }
4953
4954 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004955 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4956 *
4957 * Note: Called from the default {@link AccessibilityDelegate}.
4958 */
4959 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004960 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004961
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004962 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004963 info.setBoundsInParent(bounds);
4964
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004965 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004966 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004967
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004968 ViewParent parent = getParentForAccessibility();
4969 if (parent instanceof View) {
4970 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004971 }
4972
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004973 if (mID != View.NO_ID) {
4974 View rootView = getRootView();
4975 if (rootView == null) {
4976 rootView = this;
4977 }
4978 View label = rootView.findLabelForView(this, mID);
4979 if (label != null) {
4980 info.setLabeledBy(label);
4981 }
4982 }
4983
4984 if (mLabelForId != View.NO_ID) {
4985 View rootView = getRootView();
4986 if (rootView == null) {
4987 rootView = this;
4988 }
4989 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4990 if (labeled != null) {
4991 info.setLabelFor(labeled);
4992 }
4993 }
4994
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004995 info.setVisibleToUser(isVisibleToUser());
4996
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004997 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004998 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004999 info.setContentDescription(getContentDescription());
5000
5001 info.setEnabled(isEnabled());
5002 info.setClickable(isClickable());
5003 info.setFocusable(isFocusable());
5004 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005005 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005006 info.setSelected(isSelected());
5007 info.setLongClickable(isLongClickable());
5008
5009 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005010 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005011 // we should report as selectable view in an AdapterView.
5012 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5013 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5014
5015 if (isFocusable()) {
5016 if (isFocused()) {
5017 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5018 } else {
5019 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5020 }
5021 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005022
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005023 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005024 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005025 } else {
5026 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5027 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005028
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005029 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005030 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5031 }
5032
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005033 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005034 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5035 }
5036
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005037 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005038 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5039 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5040 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005041 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5042 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005043 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005044 }
5045
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005046 private View findLabelForView(View view, int labeledId) {
5047 if (mMatchLabelForPredicate == null) {
5048 mMatchLabelForPredicate = new MatchLabelForPredicate();
5049 }
5050 mMatchLabelForPredicate.mLabeledId = labeledId;
5051 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5052 }
5053
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005054 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005055 * Computes whether this view is visible to the user. Such a view is
5056 * attached, visible, all its predecessors are visible, it is not clipped
5057 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005058 *
5059 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005060 *
5061 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005062 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005063 protected boolean isVisibleToUser() {
5064 return isVisibleToUser(null);
5065 }
5066
5067 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005068 * Computes whether the given portion of this view is visible to the user.
5069 * Such a view is attached, visible, all its predecessors are visible,
5070 * has an alpha greater than zero, and the specified portion is not
5071 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005072 *
5073 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5074 * <code>null</code>, and the entire view will be tested in this case.
5075 * When <code>true</code> is returned by the function, the actual visible
5076 * region will be stored in this parameter; that is, if boundInView is fully
5077 * contained within the view, no modification will be made, otherwise regions
5078 * outside of the visible area of the view will be clipped.
5079 *
5080 * @return Whether the specified portion of the view is visible on the screen.
5081 *
5082 * @hide
5083 */
5084 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005085 if (mAttachInfo != null) {
5086 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5087 Point offset = mAttachInfo.mPoint;
5088 // The first two checks are made also made by isShown() which
5089 // however traverses the tree up to the parent to catch that.
5090 // Therefore, we do some fail fast check to minimize the up
5091 // tree traversal.
5092 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
5093 && getAlpha() > 0
5094 && isShown()
5095 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005096 if (isVisible && boundInView != null) {
5097 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07005098 // isVisible is always true here, use a simple assignment
5099 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005100 }
5101 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07005102 }
5103
5104 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005105 }
5106
5107 /**
alanvb72fe7a2012-08-27 16:44:25 -07005108 * Returns the delegate for implementing accessibility support via
5109 * composition. For more details see {@link AccessibilityDelegate}.
5110 *
5111 * @return The delegate, or null if none set.
5112 *
5113 * @hide
5114 */
5115 public AccessibilityDelegate getAccessibilityDelegate() {
5116 return mAccessibilityDelegate;
5117 }
5118
5119 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005120 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005121 * opposed to inheritance. The delegate's primary use is for implementing
5122 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5123 *
5124 * @param delegate The delegate instance.
5125 *
5126 * @see AccessibilityDelegate
5127 */
5128 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5129 mAccessibilityDelegate = delegate;
5130 }
5131
5132 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005133 * Gets the provider for managing a virtual view hierarchy rooted at this View
5134 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5135 * that explore the window content.
5136 * <p>
5137 * If this method returns an instance, this instance is responsible for managing
5138 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5139 * View including the one representing the View itself. Similarly the returned
5140 * instance is responsible for performing accessibility actions on any virtual
5141 * view or the root view itself.
5142 * </p>
5143 * <p>
5144 * If an {@link AccessibilityDelegate} has been specified via calling
5145 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5146 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5147 * is responsible for handling this call.
5148 * </p>
5149 *
5150 * @return The provider.
5151 *
5152 * @see AccessibilityNodeProvider
5153 */
5154 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5155 if (mAccessibilityDelegate != null) {
5156 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5157 } else {
5158 return null;
5159 }
5160 }
5161
5162 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005163 * Gets the unique identifier of this view on the screen for accessibility purposes.
5164 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5165 *
5166 * @return The view accessibility id.
5167 *
5168 * @hide
5169 */
5170 public int getAccessibilityViewId() {
5171 if (mAccessibilityViewId == NO_ID) {
5172 mAccessibilityViewId = sNextAccessibilityViewId++;
5173 }
5174 return mAccessibilityViewId;
5175 }
5176
5177 /**
5178 * Gets the unique identifier of the window in which this View reseides.
5179 *
5180 * @return The window accessibility id.
5181 *
5182 * @hide
5183 */
5184 public int getAccessibilityWindowId() {
5185 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5186 }
5187
5188 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005189 * Gets 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 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005195 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005196 *
5197 * @attr ref android.R.styleable#View_contentDescription
5198 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005199 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005200 public CharSequence getContentDescription() {
5201 return mContentDescription;
5202 }
5203
5204 /**
5205 * Sets the {@link View} description. It briefly describes the view and is
5206 * primarily used for accessibility support. Set this property to enable
5207 * better accessibility support for your application. This is especially
5208 * true for views that do not have textual representation (For example,
5209 * ImageButton).
5210 *
5211 * @param contentDescription The content description.
5212 *
5213 * @attr ref android.R.styleable#View_contentDescription
5214 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005215 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005216 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005217 if (mContentDescription == null) {
5218 if (contentDescription == null) {
5219 return;
5220 }
5221 } else if (mContentDescription.equals(contentDescription)) {
5222 return;
5223 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005224 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005225 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5226 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5227 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5228 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005229 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005230 }
5231
5232 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005233 * Gets the id of a view for which this view serves as a label for
5234 * accessibility purposes.
5235 *
5236 * @return The labeled view id.
5237 */
5238 @ViewDebug.ExportedProperty(category = "accessibility")
5239 public int getLabelFor() {
5240 return mLabelForId;
5241 }
5242
5243 /**
5244 * Sets the id of a view for which this view serves as a label for
5245 * accessibility purposes.
5246 *
5247 * @param id The labeled view id.
5248 */
5249 @RemotableViewMethod
5250 public void setLabelFor(int id) {
5251 mLabelForId = id;
5252 if (mLabelForId != View.NO_ID
5253 && mID == View.NO_ID) {
5254 mID = generateViewId();
5255 }
5256 }
5257
5258 /**
Romain Guya2431d02009-04-30 16:30:00 -07005259 * Invoked whenever this view loses focus, either by losing window focus or by losing
5260 * focus within its window. This method can be used to clear any state tied to the
5261 * focus. For instance, if a button is held pressed with the trackball and the window
5262 * loses focus, this method can be used to cancel the press.
5263 *
5264 * Subclasses of View overriding this method should always call super.onFocusLost().
5265 *
5266 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005267 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005268 *
5269 * @hide pending API council approval
5270 */
5271 protected void onFocusLost() {
5272 resetPressedState();
5273 }
5274
5275 private void resetPressedState() {
5276 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5277 return;
5278 }
5279
5280 if (isPressed()) {
5281 setPressed(false);
5282
5283 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005284 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005285 }
5286 }
5287 }
5288
5289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 * Returns true if this view has focus
5291 *
5292 * @return True if this view has focus, false otherwise.
5293 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005294 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005296 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 }
5298
5299 /**
5300 * Find the view in the hierarchy rooted at this view that currently has
5301 * focus.
5302 *
5303 * @return The view that currently has focus, or null if no focused view can
5304 * be found.
5305 */
5306 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005307 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 }
5309
5310 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005311 * Indicates whether this view is one of the set of scrollable containers in
5312 * its window.
5313 *
5314 * @return whether this view is one of the set of scrollable containers in
5315 * its window
5316 *
5317 * @attr ref android.R.styleable#View_isScrollContainer
5318 */
5319 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005320 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005321 }
5322
5323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 * Change whether this view is one of the set of scrollable containers in
5325 * its window. This will be used to determine whether the window can
5326 * resize or must pan when a soft input area is open -- scrollable
5327 * containers allow the window to use resize mode since the container
5328 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005329 *
5330 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 */
5332 public void setScrollContainer(boolean isScrollContainer) {
5333 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005334 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005335 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005336 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005338 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005340 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 mAttachInfo.mScrollContainers.remove(this);
5342 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005343 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 }
5345 }
5346
5347 /**
5348 * Returns the quality of the drawing cache.
5349 *
5350 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5351 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5352 *
5353 * @see #setDrawingCacheQuality(int)
5354 * @see #setDrawingCacheEnabled(boolean)
5355 * @see #isDrawingCacheEnabled()
5356 *
5357 * @attr ref android.R.styleable#View_drawingCacheQuality
5358 */
5359 public int getDrawingCacheQuality() {
5360 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5361 }
5362
5363 /**
5364 * Set the drawing cache quality of this view. This value is used only when the
5365 * drawing cache is enabled
5366 *
5367 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5368 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5369 *
5370 * @see #getDrawingCacheQuality()
5371 * @see #setDrawingCacheEnabled(boolean)
5372 * @see #isDrawingCacheEnabled()
5373 *
5374 * @attr ref android.R.styleable#View_drawingCacheQuality
5375 */
5376 public void setDrawingCacheQuality(int quality) {
5377 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5378 }
5379
5380 /**
5381 * Returns whether the screen should remain on, corresponding to the current
5382 * value of {@link #KEEP_SCREEN_ON}.
5383 *
5384 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5385 *
5386 * @see #setKeepScreenOn(boolean)
5387 *
5388 * @attr ref android.R.styleable#View_keepScreenOn
5389 */
5390 public boolean getKeepScreenOn() {
5391 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5392 }
5393
5394 /**
5395 * Controls whether the screen should remain on, modifying the
5396 * value of {@link #KEEP_SCREEN_ON}.
5397 *
5398 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5399 *
5400 * @see #getKeepScreenOn()
5401 *
5402 * @attr ref android.R.styleable#View_keepScreenOn
5403 */
5404 public void setKeepScreenOn(boolean keepScreenOn) {
5405 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5406 }
5407
5408 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005409 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5410 * @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 -08005411 *
5412 * @attr ref android.R.styleable#View_nextFocusLeft
5413 */
5414 public int getNextFocusLeftId() {
5415 return mNextFocusLeftId;
5416 }
5417
5418 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005419 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5420 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5421 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 *
5423 * @attr ref android.R.styleable#View_nextFocusLeft
5424 */
5425 public void setNextFocusLeftId(int nextFocusLeftId) {
5426 mNextFocusLeftId = nextFocusLeftId;
5427 }
5428
5429 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005430 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5431 * @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 -08005432 *
5433 * @attr ref android.R.styleable#View_nextFocusRight
5434 */
5435 public int getNextFocusRightId() {
5436 return mNextFocusRightId;
5437 }
5438
5439 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005440 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5441 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5442 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005443 *
5444 * @attr ref android.R.styleable#View_nextFocusRight
5445 */
5446 public void setNextFocusRightId(int nextFocusRightId) {
5447 mNextFocusRightId = nextFocusRightId;
5448 }
5449
5450 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005451 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5452 * @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 -08005453 *
5454 * @attr ref android.R.styleable#View_nextFocusUp
5455 */
5456 public int getNextFocusUpId() {
5457 return mNextFocusUpId;
5458 }
5459
5460 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005461 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5462 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5463 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 *
5465 * @attr ref android.R.styleable#View_nextFocusUp
5466 */
5467 public void setNextFocusUpId(int nextFocusUpId) {
5468 mNextFocusUpId = nextFocusUpId;
5469 }
5470
5471 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005472 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5473 * @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 -08005474 *
5475 * @attr ref android.R.styleable#View_nextFocusDown
5476 */
5477 public int getNextFocusDownId() {
5478 return mNextFocusDownId;
5479 }
5480
5481 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005482 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5483 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5484 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 *
5486 * @attr ref android.R.styleable#View_nextFocusDown
5487 */
5488 public void setNextFocusDownId(int nextFocusDownId) {
5489 mNextFocusDownId = nextFocusDownId;
5490 }
5491
5492 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005493 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5494 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5495 *
5496 * @attr ref android.R.styleable#View_nextFocusForward
5497 */
5498 public int getNextFocusForwardId() {
5499 return mNextFocusForwardId;
5500 }
5501
5502 /**
5503 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5504 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5505 * decide automatically.
5506 *
5507 * @attr ref android.R.styleable#View_nextFocusForward
5508 */
5509 public void setNextFocusForwardId(int nextFocusForwardId) {
5510 mNextFocusForwardId = nextFocusForwardId;
5511 }
5512
5513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 * Returns the visibility of this view and all of its ancestors
5515 *
5516 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5517 */
5518 public boolean isShown() {
5519 View current = this;
5520 //noinspection ConstantConditions
5521 do {
5522 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5523 return false;
5524 }
5525 ViewParent parent = current.mParent;
5526 if (parent == null) {
5527 return false; // We are not attached to the view root
5528 }
5529 if (!(parent instanceof View)) {
5530 return true;
5531 }
5532 current = (View) parent;
5533 } while (current != null);
5534
5535 return false;
5536 }
5537
5538 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005539 * Called by the view hierarchy when the content insets for a window have
5540 * changed, to allow it to adjust its content to fit within those windows.
5541 * The content insets tell you the space that the status bar, input method,
5542 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005544 * <p>You do not normally need to deal with this function, since the default
5545 * window decoration given to applications takes care of applying it to the
5546 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5547 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5548 * and your content can be placed under those system elements. You can then
5549 * use this method within your view hierarchy if you have parts of your UI
5550 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005552 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005553 * inset's to the view's padding, consuming that content (modifying the
5554 * insets to be 0), and returning true. This behavior is off by default, but can
5555 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5556 *
5557 * <p>This function's traversal down the hierarchy is depth-first. The same content
5558 * insets object is propagated down the hierarchy, so any changes made to it will
5559 * be seen by all following views (including potentially ones above in
5560 * the hierarchy since this is a depth-first traversal). The first view
5561 * that returns true will abort the entire traversal.
5562 *
5563 * <p>The default implementation works well for a situation where it is
5564 * used with a container that covers the entire window, allowing it to
5565 * apply the appropriate insets to its content on all edges. If you need
5566 * a more complicated layout (such as two different views fitting system
5567 * windows, one on the top of the window, and one on the bottom),
5568 * you can override the method and handle the insets however you would like.
5569 * Note that the insets provided by the framework are always relative to the
5570 * far edges of the window, not accounting for the location of the called view
5571 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005572 * where the layout will place the view, as it is done before layout happens.)
5573 *
5574 * <p>Note: unlike many View methods, there is no dispatch phase to this
5575 * call. If you are overriding it in a ViewGroup and want to allow the
5576 * call to continue to your children, you must be sure to call the super
5577 * implementation.
5578 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005579 * <p>Here is a sample layout that makes use of fitting system windows
5580 * to have controls for a video view placed inside of the window decorations
5581 * that it hides and shows. This can be used with code like the second
5582 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5583 *
5584 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5585 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005586 * @param insets Current content insets of the window. Prior to
5587 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5588 * the insets or else you and Android will be unhappy.
5589 *
5590 * @return Return true if this view applied the insets and it should not
5591 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005592 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005593 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005594 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 */
5596 protected boolean fitSystemWindows(Rect insets) {
5597 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005598 mUserPaddingStart = UNDEFINED_PADDING;
5599 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005600 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5601 || mAttachInfo == null
5602 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5603 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5604 return true;
5605 } else {
5606 internalSetPadding(0, 0, 0, 0);
5607 return false;
5608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005609 }
5610 return false;
5611 }
5612
5613 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005614 * Sets whether or not this view should account for system screen decorations
5615 * such as the status bar and inset its content; that is, controlling whether
5616 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5617 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005618 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005619 * <p>Note that if you are providing your own implementation of
5620 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5621 * flag to true -- your implementation will be overriding the default
5622 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005623 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005624 * @param fitSystemWindows If true, then the default implementation of
5625 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005626 *
5627 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005628 * @see #getFitsSystemWindows()
5629 * @see #fitSystemWindows(Rect)
5630 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005631 */
5632 public void setFitsSystemWindows(boolean fitSystemWindows) {
5633 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5634 }
5635
5636 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005637 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005638 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5639 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005640 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005641 * @return Returns true if the default implementation of
5642 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005643 *
5644 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005645 * @see #setFitsSystemWindows()
5646 * @see #fitSystemWindows(Rect)
5647 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005648 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005649 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005650 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5651 }
5652
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005653 /** @hide */
5654 public boolean fitsSystemWindows() {
5655 return getFitsSystemWindows();
5656 }
5657
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005658 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005659 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5660 */
5661 public void requestFitSystemWindows() {
5662 if (mParent != null) {
5663 mParent.requestFitSystemWindows();
5664 }
5665 }
5666
5667 /**
5668 * For use by PhoneWindow to make its own system window fitting optional.
5669 * @hide
5670 */
5671 public void makeOptionalFitsSystemWindows() {
5672 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5673 }
5674
5675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 * Returns the visibility status for this view.
5677 *
5678 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5679 * @attr ref android.R.styleable#View_visibility
5680 */
5681 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005682 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5683 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5684 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 })
5686 public int getVisibility() {
5687 return mViewFlags & VISIBILITY_MASK;
5688 }
5689
5690 /**
5691 * Set the enabled state of this view.
5692 *
5693 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5694 * @attr ref android.R.styleable#View_visibility
5695 */
5696 @RemotableViewMethod
5697 public void setVisibility(int visibility) {
5698 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005699 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 }
5701
5702 /**
5703 * Returns the enabled status for this view. The interpretation of the
5704 * enabled state varies by subclass.
5705 *
5706 * @return True if this view is enabled, false otherwise.
5707 */
5708 @ViewDebug.ExportedProperty
5709 public boolean isEnabled() {
5710 return (mViewFlags & ENABLED_MASK) == ENABLED;
5711 }
5712
5713 /**
5714 * Set the enabled state of this view. The interpretation of the enabled
5715 * state varies by subclass.
5716 *
5717 * @param enabled True if this view is enabled, false otherwise.
5718 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005719 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005721 if (enabled == isEnabled()) return;
5722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005723 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5724
5725 /*
5726 * The View most likely has to change its appearance, so refresh
5727 * the drawable state.
5728 */
5729 refreshDrawableState();
5730
5731 // Invalidate too, since the default behavior for views is to be
5732 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005733 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 }
5735
5736 /**
5737 * Set whether this view can receive the focus.
5738 *
5739 * Setting this to false will also ensure that this view is not focusable
5740 * in touch mode.
5741 *
5742 * @param focusable If true, this view can receive the focus.
5743 *
5744 * @see #setFocusableInTouchMode(boolean)
5745 * @attr ref android.R.styleable#View_focusable
5746 */
5747 public void setFocusable(boolean focusable) {
5748 if (!focusable) {
5749 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5750 }
5751 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5752 }
5753
5754 /**
5755 * Set whether this view can receive focus while in touch mode.
5756 *
5757 * Setting this to true will also ensure that this view is focusable.
5758 *
5759 * @param focusableInTouchMode If true, this view can receive the focus while
5760 * in touch mode.
5761 *
5762 * @see #setFocusable(boolean)
5763 * @attr ref android.R.styleable#View_focusableInTouchMode
5764 */
5765 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5766 // Focusable in touch mode should always be set before the focusable flag
5767 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5768 // which, in touch mode, will not successfully request focus on this view
5769 // because the focusable in touch mode flag is not set
5770 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5771 if (focusableInTouchMode) {
5772 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5773 }
5774 }
5775
5776 /**
5777 * Set whether this view should have sound effects enabled for events such as
5778 * clicking and touching.
5779 *
5780 * <p>You may wish to disable sound effects for a view if you already play sounds,
5781 * for instance, a dial key that plays dtmf tones.
5782 *
5783 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5784 * @see #isSoundEffectsEnabled()
5785 * @see #playSoundEffect(int)
5786 * @attr ref android.R.styleable#View_soundEffectsEnabled
5787 */
5788 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5789 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5790 }
5791
5792 /**
5793 * @return whether this view should have sound effects enabled for events such as
5794 * clicking and touching.
5795 *
5796 * @see #setSoundEffectsEnabled(boolean)
5797 * @see #playSoundEffect(int)
5798 * @attr ref android.R.styleable#View_soundEffectsEnabled
5799 */
5800 @ViewDebug.ExportedProperty
5801 public boolean isSoundEffectsEnabled() {
5802 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5803 }
5804
5805 /**
5806 * Set whether this view should have haptic feedback for events such as
5807 * long presses.
5808 *
5809 * <p>You may wish to disable haptic feedback if your view already controls
5810 * its own haptic feedback.
5811 *
5812 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5813 * @see #isHapticFeedbackEnabled()
5814 * @see #performHapticFeedback(int)
5815 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5816 */
5817 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5818 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5819 }
5820
5821 /**
5822 * @return whether this view should have haptic feedback enabled for events
5823 * long presses.
5824 *
5825 * @see #setHapticFeedbackEnabled(boolean)
5826 * @see #performHapticFeedback(int)
5827 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5828 */
5829 @ViewDebug.ExportedProperty
5830 public boolean isHapticFeedbackEnabled() {
5831 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5832 }
5833
5834 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005835 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005836 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005837 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5838 * {@link #LAYOUT_DIRECTION_RTL},
5839 * {@link #LAYOUT_DIRECTION_INHERIT} or
5840 * {@link #LAYOUT_DIRECTION_LOCALE}.
5841 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005842 *
5843 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005844 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005845 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005846 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5847 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5848 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5849 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005850 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005851 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005852 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005853 }
5854
5855 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005856 * Set the layout direction for this view. This will propagate a reset of layout direction
5857 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005858 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005859 * @param layoutDirection the layout direction to set. Should be one of:
5860 *
5861 * {@link #LAYOUT_DIRECTION_LTR},
5862 * {@link #LAYOUT_DIRECTION_RTL},
5863 * {@link #LAYOUT_DIRECTION_INHERIT},
5864 * {@link #LAYOUT_DIRECTION_LOCALE}.
5865 *
5866 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5867 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5868 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005869 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005870 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005871 */
5872 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005873 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005874 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005875 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005876 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005877 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005878 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005879 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005880 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005881 // We need to resolve all RTL properties as they all depend on layout direction
5882 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005883 requestLayout();
5884 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005885 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005886 }
5887
5888 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005889 * Returns the resolved layout direction for this view.
5890 *
5891 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005892 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005893 *
5894 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5895 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005896 */
5897 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005898 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5899 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005900 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005901 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005902 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5903 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005904 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005905 return LAYOUT_DIRECTION_LTR;
5906 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005907 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5908 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005909 }
5910
5911 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005912 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5913 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005914 *
5915 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005916 *
5917 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005918 */
5919 @ViewDebug.ExportedProperty(category = "layout")
5920 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005921 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005922 }
5923
5924 /**
Adam Powell539ee872012-02-03 19:00:49 -08005925 * Indicates whether the view is currently tracking transient state that the
5926 * app should not need to concern itself with saving and restoring, but that
5927 * the framework should take special note to preserve when possible.
5928 *
Adam Powell785c4472012-05-02 21:25:39 -07005929 * <p>A view with transient state cannot be trivially rebound from an external
5930 * data source, such as an adapter binding item views in a list. This may be
5931 * because the view is performing an animation, tracking user selection
5932 * of content, or similar.</p>
5933 *
Adam Powell539ee872012-02-03 19:00:49 -08005934 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005935 */
5936 @ViewDebug.ExportedProperty(category = "layout")
5937 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005938 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005939 }
5940
5941 /**
5942 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005943 * framework should attempt to preserve when possible. This flag is reference counted,
5944 * so every call to setHasTransientState(true) should be paired with a later call
5945 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005946 *
Adam Powell785c4472012-05-02 21:25:39 -07005947 * <p>A view with transient state cannot be trivially rebound from an external
5948 * data source, such as an adapter binding item views in a list. This may be
5949 * because the view is performing an animation, tracking user selection
5950 * of content, or similar.</p>
5951 *
Adam Powell539ee872012-02-03 19:00:49 -08005952 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005953 */
5954 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005955 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5956 mTransientStateCount - 1;
5957 if (mTransientStateCount < 0) {
5958 mTransientStateCount = 0;
5959 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5960 "unmatched pair of setHasTransientState calls");
5961 }
5962 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005963 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005964 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005965 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5966 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005967 if (mParent != null) {
5968 try {
5969 mParent.childHasTransientStateChanged(this, hasTransientState);
5970 } catch (AbstractMethodError e) {
5971 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5972 " does not fully implement ViewParent", e);
5973 }
Adam Powell539ee872012-02-03 19:00:49 -08005974 }
5975 }
5976 }
5977
5978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 * If this view doesn't do any drawing on its own, set this flag to
5980 * allow further optimizations. By default, this flag is not set on
5981 * View, but could be set on some View subclasses such as ViewGroup.
5982 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005983 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5984 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 *
5986 * @param willNotDraw whether or not this View draw on its own
5987 */
5988 public void setWillNotDraw(boolean willNotDraw) {
5989 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5990 }
5991
5992 /**
5993 * Returns whether or not this View draws on its own.
5994 *
5995 * @return true if this view has nothing to draw, false otherwise
5996 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005997 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 public boolean willNotDraw() {
5999 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6000 }
6001
6002 /**
6003 * When a View's drawing cache is enabled, drawing is redirected to an
6004 * offscreen bitmap. Some views, like an ImageView, must be able to
6005 * bypass this mechanism if they already draw a single bitmap, to avoid
6006 * unnecessary usage of the memory.
6007 *
6008 * @param willNotCacheDrawing true if this view does not cache its
6009 * drawing, false otherwise
6010 */
6011 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6012 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6013 }
6014
6015 /**
6016 * Returns whether or not this View can cache its drawing or not.
6017 *
6018 * @return true if this view does not cache its drawing, false otherwise
6019 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006020 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 public boolean willNotCacheDrawing() {
6022 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6023 }
6024
6025 /**
6026 * Indicates whether this view reacts to click events or not.
6027 *
6028 * @return true if the view is clickable, false otherwise
6029 *
6030 * @see #setClickable(boolean)
6031 * @attr ref android.R.styleable#View_clickable
6032 */
6033 @ViewDebug.ExportedProperty
6034 public boolean isClickable() {
6035 return (mViewFlags & CLICKABLE) == CLICKABLE;
6036 }
6037
6038 /**
6039 * Enables or disables click events for this view. When a view
6040 * is clickable it will change its state to "pressed" on every click.
6041 * Subclasses should set the view clickable to visually react to
6042 * user's clicks.
6043 *
6044 * @param clickable true to make the view clickable, false otherwise
6045 *
6046 * @see #isClickable()
6047 * @attr ref android.R.styleable#View_clickable
6048 */
6049 public void setClickable(boolean clickable) {
6050 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6051 }
6052
6053 /**
6054 * Indicates whether this view reacts to long click events or not.
6055 *
6056 * @return true if the view is long clickable, false otherwise
6057 *
6058 * @see #setLongClickable(boolean)
6059 * @attr ref android.R.styleable#View_longClickable
6060 */
6061 public boolean isLongClickable() {
6062 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6063 }
6064
6065 /**
6066 * Enables or disables long click events for this view. When a view is long
6067 * clickable it reacts to the user holding down the button for a longer
6068 * duration than a tap. This event can either launch the listener or a
6069 * context menu.
6070 *
6071 * @param longClickable true to make the view long clickable, false otherwise
6072 * @see #isLongClickable()
6073 * @attr ref android.R.styleable#View_longClickable
6074 */
6075 public void setLongClickable(boolean longClickable) {
6076 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6077 }
6078
6079 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006080 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 *
6082 * @see #isClickable()
6083 * @see #setClickable(boolean)
6084 *
6085 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6086 * the View's internal state from a previously set "pressed" state.
6087 */
6088 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006089 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006092 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006093 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006094 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006096
6097 if (needsRefresh) {
6098 refreshDrawableState();
6099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 dispatchSetPressed(pressed);
6101 }
6102
6103 /**
6104 * Dispatch setPressed to all of this View's children.
6105 *
6106 * @see #setPressed(boolean)
6107 *
6108 * @param pressed The new pressed state
6109 */
6110 protected void dispatchSetPressed(boolean pressed) {
6111 }
6112
6113 /**
6114 * Indicates whether the view is currently in pressed state. Unless
6115 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6116 * the pressed state.
6117 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006118 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006119 * @see #isClickable()
6120 * @see #setClickable(boolean)
6121 *
6122 * @return true if the view is currently pressed, false otherwise
6123 */
6124 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006125 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006126 }
6127
6128 /**
6129 * Indicates whether this view will save its state (that is,
6130 * whether its {@link #onSaveInstanceState} method will be called).
6131 *
6132 * @return Returns true if the view state saving is enabled, else false.
6133 *
6134 * @see #setSaveEnabled(boolean)
6135 * @attr ref android.R.styleable#View_saveEnabled
6136 */
6137 public boolean isSaveEnabled() {
6138 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6139 }
6140
6141 /**
6142 * Controls whether the saving of this view's state is
6143 * enabled (that is, whether its {@link #onSaveInstanceState} method
6144 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006145 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 * for its state to be saved. This flag can only disable the
6147 * saving of this view; any child views may still have their state saved.
6148 *
6149 * @param enabled Set to false to <em>disable</em> state saving, or true
6150 * (the default) to allow it.
6151 *
6152 * @see #isSaveEnabled()
6153 * @see #setId(int)
6154 * @see #onSaveInstanceState()
6155 * @attr ref android.R.styleable#View_saveEnabled
6156 */
6157 public void setSaveEnabled(boolean enabled) {
6158 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6159 }
6160
Jeff Brown85a31762010-09-01 17:01:00 -07006161 /**
6162 * Gets whether the framework should discard touches when the view's
6163 * window is obscured by another visible window.
6164 * Refer to the {@link View} security documentation for more details.
6165 *
6166 * @return True if touch filtering is enabled.
6167 *
6168 * @see #setFilterTouchesWhenObscured(boolean)
6169 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6170 */
6171 @ViewDebug.ExportedProperty
6172 public boolean getFilterTouchesWhenObscured() {
6173 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6174 }
6175
6176 /**
6177 * Sets whether the framework should discard touches when the view's
6178 * window is obscured by another visible window.
6179 * Refer to the {@link View} security documentation for more details.
6180 *
6181 * @param enabled True if touch filtering should be enabled.
6182 *
6183 * @see #getFilterTouchesWhenObscured
6184 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6185 */
6186 public void setFilterTouchesWhenObscured(boolean enabled) {
6187 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6188 FILTER_TOUCHES_WHEN_OBSCURED);
6189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006190
6191 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006192 * Indicates whether the entire hierarchy under this view will save its
6193 * state when a state saving traversal occurs from its parent. The default
6194 * is true; if false, these views will not be saved unless
6195 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6196 *
6197 * @return Returns true if the view state saving from parent is enabled, else false.
6198 *
6199 * @see #setSaveFromParentEnabled(boolean)
6200 */
6201 public boolean isSaveFromParentEnabled() {
6202 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6203 }
6204
6205 /**
6206 * Controls whether the entire hierarchy under this view will save its
6207 * state when a state saving traversal occurs from its parent. The default
6208 * is true; if false, these views will not be saved unless
6209 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6210 *
6211 * @param enabled Set to false to <em>disable</em> state saving, or true
6212 * (the default) to allow it.
6213 *
6214 * @see #isSaveFromParentEnabled()
6215 * @see #setId(int)
6216 * @see #onSaveInstanceState()
6217 */
6218 public void setSaveFromParentEnabled(boolean enabled) {
6219 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6220 }
6221
6222
6223 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006224 * Returns whether this View is able to take focus.
6225 *
6226 * @return True if this view can take focus, or false otherwise.
6227 * @attr ref android.R.styleable#View_focusable
6228 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006229 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 public final boolean isFocusable() {
6231 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6232 }
6233
6234 /**
6235 * When a view is focusable, it may not want to take focus when in touch mode.
6236 * For example, a button would like focus when the user is navigating via a D-pad
6237 * so that the user can click on it, but once the user starts touching the screen,
6238 * the button shouldn't take focus
6239 * @return Whether the view is focusable in touch mode.
6240 * @attr ref android.R.styleable#View_focusableInTouchMode
6241 */
6242 @ViewDebug.ExportedProperty
6243 public final boolean isFocusableInTouchMode() {
6244 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6245 }
6246
6247 /**
6248 * Find the nearest view in the specified direction that can take focus.
6249 * This does not actually give focus to that view.
6250 *
6251 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6252 *
6253 * @return The nearest focusable in the specified direction, or null if none
6254 * can be found.
6255 */
6256 public View focusSearch(int direction) {
6257 if (mParent != null) {
6258 return mParent.focusSearch(this, direction);
6259 } else {
6260 return null;
6261 }
6262 }
6263
6264 /**
6265 * This method is the last chance for the focused view and its ancestors to
6266 * respond to an arrow key. This is called when the focused view did not
6267 * consume the key internally, nor could the view system find a new view in
6268 * the requested direction to give focus to.
6269 *
6270 * @param focused The currently focused view.
6271 * @param direction The direction focus wants to move. One of FOCUS_UP,
6272 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6273 * @return True if the this view consumed this unhandled move.
6274 */
6275 public boolean dispatchUnhandledMove(View focused, int direction) {
6276 return false;
6277 }
6278
6279 /**
6280 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006281 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006283 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6284 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 * @return The user specified next view, or null if there is none.
6286 */
6287 View findUserSetNextFocus(View root, int direction) {
6288 switch (direction) {
6289 case FOCUS_LEFT:
6290 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006291 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 case FOCUS_RIGHT:
6293 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006294 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295 case FOCUS_UP:
6296 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006297 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 case FOCUS_DOWN:
6299 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006300 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006301 case FOCUS_FORWARD:
6302 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006303 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006304 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006305 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006306 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006307 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006308 @Override
6309 public boolean apply(View t) {
6310 return t.mNextFocusForwardId == id;
6311 }
6312 });
6313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006314 }
6315 return null;
6316 }
6317
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006318 private View findViewInsideOutShouldExist(View root, int id) {
6319 if (mMatchIdPredicate == null) {
6320 mMatchIdPredicate = new MatchIdPredicate();
6321 }
6322 mMatchIdPredicate.mId = id;
6323 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006325 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006326 }
6327 return result;
6328 }
6329
6330 /**
6331 * Find and return all focusable views that are descendants of this view,
6332 * possibly including this view if it is focusable itself.
6333 *
6334 * @param direction The direction of the focus
6335 * @return A list of focusable views
6336 */
6337 public ArrayList<View> getFocusables(int direction) {
6338 ArrayList<View> result = new ArrayList<View>(24);
6339 addFocusables(result, direction);
6340 return result;
6341 }
6342
6343 /**
6344 * Add any focusable views that are descendants of this view (possibly
6345 * including this view if it is focusable itself) to views. If we are in touch mode,
6346 * only add views that are also focusable in touch mode.
6347 *
6348 * @param views Focusable views found so far
6349 * @param direction The direction of the focus
6350 */
6351 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006352 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354
svetoslavganov75986cf2009-05-14 22:28:01 -07006355 /**
6356 * Adds any focusable views that are descendants of this view (possibly
6357 * including this view if it is focusable itself) to views. This method
6358 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006359 * only views focusable in touch mode if we are in touch mode or
6360 * only views that can take accessibility focus if accessibility is enabeld
6361 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006362 *
6363 * @param views Focusable views found so far or null if all we are interested is
6364 * the number of focusables.
6365 * @param direction The direction of the focus.
6366 * @param focusableMode The type of focusables to be added.
6367 *
6368 * @see #FOCUSABLES_ALL
6369 * @see #FOCUSABLES_TOUCH_MODE
6370 */
6371 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006372 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006373 return;
6374 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006375 if (!isFocusable()) {
6376 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006377 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006378 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6379 && isInTouchMode() && !isFocusableInTouchMode()) {
6380 return;
6381 }
6382 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 }
6384
6385 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006386 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006387 * The search is performed by either the text that the View renders or the content
6388 * description that describes the view for accessibility purposes and the view does
6389 * not render or both. Clients can specify how the search is to be performed via
6390 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6391 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006392 *
6393 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006394 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006395 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006396 * @see #FIND_VIEWS_WITH_TEXT
6397 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6398 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006399 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006400 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006401 if (getAccessibilityNodeProvider() != null) {
6402 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6403 outViews.add(this);
6404 }
6405 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006406 && (searched != null && searched.length() > 0)
6407 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006408 String searchedLowerCase = searched.toString().toLowerCase();
6409 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6410 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6411 outViews.add(this);
6412 }
6413 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006414 }
6415
6416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006417 * Find and return all touchable views that are descendants of this view,
6418 * possibly including this view if it is touchable itself.
6419 *
6420 * @return A list of touchable views
6421 */
6422 public ArrayList<View> getTouchables() {
6423 ArrayList<View> result = new ArrayList<View>();
6424 addTouchables(result);
6425 return result;
6426 }
6427
6428 /**
6429 * Add any touchable views that are descendants of this view (possibly
6430 * including this view if it is touchable itself) to views.
6431 *
6432 * @param views Touchable views found so far
6433 */
6434 public void addTouchables(ArrayList<View> views) {
6435 final int viewFlags = mViewFlags;
6436
6437 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6438 && (viewFlags & ENABLED_MASK) == ENABLED) {
6439 views.add(this);
6440 }
6441 }
6442
6443 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006444 * Returns whether this View is accessibility focused.
6445 *
6446 * @return True if this View is accessibility focused.
6447 */
6448 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006449 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006450 }
6451
6452 /**
6453 * Call this to try to give accessibility focus to this view.
6454 *
6455 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6456 * returns false or the view is no visible or the view already has accessibility
6457 * focus.
6458 *
6459 * See also {@link #focusSearch(int)}, which is what you call to say that you
6460 * have focus, and you want your parent to look for the next one.
6461 *
6462 * @return Whether this view actually took accessibility focus.
6463 *
6464 * @hide
6465 */
6466 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006467 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6468 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006469 return false;
6470 }
6471 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6472 return false;
6473 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006474 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6475 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006476 ViewRootImpl viewRootImpl = getViewRootImpl();
6477 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006478 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006479 }
6480 invalidate();
6481 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6482 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006483 return true;
6484 }
6485 return false;
6486 }
6487
6488 /**
6489 * Call this to try to clear accessibility focus of this view.
6490 *
6491 * See also {@link #focusSearch(int)}, which is what you call to say that you
6492 * have focus, and you want your parent to look for the next one.
6493 *
6494 * @hide
6495 */
6496 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006497 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6498 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006499 invalidate();
6500 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6501 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006502 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006503 // Clear the global reference of accessibility focus if this
6504 // view or any of its descendants had accessibility focus.
6505 ViewRootImpl viewRootImpl = getViewRootImpl();
6506 if (viewRootImpl != null) {
6507 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6508 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006509 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006510 }
6511 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006512 }
6513
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006514 private void sendAccessibilityHoverEvent(int eventType) {
6515 // Since we are not delivering to a client accessibility events from not
6516 // important views (unless the clinet request that) we need to fire the
6517 // event from the deepest view exposed to the client. As a consequence if
6518 // the user crosses a not exposed view the client will see enter and exit
6519 // of the exposed predecessor followed by and enter and exit of that same
6520 // predecessor when entering and exiting the not exposed descendant. This
6521 // is fine since the client has a clear idea which view is hovered at the
6522 // price of a couple more events being sent. This is a simple and
6523 // working solution.
6524 View source = this;
6525 while (true) {
6526 if (source.includeForAccessibility()) {
6527 source.sendAccessibilityEvent(eventType);
6528 return;
6529 }
6530 ViewParent parent = source.getParent();
6531 if (parent instanceof View) {
6532 source = (View) parent;
6533 } else {
6534 return;
6535 }
6536 }
6537 }
6538
Svetoslav Ganov42138042012-03-20 11:51:39 -07006539 /**
6540 * Clears accessibility focus without calling any callback methods
6541 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6542 * is used for clearing accessibility focus when giving this focus to
6543 * another view.
6544 */
6545 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006546 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6547 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006548 invalidate();
6549 }
6550 }
6551
6552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 * Call this to try to give focus to a specific view or to one of its
6554 * descendants.
6555 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006556 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6557 * false), or if it is focusable and it is not focusable in touch mode
6558 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006559 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006560 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 * have focus, and you want your parent to look for the next one.
6562 *
6563 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6564 * {@link #FOCUS_DOWN} and <code>null</code>.
6565 *
6566 * @return Whether this view or one of its descendants actually took focus.
6567 */
6568 public final boolean requestFocus() {
6569 return requestFocus(View.FOCUS_DOWN);
6570 }
6571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 /**
6573 * Call this to try to give focus to a specific view or to one of its
6574 * descendants and give it a hint about what direction focus is heading.
6575 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006576 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6577 * false), or if it is focusable and it is not focusable in touch mode
6578 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006580 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 * have focus, and you want your parent to look for the next one.
6582 *
6583 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6584 * <code>null</code> set for the previously focused rectangle.
6585 *
6586 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6587 * @return Whether this view or one of its descendants actually took focus.
6588 */
6589 public final boolean requestFocus(int direction) {
6590 return requestFocus(direction, null);
6591 }
6592
6593 /**
6594 * Call this to try to give focus to a specific view or to one of its descendants
6595 * and give it hints about the direction and a specific rectangle that the focus
6596 * is coming from. The rectangle can help give larger views a finer grained hint
6597 * about where focus is coming from, and therefore, where to show selection, or
6598 * forward focus change internally.
6599 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006600 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6601 * false), or if it is focusable and it is not focusable in touch mode
6602 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 *
6604 * A View will not take focus if it is not visible.
6605 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006606 * A View will not take focus if one of its parents has
6607 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6608 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006610 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 * have focus, and you want your parent to look for the next one.
6612 *
6613 * You may wish to override this method if your custom {@link View} has an internal
6614 * {@link View} that it wishes to forward the request to.
6615 *
6616 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6617 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6618 * to give a finer grained hint about where focus is coming from. May be null
6619 * if there is no hint.
6620 * @return Whether this view or one of its descendants actually took focus.
6621 */
6622 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006623 return requestFocusNoSearch(direction, previouslyFocusedRect);
6624 }
6625
6626 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 // need to be focusable
6628 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6629 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6630 return false;
6631 }
6632
6633 // need to be focusable in touch mode if in touch mode
6634 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006635 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6636 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 }
6638
6639 // need to not have any parents blocking us
6640 if (hasAncestorThatBlocksDescendantFocus()) {
6641 return false;
6642 }
6643
6644 handleFocusGainInternal(direction, previouslyFocusedRect);
6645 return true;
6646 }
6647
6648 /**
6649 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6650 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6651 * touch mode to request focus when they are touched.
6652 *
6653 * @return Whether this view or one of its descendants actually took focus.
6654 *
6655 * @see #isInTouchMode()
6656 *
6657 */
6658 public final boolean requestFocusFromTouch() {
6659 // Leave touch mode if we need to
6660 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006661 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006662 if (viewRoot != null) {
6663 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 }
6665 }
6666 return requestFocus(View.FOCUS_DOWN);
6667 }
6668
6669 /**
6670 * @return Whether any ancestor of this view blocks descendant focus.
6671 */
6672 private boolean hasAncestorThatBlocksDescendantFocus() {
6673 ViewParent ancestor = mParent;
6674 while (ancestor instanceof ViewGroup) {
6675 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6676 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6677 return true;
6678 } else {
6679 ancestor = vgAncestor.getParent();
6680 }
6681 }
6682 return false;
6683 }
6684
6685 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006686 * Gets the mode for determining whether this View is important for accessibility
6687 * which is if it fires accessibility events and if it is reported to
6688 * accessibility services that query the screen.
6689 *
6690 * @return The mode for determining whether a View is important for accessibility.
6691 *
6692 * @attr ref android.R.styleable#View_importantForAccessibility
6693 *
6694 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6695 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6696 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6697 */
6698 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006699 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6700 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6701 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006702 })
6703 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006704 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6705 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006706 }
6707
6708 /**
6709 * Sets how to determine whether this view is important for accessibility
6710 * which is if it fires accessibility events and if it is reported to
6711 * accessibility services that query the screen.
6712 *
6713 * @param mode How to determine whether this view is important for accessibility.
6714 *
6715 * @attr ref android.R.styleable#View_importantForAccessibility
6716 *
6717 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6718 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6719 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6720 */
6721 public void setImportantForAccessibility(int mode) {
6722 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006723 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6724 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6725 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006726 notifyAccessibilityStateChanged();
6727 }
6728 }
6729
6730 /**
6731 * Gets whether this view should be exposed for accessibility.
6732 *
6733 * @return Whether the view is exposed for accessibility.
6734 *
6735 * @hide
6736 */
6737 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006738 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6739 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006740 switch (mode) {
6741 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6742 return true;
6743 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6744 return false;
6745 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006746 return isActionableForAccessibility() || hasListenersForAccessibility()
6747 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006748 default:
6749 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6750 + mode);
6751 }
6752 }
6753
6754 /**
6755 * Gets the parent for accessibility purposes. Note that the parent for
6756 * accessibility is not necessary the immediate parent. It is the first
6757 * predecessor that is important for accessibility.
6758 *
6759 * @return The parent for accessibility purposes.
6760 */
6761 public ViewParent getParentForAccessibility() {
6762 if (mParent instanceof View) {
6763 View parentView = (View) mParent;
6764 if (parentView.includeForAccessibility()) {
6765 return mParent;
6766 } else {
6767 return mParent.getParentForAccessibility();
6768 }
6769 }
6770 return null;
6771 }
6772
6773 /**
6774 * Adds the children of a given View for accessibility. Since some Views are
6775 * not important for accessibility the children for accessibility are not
6776 * necessarily direct children of the riew, rather they are the first level of
6777 * descendants important for accessibility.
6778 *
6779 * @param children The list of children for accessibility.
6780 */
6781 public void addChildrenForAccessibility(ArrayList<View> children) {
6782 if (includeForAccessibility()) {
6783 children.add(this);
6784 }
6785 }
6786
6787 /**
6788 * Whether to regard this view for accessibility. A view is regarded for
6789 * accessibility if it is important for accessibility or the querying
6790 * accessibility service has explicitly requested that view not
6791 * important for accessibility are regarded.
6792 *
6793 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006794 *
6795 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006796 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006797 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006798 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006799 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006800 }
6801 return false;
6802 }
6803
6804 /**
6805 * Returns whether the View is considered actionable from
6806 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006807 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006808 *
6809 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006810 *
6811 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006812 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006813 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006814 return (isClickable() || isLongClickable() || isFocusable());
6815 }
6816
6817 /**
6818 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006819 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006820 *
6821 * @return True if the view is actionable for accessibility.
6822 */
6823 private boolean hasListenersForAccessibility() {
6824 ListenerInfo info = getListenerInfo();
6825 return mTouchDelegate != null || info.mOnKeyListener != null
6826 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6827 || info.mOnHoverListener != null || info.mOnDragListener != null;
6828 }
6829
6830 /**
6831 * Notifies accessibility services that some view's important for
6832 * accessibility state has changed. Note that such notifications
6833 * are made at most once every
6834 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6835 * to avoid unnecessary load to the system. Also once a view has
6836 * made a notifucation this method is a NOP until the notification has
6837 * been sent to clients.
6838 *
6839 * @hide
6840 *
6841 * TODO: Makse sure this method is called for any view state change
6842 * that is interesting for accessilility purposes.
6843 */
6844 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006845 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6846 return;
6847 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006848 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6849 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006850 if (mParent != null) {
6851 mParent.childAccessibilityStateChanged(this);
6852 }
6853 }
6854 }
6855
6856 /**
6857 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006858 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006859 *
6860 * @hide
6861 */
6862 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006863 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006864 }
6865
6866 /**
6867 * Performs the specified accessibility action on the view. For
6868 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006869 * <p>
6870 * If an {@link AccessibilityDelegate} has been specified via calling
6871 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6872 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6873 * is responsible for handling this call.
6874 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006875 *
6876 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006877 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006878 * @return Whether the action was performed.
6879 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006880 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006881 if (mAccessibilityDelegate != null) {
6882 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6883 } else {
6884 return performAccessibilityActionInternal(action, arguments);
6885 }
6886 }
6887
6888 /**
6889 * @see #performAccessibilityAction(int, Bundle)
6890 *
6891 * Note: Called from the default {@link AccessibilityDelegate}.
6892 */
6893 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006894 switch (action) {
6895 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006896 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006897 performClick();
6898 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006899 }
6900 } break;
6901 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6902 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006903 performLongClick();
6904 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006905 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006906 } break;
6907 case AccessibilityNodeInfo.ACTION_FOCUS: {
6908 if (!hasFocus()) {
6909 // Get out of touch mode since accessibility
6910 // wants to move focus around.
6911 getViewRootImpl().ensureTouchMode(false);
6912 return requestFocus();
6913 }
6914 } break;
6915 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6916 if (hasFocus()) {
6917 clearFocus();
6918 return !isFocused();
6919 }
6920 } break;
6921 case AccessibilityNodeInfo.ACTION_SELECT: {
6922 if (!isSelected()) {
6923 setSelected(true);
6924 return isSelected();
6925 }
6926 } break;
6927 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6928 if (isSelected()) {
6929 setSelected(false);
6930 return !isSelected();
6931 }
6932 } break;
6933 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006934 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006935 return requestAccessibilityFocus();
6936 }
6937 } break;
6938 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6939 if (isAccessibilityFocused()) {
6940 clearAccessibilityFocus();
6941 return true;
6942 }
6943 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006944 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6945 if (arguments != null) {
6946 final int granularity = arguments.getInt(
6947 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6948 return nextAtGranularity(granularity);
6949 }
6950 } break;
6951 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6952 if (arguments != null) {
6953 final int granularity = arguments.getInt(
6954 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6955 return previousAtGranularity(granularity);
6956 }
6957 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006958 }
6959 return false;
6960 }
6961
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006962 private boolean nextAtGranularity(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 }
6971 final int current = getAccessibilityCursorPosition();
6972 final int[] range = iterator.following(current);
6973 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006974 return false;
6975 }
6976 final int start = range[0];
6977 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006978 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006979 sendViewTextTraversedAtGranularityEvent(
6980 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6981 granularity, start, end);
6982 return true;
6983 }
6984
6985 private boolean previousAtGranularity(int granularity) {
6986 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006987 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006988 return false;
6989 }
6990 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6991 if (iterator == null) {
6992 return false;
6993 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006994 int current = getAccessibilityCursorPosition();
6995 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6996 current = text.length();
6997 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6998 // When traversing by character we always put the cursor after the character
6999 // to ease edit and have to compensate before asking the for previous segment.
7000 current--;
7001 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007002 final int[] range = iterator.preceding(current);
7003 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007004 return false;
7005 }
7006 final int start = range[0];
7007 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007008 // Always put the cursor after the character to ease edit.
7009 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7010 setAccessibilityCursorPosition(end);
7011 } else {
7012 setAccessibilityCursorPosition(start);
7013 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007014 sendViewTextTraversedAtGranularityEvent(
7015 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7016 granularity, start, end);
7017 return true;
7018 }
7019
7020 /**
7021 * Gets the text reported for accessibility purposes.
7022 *
7023 * @return The accessibility text.
7024 *
7025 * @hide
7026 */
7027 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007028 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007029 }
7030
7031 /**
7032 * @hide
7033 */
7034 public int getAccessibilityCursorPosition() {
7035 return mAccessibilityCursorPosition;
7036 }
7037
7038 /**
7039 * @hide
7040 */
7041 public void setAccessibilityCursorPosition(int position) {
7042 mAccessibilityCursorPosition = position;
7043 }
7044
7045 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7046 int fromIndex, int toIndex) {
7047 if (mParent == null) {
7048 return;
7049 }
7050 AccessibilityEvent event = AccessibilityEvent.obtain(
7051 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7052 onInitializeAccessibilityEvent(event);
7053 onPopulateAccessibilityEvent(event);
7054 event.setFromIndex(fromIndex);
7055 event.setToIndex(toIndex);
7056 event.setAction(action);
7057 event.setMovementGranularity(granularity);
7058 mParent.requestSendAccessibilityEvent(this, event);
7059 }
7060
7061 /**
7062 * @hide
7063 */
7064 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7065 switch (granularity) {
7066 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7067 CharSequence text = getIterableTextForAccessibility();
7068 if (text != null && text.length() > 0) {
7069 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007070 CharacterTextSegmentIterator.getInstance(
7071 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007072 iterator.initialize(text.toString());
7073 return iterator;
7074 }
7075 } break;
7076 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7077 CharSequence text = getIterableTextForAccessibility();
7078 if (text != null && text.length() > 0) {
7079 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007080 WordTextSegmentIterator.getInstance(
7081 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007082 iterator.initialize(text.toString());
7083 return iterator;
7084 }
7085 } break;
7086 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7087 CharSequence text = getIterableTextForAccessibility();
7088 if (text != null && text.length() > 0) {
7089 ParagraphTextSegmentIterator iterator =
7090 ParagraphTextSegmentIterator.getInstance();
7091 iterator.initialize(text.toString());
7092 return iterator;
7093 }
7094 } break;
7095 }
7096 return null;
7097 }
7098
Svetoslav Ganov42138042012-03-20 11:51:39 -07007099 /**
Romain Guya440b002010-02-24 15:57:54 -08007100 * @hide
7101 */
7102 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007103 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007104 clearDisplayList();
7105
Romain Guya440b002010-02-24 15:57:54 -08007106 onStartTemporaryDetach();
7107 }
7108
7109 /**
7110 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7112 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007113 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 */
7115 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007116 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007117 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007118 }
7119
7120 /**
7121 * @hide
7122 */
7123 public void dispatchFinishTemporaryDetach() {
7124 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 }
Romain Guy8506ab42009-06-11 17:35:47 -07007126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 /**
7128 * Called after {@link #onStartTemporaryDetach} when the container is done
7129 * changing the view.
7130 */
7131 public void onFinishTemporaryDetach() {
7132 }
Romain Guy8506ab42009-06-11 17:35:47 -07007133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007134 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007135 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7136 * for this view's window. Returns null if the view is not currently attached
7137 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007138 * just use the standard high-level event callbacks like
7139 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007140 */
7141 public KeyEvent.DispatcherState getKeyDispatcherState() {
7142 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7143 }
Joe Malin32736f02011-01-19 16:14:20 -08007144
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 * Dispatch a key event before it is processed by any input method
7147 * associated with the view hierarchy. This can be used to intercept
7148 * key events in special situations before the IME consumes them; a
7149 * typical example would be handling the BACK key to update the application's
7150 * UI instead of allowing the IME to see it and close itself.
7151 *
7152 * @param event The key event to be dispatched.
7153 * @return True if the event was handled, false otherwise.
7154 */
7155 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7156 return onKeyPreIme(event.getKeyCode(), event);
7157 }
7158
7159 /**
7160 * Dispatch a key event to the next view on the focus path. This path runs
7161 * from the top of the view tree down to the currently focused view. If this
7162 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7163 * the next node down the focus path. This method also fires any key
7164 * listeners.
7165 *
7166 * @param event The key event to be dispatched.
7167 * @return True if the event was handled, false otherwise.
7168 */
7169 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007170 if (mInputEventConsistencyVerifier != null) {
7171 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007173
Jeff Brown21bc5c92011-02-28 18:27:14 -08007174 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007175 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007176 ListenerInfo li = mListenerInfo;
7177 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7178 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 return true;
7180 }
7181
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007182 if (event.dispatch(this, mAttachInfo != null
7183 ? mAttachInfo.mKeyDispatchState : null, this)) {
7184 return true;
7185 }
7186
7187 if (mInputEventConsistencyVerifier != null) {
7188 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7189 }
7190 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 }
7192
7193 /**
7194 * Dispatches a key shortcut event.
7195 *
7196 * @param event The key event to be dispatched.
7197 * @return True if the event was handled by the view, false otherwise.
7198 */
7199 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7200 return onKeyShortcut(event.getKeyCode(), event);
7201 }
7202
7203 /**
7204 * Pass the touch screen motion event down to the target view, or this
7205 * view if it is the target.
7206 *
7207 * @param event The motion event to be dispatched.
7208 * @return True if the event was handled by the view, false otherwise.
7209 */
7210 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007211 if (mInputEventConsistencyVerifier != null) {
7212 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7213 }
7214
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007215 if (onFilterTouchEventForSecurity(event)) {
7216 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007217 ListenerInfo li = mListenerInfo;
7218 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7219 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007220 return true;
7221 }
7222
7223 if (onTouchEvent(event)) {
7224 return true;
7225 }
Jeff Brown85a31762010-09-01 17:01:00 -07007226 }
7227
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007228 if (mInputEventConsistencyVerifier != null) {
7229 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007231 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007232 }
7233
7234 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007235 * Filter the touch event to apply security policies.
7236 *
7237 * @param event The motion event to be filtered.
7238 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007239 *
Jeff Brown85a31762010-09-01 17:01:00 -07007240 * @see #getFilterTouchesWhenObscured
7241 */
7242 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007243 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007244 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7245 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7246 // Window is obscured, drop this touch.
7247 return false;
7248 }
7249 return true;
7250 }
7251
7252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 * Pass a trackball motion event down to the focused view.
7254 *
7255 * @param event The motion event to be dispatched.
7256 * @return True if the event was handled by the view, false otherwise.
7257 */
7258 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007259 if (mInputEventConsistencyVerifier != null) {
7260 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7261 }
7262
Romain Guy02ccac62011-06-24 13:20:23 -07007263 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 }
7265
7266 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007267 * Dispatch a generic motion event.
7268 * <p>
7269 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7270 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007271 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007272 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007273 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007274 *
7275 * @param event The motion event to be dispatched.
7276 * @return True if the event was handled by the view, false otherwise.
7277 */
7278 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007279 if (mInputEventConsistencyVerifier != null) {
7280 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7281 }
7282
Jeff Browna032cc02011-03-07 16:56:21 -08007283 final int source = event.getSource();
7284 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7285 final int action = event.getAction();
7286 if (action == MotionEvent.ACTION_HOVER_ENTER
7287 || action == MotionEvent.ACTION_HOVER_MOVE
7288 || action == MotionEvent.ACTION_HOVER_EXIT) {
7289 if (dispatchHoverEvent(event)) {
7290 return true;
7291 }
7292 } else if (dispatchGenericPointerEvent(event)) {
7293 return true;
7294 }
7295 } else if (dispatchGenericFocusedEvent(event)) {
7296 return true;
7297 }
7298
Jeff Brown10b62902011-06-20 16:40:37 -07007299 if (dispatchGenericMotionEventInternal(event)) {
7300 return true;
7301 }
7302
7303 if (mInputEventConsistencyVerifier != null) {
7304 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7305 }
7306 return false;
7307 }
7308
7309 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007310 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007311 ListenerInfo li = mListenerInfo;
7312 if (li != null && li.mOnGenericMotionListener != null
7313 && (mViewFlags & ENABLED_MASK) == ENABLED
7314 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007315 return true;
7316 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007317
7318 if (onGenericMotionEvent(event)) {
7319 return true;
7320 }
7321
7322 if (mInputEventConsistencyVerifier != null) {
7323 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7324 }
7325 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007326 }
7327
7328 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007329 * Dispatch a hover event.
7330 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007331 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007332 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007333 * </p>
7334 *
7335 * @param event The motion event to be dispatched.
7336 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007337 */
7338 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007339 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007340 ListenerInfo li = mListenerInfo;
7341 if (li != null && li.mOnHoverListener != null
7342 && (mViewFlags & ENABLED_MASK) == ENABLED
7343 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007344 return true;
7345 }
7346
Jeff Browna032cc02011-03-07 16:56:21 -08007347 return onHoverEvent(event);
7348 }
7349
7350 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007351 * Returns true if the view has a child to which it has recently sent
7352 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7353 * it does not have a hovered child, then it must be the innermost hovered view.
7354 * @hide
7355 */
7356 protected boolean hasHoveredChild() {
7357 return false;
7358 }
7359
7360 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007361 * Dispatch a generic motion event to the view under the first pointer.
7362 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007363 * Do not call this method directly.
7364 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007365 * </p>
7366 *
7367 * @param event The motion event to be dispatched.
7368 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007369 */
7370 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7371 return false;
7372 }
7373
7374 /**
7375 * Dispatch a generic motion event to the currently focused view.
7376 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007377 * Do not call this method directly.
7378 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007379 * </p>
7380 *
7381 * @param event The motion event to be dispatched.
7382 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007383 */
7384 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7385 return false;
7386 }
7387
7388 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007389 * Dispatch a pointer event.
7390 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007391 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7392 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7393 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007394 * and should not be expected to handle other pointing device features.
7395 * </p>
7396 *
7397 * @param event The motion event to be dispatched.
7398 * @return True if the event was handled by the view, false otherwise.
7399 * @hide
7400 */
7401 public final boolean dispatchPointerEvent(MotionEvent event) {
7402 if (event.isTouchEvent()) {
7403 return dispatchTouchEvent(event);
7404 } else {
7405 return dispatchGenericMotionEvent(event);
7406 }
7407 }
7408
7409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 * Called when the window containing this view gains or loses window focus.
7411 * ViewGroups should override to route to their children.
7412 *
7413 * @param hasFocus True if the window containing this view now has focus,
7414 * false otherwise.
7415 */
7416 public void dispatchWindowFocusChanged(boolean hasFocus) {
7417 onWindowFocusChanged(hasFocus);
7418 }
7419
7420 /**
7421 * Called when the window containing this view gains or loses focus. Note
7422 * that this is separate from view focus: to receive key events, both
7423 * your view and its window must have focus. If a window is displayed
7424 * on top of yours that takes input focus, then your own window will lose
7425 * focus but the view focus will remain unchanged.
7426 *
7427 * @param hasWindowFocus True if the window containing this view now has
7428 * focus, false otherwise.
7429 */
7430 public void onWindowFocusChanged(boolean hasWindowFocus) {
7431 InputMethodManager imm = InputMethodManager.peekInstance();
7432 if (!hasWindowFocus) {
7433 if (isPressed()) {
7434 setPressed(false);
7435 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007436 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 imm.focusOut(this);
7438 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007439 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007440 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007441 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007442 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 imm.focusIn(this);
7444 }
7445 refreshDrawableState();
7446 }
7447
7448 /**
7449 * Returns true if this view is in a window that currently has window focus.
7450 * Note that this is not the same as the view itself having focus.
7451 *
7452 * @return True if this view is in a window that currently has window focus.
7453 */
7454 public boolean hasWindowFocus() {
7455 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7456 }
7457
7458 /**
Adam Powell326d8082009-12-09 15:10:07 -08007459 * Dispatch a view visibility change down the view hierarchy.
7460 * ViewGroups should override to route to their children.
7461 * @param changedView The view whose visibility changed. Could be 'this' or
7462 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007463 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7464 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007465 */
7466 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7467 onVisibilityChanged(changedView, visibility);
7468 }
7469
7470 /**
7471 * Called when the visibility of the view or an ancestor of the view is changed.
7472 * @param changedView The view whose visibility changed. Could be 'this' or
7473 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007474 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7475 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007476 */
7477 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007478 if (visibility == VISIBLE) {
7479 if (mAttachInfo != null) {
7480 initialAwakenScrollBars();
7481 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007482 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007483 }
7484 }
Adam Powell326d8082009-12-09 15:10:07 -08007485 }
7486
7487 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007488 * Dispatch a hint about whether this view is displayed. For instance, when
7489 * a View moves out of the screen, it might receives a display hint indicating
7490 * the view is not displayed. Applications should not <em>rely</em> on this hint
7491 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007492 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007493 * @param hint A hint about whether or not this view is displayed:
7494 * {@link #VISIBLE} or {@link #INVISIBLE}.
7495 */
7496 public void dispatchDisplayHint(int hint) {
7497 onDisplayHint(hint);
7498 }
7499
7500 /**
7501 * Gives this view a hint about whether is displayed or not. For instance, when
7502 * a View moves out of the screen, it might receives a display hint indicating
7503 * the view is not displayed. Applications should not <em>rely</em> on this hint
7504 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007505 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007506 * @param hint A hint about whether or not this view is displayed:
7507 * {@link #VISIBLE} or {@link #INVISIBLE}.
7508 */
7509 protected void onDisplayHint(int hint) {
7510 }
7511
7512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 * Dispatch a window visibility change down the view hierarchy.
7514 * ViewGroups should override to route to their children.
7515 *
7516 * @param visibility The new visibility of the window.
7517 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007518 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007519 */
7520 public void dispatchWindowVisibilityChanged(int visibility) {
7521 onWindowVisibilityChanged(visibility);
7522 }
7523
7524 /**
7525 * Called when the window containing has change its visibility
7526 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7527 * that this tells you whether or not your window is being made visible
7528 * to the window manager; this does <em>not</em> tell you whether or not
7529 * your window is obscured by other windows on the screen, even if it
7530 * is itself visible.
7531 *
7532 * @param visibility The new visibility of the window.
7533 */
7534 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007535 if (visibility == VISIBLE) {
7536 initialAwakenScrollBars();
7537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 }
7539
7540 /**
7541 * Returns the current visibility of the window this view is attached to
7542 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7543 *
7544 * @return Returns the current visibility of the view's window.
7545 */
7546 public int getWindowVisibility() {
7547 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7548 }
7549
7550 /**
7551 * Retrieve the overall visible display size in which the window this view is
7552 * attached to has been positioned in. This takes into account screen
7553 * decorations above the window, for both cases where the window itself
7554 * is being position inside of them or the window is being placed under
7555 * then and covered insets are used for the window to position its content
7556 * inside. In effect, this tells you the available area where content can
7557 * be placed and remain visible to users.
7558 *
7559 * <p>This function requires an IPC back to the window manager to retrieve
7560 * the requested information, so should not be used in performance critical
7561 * code like drawing.
7562 *
7563 * @param outRect Filled in with the visible display frame. If the view
7564 * is not attached to a window, this is simply the raw display size.
7565 */
7566 public void getWindowVisibleDisplayFrame(Rect outRect) {
7567 if (mAttachInfo != null) {
7568 try {
7569 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7570 } catch (RemoteException e) {
7571 return;
7572 }
7573 // XXX This is really broken, and probably all needs to be done
7574 // in the window manager, and we need to know more about whether
7575 // we want the area behind or in front of the IME.
7576 final Rect insets = mAttachInfo.mVisibleInsets;
7577 outRect.left += insets.left;
7578 outRect.top += insets.top;
7579 outRect.right -= insets.right;
7580 outRect.bottom -= insets.bottom;
7581 return;
7582 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007583 // The view is not attached to a display so we don't have a context.
7584 // Make a best guess about the display size.
7585 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007586 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 }
7588
7589 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007590 * Dispatch a notification about a resource configuration change down
7591 * the view hierarchy.
7592 * ViewGroups should override to route to their children.
7593 *
7594 * @param newConfig The new resource configuration.
7595 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007596 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007597 */
7598 public void dispatchConfigurationChanged(Configuration newConfig) {
7599 onConfigurationChanged(newConfig);
7600 }
7601
7602 /**
7603 * Called when the current configuration of the resources being used
7604 * by the application have changed. You can use this to decide when
7605 * to reload resources that can changed based on orientation and other
7606 * configuration characterstics. You only need to use this if you are
7607 * not relying on the normal {@link android.app.Activity} mechanism of
7608 * recreating the activity instance upon a configuration change.
7609 *
7610 * @param newConfig The new resource configuration.
7611 */
7612 protected void onConfigurationChanged(Configuration newConfig) {
7613 }
7614
7615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 * Private function to aggregate all per-view attributes in to the view
7617 * root.
7618 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007619 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7620 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 }
7622
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007623 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7624 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007625 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007626 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007627 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007628 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007629 ListenerInfo li = mListenerInfo;
7630 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007631 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007633 }
7634 }
7635
7636 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007637 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007638 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007639 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7640 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 ai.mRecomputeGlobalAttributes = true;
7642 }
7643 }
7644 }
7645
7646 /**
7647 * Returns whether the device is currently in touch mode. Touch mode is entered
7648 * once the user begins interacting with the device by touch, and affects various
7649 * things like whether focus is always visible to the user.
7650 *
7651 * @return Whether the device is in touch mode.
7652 */
7653 @ViewDebug.ExportedProperty
7654 public boolean isInTouchMode() {
7655 if (mAttachInfo != null) {
7656 return mAttachInfo.mInTouchMode;
7657 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007658 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 }
7660 }
7661
7662 /**
7663 * Returns the context the view is running in, through which it can
7664 * access the current theme, resources, etc.
7665 *
7666 * @return The view's Context.
7667 */
7668 @ViewDebug.CapturedViewProperty
7669 public final Context getContext() {
7670 return mContext;
7671 }
7672
7673 /**
7674 * Handle a key event before it is processed by any input method
7675 * associated with the view hierarchy. This can be used to intercept
7676 * key events in special situations before the IME consumes them; a
7677 * typical example would be handling the BACK key to update the application's
7678 * UI instead of allowing the IME to see it and close itself.
7679 *
7680 * @param keyCode The value in event.getKeyCode().
7681 * @param event Description of the key event.
7682 * @return If you handled the event, return true. If you want to allow the
7683 * event to be handled by the next receiver, return false.
7684 */
7685 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7686 return false;
7687 }
7688
7689 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007690 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7691 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7693 * is released, if the view is enabled and clickable.
7694 *
Jean Chalard405bc512012-05-29 19:12:34 +09007695 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7696 * although some may elect to do so in some situations. Do not rely on this to
7697 * catch software key presses.
7698 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 * @param keyCode A key code that represents the button pressed, from
7700 * {@link android.view.KeyEvent}.
7701 * @param event The KeyEvent object that defines the button action.
7702 */
7703 public boolean onKeyDown(int keyCode, KeyEvent event) {
7704 boolean result = false;
7705
7706 switch (keyCode) {
7707 case KeyEvent.KEYCODE_DPAD_CENTER:
7708 case KeyEvent.KEYCODE_ENTER: {
7709 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7710 return true;
7711 }
7712 // Long clickable items don't necessarily have to be clickable
7713 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7714 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7715 (event.getRepeatCount() == 0)) {
7716 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007717 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 return true;
7719 }
7720 break;
7721 }
7722 }
7723 return result;
7724 }
7725
7726 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007727 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7728 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7729 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007730 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7731 * although some may elect to do so in some situations. Do not rely on this to
7732 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007733 */
7734 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7735 return false;
7736 }
7737
7738 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007739 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7740 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7742 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007743 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7744 * although some may elect to do so in some situations. Do not rely on this to
7745 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 *
7747 * @param keyCode A key code that represents the button pressed, from
7748 * {@link android.view.KeyEvent}.
7749 * @param event The KeyEvent object that defines the button action.
7750 */
7751 public boolean onKeyUp(int keyCode, KeyEvent event) {
7752 boolean result = false;
7753
7754 switch (keyCode) {
7755 case KeyEvent.KEYCODE_DPAD_CENTER:
7756 case KeyEvent.KEYCODE_ENTER: {
7757 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7758 return true;
7759 }
7760 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7761 setPressed(false);
7762
7763 if (!mHasPerformedLongPress) {
7764 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007765 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007766
7767 result = performClick();
7768 }
7769 }
7770 break;
7771 }
7772 }
7773 return result;
7774 }
7775
7776 /**
7777 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7778 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7779 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007780 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7781 * although some may elect to do so in some situations. Do not rely on this to
7782 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 *
7784 * @param keyCode A key code that represents the button pressed, from
7785 * {@link android.view.KeyEvent}.
7786 * @param repeatCount The number of times the action was made.
7787 * @param event The KeyEvent object that defines the button action.
7788 */
7789 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7790 return false;
7791 }
7792
7793 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007794 * Called on the focused view when a key shortcut event is not handled.
7795 * Override this method to implement local key shortcuts for the View.
7796 * Key shortcuts can also be implemented by setting the
7797 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 *
7799 * @param keyCode The value in event.getKeyCode().
7800 * @param event Description of the key event.
7801 * @return If you handled the event, return true. If you want to allow the
7802 * event to be handled by the next receiver, return false.
7803 */
7804 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7805 return false;
7806 }
7807
7808 /**
7809 * Check whether the called view is a text editor, in which case it
7810 * would make sense to automatically display a soft input window for
7811 * it. Subclasses should override this if they implement
7812 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007813 * a call on that method would return a non-null InputConnection, and
7814 * they are really a first-class editor that the user would normally
7815 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007816 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007817 * <p>The default implementation always returns false. This does
7818 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7819 * will not be called or the user can not otherwise perform edits on your
7820 * view; it is just a hint to the system that this is not the primary
7821 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 * @return Returns true if this view is a text editor, else false.
7824 */
7825 public boolean onCheckIsTextEditor() {
7826 return false;
7827 }
Romain Guy8506ab42009-06-11 17:35:47 -07007828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 /**
7830 * Create a new InputConnection for an InputMethod to interact
7831 * with the view. The default implementation returns null, since it doesn't
7832 * support input methods. You can override this to implement such support.
7833 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007834 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 * <p>When implementing this, you probably also want to implement
7836 * {@link #onCheckIsTextEditor()} to indicate you will return a
7837 * non-null InputConnection.
7838 *
7839 * @param outAttrs Fill in with attribute information about the connection.
7840 */
7841 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7842 return null;
7843 }
7844
7845 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007846 * Called by the {@link android.view.inputmethod.InputMethodManager}
7847 * when a view who is not the current
7848 * input connection target is trying to make a call on the manager. The
7849 * default implementation returns false; you can override this to return
7850 * true for certain views if you are performing InputConnection proxying
7851 * to them.
7852 * @param view The View that is making the InputMethodManager call.
7853 * @return Return true to allow the call, false to reject.
7854 */
7855 public boolean checkInputConnectionProxy(View view) {
7856 return false;
7857 }
Romain Guy8506ab42009-06-11 17:35:47 -07007858
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 * Show the context menu for this view. It is not safe to hold on to the
7861 * menu after returning from this method.
7862 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007863 * You should normally not overload this method. Overload
7864 * {@link #onCreateContextMenu(ContextMenu)} or define an
7865 * {@link OnCreateContextMenuListener} to add items to the context menu.
7866 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 * @param menu The context menu to populate
7868 */
7869 public void createContextMenu(ContextMenu menu) {
7870 ContextMenuInfo menuInfo = getContextMenuInfo();
7871
7872 // Sets the current menu info so all items added to menu will have
7873 // my extra info set.
7874 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7875
7876 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007877 ListenerInfo li = mListenerInfo;
7878 if (li != null && li.mOnCreateContextMenuListener != null) {
7879 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 }
7881
7882 // Clear the extra information so subsequent items that aren't mine don't
7883 // have my extra info.
7884 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7885
7886 if (mParent != null) {
7887 mParent.createContextMenu(menu);
7888 }
7889 }
7890
7891 /**
7892 * Views should implement this if they have extra information to associate
7893 * with the context menu. The return result is supplied as a parameter to
7894 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7895 * callback.
7896 *
7897 * @return Extra information about the item for which the context menu
7898 * should be shown. This information will vary across different
7899 * subclasses of View.
7900 */
7901 protected ContextMenuInfo getContextMenuInfo() {
7902 return null;
7903 }
7904
7905 /**
7906 * Views should implement this if the view itself is going to add items to
7907 * the context menu.
7908 *
7909 * @param menu the context menu to populate
7910 */
7911 protected void onCreateContextMenu(ContextMenu menu) {
7912 }
7913
7914 /**
7915 * Implement this method to handle trackball motion events. The
7916 * <em>relative</em> movement of the trackball since the last event
7917 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7918 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7919 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7920 * they will often be fractional values, representing the more fine-grained
7921 * movement information available from a trackball).
7922 *
7923 * @param event The motion event.
7924 * @return True if the event was handled, false otherwise.
7925 */
7926 public boolean onTrackballEvent(MotionEvent event) {
7927 return false;
7928 }
7929
7930 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007931 * Implement this method to handle generic motion events.
7932 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007933 * Generic motion events describe joystick movements, mouse hovers, track pad
7934 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007935 * {@link MotionEvent#getSource() source} of the motion event specifies
7936 * the class of input that was received. Implementations of this method
7937 * must examine the bits in the source before processing the event.
7938 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007939 * </p><p>
7940 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7941 * are delivered to the view under the pointer. All other generic motion events are
7942 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007943 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007944 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007945 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007946 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7947 * // process the joystick movement...
7948 * return true;
7949 * }
7950 * }
7951 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7952 * switch (event.getAction()) {
7953 * case MotionEvent.ACTION_HOVER_MOVE:
7954 * // process the mouse hover movement...
7955 * return true;
7956 * case MotionEvent.ACTION_SCROLL:
7957 * // process the scroll wheel movement...
7958 * return true;
7959 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007960 * }
7961 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007962 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007963 *
7964 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007965 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007966 */
7967 public boolean onGenericMotionEvent(MotionEvent event) {
7968 return false;
7969 }
7970
7971 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007972 * Implement this method to handle hover events.
7973 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007974 * This method is called whenever a pointer is hovering into, over, or out of the
7975 * bounds of a view and the view is not currently being touched.
7976 * Hover events are represented as pointer events with action
7977 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7978 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7979 * </p>
7980 * <ul>
7981 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7982 * when the pointer enters the bounds of the view.</li>
7983 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7984 * when the pointer has already entered the bounds of the view and has moved.</li>
7985 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7986 * when the pointer has exited the bounds of the view or when the pointer is
7987 * about to go down due to a button click, tap, or similar user action that
7988 * causes the view to be touched.</li>
7989 * </ul>
7990 * <p>
7991 * The view should implement this method to return true to indicate that it is
7992 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007993 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007994 * The default implementation calls {@link #setHovered} to update the hovered state
7995 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007996 * is enabled and is clickable. The default implementation also sends hover
7997 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007998 * </p>
7999 *
8000 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008001 * @return True if the view handled the hover event.
8002 *
8003 * @see #isHovered
8004 * @see #setHovered
8005 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008006 */
8007 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008008 // The root view may receive hover (or touch) events that are outside the bounds of
8009 // the window. This code ensures that we only send accessibility events for
8010 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008011 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008012 if (!mSendingHoverAccessibilityEvents) {
8013 if ((action == MotionEvent.ACTION_HOVER_ENTER
8014 || action == MotionEvent.ACTION_HOVER_MOVE)
8015 && !hasHoveredChild()
8016 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008017 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008018 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008019 }
8020 } else {
8021 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008022 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008023 && !pointInView(event.getX(), event.getY()))) {
8024 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008025 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008026 // If the window does not have input focus we take away accessibility
8027 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008028 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008029 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008030 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008031 }
Jeff Browna1b24182011-07-28 13:38:24 -07008032 }
8033
Jeff Brown87b7f802011-06-21 18:35:45 -07008034 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008035 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008036 case MotionEvent.ACTION_HOVER_ENTER:
8037 setHovered(true);
8038 break;
8039 case MotionEvent.ACTION_HOVER_EXIT:
8040 setHovered(false);
8041 break;
8042 }
Jeff Browna1b24182011-07-28 13:38:24 -07008043
8044 // Dispatch the event to onGenericMotionEvent before returning true.
8045 // This is to provide compatibility with existing applications that
8046 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8047 // break because of the new default handling for hoverable views
8048 // in onHoverEvent.
8049 // Note that onGenericMotionEvent will be called by default when
8050 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8051 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008052 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008053 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008054
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008055 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008056 }
8057
8058 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008059 * Returns true if the view should handle {@link #onHoverEvent}
8060 * by calling {@link #setHovered} to change its hovered state.
8061 *
8062 * @return True if the view is hoverable.
8063 */
8064 private boolean isHoverable() {
8065 final int viewFlags = mViewFlags;
8066 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8067 return false;
8068 }
8069
8070 return (viewFlags & CLICKABLE) == CLICKABLE
8071 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8072 }
8073
8074 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008075 * Returns true if the view is currently hovered.
8076 *
8077 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008078 *
8079 * @see #setHovered
8080 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008081 */
Jeff Brown10b62902011-06-20 16:40:37 -07008082 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008083 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008084 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008085 }
8086
8087 /**
8088 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008089 * <p>
8090 * Calling this method also changes the drawable state of the view. This
8091 * enables the view to react to hover by using different drawable resources
8092 * to change its appearance.
8093 * </p><p>
8094 * The {@link #onHoverChanged} method is called when the hovered state changes.
8095 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008096 *
8097 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008098 *
8099 * @see #isHovered
8100 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008101 */
8102 public void setHovered(boolean hovered) {
8103 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008104 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8105 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008106 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008107 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008108 }
8109 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008110 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8111 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008112 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008113 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008114 }
8115 }
8116 }
8117
8118 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008119 * Implement this method to handle hover state changes.
8120 * <p>
8121 * This method is called whenever the hover state changes as a result of a
8122 * call to {@link #setHovered}.
8123 * </p>
8124 *
8125 * @param hovered The current hover state, as returned by {@link #isHovered}.
8126 *
8127 * @see #isHovered
8128 * @see #setHovered
8129 */
8130 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008131 }
8132
8133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 * Implement this method to handle touch screen motion events.
8135 *
8136 * @param event The motion event.
8137 * @return True if the event was handled, false otherwise.
8138 */
8139 public boolean onTouchEvent(MotionEvent event) {
8140 final int viewFlags = mViewFlags;
8141
8142 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008143 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008144 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146 // A disabled view that is clickable still consumes the touch
8147 // events, it just doesn't respond to them.
8148 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8149 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8150 }
8151
8152 if (mTouchDelegate != null) {
8153 if (mTouchDelegate.onTouchEvent(event)) {
8154 return true;
8155 }
8156 }
8157
8158 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8159 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8160 switch (event.getAction()) {
8161 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008162 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8163 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164 // take focus if we don't have it already and we should in
8165 // touch mode.
8166 boolean focusTaken = false;
8167 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8168 focusTaken = requestFocus();
8169 }
8170
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008171 if (prepressed) {
8172 // The button is being released before we actually
8173 // showed it as pressed. Make it show the pressed
8174 // state now (before scheduling the click) to ensure
8175 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008176 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008177 }
Joe Malin32736f02011-01-19 16:14:20 -08008178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 if (!mHasPerformedLongPress) {
8180 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008181 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008182
8183 // Only perform take click actions if we were in the pressed state
8184 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008185 // Use a Runnable and post this rather than calling
8186 // performClick directly. This lets other visual state
8187 // of the view update before click actions start.
8188 if (mPerformClick == null) {
8189 mPerformClick = new PerformClick();
8190 }
8191 if (!post(mPerformClick)) {
8192 performClick();
8193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 }
8195 }
8196
8197 if (mUnsetPressedState == null) {
8198 mUnsetPressedState = new UnsetPressedState();
8199 }
8200
Adam Powelle14579b2009-12-16 18:39:52 -08008201 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008202 postDelayed(mUnsetPressedState,
8203 ViewConfiguration.getPressedStateDuration());
8204 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205 // If the post failed, unpress right now
8206 mUnsetPressedState.run();
8207 }
Adam Powelle14579b2009-12-16 18:39:52 -08008208 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008209 }
8210 break;
8211
8212 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008213 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008214
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008215 if (performButtonActionOnTouchDown(event)) {
8216 break;
8217 }
8218
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008219 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008220 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008221
8222 // For views inside a scrolling container, delay the pressed feedback for
8223 // a short period in case this is a scroll.
8224 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008225 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008226 if (mPendingCheckForTap == null) {
8227 mPendingCheckForTap = new CheckForTap();
8228 }
8229 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8230 } else {
8231 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008232 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008233 checkForLongClick(0);
8234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 break;
8236
8237 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008238 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008239 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 break;
8241
8242 case MotionEvent.ACTION_MOVE:
8243 final int x = (int) event.getX();
8244 final int y = (int) event.getY();
8245
8246 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008247 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008249 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008250 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008251 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008252 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008253
Adam Powell4d6f0662012-02-21 15:11:11 -08008254 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008256 }
8257 break;
8258 }
8259 return true;
8260 }
8261
8262 return false;
8263 }
8264
8265 /**
Adam Powell10298662011-08-14 18:26:30 -07008266 * @hide
8267 */
8268 public boolean isInScrollingContainer() {
8269 ViewParent p = getParent();
8270 while (p != null && p instanceof ViewGroup) {
8271 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8272 return true;
8273 }
8274 p = p.getParent();
8275 }
8276 return false;
8277 }
8278
8279 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008280 * Remove the longpress detection timer.
8281 */
8282 private void removeLongPressCallback() {
8283 if (mPendingCheckForLongPress != null) {
8284 removeCallbacks(mPendingCheckForLongPress);
8285 }
8286 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008287
8288 /**
8289 * Remove the pending click action
8290 */
8291 private void removePerformClickCallback() {
8292 if (mPerformClick != null) {
8293 removeCallbacks(mPerformClick);
8294 }
8295 }
8296
Adam Powelle14579b2009-12-16 18:39:52 -08008297 /**
Romain Guya440b002010-02-24 15:57:54 -08008298 * Remove the prepress detection timer.
8299 */
8300 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008301 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008302 setPressed(false);
8303 removeCallbacks(mUnsetPressedState);
8304 }
8305 }
8306
8307 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008308 * Remove the tap detection timer.
8309 */
8310 private void removeTapCallback() {
8311 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008312 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008313 removeCallbacks(mPendingCheckForTap);
8314 }
8315 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008316
8317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 * Cancels a pending long press. Your subclass can use this if you
8319 * want the context menu to come up if the user presses and holds
8320 * at the same place, but you don't want it to come up if they press
8321 * and then move around enough to cause scrolling.
8322 */
8323 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008324 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008325
8326 /*
8327 * The prepressed state handled by the tap callback is a display
8328 * construct, but the tap callback will post a long press callback
8329 * less its own timeout. Remove it here.
8330 */
8331 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 }
8333
8334 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008335 * Remove the pending callback for sending a
8336 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8337 */
8338 private void removeSendViewScrolledAccessibilityEventCallback() {
8339 if (mSendViewScrolledAccessibilityEvent != null) {
8340 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008341 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008342 }
8343 }
8344
8345 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008346 * Sets the TouchDelegate for this View.
8347 */
8348 public void setTouchDelegate(TouchDelegate delegate) {
8349 mTouchDelegate = delegate;
8350 }
8351
8352 /**
8353 * Gets the TouchDelegate for this View.
8354 */
8355 public TouchDelegate getTouchDelegate() {
8356 return mTouchDelegate;
8357 }
8358
8359 /**
8360 * Set flags controlling behavior of this view.
8361 *
8362 * @param flags Constant indicating the value which should be set
8363 * @param mask Constant indicating the bit range that should be changed
8364 */
8365 void setFlags(int flags, int mask) {
8366 int old = mViewFlags;
8367 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8368
8369 int changed = mViewFlags ^ old;
8370 if (changed == 0) {
8371 return;
8372 }
8373 int privateFlags = mPrivateFlags;
8374
8375 /* Check if the FOCUSABLE bit has changed */
8376 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008377 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008379 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008380 /* Give up focus if we are no longer focusable */
8381 clearFocus();
8382 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008383 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 /*
8385 * Tell the view system that we are now available to take focus
8386 * if no one else already has it.
8387 */
8388 if (mParent != null) mParent.focusableViewAvailable(this);
8389 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008390 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8391 notifyAccessibilityStateChanged();
8392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008393 }
8394
8395 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8396 if ((changed & VISIBILITY_MASK) != 0) {
8397 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008398 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008399 * it was not visible. Marking it drawn ensures that the invalidation will
8400 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008401 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008402 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008403 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008404
8405 needGlobalAttributesUpdate(true);
8406
8407 // a view becoming visible is worth notifying the parent
8408 // about in case nothing has focus. even if this specific view
8409 // isn't focusable, it may contain something that is, so let
8410 // the root view try to give this focus if nothing else does.
8411 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8412 mParent.focusableViewAvailable(this);
8413 }
8414 }
8415 }
8416
8417 /* Check if the GONE bit has changed */
8418 if ((changed & GONE) != 0) {
8419 needGlobalAttributesUpdate(false);
8420 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421
Romain Guyecd80ee2009-12-03 17:13:02 -08008422 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8423 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008424 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008425 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008426 if (mParent instanceof View) {
8427 // GONE views noop invalidation, so invalidate the parent
8428 ((View) mParent).invalidate(true);
8429 }
8430 // Mark the view drawn to ensure that it gets invalidated properly the next
8431 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008432 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 }
8434 if (mAttachInfo != null) {
8435 mAttachInfo.mViewVisibilityChanged = true;
8436 }
8437 }
8438
8439 /* Check if the VISIBLE bit has changed */
8440 if ((changed & INVISIBLE) != 0) {
8441 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008442 /*
8443 * If this view is becoming invisible, set the DRAWN flag so that
8444 * the next invalidate() will not be skipped.
8445 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008446 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008447
8448 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008449 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008450 if (getRootView() != this) {
8451 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008452 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453 }
8454 }
8455 if (mAttachInfo != null) {
8456 mAttachInfo.mViewVisibilityChanged = true;
8457 }
8458 }
8459
Adam Powell326d8082009-12-09 15:10:07 -08008460 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008461 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008462 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8463 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008464 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008465 } else if (mParent != null) {
8466 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008467 }
Adam Powell326d8082009-12-09 15:10:07 -08008468 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8469 }
8470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8472 destroyDrawingCache();
8473 }
8474
8475 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8476 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008477 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008478 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 }
8480
8481 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8482 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008483 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008484 }
8485
8486 if ((changed & DRAW_MASK) != 0) {
8487 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008488 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008489 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8490 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008492 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008493 }
8494 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008495 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 }
8497 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008498 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 }
8500
8501 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008502 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008503 mParent.recomputeViewAttributes(this);
8504 }
8505 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008506
8507 if (AccessibilityManager.getInstance(mContext).isEnabled()
8508 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8509 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8510 notifyAccessibilityStateChanged();
8511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008512 }
8513
8514 /**
8515 * Change the view's z order in the tree, so it's on top of other sibling
8516 * views
8517 */
8518 public void bringToFront() {
8519 if (mParent != null) {
8520 mParent.bringChildToFront(this);
8521 }
8522 }
8523
8524 /**
8525 * This is called in response to an internal scroll in this view (i.e., the
8526 * view scrolled its own contents). This is typically as a result of
8527 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8528 * called.
8529 *
8530 * @param l Current horizontal scroll origin.
8531 * @param t Current vertical scroll origin.
8532 * @param oldl Previous horizontal scroll origin.
8533 * @param oldt Previous vertical scroll origin.
8534 */
8535 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008536 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8537 postSendViewScrolledAccessibilityEventCallback();
8538 }
8539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 mBackgroundSizeChanged = true;
8541
8542 final AttachInfo ai = mAttachInfo;
8543 if (ai != null) {
8544 ai.mViewScrollChanged = true;
8545 }
8546 }
8547
8548 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008549 * Interface definition for a callback to be invoked when the layout bounds of a view
8550 * changes due to layout processing.
8551 */
8552 public interface OnLayoutChangeListener {
8553 /**
8554 * Called when the focus state of a view has changed.
8555 *
8556 * @param v The view whose state has changed.
8557 * @param left The new value of the view's left property.
8558 * @param top The new value of the view's top property.
8559 * @param right The new value of the view's right property.
8560 * @param bottom The new value of the view's bottom property.
8561 * @param oldLeft The previous value of the view's left property.
8562 * @param oldTop The previous value of the view's top property.
8563 * @param oldRight The previous value of the view's right property.
8564 * @param oldBottom The previous value of the view's bottom property.
8565 */
8566 void onLayoutChange(View v, int left, int top, int right, int bottom,
8567 int oldLeft, int oldTop, int oldRight, int oldBottom);
8568 }
8569
8570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 * This is called during layout when the size of this view has changed. If
8572 * you were just added to the view hierarchy, you're called with the old
8573 * values of 0.
8574 *
8575 * @param w Current width of this view.
8576 * @param h Current height of this view.
8577 * @param oldw Old width of this view.
8578 * @param oldh Old height of this view.
8579 */
8580 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8581 }
8582
8583 /**
8584 * Called by draw to draw the child views. This may be overridden
8585 * by derived classes to gain control just before its children are drawn
8586 * (but after its own view has been drawn).
8587 * @param canvas the canvas on which to draw the view
8588 */
8589 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 }
8592
8593 /**
8594 * Gets the parent of this view. Note that the parent is a
8595 * ViewParent and not necessarily a View.
8596 *
8597 * @return Parent of this view.
8598 */
8599 public final ViewParent getParent() {
8600 return mParent;
8601 }
8602
8603 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008604 * Set the horizontal scrolled position of your view. This will cause a call to
8605 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8606 * invalidated.
8607 * @param value the x position to scroll to
8608 */
8609 public void setScrollX(int value) {
8610 scrollTo(value, mScrollY);
8611 }
8612
8613 /**
8614 * Set the vertical scrolled position of your view. This will cause a call to
8615 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8616 * invalidated.
8617 * @param value the y position to scroll to
8618 */
8619 public void setScrollY(int value) {
8620 scrollTo(mScrollX, value);
8621 }
8622
8623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 * Return the scrolled left position of this view. This is the left edge of
8625 * the displayed part of your view. You do not need to draw any pixels
8626 * farther left, since those are outside of the frame of your view on
8627 * screen.
8628 *
8629 * @return The left edge of the displayed part of your view, in pixels.
8630 */
8631 public final int getScrollX() {
8632 return mScrollX;
8633 }
8634
8635 /**
8636 * Return the scrolled top position of this view. This is the top edge of
8637 * the displayed part of your view. You do not need to draw any pixels above
8638 * it, since those are outside of the frame of your view on screen.
8639 *
8640 * @return The top edge of the displayed part of your view, in pixels.
8641 */
8642 public final int getScrollY() {
8643 return mScrollY;
8644 }
8645
8646 /**
8647 * Return the width of the your view.
8648 *
8649 * @return The width of your view, in pixels.
8650 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008651 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 public final int getWidth() {
8653 return mRight - mLeft;
8654 }
8655
8656 /**
8657 * Return the height of your view.
8658 *
8659 * @return The height of your view, in pixels.
8660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008661 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008662 public final int getHeight() {
8663 return mBottom - mTop;
8664 }
8665
8666 /**
8667 * Return the visible drawing bounds of your view. Fills in the output
8668 * rectangle with the values from getScrollX(), getScrollY(),
8669 * getWidth(), and getHeight().
8670 *
8671 * @param outRect The (scrolled) drawing bounds of the view.
8672 */
8673 public void getDrawingRect(Rect outRect) {
8674 outRect.left = mScrollX;
8675 outRect.top = mScrollY;
8676 outRect.right = mScrollX + (mRight - mLeft);
8677 outRect.bottom = mScrollY + (mBottom - mTop);
8678 }
8679
8680 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008681 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8682 * raw width component (that is the result is masked by
8683 * {@link #MEASURED_SIZE_MASK}).
8684 *
8685 * @return The raw measured width of this view.
8686 */
8687 public final int getMeasuredWidth() {
8688 return mMeasuredWidth & MEASURED_SIZE_MASK;
8689 }
8690
8691 /**
8692 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008693 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008694 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 * This should be used during measurement and layout calculations only. Use
8696 * {@link #getWidth()} to see how wide a view is after layout.
8697 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008698 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008699 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008700 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 return mMeasuredWidth;
8702 }
8703
8704 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008705 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8706 * raw width component (that is the result is masked by
8707 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008709 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 */
8711 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008712 return mMeasuredHeight & MEASURED_SIZE_MASK;
8713 }
8714
8715 /**
8716 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008717 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008718 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8719 * This should be used during measurement and layout calculations only. Use
8720 * {@link #getHeight()} to see how wide a view is after layout.
8721 *
8722 * @return The measured width of this view as a bit mask.
8723 */
8724 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008725 return mMeasuredHeight;
8726 }
8727
8728 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008729 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8730 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8731 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8732 * and the height component is at the shifted bits
8733 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8734 */
8735 public final int getMeasuredState() {
8736 return (mMeasuredWidth&MEASURED_STATE_MASK)
8737 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8738 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8739 }
8740
8741 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008742 * The transform matrix of this view, which is calculated based on the current
8743 * roation, scale, and pivot properties.
8744 *
8745 * @see #getRotation()
8746 * @see #getScaleX()
8747 * @see #getScaleY()
8748 * @see #getPivotX()
8749 * @see #getPivotY()
8750 * @return The current transform matrix for the view
8751 */
8752 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008753 if (mTransformationInfo != null) {
8754 updateMatrix();
8755 return mTransformationInfo.mMatrix;
8756 }
8757 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008758 }
8759
8760 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008761 * Utility function to determine if the value is far enough away from zero to be
8762 * considered non-zero.
8763 * @param value A floating point value to check for zero-ness
8764 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8765 */
8766 private static boolean nonzero(float value) {
8767 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8768 }
8769
8770 /**
Jeff Brown86671742010-09-30 20:00:15 -07008771 * Returns true if the transform matrix is the identity matrix.
8772 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008773 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008774 * @return True if the transform matrix is the identity matrix, false otherwise.
8775 */
Jeff Brown86671742010-09-30 20:00:15 -07008776 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008777 if (mTransformationInfo != null) {
8778 updateMatrix();
8779 return mTransformationInfo.mMatrixIsIdentity;
8780 }
8781 return true;
8782 }
8783
8784 void ensureTransformationInfo() {
8785 if (mTransformationInfo == null) {
8786 mTransformationInfo = new TransformationInfo();
8787 }
Jeff Brown86671742010-09-30 20:00:15 -07008788 }
8789
8790 /**
8791 * Recomputes the transform matrix if necessary.
8792 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008793 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008794 final TransformationInfo info = mTransformationInfo;
8795 if (info == null) {
8796 return;
8797 }
8798 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008799 // transform-related properties have changed since the last time someone
8800 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008801
8802 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008803 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008804 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8805 info.mPrevWidth = mRight - mLeft;
8806 info.mPrevHeight = mBottom - mTop;
8807 info.mPivotX = info.mPrevWidth / 2f;
8808 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008809 }
8810 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008811 info.mMatrix.reset();
8812 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8813 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8814 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8815 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008816 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008817 if (info.mCamera == null) {
8818 info.mCamera = new Camera();
8819 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008820 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008821 info.mCamera.save();
8822 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8823 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8824 info.mCamera.getMatrix(info.matrix3D);
8825 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8826 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8827 info.mPivotY + info.mTranslationY);
8828 info.mMatrix.postConcat(info.matrix3D);
8829 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008830 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008831 info.mMatrixDirty = false;
8832 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8833 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008834 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008835 }
8836
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008837 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008838 * Utility method to retrieve the inverse of the current mMatrix property.
8839 * We cache the matrix to avoid recalculating it when transform properties
8840 * have not changed.
8841 *
8842 * @return The inverse of the current matrix of this view.
8843 */
Jeff Brown86671742010-09-30 20:00:15 -07008844 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008845 final TransformationInfo info = mTransformationInfo;
8846 if (info != null) {
8847 updateMatrix();
8848 if (info.mInverseMatrixDirty) {
8849 if (info.mInverseMatrix == null) {
8850 info.mInverseMatrix = new Matrix();
8851 }
8852 info.mMatrix.invert(info.mInverseMatrix);
8853 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008854 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008855 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008856 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008857 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008858 }
8859
8860 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008861 * Gets the distance along the Z axis from the camera to this view.
8862 *
8863 * @see #setCameraDistance(float)
8864 *
8865 * @return The distance along the Z axis.
8866 */
8867 public float getCameraDistance() {
8868 ensureTransformationInfo();
8869 final float dpi = mResources.getDisplayMetrics().densityDpi;
8870 final TransformationInfo info = mTransformationInfo;
8871 if (info.mCamera == null) {
8872 info.mCamera = new Camera();
8873 info.matrix3D = new Matrix();
8874 }
8875 return -(info.mCamera.getLocationZ() * dpi);
8876 }
8877
8878 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008879 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8880 * views are drawn) from the camera to this view. The camera's distance
8881 * affects 3D transformations, for instance rotations around the X and Y
8882 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008883 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008884 * use a camera distance that's greater than the height (X axis rotation) or
8885 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008886 *
Romain Guya5364ee2011-02-24 14:46:04 -08008887 * <p>The distance of the camera from the view plane can have an affect on the
8888 * perspective distortion of the view when it is rotated around the x or y axis.
8889 * For example, a large distance will result in a large viewing angle, and there
8890 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008891 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008892 * also result in some drawing artifacts if the rotated view ends up partially
8893 * behind the camera (which is why the recommendation is to use a distance at
8894 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008895 *
Romain Guya5364ee2011-02-24 14:46:04 -08008896 * <p>The distance is expressed in "depth pixels." The default distance depends
8897 * on the screen density. For instance, on a medium density display, the
8898 * default distance is 1280. On a high density display, the default distance
8899 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008900 *
Romain Guya5364ee2011-02-24 14:46:04 -08008901 * <p>If you want to specify a distance that leads to visually consistent
8902 * results across various densities, use the following formula:</p>
8903 * <pre>
8904 * float scale = context.getResources().getDisplayMetrics().density;
8905 * view.setCameraDistance(distance * scale);
8906 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008907 *
Romain Guya5364ee2011-02-24 14:46:04 -08008908 * <p>The density scale factor of a high density display is 1.5,
8909 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008910 *
Romain Guya5364ee2011-02-24 14:46:04 -08008911 * @param distance The distance in "depth pixels", if negative the opposite
8912 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008913 *
8914 * @see #setRotationX(float)
8915 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008916 */
8917 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008918 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008919
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008920 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008921 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008922 final TransformationInfo info = mTransformationInfo;
8923 if (info.mCamera == null) {
8924 info.mCamera = new Camera();
8925 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008926 }
8927
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008928 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8929 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008930
Chet Haase9d1992d2012-03-13 11:03:25 -07008931 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008932 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008933 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008934 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008935 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008936 // View was rejected last time it was drawn by its parent; this may have changed
8937 invalidateParentIfNeeded();
8938 }
Romain Guya5364ee2011-02-24 14:46:04 -08008939 }
8940
8941 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008942 * The degrees that the view is rotated around the pivot point.
8943 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008944 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008945 * @see #getPivotX()
8946 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008947 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 * @return The degrees of rotation.
8949 */
Chet Haasea5531132012-02-02 13:41:44 -08008950 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008951 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008952 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008953 }
8954
8955 /**
Chet Haase897247b2010-09-09 14:54:47 -07008956 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8957 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008958 *
8959 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008960 *
8961 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008962 * @see #getPivotX()
8963 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008964 * @see #setRotationX(float)
8965 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008966 *
8967 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008968 */
8969 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008970 ensureTransformationInfo();
8971 final TransformationInfo info = mTransformationInfo;
8972 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008973 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008974 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008975 info.mRotation = rotation;
8976 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008977 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008978 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008979 mDisplayList.setRotation(rotation);
8980 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008981 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008982 // View was rejected last time it was drawn by its parent; this may have changed
8983 invalidateParentIfNeeded();
8984 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008985 }
8986 }
8987
8988 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008989 * The degrees that the view is rotated around the vertical axis through the pivot point.
8990 *
8991 * @see #getPivotX()
8992 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008993 * @see #setRotationY(float)
8994 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008995 * @return The degrees of Y rotation.
8996 */
Chet Haasea5531132012-02-02 13:41:44 -08008997 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008998 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008999 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009000 }
9001
9002 /**
Chet Haase897247b2010-09-09 14:54:47 -07009003 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9004 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9005 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009006 *
Romain Guya5364ee2011-02-24 14:46:04 -08009007 * When rotating large views, it is recommended to adjust the camera distance
9008 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009009 *
9010 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009011 *
9012 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009013 * @see #getPivotX()
9014 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009015 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009016 * @see #setRotationX(float)
9017 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009018 *
9019 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009020 */
9021 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009022 ensureTransformationInfo();
9023 final TransformationInfo info = mTransformationInfo;
9024 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009025 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009026 info.mRotationY = rotationY;
9027 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009028 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009029 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009030 mDisplayList.setRotationY(rotationY);
9031 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009032 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009033 // View was rejected last time it was drawn by its parent; this may have changed
9034 invalidateParentIfNeeded();
9035 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009036 }
9037 }
9038
9039 /**
9040 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9041 *
9042 * @see #getPivotX()
9043 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009044 * @see #setRotationX(float)
9045 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009046 * @return The degrees of X rotation.
9047 */
Chet Haasea5531132012-02-02 13:41:44 -08009048 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009049 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009050 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009051 }
9052
9053 /**
Chet Haase897247b2010-09-09 14:54:47 -07009054 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9055 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9056 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009057 *
Romain Guya5364ee2011-02-24 14:46:04 -08009058 * When rotating large views, it is recommended to adjust the camera distance
9059 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009060 *
9061 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009062 *
9063 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009064 * @see #getPivotX()
9065 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009066 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009067 * @see #setRotationY(float)
9068 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009069 *
9070 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009071 */
9072 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009073 ensureTransformationInfo();
9074 final TransformationInfo info = mTransformationInfo;
9075 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009076 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009077 info.mRotationX = rotationX;
9078 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009079 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009080 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009081 mDisplayList.setRotationX(rotationX);
9082 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009083 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009084 // View was rejected last time it was drawn by its parent; this may have changed
9085 invalidateParentIfNeeded();
9086 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009087 }
9088 }
9089
9090 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009091 * The amount that the view is scaled in x around the pivot point, as a proportion of
9092 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9093 *
Joe Onorato93162322010-09-16 15:42:01 -04009094 * <p>By default, this is 1.0f.
9095 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009096 * @see #getPivotX()
9097 * @see #getPivotY()
9098 * @return The scaling factor.
9099 */
Chet Haasea5531132012-02-02 13:41:44 -08009100 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009101 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009102 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009103 }
9104
9105 /**
9106 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9107 * the view's unscaled width. A value of 1 means that no scaling is applied.
9108 *
9109 * @param scaleX The scaling factor.
9110 * @see #getPivotX()
9111 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009112 *
9113 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009114 */
9115 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009116 ensureTransformationInfo();
9117 final TransformationInfo info = mTransformationInfo;
9118 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009119 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009120 info.mScaleX = scaleX;
9121 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009122 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009123 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009124 mDisplayList.setScaleX(scaleX);
9125 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009126 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009127 // View was rejected last time it was drawn by its parent; this may have changed
9128 invalidateParentIfNeeded();
9129 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009130 }
9131 }
9132
9133 /**
9134 * The amount that the view is scaled in y around the pivot point, as a proportion of
9135 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9136 *
Joe Onorato93162322010-09-16 15:42:01 -04009137 * <p>By default, this is 1.0f.
9138 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009139 * @see #getPivotX()
9140 * @see #getPivotY()
9141 * @return The scaling factor.
9142 */
Chet Haasea5531132012-02-02 13:41:44 -08009143 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009144 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009145 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009146 }
9147
9148 /**
9149 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9150 * the view's unscaled width. A value of 1 means that no scaling is applied.
9151 *
9152 * @param scaleY The scaling factor.
9153 * @see #getPivotX()
9154 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009155 *
9156 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009157 */
9158 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009159 ensureTransformationInfo();
9160 final TransformationInfo info = mTransformationInfo;
9161 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009162 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009163 info.mScaleY = scaleY;
9164 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009165 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009166 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009167 mDisplayList.setScaleY(scaleY);
9168 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009169 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009170 // View was rejected last time it was drawn by its parent; this may have changed
9171 invalidateParentIfNeeded();
9172 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009173 }
9174 }
9175
9176 /**
9177 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9178 * and {@link #setScaleX(float) scaled}.
9179 *
9180 * @see #getRotation()
9181 * @see #getScaleX()
9182 * @see #getScaleY()
9183 * @see #getPivotY()
9184 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009185 *
9186 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009187 */
Chet Haasea5531132012-02-02 13:41:44 -08009188 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009189 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009190 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009191 }
9192
9193 /**
9194 * Sets the x location of the point around which the view is
9195 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009196 * By default, the pivot point is centered on the object.
9197 * Setting this property disables this behavior and causes the view to use only the
9198 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 *
9200 * @param pivotX The x location of the pivot point.
9201 * @see #getRotation()
9202 * @see #getScaleX()
9203 * @see #getScaleY()
9204 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009205 *
9206 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009207 */
9208 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009209 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009210 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009211 final TransformationInfo info = mTransformationInfo;
9212 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009213 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009214 info.mPivotX = pivotX;
9215 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009216 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009217 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009218 mDisplayList.setPivotX(pivotX);
9219 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009220 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009221 // View was rejected last time it was drawn by its parent; this may have changed
9222 invalidateParentIfNeeded();
9223 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009224 }
9225 }
9226
9227 /**
9228 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9229 * and {@link #setScaleY(float) scaled}.
9230 *
9231 * @see #getRotation()
9232 * @see #getScaleX()
9233 * @see #getScaleY()
9234 * @see #getPivotY()
9235 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009236 *
9237 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009238 */
Chet Haasea5531132012-02-02 13:41:44 -08009239 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009240 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009241 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009242 }
9243
9244 /**
9245 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009246 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9247 * Setting this property disables this behavior and causes the view to use only the
9248 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009249 *
9250 * @param pivotY The y location of the pivot point.
9251 * @see #getRotation()
9252 * @see #getScaleX()
9253 * @see #getScaleY()
9254 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009255 *
9256 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009257 */
9258 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009259 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009260 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 final TransformationInfo info = mTransformationInfo;
9262 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009263 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009264 info.mPivotY = pivotY;
9265 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009266 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009267 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009268 mDisplayList.setPivotY(pivotY);
9269 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009270 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009271 // View was rejected last time it was drawn by its parent; this may have changed
9272 invalidateParentIfNeeded();
9273 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009274 }
9275 }
9276
9277 /**
9278 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9279 * completely transparent and 1 means the view is completely opaque.
9280 *
Joe Onorato93162322010-09-16 15:42:01 -04009281 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009282 * @return The opacity of the view.
9283 */
Chet Haasea5531132012-02-02 13:41:44 -08009284 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009285 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009286 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009287 }
9288
9289 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009290 * Returns whether this View has content which overlaps. This function, intended to be
9291 * overridden by specific View types, is an optimization when alpha is set on a view. If
9292 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9293 * and then composited it into place, which can be expensive. If the view has no overlapping
9294 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9295 * An example of overlapping rendering is a TextView with a background image, such as a
9296 * Button. An example of non-overlapping rendering is a TextView with no background, or
9297 * an ImageView with only the foreground image. The default implementation returns true;
9298 * subclasses should override if they have cases which can be optimized.
9299 *
9300 * @return true if the content in this view might overlap, false otherwise.
9301 */
9302 public boolean hasOverlappingRendering() {
9303 return true;
9304 }
9305
9306 /**
Romain Guy171c5922011-01-06 10:04:23 -08009307 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9308 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009309 *
Romain Guy171c5922011-01-06 10:04:23 -08009310 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9311 * responsible for applying the opacity itself. Otherwise, calling this method is
9312 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009313 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009314 *
Chet Haasea5531132012-02-02 13:41:44 -08009315 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9316 * performance implications. It is generally best to use the alpha property sparingly and
9317 * transiently, as in the case of fading animations.</p>
9318 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009319 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009320 *
Joe Malin32736f02011-01-19 16:14:20 -08009321 * @see #setLayerType(int, android.graphics.Paint)
9322 *
Chet Haase73066682010-11-29 15:55:32 -08009323 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009324 */
9325 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009326 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009327 if (mTransformationInfo.mAlpha != alpha) {
9328 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009329 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009330 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009331 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009332 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009333 invalidate(true);
9334 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009335 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009336 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009337 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009338 mDisplayList.setAlpha(alpha);
9339 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009340 }
Chet Haaseed032702010-10-01 14:05:54 -07009341 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009342 }
9343
9344 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009345 * Faster version of setAlpha() which performs the same steps except there are
9346 * no calls to invalidate(). The caller of this function should perform proper invalidation
9347 * on the parent and this object. The return value indicates whether the subclass handles
9348 * alpha (the return value for onSetAlpha()).
9349 *
9350 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009351 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9352 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009353 */
9354 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009355 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009356 if (mTransformationInfo.mAlpha != alpha) {
9357 mTransformationInfo.mAlpha = alpha;
9358 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9359 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009360 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009361 return true;
9362 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009363 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009364 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009365 mDisplayList.setAlpha(alpha);
9366 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009367 }
Chet Haasea00f3862011-02-22 06:34:40 -08009368 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009369 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009370 }
9371
9372 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 * Top position of this view relative to its parent.
9374 *
9375 * @return The top of this view, in pixels.
9376 */
9377 @ViewDebug.CapturedViewProperty
9378 public final int getTop() {
9379 return mTop;
9380 }
9381
9382 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009383 * Sets the top position of this view relative to its parent. This method is meant to be called
9384 * by the layout system and should not generally be called otherwise, because the property
9385 * may be changed at any time by the layout.
9386 *
9387 * @param top The top of this view, in pixels.
9388 */
9389 public final void setTop(int top) {
9390 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009391 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009392 final boolean matrixIsIdentity = mTransformationInfo == null
9393 || mTransformationInfo.mMatrixIsIdentity;
9394 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009395 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009396 int minTop;
9397 int yLoc;
9398 if (top < mTop) {
9399 minTop = top;
9400 yLoc = top - mTop;
9401 } else {
9402 minTop = mTop;
9403 yLoc = 0;
9404 }
Chet Haasee9140a72011-02-16 16:23:29 -08009405 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009406 }
9407 } else {
9408 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009409 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009410 }
9411
Chet Haaseed032702010-10-01 14:05:54 -07009412 int width = mRight - mLeft;
9413 int oldHeight = mBottom - mTop;
9414
Chet Haase21cd1382010-09-01 17:42:29 -07009415 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009416 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009417 mDisplayList.setTop(mTop);
9418 }
Chet Haase21cd1382010-09-01 17:42:29 -07009419
Chet Haaseed032702010-10-01 14:05:54 -07009420 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9421
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009422 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009423 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009424 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009425 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009426 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009427 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009428 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009429 }
Chet Haase55dbb652010-12-21 20:15:08 -08009430 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009431 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009432 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009433 // View was rejected last time it was drawn by its parent; this may have changed
9434 invalidateParentIfNeeded();
9435 }
Chet Haase21cd1382010-09-01 17:42:29 -07009436 }
9437 }
9438
9439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 * Bottom position of this view relative to its parent.
9441 *
9442 * @return The bottom of this view, in pixels.
9443 */
9444 @ViewDebug.CapturedViewProperty
9445 public final int getBottom() {
9446 return mBottom;
9447 }
9448
9449 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009450 * True if this view has changed since the last time being drawn.
9451 *
9452 * @return The dirty state of this view.
9453 */
9454 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009455 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009456 }
9457
9458 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009459 * Sets the bottom position of this view relative to its parent. This method is meant to be
9460 * called by the layout system and should not generally be called otherwise, because the
9461 * property may be changed at any time by the layout.
9462 *
9463 * @param bottom The bottom of this view, in pixels.
9464 */
9465 public final void setBottom(int bottom) {
9466 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009467 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009468 final boolean matrixIsIdentity = mTransformationInfo == null
9469 || mTransformationInfo.mMatrixIsIdentity;
9470 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009471 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009472 int maxBottom;
9473 if (bottom < mBottom) {
9474 maxBottom = mBottom;
9475 } else {
9476 maxBottom = bottom;
9477 }
Chet Haasee9140a72011-02-16 16:23:29 -08009478 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009479 }
9480 } else {
9481 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009482 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009483 }
9484
Chet Haaseed032702010-10-01 14:05:54 -07009485 int width = mRight - mLeft;
9486 int oldHeight = mBottom - mTop;
9487
Chet Haase21cd1382010-09-01 17:42:29 -07009488 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009489 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009490 mDisplayList.setBottom(mBottom);
9491 }
Chet Haase21cd1382010-09-01 17:42:29 -07009492
Chet Haaseed032702010-10-01 14:05:54 -07009493 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9494
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009495 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009496 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009497 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009498 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009499 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009500 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009501 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009502 }
Chet Haase55dbb652010-12-21 20:15:08 -08009503 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009504 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009505 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009506 // View was rejected last time it was drawn by its parent; this may have changed
9507 invalidateParentIfNeeded();
9508 }
Chet Haase21cd1382010-09-01 17:42:29 -07009509 }
9510 }
9511
9512 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009513 * Left position of this view relative to its parent.
9514 *
9515 * @return The left edge of this view, in pixels.
9516 */
9517 @ViewDebug.CapturedViewProperty
9518 public final int getLeft() {
9519 return mLeft;
9520 }
9521
9522 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009523 * Sets the left position of this view relative to its parent. This method is meant to be called
9524 * by the layout system and should not generally be called otherwise, because the property
9525 * may be changed at any time by the layout.
9526 *
9527 * @param left The bottom of this view, in pixels.
9528 */
9529 public final void setLeft(int left) {
9530 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009531 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009532 final boolean matrixIsIdentity = mTransformationInfo == null
9533 || mTransformationInfo.mMatrixIsIdentity;
9534 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009535 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009536 int minLeft;
9537 int xLoc;
9538 if (left < mLeft) {
9539 minLeft = left;
9540 xLoc = left - mLeft;
9541 } else {
9542 minLeft = mLeft;
9543 xLoc = 0;
9544 }
Chet Haasee9140a72011-02-16 16:23:29 -08009545 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009546 }
9547 } else {
9548 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009549 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009550 }
9551
Chet Haaseed032702010-10-01 14:05:54 -07009552 int oldWidth = mRight - mLeft;
9553 int height = mBottom - mTop;
9554
Chet Haase21cd1382010-09-01 17:42:29 -07009555 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009556 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009557 mDisplayList.setLeft(left);
9558 }
Chet Haase21cd1382010-09-01 17:42:29 -07009559
Chet Haaseed032702010-10-01 14:05:54 -07009560 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9561
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009562 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009563 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009564 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009565 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009566 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009567 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009568 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009569 }
Chet Haase55dbb652010-12-21 20:15:08 -08009570 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009571 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009572 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009573 // View was rejected last time it was drawn by its parent; this may have changed
9574 invalidateParentIfNeeded();
9575 }
Chet Haase21cd1382010-09-01 17:42:29 -07009576 }
9577 }
9578
9579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 * Right position of this view relative to its parent.
9581 *
9582 * @return The right edge of this view, in pixels.
9583 */
9584 @ViewDebug.CapturedViewProperty
9585 public final int getRight() {
9586 return mRight;
9587 }
9588
9589 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009590 * Sets the right position of this view relative to its parent. This method is meant to be called
9591 * by the layout system and should not generally be called otherwise, because the property
9592 * may be changed at any time by the layout.
9593 *
9594 * @param right The bottom of this view, in pixels.
9595 */
9596 public final void setRight(int right) {
9597 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009598 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009599 final boolean matrixIsIdentity = mTransformationInfo == null
9600 || mTransformationInfo.mMatrixIsIdentity;
9601 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009602 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009603 int maxRight;
9604 if (right < mRight) {
9605 maxRight = mRight;
9606 } else {
9607 maxRight = right;
9608 }
Chet Haasee9140a72011-02-16 16:23:29 -08009609 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009610 }
9611 } else {
9612 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009613 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009614 }
9615
Chet Haaseed032702010-10-01 14:05:54 -07009616 int oldWidth = mRight - mLeft;
9617 int height = mBottom - mTop;
9618
Chet Haase21cd1382010-09-01 17:42:29 -07009619 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009620 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009621 mDisplayList.setRight(mRight);
9622 }
Chet Haase21cd1382010-09-01 17:42:29 -07009623
Chet Haaseed032702010-10-01 14:05:54 -07009624 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9625
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009626 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009627 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009628 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009629 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009630 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009631 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009632 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009633 }
Chet Haase55dbb652010-12-21 20:15:08 -08009634 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009635 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009636 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009637 // View was rejected last time it was drawn by its parent; this may have changed
9638 invalidateParentIfNeeded();
9639 }
Chet Haase21cd1382010-09-01 17:42:29 -07009640 }
9641 }
9642
9643 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009644 * The visual x position of this view, in pixels. This is equivalent to the
9645 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009646 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009647 *
Chet Haasedf030d22010-07-30 17:22:38 -07009648 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009649 */
Chet Haasea5531132012-02-02 13:41:44 -08009650 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009651 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009652 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009653 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009654
Chet Haasedf030d22010-07-30 17:22:38 -07009655 /**
9656 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9657 * {@link #setTranslationX(float) translationX} property to be the difference between
9658 * the x value passed in and the current {@link #getLeft() left} property.
9659 *
9660 * @param x The visual x position of this view, in pixels.
9661 */
9662 public void setX(float x) {
9663 setTranslationX(x - mLeft);
9664 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009665
Chet Haasedf030d22010-07-30 17:22:38 -07009666 /**
9667 * The visual y position of this view, in pixels. This is equivalent to the
9668 * {@link #setTranslationY(float) translationY} property plus the current
9669 * {@link #getTop() top} property.
9670 *
9671 * @return The visual y position of this view, in pixels.
9672 */
Chet Haasea5531132012-02-02 13:41:44 -08009673 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009674 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009675 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009676 }
9677
9678 /**
9679 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9680 * {@link #setTranslationY(float) translationY} property to be the difference between
9681 * the y value passed in and the current {@link #getTop() top} property.
9682 *
9683 * @param y The visual y position of this view, in pixels.
9684 */
9685 public void setY(float y) {
9686 setTranslationY(y - mTop);
9687 }
9688
9689
9690 /**
9691 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9692 * This position is post-layout, in addition to wherever the object's
9693 * layout placed it.
9694 *
9695 * @return The horizontal position of this view relative to its left position, in pixels.
9696 */
Chet Haasea5531132012-02-02 13:41:44 -08009697 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009698 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009699 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009700 }
9701
9702 /**
9703 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9704 * This effectively positions the object post-layout, in addition to wherever the object's
9705 * layout placed it.
9706 *
9707 * @param translationX The horizontal position of this view relative to its left position,
9708 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009709 *
9710 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009711 */
9712 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009713 ensureTransformationInfo();
9714 final TransformationInfo info = mTransformationInfo;
9715 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009716 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009717 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009718 info.mTranslationX = translationX;
9719 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009720 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009721 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009722 mDisplayList.setTranslationX(translationX);
9723 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009724 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009725 // View was rejected last time it was drawn by its parent; this may have changed
9726 invalidateParentIfNeeded();
9727 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009728 }
9729 }
9730
9731 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009732 * The horizontal location of this view relative to its {@link #getTop() top} position.
9733 * This position is post-layout, in addition to wherever the object's
9734 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009735 *
Chet Haasedf030d22010-07-30 17:22:38 -07009736 * @return The vertical position of this view relative to its top position,
9737 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009738 */
Chet Haasea5531132012-02-02 13:41:44 -08009739 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009740 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009741 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009742 }
9743
9744 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009745 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9746 * This effectively positions the object post-layout, in addition to wherever the object's
9747 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009748 *
Chet Haasedf030d22010-07-30 17:22:38 -07009749 * @param translationY The vertical position of this view relative to its top position,
9750 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009751 *
9752 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009753 */
Chet Haasedf030d22010-07-30 17:22:38 -07009754 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009755 ensureTransformationInfo();
9756 final TransformationInfo info = mTransformationInfo;
9757 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009758 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009759 info.mTranslationY = translationY;
9760 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009761 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009762 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009763 mDisplayList.setTranslationY(translationY);
9764 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009765 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009766 // View was rejected last time it was drawn by its parent; this may have changed
9767 invalidateParentIfNeeded();
9768 }
Chet Haasedf030d22010-07-30 17:22:38 -07009769 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009770 }
9771
9772 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009773 * Hit rectangle in parent's coordinates
9774 *
9775 * @param outRect The hit rectangle of the view.
9776 */
9777 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009778 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009779 final TransformationInfo info = mTransformationInfo;
9780 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009781 outRect.set(mLeft, mTop, mRight, mBottom);
9782 } else {
9783 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009784 tmpRect.set(-info.mPivotX, -info.mPivotY,
9785 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9786 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009787 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9788 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009789 }
9790 }
9791
9792 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009793 * Determines whether the given point, in local coordinates is inside the view.
9794 */
9795 /*package*/ final boolean pointInView(float localX, float localY) {
9796 return localX >= 0 && localX < (mRight - mLeft)
9797 && localY >= 0 && localY < (mBottom - mTop);
9798 }
9799
9800 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009801 * Utility method to determine whether the given point, in local coordinates,
9802 * is inside the view, where the area of the view is expanded by the slop factor.
9803 * This method is called while processing touch-move events to determine if the event
9804 * is still within the view.
9805 */
9806 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009807 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009808 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 }
9810
9811 /**
9812 * When a view has focus and the user navigates away from it, the next view is searched for
9813 * starting from the rectangle filled in by this method.
9814 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009815 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009816 * of the view. However, if your view maintains some idea of internal selection,
9817 * such as a cursor, or a selected row or column, you should override this method and
9818 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 *
9820 * @param r The rectangle to fill in, in this view's coordinates.
9821 */
9822 public void getFocusedRect(Rect r) {
9823 getDrawingRect(r);
9824 }
9825
9826 /**
9827 * If some part of this view is not clipped by any of its parents, then
9828 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009829 * coordinates (without taking possible View rotations into account), offset
9830 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9831 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009832 *
9833 * @param r If true is returned, r holds the global coordinates of the
9834 * visible portion of this view.
9835 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9836 * between this view and its root. globalOffet may be null.
9837 * @return true if r is non-empty (i.e. part of the view is visible at the
9838 * root level.
9839 */
9840 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9841 int width = mRight - mLeft;
9842 int height = mBottom - mTop;
9843 if (width > 0 && height > 0) {
9844 r.set(0, 0, width, height);
9845 if (globalOffset != null) {
9846 globalOffset.set(-mScrollX, -mScrollY);
9847 }
9848 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9849 }
9850 return false;
9851 }
9852
9853 public final boolean getGlobalVisibleRect(Rect r) {
9854 return getGlobalVisibleRect(r, null);
9855 }
9856
9857 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009858 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 if (getGlobalVisibleRect(r, offset)) {
9860 r.offset(-offset.x, -offset.y); // make r local
9861 return true;
9862 }
9863 return false;
9864 }
9865
9866 /**
9867 * Offset this view's vertical location by the specified number of pixels.
9868 *
9869 * @param offset the number of pixels to offset the view by
9870 */
9871 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009872 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009873 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009874 final boolean matrixIsIdentity = mTransformationInfo == null
9875 || mTransformationInfo.mMatrixIsIdentity;
9876 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009877 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009878 invalidateViewProperty(false, false);
9879 } else {
9880 final ViewParent p = mParent;
9881 if (p != null && mAttachInfo != null) {
9882 final Rect r = mAttachInfo.mTmpInvalRect;
9883 int minTop;
9884 int maxBottom;
9885 int yLoc;
9886 if (offset < 0) {
9887 minTop = mTop + offset;
9888 maxBottom = mBottom;
9889 yLoc = offset;
9890 } else {
9891 minTop = mTop;
9892 maxBottom = mBottom + offset;
9893 yLoc = 0;
9894 }
9895 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9896 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009897 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009898 }
9899 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009900 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009901 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009902
Chet Haasec3aa3612010-06-17 08:50:37 -07009903 mTop += offset;
9904 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009905 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009906 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009907 invalidateViewProperty(false, false);
9908 } else {
9909 if (!matrixIsIdentity) {
9910 invalidateViewProperty(false, true);
9911 }
9912 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009913 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009915 }
9916
9917 /**
9918 * Offset this view's horizontal location by the specified amount of pixels.
9919 *
9920 * @param offset the numer of pixels to offset the view by
9921 */
9922 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009923 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009924 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009925 final boolean matrixIsIdentity = mTransformationInfo == null
9926 || mTransformationInfo.mMatrixIsIdentity;
9927 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009928 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009929 invalidateViewProperty(false, false);
9930 } else {
9931 final ViewParent p = mParent;
9932 if (p != null && mAttachInfo != null) {
9933 final Rect r = mAttachInfo.mTmpInvalRect;
9934 int minLeft;
9935 int maxRight;
9936 if (offset < 0) {
9937 minLeft = mLeft + offset;
9938 maxRight = mRight;
9939 } else {
9940 minLeft = mLeft;
9941 maxRight = mRight + offset;
9942 }
9943 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9944 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009945 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009946 }
9947 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009948 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009949 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009950
Chet Haasec3aa3612010-06-17 08:50:37 -07009951 mLeft += offset;
9952 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009953 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009954 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009955 invalidateViewProperty(false, false);
9956 } else {
9957 if (!matrixIsIdentity) {
9958 invalidateViewProperty(false, true);
9959 }
9960 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009961 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 }
9964
9965 /**
9966 * Get the LayoutParams associated with this view. All views should have
9967 * layout parameters. These supply parameters to the <i>parent</i> of this
9968 * view specifying how it should be arranged. There are many subclasses of
9969 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9970 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009971 *
9972 * This method may return null if this View is not attached to a parent
9973 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9974 * was not invoked successfully. When a View is attached to a parent
9975 * ViewGroup, this method must not return null.
9976 *
9977 * @return The LayoutParams associated with this view, or null if no
9978 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009979 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009980 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009981 public ViewGroup.LayoutParams getLayoutParams() {
9982 return mLayoutParams;
9983 }
9984
9985 /**
9986 * Set the layout parameters associated with this view. These supply
9987 * parameters to the <i>parent</i> of this view specifying how it should be
9988 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9989 * correspond to the different subclasses of ViewGroup that are responsible
9990 * for arranging their children.
9991 *
Romain Guy01c174b2011-02-22 11:51:06 -08009992 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 */
9994 public void setLayoutParams(ViewGroup.LayoutParams params) {
9995 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009996 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009997 }
9998 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009999 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010000 if (mParent instanceof ViewGroup) {
10001 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 requestLayout();
10004 }
10005
10006 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010007 * Resolve the layout parameters depending on the resolved layout direction
10008 */
10009 private void resolveLayoutParams() {
10010 if (mLayoutParams != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070010011 mLayoutParams.onResolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010012 }
10013 }
10014
10015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 * Set the scrolled position of your view. This will cause a call to
10017 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10018 * invalidated.
10019 * @param x the x position to scroll to
10020 * @param y the y position to scroll to
10021 */
10022 public void scrollTo(int x, int y) {
10023 if (mScrollX != x || mScrollY != y) {
10024 int oldX = mScrollX;
10025 int oldY = mScrollY;
10026 mScrollX = x;
10027 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010028 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010030 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010031 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 }
10034 }
10035
10036 /**
10037 * Move the scrolled position of your view. This will cause a call to
10038 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10039 * invalidated.
10040 * @param x the amount of pixels to scroll by horizontally
10041 * @param y the amount of pixels to scroll by vertically
10042 */
10043 public void scrollBy(int x, int y) {
10044 scrollTo(mScrollX + x, mScrollY + y);
10045 }
10046
10047 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010048 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10049 * animation to fade the scrollbars out after a default delay. If a subclass
10050 * provides animated scrolling, the start delay should equal the duration
10051 * of the scrolling animation.</p>
10052 *
10053 * <p>The animation starts only if at least one of the scrollbars is
10054 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10055 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10056 * this method returns true, and false otherwise. If the animation is
10057 * started, this method calls {@link #invalidate()}; in that case the
10058 * caller should not call {@link #invalidate()}.</p>
10059 *
10060 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010061 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010062 *
10063 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10064 * and {@link #scrollTo(int, int)}.</p>
10065 *
10066 * @return true if the animation is played, false otherwise
10067 *
10068 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010069 * @see #scrollBy(int, int)
10070 * @see #scrollTo(int, int)
10071 * @see #isHorizontalScrollBarEnabled()
10072 * @see #isVerticalScrollBarEnabled()
10073 * @see #setHorizontalScrollBarEnabled(boolean)
10074 * @see #setVerticalScrollBarEnabled(boolean)
10075 */
10076 protected boolean awakenScrollBars() {
10077 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010078 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010079 }
10080
10081 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010082 * Trigger the scrollbars to draw.
10083 * This method differs from awakenScrollBars() only in its default duration.
10084 * initialAwakenScrollBars() will show the scroll bars for longer than
10085 * usual to give the user more of a chance to notice them.
10086 *
10087 * @return true if the animation is played, false otherwise.
10088 */
10089 private boolean initialAwakenScrollBars() {
10090 return mScrollCache != null &&
10091 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10092 }
10093
10094 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010095 * <p>
10096 * Trigger the scrollbars to draw. When invoked this method starts an
10097 * animation to fade the scrollbars out after a fixed delay. If a subclass
10098 * provides animated scrolling, the start delay should equal the duration of
10099 * the scrolling animation.
10100 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010101 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010102 * <p>
10103 * The animation starts only if at least one of the scrollbars is enabled,
10104 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10105 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10106 * this method returns true, and false otherwise. If the animation is
10107 * started, this method calls {@link #invalidate()}; in that case the caller
10108 * should not call {@link #invalidate()}.
10109 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010110 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010111 * <p>
10112 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010113 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010114 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010115 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010116 * @param startDelay the delay, in milliseconds, after which the animation
10117 * should start; when the delay is 0, the animation starts
10118 * immediately
10119 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010120 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010121 * @see #scrollBy(int, int)
10122 * @see #scrollTo(int, int)
10123 * @see #isHorizontalScrollBarEnabled()
10124 * @see #isVerticalScrollBarEnabled()
10125 * @see #setHorizontalScrollBarEnabled(boolean)
10126 * @see #setVerticalScrollBarEnabled(boolean)
10127 */
10128 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010129 return awakenScrollBars(startDelay, true);
10130 }
Joe Malin32736f02011-01-19 16:14:20 -080010131
Mike Cleron290947b2009-09-29 18:34:32 -070010132 /**
10133 * <p>
10134 * Trigger the scrollbars to draw. When invoked this method starts an
10135 * animation to fade the scrollbars out after a fixed delay. If a subclass
10136 * provides animated scrolling, the start delay should equal the duration of
10137 * the scrolling animation.
10138 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010139 *
Mike Cleron290947b2009-09-29 18:34:32 -070010140 * <p>
10141 * The animation starts only if at least one of the scrollbars is enabled,
10142 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10143 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10144 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010145 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010146 * is set to true; in that case the caller
10147 * should not call {@link #invalidate()}.
10148 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010149 *
Mike Cleron290947b2009-09-29 18:34:32 -070010150 * <p>
10151 * This method should be invoked everytime a subclass directly updates the
10152 * scroll parameters.
10153 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010154 *
Mike Cleron290947b2009-09-29 18:34:32 -070010155 * @param startDelay the delay, in milliseconds, after which the animation
10156 * should start; when the delay is 0, the animation starts
10157 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010158 *
Mike Cleron290947b2009-09-29 18:34:32 -070010159 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010160 *
Mike Cleron290947b2009-09-29 18:34:32 -070010161 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010162 *
Mike Cleron290947b2009-09-29 18:34:32 -070010163 * @see #scrollBy(int, int)
10164 * @see #scrollTo(int, int)
10165 * @see #isHorizontalScrollBarEnabled()
10166 * @see #isVerticalScrollBarEnabled()
10167 * @see #setHorizontalScrollBarEnabled(boolean)
10168 * @see #setVerticalScrollBarEnabled(boolean)
10169 */
10170 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010171 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010172
Mike Cleronf116bf82009-09-27 19:14:12 -070010173 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10174 return false;
10175 }
10176
10177 if (scrollCache.scrollBar == null) {
10178 scrollCache.scrollBar = new ScrollBarDrawable();
10179 }
10180
10181 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10182
Mike Cleron290947b2009-09-29 18:34:32 -070010183 if (invalidate) {
10184 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010185 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010186 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010187
10188 if (scrollCache.state == ScrollabilityCache.OFF) {
10189 // FIXME: this is copied from WindowManagerService.
10190 // We should get this value from the system when it
10191 // is possible to do so.
10192 final int KEY_REPEAT_FIRST_DELAY = 750;
10193 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10194 }
10195
10196 // Tell mScrollCache when we should start fading. This may
10197 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010198 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010199 scrollCache.fadeStartTime = fadeStartTime;
10200 scrollCache.state = ScrollabilityCache.ON;
10201
10202 // Schedule our fader to run, unscheduling any old ones first
10203 if (mAttachInfo != null) {
10204 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10205 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10206 }
10207
10208 return true;
10209 }
10210
10211 return false;
10212 }
10213
10214 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010215 * Do not invalidate views which are not visible and which are not running an animation. They
10216 * will not get drawn and they should not set dirty flags as if they will be drawn
10217 */
10218 private boolean skipInvalidate() {
10219 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10220 (!(mParent instanceof ViewGroup) ||
10221 !((ViewGroup) mParent).isViewTransitioning(this));
10222 }
10223 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010224 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010225 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10226 * in the future. This must be called from a UI thread. To call from a non-UI
10227 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010228 *
10229 * WARNING: This method is destructive to dirty.
10230 * @param dirty the rectangle representing the bounds of the dirty region
10231 */
10232 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010233 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010234 return;
10235 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010236 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10237 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10238 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10239 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10240 mPrivateFlags |= PFLAG_INVALIDATED;
10241 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242 final ViewParent p = mParent;
10243 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010244 //noinspection PointlessBooleanExpression,ConstantConditions
10245 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10246 if (p != null && ai != null && ai.mHardwareAccelerated) {
10247 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010248 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010249 p.invalidateChild(this, null);
10250 return;
10251 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 if (p != null && ai != null) {
10254 final int scrollX = mScrollX;
10255 final int scrollY = mScrollY;
10256 final Rect r = ai.mTmpInvalRect;
10257 r.set(dirty.left - scrollX, dirty.top - scrollY,
10258 dirty.right - scrollX, dirty.bottom - scrollY);
10259 mParent.invalidateChild(this, r);
10260 }
10261 }
10262 }
10263
10264 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010265 * 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 -080010266 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010267 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10268 * will be called at some point in the future. This must be called from
10269 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010270 * @param l the left position of the dirty region
10271 * @param t the top position of the dirty region
10272 * @param r the right position of the dirty region
10273 * @param b the bottom position of the dirty region
10274 */
10275 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010276 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010277 return;
10278 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010279 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10280 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10281 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10282 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10283 mPrivateFlags |= PFLAG_INVALIDATED;
10284 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 final ViewParent p = mParent;
10286 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010287 //noinspection PointlessBooleanExpression,ConstantConditions
10288 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10289 if (p != null && ai != null && ai.mHardwareAccelerated) {
10290 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010291 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010292 p.invalidateChild(this, null);
10293 return;
10294 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010296 if (p != null && ai != null && l < r && t < b) {
10297 final int scrollX = mScrollX;
10298 final int scrollY = mScrollY;
10299 final Rect tmpr = ai.mTmpInvalRect;
10300 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10301 p.invalidateChild(this, tmpr);
10302 }
10303 }
10304 }
10305
10306 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010307 * Invalidate the whole view. If the view is visible,
10308 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10309 * the future. This must be called from a UI thread. To call from a non-UI thread,
10310 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010311 */
10312 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010313 invalidate(true);
10314 }
Joe Malin32736f02011-01-19 16:14:20 -080010315
Chet Haaseed032702010-10-01 14:05:54 -070010316 /**
10317 * This is where the invalidate() work actually happens. A full invalidate()
10318 * causes the drawing cache to be invalidated, but this function can be called with
10319 * invalidateCache set to false to skip that invalidation step for cases that do not
10320 * need it (for example, a component that remains at the same dimensions with the same
10321 * content).
10322 *
10323 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10324 * well. This is usually true for a full invalidate, but may be set to false if the
10325 * View's contents or dimensions have not changed.
10326 */
Romain Guy849d0a32011-02-01 17:20:48 -080010327 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010328 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010329 return;
10330 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010331 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10332 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10333 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010334 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010335 mPrivateFlags &= ~PFLAG_DRAWN;
10336 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010337 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010338 mPrivateFlags |= PFLAG_INVALIDATED;
10339 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010342 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010343 //noinspection PointlessBooleanExpression,ConstantConditions
10344 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10345 if (p != null && ai != null && ai.mHardwareAccelerated) {
10346 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010347 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010348 p.invalidateChild(this, null);
10349 return;
10350 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010351 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010353 if (p != null && ai != null) {
10354 final Rect r = ai.mTmpInvalRect;
10355 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10356 // Don't call invalidate -- we don't want to internally scroll
10357 // our own bounds
10358 p.invalidateChild(this, r);
10359 }
10360 }
10361 }
10362
10363 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010364 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10365 * set any flags or handle all of the cases handled by the default invalidation methods.
10366 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10367 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10368 * walk up the hierarchy, transforming the dirty rect as necessary.
10369 *
10370 * The method also handles normal invalidation logic if display list properties are not
10371 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10372 * backup approach, to handle these cases used in the various property-setting methods.
10373 *
10374 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10375 * are not being used in this view
10376 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10377 * list properties are not being used in this view
10378 */
10379 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010380 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010381 if (invalidateParent) {
10382 invalidateParentCaches();
10383 }
10384 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010385 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010386 }
10387 invalidate(false);
10388 } else {
10389 final AttachInfo ai = mAttachInfo;
10390 final ViewParent p = mParent;
10391 if (p != null && ai != null) {
10392 final Rect r = ai.mTmpInvalRect;
10393 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10394 if (mParent instanceof ViewGroup) {
10395 ((ViewGroup) mParent).invalidateChildFast(this, r);
10396 } else {
10397 mParent.invalidateChild(this, r);
10398 }
10399 }
10400 }
10401 }
10402
10403 /**
10404 * Utility method to transform a given Rect by the current matrix of this view.
10405 */
10406 void transformRect(final Rect rect) {
10407 if (!getMatrix().isIdentity()) {
10408 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10409 boundingRect.set(rect);
10410 getMatrix().mapRect(boundingRect);
10411 rect.set((int) (boundingRect.left - 0.5f),
10412 (int) (boundingRect.top - 0.5f),
10413 (int) (boundingRect.right + 0.5f),
10414 (int) (boundingRect.bottom + 0.5f));
10415 }
10416 }
10417
10418 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010419 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010420 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10421 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010422 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10423 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010424 *
10425 * @hide
10426 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010427 protected void invalidateParentCaches() {
10428 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010429 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010430 }
10431 }
Joe Malin32736f02011-01-19 16:14:20 -080010432
Romain Guy0fd89bf2011-01-26 15:41:30 -080010433 /**
10434 * Used to indicate that the parent of this view should be invalidated. This functionality
10435 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10436 * which is necessary when various parent-managed properties of the view change, such as
10437 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10438 * an invalidation event to the parent.
10439 *
10440 * @hide
10441 */
10442 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010443 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010444 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010445 }
10446 }
10447
10448 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010449 * Indicates whether this View is opaque. An opaque View guarantees that it will
10450 * draw all the pixels overlapping its bounds using a fully opaque color.
10451 *
10452 * Subclasses of View should override this method whenever possible to indicate
10453 * whether an instance is opaque. Opaque Views are treated in a special way by
10454 * the View hierarchy, possibly allowing it to perform optimizations during
10455 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010456 *
Romain Guy24443ea2009-05-11 11:56:30 -070010457 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010459 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010460 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010461 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010462 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010463 }
10464
Adam Powell20232d02010-12-08 21:08:53 -080010465 /**
10466 * @hide
10467 */
10468 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010469 // Opaque if:
10470 // - Has a background
10471 // - Background is opaque
10472 // - Doesn't have scrollbars or scrollbars are inside overlay
10473
Philip Milne6c8ea062012-04-03 17:38:43 -070010474 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010475 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010476 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010477 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010478 }
10479
10480 final int flags = mViewFlags;
10481 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10482 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010483 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010484 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010485 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010486 }
10487 }
10488
10489 /**
10490 * @hide
10491 */
10492 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010493 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010494 }
10495
10496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497 * @return A handler associated with the thread running the View. This
10498 * handler can be used to pump events in the UI events queue.
10499 */
10500 public Handler getHandler() {
10501 if (mAttachInfo != null) {
10502 return mAttachInfo.mHandler;
10503 }
10504 return null;
10505 }
10506
10507 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010508 * Gets the view root associated with the View.
10509 * @return The view root, or null if none.
10510 * @hide
10511 */
10512 public ViewRootImpl getViewRootImpl() {
10513 if (mAttachInfo != null) {
10514 return mAttachInfo.mViewRootImpl;
10515 }
10516 return null;
10517 }
10518
10519 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010520 * <p>Causes the Runnable to be added to the message queue.
10521 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010522 *
Romain Guye63a4f32011-08-11 11:33:31 -070010523 * <p>This method can be invoked from outside of the UI thread
10524 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 *
10526 * @param action The Runnable that will be executed.
10527 *
10528 * @return Returns true if the Runnable was successfully placed in to the
10529 * message queue. Returns false on failure, usually because the
10530 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010531 *
10532 * @see #postDelayed
10533 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010534 */
10535 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010536 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010537 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010538 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010540 // Assume that post will succeed later
10541 ViewRootImpl.getRunQueue().post(action);
10542 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 }
10544
10545 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010546 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010548 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010549 *
Romain Guye63a4f32011-08-11 11:33:31 -070010550 * <p>This method can be invoked from outside of the UI thread
10551 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552 *
10553 * @param action The Runnable that will be executed.
10554 * @param delayMillis The delay (in milliseconds) until the Runnable
10555 * will be executed.
10556 *
10557 * @return true if the Runnable was successfully placed in to the
10558 * message queue. Returns false on failure, usually because the
10559 * looper processing the message queue is exiting. Note that a
10560 * result of true does not mean the Runnable will be processed --
10561 * if the looper is quit before the delivery time of the message
10562 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010563 *
10564 * @see #post
10565 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 */
10567 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010568 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010569 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010570 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010571 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010572 // Assume that post will succeed later
10573 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10574 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
10576
10577 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010578 * <p>Causes the Runnable to execute on the next animation time step.
10579 * The runnable will be run on the user interface thread.</p>
10580 *
10581 * <p>This method can be invoked from outside of the UI thread
10582 * only when this View is attached to a window.</p>
10583 *
10584 * @param action The Runnable that will be executed.
10585 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010586 * @see #postOnAnimationDelayed
10587 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010588 */
10589 public void postOnAnimation(Runnable action) {
10590 final AttachInfo attachInfo = mAttachInfo;
10591 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010592 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10593 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010594 } else {
10595 // Assume that post will succeed later
10596 ViewRootImpl.getRunQueue().post(action);
10597 }
10598 }
10599
10600 /**
10601 * <p>Causes the Runnable to execute on the next animation time step,
10602 * after the specified amount of time elapses.
10603 * The runnable will be run on the user interface thread.</p>
10604 *
10605 * <p>This method can be invoked from outside of the UI thread
10606 * only when this View is attached to a window.</p>
10607 *
10608 * @param action The Runnable that will be executed.
10609 * @param delayMillis The delay (in milliseconds) until the Runnable
10610 * will be executed.
10611 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010612 * @see #postOnAnimation
10613 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010614 */
10615 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10616 final AttachInfo attachInfo = mAttachInfo;
10617 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010618 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10619 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010620 } else {
10621 // Assume that post will succeed later
10622 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10623 }
10624 }
10625
10626 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010627 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010628 *
Romain Guye63a4f32011-08-11 11:33:31 -070010629 * <p>This method can be invoked from outside of the UI thread
10630 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010631 *
10632 * @param action The Runnable to remove from the message handling queue
10633 *
10634 * @return true if this view could ask the Handler to remove the Runnable,
10635 * false otherwise. When the returned value is true, the Runnable
10636 * may or may not have been actually removed from the message queue
10637 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010638 *
10639 * @see #post
10640 * @see #postDelayed
10641 * @see #postOnAnimation
10642 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 */
10644 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010645 if (action != null) {
10646 final AttachInfo attachInfo = mAttachInfo;
10647 if (attachInfo != null) {
10648 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010649 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10650 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010651 } else {
10652 // Assume that post will succeed later
10653 ViewRootImpl.getRunQueue().removeCallbacks(action);
10654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010656 return true;
10657 }
10658
10659 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010660 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10661 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 *
Romain Guye63a4f32011-08-11 11:33:31 -070010663 * <p>This method can be invoked from outside of the UI thread
10664 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010667 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 */
10669 public void postInvalidate() {
10670 postInvalidateDelayed(0);
10671 }
10672
10673 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010674 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10675 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010676 *
Romain Guye63a4f32011-08-11 11:33:31 -070010677 * <p>This method can be invoked from outside of the UI thread
10678 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 *
10680 * @param left The left coordinate of the rectangle to invalidate.
10681 * @param top The top coordinate of the rectangle to invalidate.
10682 * @param right The right coordinate of the rectangle to invalidate.
10683 * @param bottom The bottom coordinate of the rectangle to invalidate.
10684 *
10685 * @see #invalidate(int, int, int, int)
10686 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010687 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 */
10689 public void postInvalidate(int left, int top, int right, int bottom) {
10690 postInvalidateDelayed(0, left, top, right, bottom);
10691 }
10692
10693 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010694 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10695 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010696 *
Romain Guye63a4f32011-08-11 11:33:31 -070010697 * <p>This method can be invoked from outside of the UI thread
10698 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 *
10700 * @param delayMilliseconds the duration in milliseconds to delay the
10701 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010702 *
10703 * @see #invalidate()
10704 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 */
10706 public void postInvalidateDelayed(long delayMilliseconds) {
10707 // We try only with the AttachInfo because there's no point in invalidating
10708 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010709 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010710 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010711 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 }
10713 }
10714
10715 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010716 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10717 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010718 *
Romain Guye63a4f32011-08-11 11:33:31 -070010719 * <p>This method can be invoked from outside of the UI thread
10720 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 *
10722 * @param delayMilliseconds the duration in milliseconds to delay the
10723 * invalidation by
10724 * @param left The left coordinate of the rectangle to invalidate.
10725 * @param top The top coordinate of the rectangle to invalidate.
10726 * @param right The right coordinate of the rectangle to invalidate.
10727 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010728 *
10729 * @see #invalidate(int, int, int, int)
10730 * @see #invalidate(Rect)
10731 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 */
10733 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10734 int right, int bottom) {
10735
10736 // We try only with the AttachInfo because there's no point in invalidating
10737 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010738 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010739 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10741 info.target = this;
10742 info.left = left;
10743 info.top = top;
10744 info.right = right;
10745 info.bottom = bottom;
10746
Jeff Browna175a5b2012-02-15 19:18:31 -080010747 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 }
10749 }
10750
10751 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010752 * <p>Cause an invalidate to happen on the next animation time step, typically the
10753 * next display frame.</p>
10754 *
10755 * <p>This method can be invoked from outside of the UI thread
10756 * only when this View is attached to a window.</p>
10757 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010758 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010759 */
10760 public void postInvalidateOnAnimation() {
10761 // We try only with the AttachInfo because there's no point in invalidating
10762 // if we are not attached to our window
10763 final AttachInfo attachInfo = mAttachInfo;
10764 if (attachInfo != null) {
10765 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10766 }
10767 }
10768
10769 /**
10770 * <p>Cause an invalidate of the specified area to happen on the next animation
10771 * time step, typically the next display frame.</p>
10772 *
10773 * <p>This method can be invoked from outside of the UI thread
10774 * only when this View is attached to a window.</p>
10775 *
10776 * @param left The left coordinate of the rectangle to invalidate.
10777 * @param top The top coordinate of the rectangle to invalidate.
10778 * @param right The right coordinate of the rectangle to invalidate.
10779 * @param bottom The bottom coordinate of the rectangle to invalidate.
10780 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010781 * @see #invalidate(int, int, int, int)
10782 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010783 */
10784 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10785 // We try only with the AttachInfo because there's no point in invalidating
10786 // if we are not attached to our window
10787 final AttachInfo attachInfo = mAttachInfo;
10788 if (attachInfo != null) {
10789 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10790 info.target = this;
10791 info.left = left;
10792 info.top = top;
10793 info.right = right;
10794 info.bottom = bottom;
10795
10796 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10797 }
10798 }
10799
10800 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010801 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10802 * This event is sent at most once every
10803 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10804 */
10805 private void postSendViewScrolledAccessibilityEventCallback() {
10806 if (mSendViewScrolledAccessibilityEvent == null) {
10807 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10808 }
10809 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10810 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10811 postDelayed(mSendViewScrolledAccessibilityEvent,
10812 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10813 }
10814 }
10815
10816 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 * Called by a parent to request that a child update its values for mScrollX
10818 * and mScrollY if necessary. This will typically be done if the child is
10819 * animating a scroll using a {@link android.widget.Scroller Scroller}
10820 * object.
10821 */
10822 public void computeScroll() {
10823 }
10824
10825 /**
10826 * <p>Indicate whether the horizontal edges are faded when the view is
10827 * scrolled horizontally.</p>
10828 *
10829 * @return true if the horizontal edges should are faded on scroll, false
10830 * otherwise
10831 *
10832 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010833 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010834 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 */
10836 public boolean isHorizontalFadingEdgeEnabled() {
10837 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10838 }
10839
10840 /**
10841 * <p>Define whether the horizontal edges should be faded when this view
10842 * is scrolled horizontally.</p>
10843 *
10844 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10845 * be faded when the view is scrolled
10846 * horizontally
10847 *
10848 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010849 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010850 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 */
10852 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10853 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10854 if (horizontalFadingEdgeEnabled) {
10855 initScrollCache();
10856 }
10857
10858 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10859 }
10860 }
10861
10862 /**
10863 * <p>Indicate whether the vertical edges are faded when the view is
10864 * scrolled horizontally.</p>
10865 *
10866 * @return true if the vertical edges should are faded on scroll, false
10867 * otherwise
10868 *
10869 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010870 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010871 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010872 */
10873 public boolean isVerticalFadingEdgeEnabled() {
10874 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10875 }
10876
10877 /**
10878 * <p>Define whether the vertical edges should be faded when this view
10879 * is scrolled vertically.</p>
10880 *
10881 * @param verticalFadingEdgeEnabled true if the vertical edges should
10882 * be faded when the view is scrolled
10883 * vertically
10884 *
10885 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010886 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010887 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010888 */
10889 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10890 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10891 if (verticalFadingEdgeEnabled) {
10892 initScrollCache();
10893 }
10894
10895 mViewFlags ^= FADING_EDGE_VERTICAL;
10896 }
10897 }
10898
10899 /**
10900 * Returns the strength, or intensity, of the top faded edge. The strength is
10901 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10902 * returns 0.0 or 1.0 but no value in between.
10903 *
10904 * Subclasses should override this method to provide a smoother fade transition
10905 * when scrolling occurs.
10906 *
10907 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10908 */
10909 protected float getTopFadingEdgeStrength() {
10910 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10911 }
10912
10913 /**
10914 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10915 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10916 * returns 0.0 or 1.0 but no value in between.
10917 *
10918 * Subclasses should override this method to provide a smoother fade transition
10919 * when scrolling occurs.
10920 *
10921 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10922 */
10923 protected float getBottomFadingEdgeStrength() {
10924 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10925 computeVerticalScrollRange() ? 1.0f : 0.0f;
10926 }
10927
10928 /**
10929 * Returns the strength, or intensity, of the left faded edge. The strength is
10930 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10931 * returns 0.0 or 1.0 but no value in between.
10932 *
10933 * Subclasses should override this method to provide a smoother fade transition
10934 * when scrolling occurs.
10935 *
10936 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10937 */
10938 protected float getLeftFadingEdgeStrength() {
10939 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10940 }
10941
10942 /**
10943 * Returns the strength, or intensity, of the right faded edge. The strength is
10944 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10945 * returns 0.0 or 1.0 but no value in between.
10946 *
10947 * Subclasses should override this method to provide a smoother fade transition
10948 * when scrolling occurs.
10949 *
10950 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10951 */
10952 protected float getRightFadingEdgeStrength() {
10953 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10954 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10955 }
10956
10957 /**
10958 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10959 * scrollbar is not drawn by default.</p>
10960 *
10961 * @return true if the horizontal scrollbar should be painted, false
10962 * otherwise
10963 *
10964 * @see #setHorizontalScrollBarEnabled(boolean)
10965 */
10966 public boolean isHorizontalScrollBarEnabled() {
10967 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10968 }
10969
10970 /**
10971 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10972 * scrollbar is not drawn by default.</p>
10973 *
10974 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10975 * be painted
10976 *
10977 * @see #isHorizontalScrollBarEnabled()
10978 */
10979 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10980 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10981 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010982 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010983 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010984 }
10985 }
10986
10987 /**
10988 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10989 * scrollbar is not drawn by default.</p>
10990 *
10991 * @return true if the vertical scrollbar should be painted, false
10992 * otherwise
10993 *
10994 * @see #setVerticalScrollBarEnabled(boolean)
10995 */
10996 public boolean isVerticalScrollBarEnabled() {
10997 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10998 }
10999
11000 /**
11001 * <p>Define whether the vertical scrollbar should be drawn or not. The
11002 * scrollbar is not drawn by default.</p>
11003 *
11004 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11005 * be painted
11006 *
11007 * @see #isVerticalScrollBarEnabled()
11008 */
11009 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11010 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11011 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011012 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011013 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011014 }
11015 }
11016
Adam Powell20232d02010-12-08 21:08:53 -080011017 /**
11018 * @hide
11019 */
11020 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011021 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011022 }
Joe Malin32736f02011-01-19 16:14:20 -080011023
Mike Cleronfe81d382009-09-28 14:22:16 -070011024 /**
11025 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011026 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011027 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011028 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011029 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011030 */
11031 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11032 initScrollCache();
11033 final ScrollabilityCache scrollabilityCache = mScrollCache;
11034 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011035 if (fadeScrollbars) {
11036 scrollabilityCache.state = ScrollabilityCache.OFF;
11037 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011038 scrollabilityCache.state = ScrollabilityCache.ON;
11039 }
11040 }
Joe Malin32736f02011-01-19 16:14:20 -080011041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011042 /**
Joe Malin32736f02011-01-19 16:14:20 -080011043 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011044 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011045 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011046 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011047 *
11048 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011049 */
11050 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011051 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011052 }
Joe Malin32736f02011-01-19 16:14:20 -080011053
Mike Cleron52f0a642009-09-28 18:21:37 -070011054 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011055 *
11056 * Returns the delay before scrollbars fade.
11057 *
11058 * @return the delay before scrollbars fade
11059 *
11060 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11061 */
11062 public int getScrollBarDefaultDelayBeforeFade() {
11063 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11064 mScrollCache.scrollBarDefaultDelayBeforeFade;
11065 }
11066
11067 /**
11068 * Define the delay before scrollbars fade.
11069 *
11070 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11071 *
11072 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11073 */
11074 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11075 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11076 }
11077
11078 /**
11079 *
11080 * Returns the scrollbar fade duration.
11081 *
11082 * @return the scrollbar fade duration
11083 *
11084 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11085 */
11086 public int getScrollBarFadeDuration() {
11087 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11088 mScrollCache.scrollBarFadeDuration;
11089 }
11090
11091 /**
11092 * Define the scrollbar fade duration.
11093 *
11094 * @param scrollBarFadeDuration - the scrollbar fade duration
11095 *
11096 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11097 */
11098 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11099 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11100 }
11101
11102 /**
11103 *
11104 * Returns the scrollbar size.
11105 *
11106 * @return the scrollbar size
11107 *
11108 * @attr ref android.R.styleable#View_scrollbarSize
11109 */
11110 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011111 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011112 mScrollCache.scrollBarSize;
11113 }
11114
11115 /**
11116 * Define the scrollbar size.
11117 *
11118 * @param scrollBarSize - the scrollbar size
11119 *
11120 * @attr ref android.R.styleable#View_scrollbarSize
11121 */
11122 public void setScrollBarSize(int scrollBarSize) {
11123 getScrollCache().scrollBarSize = scrollBarSize;
11124 }
11125
11126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11128 * inset. When inset, they add to the padding of the view. And the scrollbars
11129 * can be drawn inside the padding area or on the edge of the view. For example,
11130 * if a view has a background drawable and you want to draw the scrollbars
11131 * inside the padding specified by the drawable, you can use
11132 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11133 * appear at the edge of the view, ignoring the padding, then you can use
11134 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11135 * @param style the style of the scrollbars. Should be one of
11136 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11137 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11138 * @see #SCROLLBARS_INSIDE_OVERLAY
11139 * @see #SCROLLBARS_INSIDE_INSET
11140 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11141 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011142 *
11143 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 */
11145 public void setScrollBarStyle(int style) {
11146 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11147 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011148 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011149 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 }
11151 }
11152
11153 /**
11154 * <p>Returns the current scrollbar style.</p>
11155 * @return the current scrollbar style
11156 * @see #SCROLLBARS_INSIDE_OVERLAY
11157 * @see #SCROLLBARS_INSIDE_INSET
11158 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11159 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011160 *
11161 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011163 @ViewDebug.ExportedProperty(mapping = {
11164 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11165 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11166 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11167 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11168 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011169 public int getScrollBarStyle() {
11170 return mViewFlags & SCROLLBARS_STYLE_MASK;
11171 }
11172
11173 /**
11174 * <p>Compute the horizontal range that the horizontal scrollbar
11175 * represents.</p>
11176 *
11177 * <p>The range is expressed in arbitrary units that must be the same as the
11178 * units used by {@link #computeHorizontalScrollExtent()} and
11179 * {@link #computeHorizontalScrollOffset()}.</p>
11180 *
11181 * <p>The default range is the drawing width of this view.</p>
11182 *
11183 * @return the total horizontal range represented by the horizontal
11184 * scrollbar
11185 *
11186 * @see #computeHorizontalScrollExtent()
11187 * @see #computeHorizontalScrollOffset()
11188 * @see android.widget.ScrollBarDrawable
11189 */
11190 protected int computeHorizontalScrollRange() {
11191 return getWidth();
11192 }
11193
11194 /**
11195 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11196 * within the horizontal range. This value is used to compute the position
11197 * of the thumb within the scrollbar's track.</p>
11198 *
11199 * <p>The range is expressed in arbitrary units that must be the same as the
11200 * units used by {@link #computeHorizontalScrollRange()} and
11201 * {@link #computeHorizontalScrollExtent()}.</p>
11202 *
11203 * <p>The default offset is the scroll offset of this view.</p>
11204 *
11205 * @return the horizontal offset of the scrollbar's thumb
11206 *
11207 * @see #computeHorizontalScrollRange()
11208 * @see #computeHorizontalScrollExtent()
11209 * @see android.widget.ScrollBarDrawable
11210 */
11211 protected int computeHorizontalScrollOffset() {
11212 return mScrollX;
11213 }
11214
11215 /**
11216 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11217 * within the horizontal range. This value is used to compute the length
11218 * of the thumb within the scrollbar's track.</p>
11219 *
11220 * <p>The range is expressed in arbitrary units that must be the same as the
11221 * units used by {@link #computeHorizontalScrollRange()} and
11222 * {@link #computeHorizontalScrollOffset()}.</p>
11223 *
11224 * <p>The default extent is the drawing width of this view.</p>
11225 *
11226 * @return the horizontal extent of the scrollbar's thumb
11227 *
11228 * @see #computeHorizontalScrollRange()
11229 * @see #computeHorizontalScrollOffset()
11230 * @see android.widget.ScrollBarDrawable
11231 */
11232 protected int computeHorizontalScrollExtent() {
11233 return getWidth();
11234 }
11235
11236 /**
11237 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11238 *
11239 * <p>The range is expressed in arbitrary units that must be the same as the
11240 * units used by {@link #computeVerticalScrollExtent()} and
11241 * {@link #computeVerticalScrollOffset()}.</p>
11242 *
11243 * @return the total vertical range represented by the vertical scrollbar
11244 *
11245 * <p>The default range is the drawing height of this view.</p>
11246 *
11247 * @see #computeVerticalScrollExtent()
11248 * @see #computeVerticalScrollOffset()
11249 * @see android.widget.ScrollBarDrawable
11250 */
11251 protected int computeVerticalScrollRange() {
11252 return getHeight();
11253 }
11254
11255 /**
11256 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11257 * within the horizontal range. This value is used to compute the position
11258 * of the thumb within the scrollbar's track.</p>
11259 *
11260 * <p>The range is expressed in arbitrary units that must be the same as the
11261 * units used by {@link #computeVerticalScrollRange()} and
11262 * {@link #computeVerticalScrollExtent()}.</p>
11263 *
11264 * <p>The default offset is the scroll offset of this view.</p>
11265 *
11266 * @return the vertical offset of the scrollbar's thumb
11267 *
11268 * @see #computeVerticalScrollRange()
11269 * @see #computeVerticalScrollExtent()
11270 * @see android.widget.ScrollBarDrawable
11271 */
11272 protected int computeVerticalScrollOffset() {
11273 return mScrollY;
11274 }
11275
11276 /**
11277 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11278 * within the vertical range. This value is used to compute the length
11279 * of the thumb within the scrollbar's track.</p>
11280 *
11281 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011282 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011283 * {@link #computeVerticalScrollOffset()}.</p>
11284 *
11285 * <p>The default extent is the drawing height of this view.</p>
11286 *
11287 * @return the vertical extent of the scrollbar's thumb
11288 *
11289 * @see #computeVerticalScrollRange()
11290 * @see #computeVerticalScrollOffset()
11291 * @see android.widget.ScrollBarDrawable
11292 */
11293 protected int computeVerticalScrollExtent() {
11294 return getHeight();
11295 }
11296
11297 /**
Adam Powell69159442011-06-13 17:53:06 -070011298 * Check if this view can be scrolled horizontally in a certain direction.
11299 *
11300 * @param direction Negative to check scrolling left, positive to check scrolling right.
11301 * @return true if this view can be scrolled in the specified direction, false otherwise.
11302 */
11303 public boolean canScrollHorizontally(int direction) {
11304 final int offset = computeHorizontalScrollOffset();
11305 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11306 if (range == 0) return false;
11307 if (direction < 0) {
11308 return offset > 0;
11309 } else {
11310 return offset < range - 1;
11311 }
11312 }
11313
11314 /**
11315 * Check if this view can be scrolled vertically in a certain direction.
11316 *
11317 * @param direction Negative to check scrolling up, positive to check scrolling down.
11318 * @return true if this view can be scrolled in the specified direction, false otherwise.
11319 */
11320 public boolean canScrollVertically(int direction) {
11321 final int offset = computeVerticalScrollOffset();
11322 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11323 if (range == 0) return false;
11324 if (direction < 0) {
11325 return offset > 0;
11326 } else {
11327 return offset < range - 1;
11328 }
11329 }
11330
11331 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11333 * scrollbars are painted only if they have been awakened first.</p>
11334 *
11335 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011336 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011337 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011338 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011339 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011340 // scrollbars are drawn only when the animation is running
11341 final ScrollabilityCache cache = mScrollCache;
11342 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011343
Mike Cleronf116bf82009-09-27 19:14:12 -070011344 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011345
Mike Cleronf116bf82009-09-27 19:14:12 -070011346 if (state == ScrollabilityCache.OFF) {
11347 return;
11348 }
Joe Malin32736f02011-01-19 16:14:20 -080011349
Mike Cleronf116bf82009-09-27 19:14:12 -070011350 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011351
Mike Cleronf116bf82009-09-27 19:14:12 -070011352 if (state == ScrollabilityCache.FADING) {
11353 // We're fading -- get our fade interpolation
11354 if (cache.interpolatorValues == null) {
11355 cache.interpolatorValues = new float[1];
11356 }
Joe Malin32736f02011-01-19 16:14:20 -080011357
Mike Cleronf116bf82009-09-27 19:14:12 -070011358 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011359
Mike Cleronf116bf82009-09-27 19:14:12 -070011360 // Stops the animation if we're done
11361 if (cache.scrollBarInterpolator.timeToValues(values) ==
11362 Interpolator.Result.FREEZE_END) {
11363 cache.state = ScrollabilityCache.OFF;
11364 } else {
11365 cache.scrollBar.setAlpha(Math.round(values[0]));
11366 }
Joe Malin32736f02011-01-19 16:14:20 -080011367
11368 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011369 // drawing. We only want this when we're fading so that
11370 // we prevent excessive redraws
11371 invalidate = true;
11372 } else {
11373 // We're just on -- but we may have been fading before so
11374 // reset alpha
11375 cache.scrollBar.setAlpha(255);
11376 }
11377
Joe Malin32736f02011-01-19 16:14:20 -080011378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 final int viewFlags = mViewFlags;
11380
11381 final boolean drawHorizontalScrollBar =
11382 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11383 final boolean drawVerticalScrollBar =
11384 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11385 && !isVerticalScrollBarHidden();
11386
11387 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11388 final int width = mRight - mLeft;
11389 final int height = mBottom - mTop;
11390
11391 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011392
Mike Reede8853fc2009-09-04 14:01:48 -040011393 final int scrollX = mScrollX;
11394 final int scrollY = mScrollY;
11395 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11396
Mike Cleronf116bf82009-09-27 19:14:12 -070011397 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011399 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011400 int size = scrollBar.getSize(false);
11401 if (size <= 0) {
11402 size = cache.scrollBarSize;
11403 }
11404
Mike Cleronf116bf82009-09-27 19:14:12 -070011405 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011406 computeHorizontalScrollOffset(),
11407 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011408 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011409 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011410 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011411 left = scrollX + (mPaddingLeft & inside);
11412 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11413 bottom = top + size;
11414 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11415 if (invalidate) {
11416 invalidate(left, top, right, bottom);
11417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011418 }
11419
11420 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011421 int size = scrollBar.getSize(true);
11422 if (size <= 0) {
11423 size = cache.scrollBarSize;
11424 }
11425
Mike Reede8853fc2009-09-04 14:01:48 -040011426 scrollBar.setParameters(computeVerticalScrollRange(),
11427 computeVerticalScrollOffset(),
11428 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011429 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11430 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11431 verticalScrollbarPosition = isLayoutRtl() ?
11432 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11433 }
11434 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011435 default:
Adam Powell20232d02010-12-08 21:08:53 -080011436 case SCROLLBAR_POSITION_RIGHT:
11437 left = scrollX + width - size - (mUserPaddingRight & inside);
11438 break;
11439 case SCROLLBAR_POSITION_LEFT:
11440 left = scrollX + (mUserPaddingLeft & inside);
11441 break;
11442 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011443 top = scrollY + (mPaddingTop & inside);
11444 right = left + size;
11445 bottom = scrollY + height - (mUserPaddingBottom & inside);
11446 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11447 if (invalidate) {
11448 invalidate(left, top, right, bottom);
11449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 }
11451 }
11452 }
11453 }
Romain Guy8506ab42009-06-11 17:35:47 -070011454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011456 * 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 -080011457 * FastScroller is visible.
11458 * @return whether to temporarily hide the vertical scrollbar
11459 * @hide
11460 */
11461 protected boolean isVerticalScrollBarHidden() {
11462 return false;
11463 }
11464
11465 /**
11466 * <p>Draw the horizontal scrollbar if
11467 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11468 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 * @param canvas the canvas on which to draw the scrollbar
11470 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011471 *
11472 * @see #isHorizontalScrollBarEnabled()
11473 * @see #computeHorizontalScrollRange()
11474 * @see #computeHorizontalScrollExtent()
11475 * @see #computeHorizontalScrollOffset()
11476 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011477 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011478 */
Romain Guy8fb95422010-08-17 18:38:51 -070011479 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11480 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011481 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011482 scrollBar.draw(canvas);
11483 }
Mike Reede8853fc2009-09-04 14:01:48 -040011484
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11487 * returns true.</p>
11488 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 * @param canvas the canvas on which to draw the scrollbar
11490 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011491 *
11492 * @see #isVerticalScrollBarEnabled()
11493 * @see #computeVerticalScrollRange()
11494 * @see #computeVerticalScrollExtent()
11495 * @see #computeVerticalScrollOffset()
11496 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011497 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 */
Romain Guy8fb95422010-08-17 18:38:51 -070011499 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11500 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011501 scrollBar.setBounds(l, t, r, b);
11502 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 }
11504
11505 /**
11506 * Implement this to do your drawing.
11507 *
11508 * @param canvas the canvas on which the background will be drawn
11509 */
11510 protected void onDraw(Canvas canvas) {
11511 }
11512
11513 /*
11514 * Caller is responsible for calling requestLayout if necessary.
11515 * (This allows addViewInLayout to not request a new layout.)
11516 */
11517 void assignParent(ViewParent parent) {
11518 if (mParent == null) {
11519 mParent = parent;
11520 } else if (parent == null) {
11521 mParent = null;
11522 } else {
11523 throw new RuntimeException("view " + this + " being added, but"
11524 + " it already has a parent");
11525 }
11526 }
11527
11528 /**
11529 * This is called when the view is attached to a window. At this point it
11530 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011531 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11532 * however it may be called any time before the first onDraw -- including
11533 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011534 *
11535 * @see #onDetachedFromWindow()
11536 */
11537 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011538 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011539 mParent.requestTransparentRegion(this);
11540 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011541
Dianne Hackborn4702a852012-08-17 15:18:29 -070011542 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011543 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011544 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011545 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011546
Chet Haasea9b61ac2010-12-20 07:40:25 -080011547 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011548
Svetoslav Ganov42138042012-03-20 11:51:39 -070011549 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011550 if (isFocused()) {
11551 InputMethodManager imm = InputMethodManager.peekInstance();
11552 imm.focusIn(this);
11553 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011554
11555 if (mAttachInfo != null && mDisplayList != null) {
11556 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11557 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011558 }
Cibu Johny86666632010-02-22 13:01:02 -080011559
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011560 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011561 * Resolve all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011562 */
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011563 void resolveRtlPropertiesIfNeeded() {
11564 if (!needRtlPropertiesResolution()) return;
11565
11566 // Order is important here: LayoutDirection MUST be resolved first
11567 if (!isLayoutDirectionResolved()) {
11568 resolveLayoutDirection();
11569 resolveLayoutParams();
11570 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011571 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011572 if (!isTextDirectionResolved()) {
11573 resolveTextDirection();
11574 }
11575 if (!isTextAlignmentResolved()) {
11576 resolveTextAlignment();
11577 }
11578 if (!isPaddingResolved()) {
11579 resolvePadding();
11580 }
11581 if (!isDrawablesResolved()) {
11582 resolveDrawables();
11583 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011584 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011585 }
11586
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011587 // Reset resolution of all RTL related properties.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011588 void resetRtlProperties() {
11589 resetResolvedLayoutDirection();
11590 resetResolvedTextDirection();
11591 resetResolvedTextAlignment();
11592 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011593 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011594 }
11595
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011596 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011597 * @see #onScreenStateChanged(int)
11598 */
11599 void dispatchScreenStateChanged(int screenState) {
11600 onScreenStateChanged(screenState);
11601 }
11602
11603 /**
11604 * This method is called whenever the state of the screen this view is
11605 * attached to changes. A state change will usually occurs when the screen
11606 * turns on or off (whether it happens automatically or the user does it
11607 * manually.)
11608 *
11609 * @param screenState The new state of the screen. Can be either
11610 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11611 */
11612 public void onScreenStateChanged(int screenState) {
11613 }
11614
11615 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011616 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11617 */
11618 private boolean hasRtlSupport() {
11619 return mContext.getApplicationInfo().hasRtlSupport();
11620 }
11621
11622 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011623 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11624 * RTL not supported)
11625 */
11626 private boolean isRtlCompatibilityMode() {
11627 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11628 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11629 }
11630
11631 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011632 * @return true if RTL properties need resolution.
11633 */
11634 private boolean needRtlPropertiesResolution() {
11635 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11636 }
11637
11638 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011639 * Called when any RTL property (layout direction or text direction or text alignment) has
11640 * been changed.
11641 *
11642 * Subclasses need to override this method to take care of cached information that depends on the
11643 * resolved layout direction, or to inform child views that inherit their layout direction.
11644 *
11645 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011646 *
11647 * @param layoutDirection the direction of the layout
11648 *
11649 * @see #LAYOUT_DIRECTION_LTR
11650 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011651 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011652 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011653 }
11654
11655 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011656 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11657 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011658 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011659 * @return true if resolution has been done, false otherwise.
11660 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011661 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011662 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011663 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011664 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011665 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011666
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011667 if (hasRtlSupport()) {
11668 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011669 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11670 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011671 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011672 // We cannot resolve yet. LTR is by default and let the resolution happen again
11673 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011674 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011675
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011676 View parent = ((View) mParent);
11677 // Parent has not yet resolved, LTR is still the default
11678 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011679
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011680 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011681 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011682 }
11683 break;
11684 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011685 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011686 break;
11687 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011688 if((LAYOUT_DIRECTION_RTL ==
11689 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011690 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011691 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011692 break;
11693 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011694 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011695 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011696 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011697
11698 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011699 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011700 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011701 }
11702
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011703 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011704 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011705 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011706 * @return true if layout direction resolution can be done otherwise return false.
11707 *
11708 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011709 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011710 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011711 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011712 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011713 return (mParent != null) && (mParent instanceof ViewGroup) &&
11714 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011715 default:
11716 return true;
11717 }
11718 }
11719
11720 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011721 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11722 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011723 *
11724 * @hide
11725 */
11726 public void resetResolvedLayoutDirection() {
11727 // Reset the current resolved bits
11728 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011729 }
11730
11731 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011732 * @return true if the layout direction is inherited.
11733 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011734 * @hide
11735 */
11736 public boolean isLayoutDirectionInherited() {
11737 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11738 }
11739
11740 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011741 * @return true if layout direction has been resolved.
11742 */
11743 private boolean isLayoutDirectionResolved() {
11744 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11745 }
11746
11747 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011748 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011749 *
11750 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011751 */
11752 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011753 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011754 }
11755
11756 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011757 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011758 *
11759 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011760 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011761 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011762 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011763 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11764 // If start / end padding are defined, they will be resolved (hence overriding) to
11765 // left / right or right / left depending on the resolved layout direction.
11766 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011767 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011768 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011769 mUserPaddingLeft = mUserPaddingLeftInitial;
11770 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011771 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011772 switch (resolvedLayoutDirection) {
11773 case LAYOUT_DIRECTION_RTL:
11774 if (mUserPaddingStart != UNDEFINED_PADDING) {
11775 mUserPaddingRight = mUserPaddingStart;
11776 }
11777 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11778 mUserPaddingLeft = mUserPaddingEnd;
11779 }
11780 break;
11781 case LAYOUT_DIRECTION_LTR:
11782 default:
11783 if (mUserPaddingStart != UNDEFINED_PADDING) {
11784 mUserPaddingLeft = mUserPaddingStart;
11785 }
11786 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11787 mUserPaddingRight = mUserPaddingEnd;
11788 }
11789 }
11790
11791 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11792
11793 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11794 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011795 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011796 }
11797
Dianne Hackborn4702a852012-08-17 15:18:29 -070011798 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011799 }
11800
11801 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011802 * Reset the resolved layout direction.
11803 *
11804 * @hide
11805 */
11806 public void resetResolvedPadding() {
11807 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11808 }
11809
11810 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011811 * This is called when the view is detached from a window. At this point it
11812 * no longer has a surface for drawing.
11813 *
11814 * @see #onAttachedToWindow()
11815 */
11816 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011817 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011818
Romain Guya440b002010-02-24 15:57:54 -080011819 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011820 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011821 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011822 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011824 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011825
Romain Guya998dff2012-03-23 18:58:36 -070011826 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011827
11828 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011829 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011830 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011831 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011832 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011833 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011834 // Should never happen
11835 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011836 }
11837
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011838 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011839
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011840 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011841 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011842 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011843 }
11844
11845 /**
11846 * @return The number of times this view has been attached to a window
11847 */
11848 protected int getWindowAttachCount() {
11849 return mWindowAttachCount;
11850 }
11851
11852 /**
11853 * Retrieve a unique token identifying the window this view is attached to.
11854 * @return Return the window's token for use in
11855 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11856 */
11857 public IBinder getWindowToken() {
11858 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11859 }
11860
11861 /**
11862 * Retrieve a unique token identifying the top-level "real" window of
11863 * the window that this view is attached to. That is, this is like
11864 * {@link #getWindowToken}, except if the window this view in is a panel
11865 * window (attached to another containing window), then the token of
11866 * the containing window is returned instead.
11867 *
11868 * @return Returns the associated window token, either
11869 * {@link #getWindowToken()} or the containing window's token.
11870 */
11871 public IBinder getApplicationWindowToken() {
11872 AttachInfo ai = mAttachInfo;
11873 if (ai != null) {
11874 IBinder appWindowToken = ai.mPanelParentWindowToken;
11875 if (appWindowToken == null) {
11876 appWindowToken = ai.mWindowToken;
11877 }
11878 return appWindowToken;
11879 }
11880 return null;
11881 }
11882
11883 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011884 * Gets the logical display to which the view's window has been attached.
11885 *
11886 * @return The logical display, or null if the view is not currently attached to a window.
11887 */
11888 public Display getDisplay() {
11889 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11890 }
11891
11892 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011893 * Retrieve private session object this view hierarchy is using to
11894 * communicate with the window manager.
11895 * @return the session object to communicate with the window manager
11896 */
11897 /*package*/ IWindowSession getWindowSession() {
11898 return mAttachInfo != null ? mAttachInfo.mSession : null;
11899 }
11900
11901 /**
11902 * @param info the {@link android.view.View.AttachInfo} to associated with
11903 * this view
11904 */
11905 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11906 //System.out.println("Attached! " + this);
11907 mAttachInfo = info;
11908 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011909 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011910 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911 if (mFloatingTreeObserver != null) {
11912 info.mTreeObserver.merge(mFloatingTreeObserver);
11913 mFloatingTreeObserver = null;
11914 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011915 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011917 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011918 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011919 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011921
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011922 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011923 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011924 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011925 if (listeners != null && listeners.size() > 0) {
11926 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11927 // perform the dispatching. The iterator is a safe guard against listeners that
11928 // could mutate the list by calling the various add/remove methods. This prevents
11929 // the array from being modified while we iterate it.
11930 for (OnAttachStateChangeListener listener : listeners) {
11931 listener.onViewAttachedToWindow(this);
11932 }
11933 }
11934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 int vis = info.mWindowVisibility;
11936 if (vis != GONE) {
11937 onWindowVisibilityChanged(vis);
11938 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011939 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011940 // If nobody has evaluated the drawable state yet, then do it now.
11941 refreshDrawableState();
11942 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011943 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 }
11945
11946 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011947 AttachInfo info = mAttachInfo;
11948 if (info != null) {
11949 int vis = info.mWindowVisibility;
11950 if (vis != GONE) {
11951 onWindowVisibilityChanged(GONE);
11952 }
11953 }
11954
11955 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011956
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011957 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011958 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011959 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011960 if (listeners != null && listeners.size() > 0) {
11961 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11962 // perform the dispatching. The iterator is a safe guard against listeners that
11963 // could mutate the list by calling the various add/remove methods. This prevents
11964 // the array from being modified while we iterate it.
11965 for (OnAttachStateChangeListener listener : listeners) {
11966 listener.onViewDetachedFromWindow(this);
11967 }
11968 }
11969
Dianne Hackborn4702a852012-08-17 15:18:29 -070011970 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011972 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011973 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011975 mAttachInfo = null;
11976 }
11977
11978 /**
11979 * Store this view hierarchy's frozen state into the given container.
11980 *
11981 * @param container The SparseArray in which to save the view's state.
11982 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011983 * @see #restoreHierarchyState(android.util.SparseArray)
11984 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11985 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986 */
11987 public void saveHierarchyState(SparseArray<Parcelable> container) {
11988 dispatchSaveInstanceState(container);
11989 }
11990
11991 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011992 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11993 * this view and its children. May be overridden to modify how freezing happens to a
11994 * 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 -080011995 *
11996 * @param container The SparseArray in which to save the view's state.
11997 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011998 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11999 * @see #saveHierarchyState(android.util.SparseArray)
12000 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012001 */
12002 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12003 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012004 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012006 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012007 throw new IllegalStateException(
12008 "Derived class did not call super.onSaveInstanceState()");
12009 }
12010 if (state != null) {
12011 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12012 // + ": " + state);
12013 container.put(mID, state);
12014 }
12015 }
12016 }
12017
12018 /**
12019 * Hook allowing a view to generate a representation of its internal state
12020 * that can later be used to create a new instance with that same state.
12021 * This state should only contain information that is not persistent or can
12022 * not be reconstructed later. For example, you will never store your
12023 * current position on screen because that will be computed again when a
12024 * new instance of the view is placed in its view hierarchy.
12025 * <p>
12026 * Some examples of things you may store here: the current cursor position
12027 * in a text view (but usually not the text itself since that is stored in a
12028 * content provider or other persistent storage), the currently selected
12029 * item in a list view.
12030 *
12031 * @return Returns a Parcelable object containing the view's current dynamic
12032 * state, or null if there is nothing interesting to save. The
12033 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012034 * @see #onRestoreInstanceState(android.os.Parcelable)
12035 * @see #saveHierarchyState(android.util.SparseArray)
12036 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012037 * @see #setSaveEnabled(boolean)
12038 */
12039 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012040 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012041 return BaseSavedState.EMPTY_STATE;
12042 }
12043
12044 /**
12045 * Restore this view hierarchy's frozen state from the given container.
12046 *
12047 * @param container The SparseArray which holds previously frozen states.
12048 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012049 * @see #saveHierarchyState(android.util.SparseArray)
12050 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12051 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012052 */
12053 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12054 dispatchRestoreInstanceState(container);
12055 }
12056
12057 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012058 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12059 * state for this view and its children. May be overridden to modify how restoring
12060 * happens to a view's children; for example, some views may want to not store state
12061 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012062 *
12063 * @param container The SparseArray which holds previously saved state.
12064 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012065 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12066 * @see #restoreHierarchyState(android.util.SparseArray)
12067 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 */
12069 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12070 if (mID != NO_ID) {
12071 Parcelable state = container.get(mID);
12072 if (state != null) {
12073 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12074 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012075 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012076 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012077 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012078 throw new IllegalStateException(
12079 "Derived class did not call super.onRestoreInstanceState()");
12080 }
12081 }
12082 }
12083 }
12084
12085 /**
12086 * Hook allowing a view to re-apply a representation of its internal state that had previously
12087 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12088 * null state.
12089 *
12090 * @param state The frozen state that had previously been returned by
12091 * {@link #onSaveInstanceState}.
12092 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012093 * @see #onSaveInstanceState()
12094 * @see #restoreHierarchyState(android.util.SparseArray)
12095 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012096 */
12097 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012098 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012100 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12101 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012102 + "when two views of different type have the same id in the same hierarchy. "
12103 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012104 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012105 }
12106 }
12107
12108 /**
12109 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12110 *
12111 * @return the drawing start time in milliseconds
12112 */
12113 public long getDrawingTime() {
12114 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12115 }
12116
12117 /**
12118 * <p>Enables or disables the duplication of the parent's state into this view. When
12119 * duplication is enabled, this view gets its drawable state from its parent rather
12120 * than from its own internal properties.</p>
12121 *
12122 * <p>Note: in the current implementation, setting this property to true after the
12123 * view was added to a ViewGroup might have no effect at all. This property should
12124 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12125 *
12126 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12127 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012128 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012129 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12130 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012131 *
12132 * @param enabled True to enable duplication of the parent's drawable state, false
12133 * to disable it.
12134 *
12135 * @see #getDrawableState()
12136 * @see #isDuplicateParentStateEnabled()
12137 */
12138 public void setDuplicateParentStateEnabled(boolean enabled) {
12139 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12140 }
12141
12142 /**
12143 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12144 *
12145 * @return True if this view's drawable state is duplicated from the parent,
12146 * false otherwise
12147 *
12148 * @see #getDrawableState()
12149 * @see #setDuplicateParentStateEnabled(boolean)
12150 */
12151 public boolean isDuplicateParentStateEnabled() {
12152 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12153 }
12154
12155 /**
Romain Guy171c5922011-01-06 10:04:23 -080012156 * <p>Specifies the type of layer backing this view. The layer can be
12157 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12158 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012159 *
Romain Guy171c5922011-01-06 10:04:23 -080012160 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12161 * instance that controls how the layer is composed on screen. The following
12162 * properties of the paint are taken into account when composing the layer:</p>
12163 * <ul>
12164 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12165 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12166 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12167 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012168 *
Romain Guy171c5922011-01-06 10:04:23 -080012169 * <p>If this view has an alpha value set to < 1.0 by calling
12170 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12171 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12172 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012173 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012174 *
Romain Guy171c5922011-01-06 10:04:23 -080012175 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12176 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12177 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012178 *
Chet Haased15ebf22012-09-05 11:40:29 -070012179 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012180 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12181 * {@link #LAYER_TYPE_HARDWARE}
12182 * @param paint The paint used to compose the layer. This argument is optional
12183 * and can be null. It is ignored when the layer type is
12184 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012185 *
12186 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012187 * @see #LAYER_TYPE_NONE
12188 * @see #LAYER_TYPE_SOFTWARE
12189 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012190 * @see #setAlpha(float)
12191 *
Romain Guy171c5922011-01-06 10:04:23 -080012192 * @attr ref android.R.styleable#View_layerType
12193 */
12194 public void setLayerType(int layerType, Paint paint) {
12195 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012196 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012197 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12198 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012199
Romain Guyd6cd5722011-01-17 14:42:41 -080012200 if (layerType == mLayerType) {
12201 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12202 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012203 invalidateParentCaches();
12204 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012205 }
12206 return;
12207 }
Romain Guy171c5922011-01-06 10:04:23 -080012208
12209 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012210 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012211 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012212 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012213 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012214 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012215 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012216 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012217 default:
12218 break;
Romain Guy171c5922011-01-06 10:04:23 -080012219 }
12220
12221 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012222 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12223 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12224 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012225
Romain Guy0fd89bf2011-01-26 15:41:30 -080012226 invalidateParentCaches();
12227 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012228 }
12229
12230 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012231 * Updates the {@link Paint} object used with the current layer (used only if the current
12232 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12233 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12234 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12235 * ensure that the view gets redrawn immediately.
12236 *
12237 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12238 * instance that controls how the layer is composed on screen. The following
12239 * properties of the paint are taken into account when composing the layer:</p>
12240 * <ul>
12241 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12242 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12243 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12244 * </ul>
12245 *
12246 * <p>If this view has an alpha value set to < 1.0 by calling
12247 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12248 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12249 * equivalent to setting a hardware layer on this view and providing a paint with
12250 * the desired alpha value.</p>
12251 *
12252 * @param paint The paint used to compose the layer. This argument is optional
12253 * and can be null. It is ignored when the layer type is
12254 * {@link #LAYER_TYPE_NONE}
12255 *
12256 * @see #setLayerType(int, android.graphics.Paint)
12257 */
12258 public void setLayerPaint(Paint paint) {
12259 int layerType = getLayerType();
12260 if (layerType != LAYER_TYPE_NONE) {
12261 mLayerPaint = paint == null ? new Paint() : paint;
12262 if (layerType == LAYER_TYPE_HARDWARE) {
12263 HardwareLayer layer = getHardwareLayer();
12264 if (layer != null) {
12265 layer.setLayerPaint(paint);
12266 }
12267 invalidateViewProperty(false, false);
12268 } else {
12269 invalidate();
12270 }
12271 }
12272 }
12273
12274 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012275 * Indicates whether this view has a static layer. A view with layer type
12276 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12277 * dynamic.
12278 */
12279 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012280 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012281 }
12282
12283 /**
Romain Guy171c5922011-01-06 10:04:23 -080012284 * Indicates what type of layer is currently associated with this view. By default
12285 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12286 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12287 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012288 *
Romain Guy171c5922011-01-06 10:04:23 -080012289 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12290 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012291 *
12292 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012293 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012294 * @see #LAYER_TYPE_NONE
12295 * @see #LAYER_TYPE_SOFTWARE
12296 * @see #LAYER_TYPE_HARDWARE
12297 */
12298 public int getLayerType() {
12299 return mLayerType;
12300 }
Joe Malin32736f02011-01-19 16:14:20 -080012301
Romain Guy6c319ca2011-01-11 14:29:25 -080012302 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012303 * Forces this view's layer to be created and this view to be rendered
12304 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12305 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012306 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012307 * This method can for instance be used to render a view into its layer before
12308 * starting an animation. If this view is complex, rendering into the layer
12309 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012310 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012311 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012312 *
12313 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012314 */
12315 public void buildLayer() {
12316 if (mLayerType == LAYER_TYPE_NONE) return;
12317
12318 if (mAttachInfo == null) {
12319 throw new IllegalStateException("This view must be attached to a window first");
12320 }
12321
12322 switch (mLayerType) {
12323 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012324 if (mAttachInfo.mHardwareRenderer != null &&
12325 mAttachInfo.mHardwareRenderer.isEnabled() &&
12326 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012327 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012328 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012329 break;
12330 case LAYER_TYPE_SOFTWARE:
12331 buildDrawingCache(true);
12332 break;
12333 }
12334 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012335
Romain Guyf1ae1062011-03-02 18:16:04 -080012336 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012337 * <p>Returns a hardware layer that can be used to draw this view again
12338 * without executing its draw method.</p>
12339 *
12340 * @return A HardwareLayer ready to render, or null if an error occurred.
12341 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012342 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012343 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12344 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012345 return null;
12346 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012347
Romain Guy9c4b79a2011-11-10 19:23:58 -080012348 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012349
12350 final int width = mRight - mLeft;
12351 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012352
Romain Guy6c319ca2011-01-11 14:29:25 -080012353 if (width == 0 || height == 0) {
12354 return null;
12355 }
12356
Dianne Hackborn4702a852012-08-17 15:18:29 -070012357 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012358 if (mHardwareLayer == null) {
12359 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12360 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012361 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012362 } else {
12363 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012364 if (mHardwareLayer.resize(width, height)) {
12365 mLocalDirtyRect.set(0, 0, width, height);
12366 }
Romain Guy846a5332012-07-11 17:44:57 -070012367 }
12368
12369 // This should not be necessary but applications that change
12370 // the parameters of their background drawable without calling
12371 // this.setBackground(Drawable) can leave the view in a bad state
12372 // (for instance isOpaque() returns true, but the background is
12373 // not opaque.)
12374 computeOpaqueFlags();
12375
12376 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012377 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012378 mHardwareLayer.setOpaque(opaque);
12379 mLocalDirtyRect.set(0, 0, width, height);
12380 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012381 }
12382
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012383 // The layer is not valid if the underlying GPU resources cannot be allocated
12384 if (!mHardwareLayer.isValid()) {
12385 return null;
12386 }
12387
Romain Guy11cb6422012-09-21 00:39:43 -070012388 mHardwareLayer.setLayerPaint(mLayerPaint);
12389 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12390 ViewRootImpl viewRoot = getViewRootImpl();
12391 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12392
Michael Jurka7e52caf2012-03-06 15:57:06 -080012393 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012394 }
12395
12396 return mHardwareLayer;
12397 }
Romain Guy171c5922011-01-06 10:04:23 -080012398
Romain Guy589b0bb2011-10-10 13:57:47 -070012399 /**
12400 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012401 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012402 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012403 *
12404 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012405 * @see #LAYER_TYPE_HARDWARE
12406 */
Romain Guya998dff2012-03-23 18:58:36 -070012407 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012408 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012409 AttachInfo info = mAttachInfo;
12410 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012411 info.mHardwareRenderer.isEnabled() &&
12412 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012413 mHardwareLayer.destroy();
12414 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012415
Chet Haase6a2d17f2012-09-30 12:14:13 -070012416 if (mDisplayList != null) {
12417 mDisplayList.reset();
12418 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012419 invalidate(true);
12420 invalidateParentCaches();
12421 }
Romain Guy65b345f2011-07-27 18:51:50 -070012422 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012423 }
Romain Guy65b345f2011-07-27 18:51:50 -070012424 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012425 }
12426
Romain Guy171c5922011-01-06 10:04:23 -080012427 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012428 * Destroys all hardware rendering resources. This method is invoked
12429 * when the system needs to reclaim resources. Upon execution of this
12430 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012431 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012432 * Note: you <strong>must</strong> call
12433 * <code>super.destroyHardwareResources()</code> when overriding
12434 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012435 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012436 * @hide
12437 */
12438 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012439 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012440 }
12441
12442 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012443 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12444 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12445 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12446 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12447 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12448 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012449 *
Romain Guy171c5922011-01-06 10:04:23 -080012450 * <p>Enabling the drawing cache is similar to
12451 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012452 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12453 * drawing cache has no effect on rendering because the system uses a different mechanism
12454 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12455 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12456 * for information on how to enable software and hardware layers.</p>
12457 *
12458 * <p>This API can be used to manually generate
12459 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12460 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 *
12462 * @param enabled true to enable the drawing cache, false otherwise
12463 *
12464 * @see #isDrawingCacheEnabled()
12465 * @see #getDrawingCache()
12466 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012467 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 */
12469 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012470 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012471 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12472 }
12473
12474 /**
12475 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12476 *
12477 * @return true if the drawing cache is enabled
12478 *
12479 * @see #setDrawingCacheEnabled(boolean)
12480 * @see #getDrawingCache()
12481 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012482 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 public boolean isDrawingCacheEnabled() {
12484 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12485 }
12486
12487 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012488 * Debugging utility which recursively outputs the dirty state of a view and its
12489 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012490 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012491 * @hide
12492 */
Romain Guy676b1732011-02-14 14:45:33 -080012493 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012494 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012495 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12496 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12497 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12498 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012499 if (clear) {
12500 mPrivateFlags &= clearMask;
12501 }
12502 if (this instanceof ViewGroup) {
12503 ViewGroup parent = (ViewGroup) this;
12504 final int count = parent.getChildCount();
12505 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012506 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012507 child.outputDirtyFlags(indent + " ", clear, clearMask);
12508 }
12509 }
12510 }
12511
12512 /**
12513 * This method is used by ViewGroup to cause its children to restore or recreate their
12514 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12515 * to recreate its own display list, which would happen if it went through the normal
12516 * draw/dispatchDraw mechanisms.
12517 *
12518 * @hide
12519 */
12520 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012521
12522 /**
12523 * A view that is not attached or hardware accelerated cannot create a display list.
12524 * This method checks these conditions and returns the appropriate result.
12525 *
12526 * @return true if view has the ability to create a display list, false otherwise.
12527 *
12528 * @hide
12529 */
12530 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012531 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012532 }
Joe Malin32736f02011-01-19 16:14:20 -080012533
Chet Haasedaf98e92011-01-10 14:10:36 -080012534 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012535 * @return The HardwareRenderer associated with that view or null if hardware rendering
12536 * is not supported or this this has not been attached to a window.
12537 *
12538 * @hide
12539 */
12540 public HardwareRenderer getHardwareRenderer() {
12541 if (mAttachInfo != null) {
12542 return mAttachInfo.mHardwareRenderer;
12543 }
12544 return null;
12545 }
12546
12547 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012548 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12549 * Otherwise, the same display list will be returned (after having been rendered into
12550 * along the way, depending on the invalidation state of the view).
12551 *
12552 * @param displayList The previous version of this displayList, could be null.
12553 * @param isLayer Whether the requester of the display list is a layer. If so,
12554 * the view will avoid creating a layer inside the resulting display list.
12555 * @return A new or reused DisplayList object.
12556 */
12557 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12558 if (!canHaveDisplayList()) {
12559 return null;
12560 }
12561
Dianne Hackborn4702a852012-08-17 15:18:29 -070012562 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012563 displayList == null || !displayList.isValid() ||
12564 (!isLayer && mRecreateDisplayList))) {
12565 // Don't need to recreate the display list, just need to tell our
12566 // children to restore/recreate theirs
12567 if (displayList != null && displayList.isValid() &&
12568 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012569 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12570 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012571 dispatchGetDisplayList();
12572
12573 return displayList;
12574 }
12575
12576 if (!isLayer) {
12577 // If we got here, we're recreating it. Mark it as such to ensure that
12578 // we copy in child display lists into ours in drawChild()
12579 mRecreateDisplayList = true;
12580 }
12581 if (displayList == null) {
12582 final String name = getClass().getSimpleName();
12583 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12584 // If we're creating a new display list, make sure our parent gets invalidated
12585 // since they will need to recreate their display list to account for this
12586 // new child display list.
12587 invalidateParentCaches();
12588 }
12589
12590 boolean caching = false;
12591 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012592 int width = mRight - mLeft;
12593 int height = mBottom - mTop;
12594
12595 try {
12596 canvas.setViewport(width, height);
12597 // The dirty rect should always be null for a display list
12598 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012599 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012600 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012601 if (layerType == LAYER_TYPE_HARDWARE) {
12602 final HardwareLayer layer = getHardwareLayer();
12603 if (layer != null && layer.isValid()) {
12604 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12605 } else {
12606 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12607 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12608 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12609 }
12610 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012611 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012612 buildDrawingCache(true);
12613 Bitmap cache = getDrawingCache(true);
12614 if (cache != null) {
12615 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12616 caching = true;
12617 }
Chet Haasea1cff502012-02-21 13:43:44 -080012618 }
Chet Haasea1cff502012-02-21 13:43:44 -080012619 } else {
12620
12621 computeScroll();
12622
Chet Haasea1cff502012-02-21 13:43:44 -080012623 canvas.translate(-mScrollX, -mScrollY);
12624 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012625 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12626 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012627 }
12628
12629 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012630 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012631 dispatchDraw(canvas);
12632 } else {
12633 draw(canvas);
12634 }
12635 }
12636 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012637 canvas.onPostDraw();
12638
12639 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012640 displayList.setCaching(caching);
12641 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012642 displayList.setLeftTopRightBottom(0, 0, width, height);
12643 } else {
12644 setDisplayListProperties(displayList);
12645 }
12646 }
12647 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012648 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12649 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012650 }
12651
12652 return displayList;
12653 }
12654
12655 /**
12656 * Get the DisplayList for the HardwareLayer
12657 *
12658 * @param layer The HardwareLayer whose DisplayList we want
12659 * @return A DisplayList fopr the specified HardwareLayer
12660 */
12661 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12662 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12663 layer.setDisplayList(displayList);
12664 return displayList;
12665 }
12666
12667
12668 /**
Romain Guyb051e892010-09-28 19:09:36 -070012669 * <p>Returns a display list that can be used to draw this view again
12670 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012671 *
Romain Guyb051e892010-09-28 19:09:36 -070012672 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012673 *
12674 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012675 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012676 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012677 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012678 return mDisplayList;
12679 }
12680
Romain Guy38c2ece2012-05-24 14:20:56 -070012681 private void clearDisplayList() {
12682 if (mDisplayList != null) {
12683 mDisplayList.invalidate();
12684 mDisplayList.clear();
12685 }
12686 }
12687
Romain Guyb051e892010-09-28 19:09:36 -070012688 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012689 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012690 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012691 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012692 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012693 * @see #getDrawingCache(boolean)
12694 */
12695 public Bitmap getDrawingCache() {
12696 return getDrawingCache(false);
12697 }
12698
12699 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012700 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12701 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12702 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12703 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12704 * request the drawing cache by calling this method and draw it on screen if the
12705 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012706 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012707 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12708 * this method will create a bitmap of the same size as this view. Because this bitmap
12709 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12710 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12711 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12712 * size than the view. This implies that your application must be able to handle this
12713 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012714 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012715 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12716 * the current density of the screen when the application is in compatibility
12717 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012718 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012719 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012720 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721 * @see #setDrawingCacheEnabled(boolean)
12722 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012723 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 * @see #destroyDrawingCache()
12725 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012726 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012727 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12728 return null;
12729 }
12730 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012731 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012732 }
Romain Guy02890fd2010-08-06 17:58:44 -070012733 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012734 }
12735
12736 /**
12737 * <p>Frees the resources used by the drawing cache. If you call
12738 * {@link #buildDrawingCache()} manually without calling
12739 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12740 * should cleanup the cache with this method afterwards.</p>
12741 *
12742 * @see #setDrawingCacheEnabled(boolean)
12743 * @see #buildDrawingCache()
12744 * @see #getDrawingCache()
12745 */
12746 public void destroyDrawingCache() {
12747 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012748 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012749 mDrawingCache = null;
12750 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012751 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012752 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012753 mUnscaledDrawingCache = null;
12754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012755 }
12756
12757 /**
12758 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012759 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 * view will always be drawn on top of a solid color.
12761 *
12762 * @param color The background color to use for the drawing cache's bitmap
12763 *
12764 * @see #setDrawingCacheEnabled(boolean)
12765 * @see #buildDrawingCache()
12766 * @see #getDrawingCache()
12767 */
12768 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012769 if (color != mDrawingCacheBackgroundColor) {
12770 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012771 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012773 }
12774
12775 /**
12776 * @see #setDrawingCacheBackgroundColor(int)
12777 *
12778 * @return The background color to used for the drawing cache's bitmap
12779 */
12780 public int getDrawingCacheBackgroundColor() {
12781 return mDrawingCacheBackgroundColor;
12782 }
12783
12784 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012785 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012786 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012787 * @see #buildDrawingCache(boolean)
12788 */
12789 public void buildDrawingCache() {
12790 buildDrawingCache(false);
12791 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012792
Romain Guyfbd8f692009-06-26 14:51:58 -070012793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012794 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12795 *
12796 * <p>If you call {@link #buildDrawingCache()} manually without calling
12797 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12798 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012799 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012800 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12801 * this method will create a bitmap of the same size as this view. Because this bitmap
12802 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12803 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12804 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12805 * size than the view. This implies that your application must be able to handle this
12806 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012807 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012808 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12809 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012810 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012811 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 *
12813 * @see #getDrawingCache()
12814 * @see #destroyDrawingCache()
12815 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012816 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012817 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012818 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012819 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012820
Romain Guy8506ab42009-06-11 17:35:47 -070012821 int width = mRight - mLeft;
12822 int height = mBottom - mTop;
12823
12824 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012825 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012826
Romain Guye1123222009-06-29 14:24:56 -070012827 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012828 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12829 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012831
12832 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012833 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012834 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012835
Romain Guyaf61cc42012-09-30 10:49:15 -070012836 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12837 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012838 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12839 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012840 if (width > 0 && height > 0) {
12841 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12842 + projectedBitmapSize + " bytes, only "
12843 + drawingCacheSize + " available");
12844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012845 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012846 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012847 return;
12848 }
12849
12850 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012851 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012852
12853 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 Bitmap.Config quality;
12855 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012856 // Never pick ARGB_4444 because it looks awful
12857 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012858 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12859 case DRAWING_CACHE_QUALITY_AUTO:
12860 quality = Bitmap.Config.ARGB_8888;
12861 break;
12862 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012863 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012864 break;
12865 case DRAWING_CACHE_QUALITY_HIGH:
12866 quality = Bitmap.Config.ARGB_8888;
12867 break;
12868 default:
12869 quality = Bitmap.Config.ARGB_8888;
12870 break;
12871 }
12872 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012873 // Optimization for translucent windows
12874 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012875 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012876 }
12877
12878 // Try to cleanup memory
12879 if (bitmap != null) bitmap.recycle();
12880
12881 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012882 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12883 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012884 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012885 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012886 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012887 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012888 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012889 }
Adam Powell26153a32010-11-08 15:22:27 -080012890 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 } catch (OutOfMemoryError e) {
12892 // If there is not enough memory to create the bitmap cache, just
12893 // ignore the issue as bitmap caches are not required to draw the
12894 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012895 if (autoScale) {
12896 mDrawingCache = null;
12897 } else {
12898 mUnscaledDrawingCache = null;
12899 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012900 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012901 return;
12902 }
12903
12904 clear = drawingCacheBackgroundColor != 0;
12905 }
12906
12907 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012908 if (attachInfo != null) {
12909 canvas = attachInfo.mCanvas;
12910 if (canvas == null) {
12911 canvas = new Canvas();
12912 }
12913 canvas.setBitmap(bitmap);
12914 // Temporarily clobber the cached Canvas in case one of our children
12915 // is also using a drawing cache. Without this, the children would
12916 // steal the canvas by attaching their own bitmap to it and bad, bad
12917 // thing would happen (invisible views, corrupted drawings, etc.)
12918 attachInfo.mCanvas = null;
12919 } else {
12920 // This case should hopefully never or seldom happen
12921 canvas = new Canvas(bitmap);
12922 }
12923
12924 if (clear) {
12925 bitmap.eraseColor(drawingCacheBackgroundColor);
12926 }
12927
12928 computeScroll();
12929 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012930
Romain Guye1123222009-06-29 14:24:56 -070012931 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012932 final float scale = attachInfo.mApplicationScale;
12933 canvas.scale(scale, scale);
12934 }
Joe Malin32736f02011-01-19 16:14:20 -080012935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012936 canvas.translate(-mScrollX, -mScrollY);
12937
Dianne Hackborn4702a852012-08-17 15:18:29 -070012938 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012939 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12940 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012941 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012943
12944 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012945 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12946 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012947 dispatchDraw(canvas);
12948 } else {
12949 draw(canvas);
12950 }
12951
12952 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012953 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954
12955 if (attachInfo != null) {
12956 // Restore the cached Canvas for our siblings
12957 attachInfo.mCanvas = canvas;
12958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012959 }
12960 }
12961
12962 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012963 * Create a snapshot of the view into a bitmap. We should probably make
12964 * some form of this public, but should think about the API.
12965 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012966 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012967 int width = mRight - mLeft;
12968 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012969
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012970 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012971 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012972 width = (int) ((width * scale) + 0.5f);
12973 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012974
Dianne Hackborndde331c2012-08-03 14:01:57 -070012975 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12976 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012977 if (bitmap == null) {
12978 throw new OutOfMemoryError();
12979 }
12980
Romain Guyc529d8d2011-09-06 15:01:39 -070012981 Resources resources = getResources();
12982 if (resources != null) {
12983 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12984 }
Joe Malin32736f02011-01-19 16:14:20 -080012985
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012986 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012987 if (attachInfo != null) {
12988 canvas = attachInfo.mCanvas;
12989 if (canvas == null) {
12990 canvas = new Canvas();
12991 }
12992 canvas.setBitmap(bitmap);
12993 // Temporarily clobber the cached Canvas in case one of our children
12994 // is also using a drawing cache. Without this, the children would
12995 // steal the canvas by attaching their own bitmap to it and bad, bad
12996 // things would happen (invisible views, corrupted drawings, etc.)
12997 attachInfo.mCanvas = null;
12998 } else {
12999 // This case should hopefully never or seldom happen
13000 canvas = new Canvas(bitmap);
13001 }
13002
Romain Guy5bcdff42009-05-14 21:27:18 -070013003 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013004 bitmap.eraseColor(backgroundColor);
13005 }
13006
13007 computeScroll();
13008 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013009 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013010 canvas.translate(-mScrollX, -mScrollY);
13011
Romain Guy5bcdff42009-05-14 21:27:18 -070013012 // Temporarily remove the dirty mask
13013 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013014 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013015
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013016 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013017 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013018 dispatchDraw(canvas);
13019 } else {
13020 draw(canvas);
13021 }
13022
Romain Guy5bcdff42009-05-14 21:27:18 -070013023 mPrivateFlags = flags;
13024
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013025 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013026 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013027
13028 if (attachInfo != null) {
13029 // Restore the cached Canvas for our siblings
13030 attachInfo.mCanvas = canvas;
13031 }
Romain Guy8506ab42009-06-11 17:35:47 -070013032
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013033 return bitmap;
13034 }
13035
13036 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013037 * Indicates whether this View is currently in edit mode. A View is usually
13038 * in edit mode when displayed within a developer tool. For instance, if
13039 * this View is being drawn by a visual user interface builder, this method
13040 * should return true.
13041 *
13042 * Subclasses should check the return value of this method to provide
13043 * different behaviors if their normal behavior might interfere with the
13044 * host environment. For instance: the class spawns a thread in its
13045 * constructor, the drawing code relies on device-specific features, etc.
13046 *
13047 * This method is usually checked in the drawing code of custom widgets.
13048 *
13049 * @return True if this View is in edit mode, false otherwise.
13050 */
13051 public boolean isInEditMode() {
13052 return false;
13053 }
13054
13055 /**
13056 * If the View draws content inside its padding and enables fading edges,
13057 * it needs to support padding offsets. Padding offsets are added to the
13058 * fading edges to extend the length of the fade so that it covers pixels
13059 * drawn inside the padding.
13060 *
13061 * Subclasses of this class should override this method if they need
13062 * to draw content inside the padding.
13063 *
13064 * @return True if padding offset must be applied, false otherwise.
13065 *
13066 * @see #getLeftPaddingOffset()
13067 * @see #getRightPaddingOffset()
13068 * @see #getTopPaddingOffset()
13069 * @see #getBottomPaddingOffset()
13070 *
13071 * @since CURRENT
13072 */
13073 protected boolean isPaddingOffsetRequired() {
13074 return false;
13075 }
13076
13077 /**
13078 * Amount by which to extend the left fading region. Called only when
13079 * {@link #isPaddingOffsetRequired()} returns true.
13080 *
13081 * @return The left padding offset in pixels.
13082 *
13083 * @see #isPaddingOffsetRequired()
13084 *
13085 * @since CURRENT
13086 */
13087 protected int getLeftPaddingOffset() {
13088 return 0;
13089 }
13090
13091 /**
13092 * Amount by which to extend the right fading region. Called only when
13093 * {@link #isPaddingOffsetRequired()} returns true.
13094 *
13095 * @return The right padding offset in pixels.
13096 *
13097 * @see #isPaddingOffsetRequired()
13098 *
13099 * @since CURRENT
13100 */
13101 protected int getRightPaddingOffset() {
13102 return 0;
13103 }
13104
13105 /**
13106 * Amount by which to extend the top fading region. Called only when
13107 * {@link #isPaddingOffsetRequired()} returns true.
13108 *
13109 * @return The top padding offset in pixels.
13110 *
13111 * @see #isPaddingOffsetRequired()
13112 *
13113 * @since CURRENT
13114 */
13115 protected int getTopPaddingOffset() {
13116 return 0;
13117 }
13118
13119 /**
13120 * Amount by which to extend the bottom fading region. Called only when
13121 * {@link #isPaddingOffsetRequired()} returns true.
13122 *
13123 * @return The bottom padding offset in pixels.
13124 *
13125 * @see #isPaddingOffsetRequired()
13126 *
13127 * @since CURRENT
13128 */
13129 protected int getBottomPaddingOffset() {
13130 return 0;
13131 }
13132
13133 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013134 * @hide
13135 * @param offsetRequired
13136 */
13137 protected int getFadeTop(boolean offsetRequired) {
13138 int top = mPaddingTop;
13139 if (offsetRequired) top += getTopPaddingOffset();
13140 return top;
13141 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013142
Romain Guyf2fc4602011-07-19 15:20:03 -070013143 /**
13144 * @hide
13145 * @param offsetRequired
13146 */
13147 protected int getFadeHeight(boolean offsetRequired) {
13148 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013149 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013150 return mBottom - mTop - mPaddingBottom - padding;
13151 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013152
Romain Guyf2fc4602011-07-19 15:20:03 -070013153 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013154 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013155 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013156 *
Romain Guy2bffd262010-09-12 17:40:02 -070013157 * <p>Even if this method returns true, it does not mean that every call
13158 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13159 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013160 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013161 * window is hardware accelerated,
13162 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13163 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013164 *
Romain Guy2bffd262010-09-12 17:40:02 -070013165 * @return True if the view is attached to a window and the window is
13166 * hardware accelerated; false in any other case.
13167 */
13168 public boolean isHardwareAccelerated() {
13169 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13170 }
Joe Malin32736f02011-01-19 16:14:20 -080013171
Romain Guy2bffd262010-09-12 17:40:02 -070013172 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013173 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13174 * case of an active Animation being run on the view.
13175 */
13176 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13177 Animation a, boolean scalingRequired) {
13178 Transformation invalidationTransform;
13179 final int flags = parent.mGroupFlags;
13180 final boolean initialized = a.isInitialized();
13181 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013182 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013183 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013184 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013185 onAnimationStart();
13186 }
13187
13188 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13189 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13190 if (parent.mInvalidationTransformation == null) {
13191 parent.mInvalidationTransformation = new Transformation();
13192 }
13193 invalidationTransform = parent.mInvalidationTransformation;
13194 a.getTransformation(drawingTime, invalidationTransform, 1f);
13195 } else {
13196 invalidationTransform = parent.mChildTransformation;
13197 }
Romain Guy393a52c2012-05-22 20:21:08 -070013198
Chet Haasebcca79a2012-02-14 08:45:14 -080013199 if (more) {
13200 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013201 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13202 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13203 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13204 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013205 // The child need to draw an animation, potentially offscreen, so
13206 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013207 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013208 parent.invalidate(mLeft, mTop, mRight, mBottom);
13209 }
13210 } else {
13211 if (parent.mInvalidateRegion == null) {
13212 parent.mInvalidateRegion = new RectF();
13213 }
13214 final RectF region = parent.mInvalidateRegion;
13215 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13216 invalidationTransform);
13217
13218 // The child need to draw an animation, potentially offscreen, so
13219 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013220 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013221
13222 final int left = mLeft + (int) region.left;
13223 final int top = mTop + (int) region.top;
13224 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13225 top + (int) (region.height() + .5f));
13226 }
13227 }
13228 return more;
13229 }
13230
Chet Haasea1cff502012-02-21 13:43:44 -080013231 /**
13232 * This method is called by getDisplayList() when a display list is created or re-rendered.
13233 * It sets or resets the current value of all properties on that display list (resetting is
13234 * necessary when a display list is being re-created, because we need to make sure that
13235 * previously-set transform values
13236 */
13237 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013238 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013239 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013240 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013241 if (mParent instanceof ViewGroup) {
13242 displayList.setClipChildren(
13243 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13244 }
Chet Haase9420abd2012-03-29 16:28:32 -070013245 float alpha = 1;
13246 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13247 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13248 ViewGroup parentVG = (ViewGroup) mParent;
13249 final boolean hasTransform =
13250 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13251 if (hasTransform) {
13252 Transformation transform = parentVG.mChildTransformation;
13253 final int transformType = parentVG.mChildTransformation.getTransformationType();
13254 if (transformType != Transformation.TYPE_IDENTITY) {
13255 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13256 alpha = transform.getAlpha();
13257 }
13258 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13259 displayList.setStaticMatrix(transform.getMatrix());
13260 }
13261 }
13262 }
Chet Haasea1cff502012-02-21 13:43:44 -080013263 }
13264 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013265 alpha *= mTransformationInfo.mAlpha;
13266 if (alpha < 1) {
13267 final int multipliedAlpha = (int) (255 * alpha);
13268 if (onSetAlpha(multipliedAlpha)) {
13269 alpha = 1;
13270 }
13271 }
13272 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013273 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13274 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13275 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13276 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013277 if (mTransformationInfo.mCamera == null) {
13278 mTransformationInfo.mCamera = new Camera();
13279 mTransformationInfo.matrix3D = new Matrix();
13280 }
13281 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013282 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013283 displayList.setPivotX(getPivotX());
13284 displayList.setPivotY(getPivotY());
13285 }
Chet Haase9420abd2012-03-29 16:28:32 -070013286 } else if (alpha < 1) {
13287 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013288 }
13289 }
13290 }
13291
Chet Haasebcca79a2012-02-14 08:45:14 -080013292 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013293 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13294 * This draw() method is an implementation detail and is not intended to be overridden or
13295 * to be called from anywhere else other than ViewGroup.drawChild().
13296 */
13297 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013298 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013299 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013300 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013301 final int flags = parent.mGroupFlags;
13302
Chet Haasea1cff502012-02-21 13:43:44 -080013303 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013304 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013305 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013306 }
13307
13308 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013309 boolean concatMatrix = false;
13310
13311 boolean scalingRequired = false;
13312 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013313 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013314
13315 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013316 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13317 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013318 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013319 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013320 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13321 } else {
13322 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13323 }
13324
Chet Haasebcca79a2012-02-14 08:45:14 -080013325 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013326 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013327 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013328 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013329 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013330 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013331 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013332 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013333 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013334 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013335 mDisplayList != null) {
13336 // No longer animating: clear out old animation matrix
13337 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013338 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013339 }
13340 if (!useDisplayListProperties &&
13341 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13342 final boolean hasTransform =
13343 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13344 if (hasTransform) {
13345 final int transformType = parent.mChildTransformation.getTransformationType();
13346 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13347 parent.mChildTransformation : null;
13348 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13349 }
Chet Haase64a48c12012-02-13 16:33:29 -080013350 }
13351 }
13352
13353 concatMatrix |= !childHasIdentityMatrix;
13354
13355 // Sets the flag as early as possible to allow draw() implementations
13356 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013357 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013358
Romain Guyfbb43212012-08-30 15:19:27 -070013359 if (!concatMatrix &&
13360 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13361 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013362 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013363 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13364 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013365 return more;
13366 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013367 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013368
13369 if (hardwareAccelerated) {
13370 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13371 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013372 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13373 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013374 }
13375
Chet Haase64a48c12012-02-13 16:33:29 -080013376 DisplayList displayList = null;
13377 Bitmap cache = null;
13378 boolean hasDisplayList = false;
13379 if (caching) {
13380 if (!hardwareAccelerated) {
13381 if (layerType != LAYER_TYPE_NONE) {
13382 layerType = LAYER_TYPE_SOFTWARE;
13383 buildDrawingCache(true);
13384 }
13385 cache = getDrawingCache(true);
13386 } else {
13387 switch (layerType) {
13388 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013389 if (useDisplayListProperties) {
13390 hasDisplayList = canHaveDisplayList();
13391 } else {
13392 buildDrawingCache(true);
13393 cache = getDrawingCache(true);
13394 }
Chet Haase64a48c12012-02-13 16:33:29 -080013395 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013396 case LAYER_TYPE_HARDWARE:
13397 if (useDisplayListProperties) {
13398 hasDisplayList = canHaveDisplayList();
13399 }
13400 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013401 case LAYER_TYPE_NONE:
13402 // Delay getting the display list until animation-driven alpha values are
13403 // set up and possibly passed on to the view
13404 hasDisplayList = canHaveDisplayList();
13405 break;
13406 }
13407 }
13408 }
Chet Haasea1cff502012-02-21 13:43:44 -080013409 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013410 if (useDisplayListProperties) {
13411 displayList = getDisplayList();
13412 if (!displayList.isValid()) {
13413 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13414 // to getDisplayList(), the display list will be marked invalid and we should not
13415 // try to use it again.
13416 displayList = null;
13417 hasDisplayList = false;
13418 useDisplayListProperties = false;
13419 }
13420 }
Chet Haase64a48c12012-02-13 16:33:29 -080013421
Chet Haase526057b2012-07-12 17:50:41 -070013422 int sx = 0;
13423 int sy = 0;
13424 if (!hasDisplayList) {
13425 computeScroll();
13426 sx = mScrollX;
13427 sy = mScrollY;
13428 }
13429
Chet Haase64a48c12012-02-13 16:33:29 -080013430 final boolean hasNoCache = cache == null || hasDisplayList;
13431 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13432 layerType != LAYER_TYPE_HARDWARE;
13433
Chet Haasea1cff502012-02-21 13:43:44 -080013434 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013435 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013436 restoreTo = canvas.save();
13437 }
Chet Haase64a48c12012-02-13 16:33:29 -080013438 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013439 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013440 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013441 if (!useDisplayListProperties) {
13442 canvas.translate(mLeft, mTop);
13443 }
Chet Haase64a48c12012-02-13 16:33:29 -080013444 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013445 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013446 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013447 restoreTo = canvas.save();
13448 }
Chet Haase64a48c12012-02-13 16:33:29 -080013449 // mAttachInfo cannot be null, otherwise scalingRequired == false
13450 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13451 canvas.scale(scale, scale);
13452 }
13453 }
13454
Chet Haasea1cff502012-02-21 13:43:44 -080013455 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013456 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013457 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013458 if (transformToApply != null || !childHasIdentityMatrix) {
13459 int transX = 0;
13460 int transY = 0;
13461
13462 if (offsetForScroll) {
13463 transX = -sx;
13464 transY = -sy;
13465 }
13466
13467 if (transformToApply != null) {
13468 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013469 if (useDisplayListProperties) {
13470 displayList.setAnimationMatrix(transformToApply.getMatrix());
13471 } else {
13472 // Undo the scroll translation, apply the transformation matrix,
13473 // then redo the scroll translate to get the correct result.
13474 canvas.translate(-transX, -transY);
13475 canvas.concat(transformToApply.getMatrix());
13476 canvas.translate(transX, transY);
13477 }
Chet Haasea1cff502012-02-21 13:43:44 -080013478 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013479 }
13480
13481 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013482 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013483 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013484 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013485 }
13486 }
13487
Chet Haasea1cff502012-02-21 13:43:44 -080013488 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013489 canvas.translate(-transX, -transY);
13490 canvas.concat(getMatrix());
13491 canvas.translate(transX, transY);
13492 }
13493 }
13494
Chet Haase21433372012-06-05 07:54:09 -070013495 // Deal with alpha if it is or used to be <1
13496 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013497 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013498 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013499 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013500 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013501 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013502 }
Chet Haasea1cff502012-02-21 13:43:44 -080013503 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013504 if (hasNoCache) {
13505 final int multipliedAlpha = (int) (255 * alpha);
13506 if (!onSetAlpha(multipliedAlpha)) {
13507 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013508 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013509 layerType != LAYER_TYPE_NONE) {
13510 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13511 }
Chet Haase9420abd2012-03-29 16:28:32 -070013512 if (useDisplayListProperties) {
13513 displayList.setAlpha(alpha * getAlpha());
13514 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013515 final int scrollX = hasDisplayList ? 0 : sx;
13516 final int scrollY = hasDisplayList ? 0 : sy;
13517 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13518 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013519 }
13520 } else {
13521 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013522 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013523 }
13524 }
13525 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013526 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013527 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013528 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013529 }
13530
Chet Haasea1cff502012-02-21 13:43:44 -080013531 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13532 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013533 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013534 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013535 } else {
13536 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013537 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013538 } else {
13539 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13540 }
13541 }
13542 }
13543
Chet Haase9420abd2012-03-29 16:28:32 -070013544 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013545 displayList = getDisplayList();
13546 if (!displayList.isValid()) {
13547 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13548 // to getDisplayList(), the display list will be marked invalid and we should not
13549 // try to use it again.
13550 displayList = null;
13551 hasDisplayList = false;
13552 }
13553 }
13554
13555 if (hasNoCache) {
13556 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013557 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013558 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013559 if (layer != null && layer.isValid()) {
13560 mLayerPaint.setAlpha((int) (alpha * 255));
13561 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13562 layerRendered = true;
13563 } else {
13564 final int scrollX = hasDisplayList ? 0 : sx;
13565 final int scrollY = hasDisplayList ? 0 : sy;
13566 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013567 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013568 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13569 }
13570 }
13571
13572 if (!layerRendered) {
13573 if (!hasDisplayList) {
13574 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013575 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13576 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013577 dispatchDraw(canvas);
13578 } else {
13579 draw(canvas);
13580 }
13581 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013582 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013583 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013584 }
13585 }
13586 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013587 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013588 Paint cachePaint;
13589
13590 if (layerType == LAYER_TYPE_NONE) {
13591 cachePaint = parent.mCachePaint;
13592 if (cachePaint == null) {
13593 cachePaint = new Paint();
13594 cachePaint.setDither(false);
13595 parent.mCachePaint = cachePaint;
13596 }
Chet Haase9420abd2012-03-29 16:28:32 -070013597 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013598 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013599 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13600 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013601 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013602 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013603 }
13604 } else {
13605 cachePaint = mLayerPaint;
13606 cachePaint.setAlpha((int) (alpha * 255));
13607 }
13608 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13609 }
13610
Chet Haasea1cff502012-02-21 13:43:44 -080013611 if (restoreTo >= 0) {
13612 canvas.restoreToCount(restoreTo);
13613 }
Chet Haase64a48c12012-02-13 16:33:29 -080013614
13615 if (a != null && !more) {
13616 if (!hardwareAccelerated && !a.getFillAfter()) {
13617 onSetAlpha(255);
13618 }
13619 parent.finishAnimatingView(this, a);
13620 }
13621
13622 if (more && hardwareAccelerated) {
13623 // invalidation is the trigger to recreate display lists, so if we're using
13624 // display lists to render, force an invalidate to allow the animation to
13625 // continue drawing another frame
13626 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013627 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013628 // alpha animations should cause the child to recreate its display list
13629 invalidate(true);
13630 }
13631 }
13632
13633 mRecreateDisplayList = false;
13634
13635 return more;
13636 }
13637
13638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013639 * Manually render this view (and all of its children) to the given Canvas.
13640 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013641 * called. When implementing a view, implement
13642 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13643 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013644 *
13645 * @param canvas The Canvas to which the View is rendered.
13646 */
13647 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013648 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013649 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013650 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013651 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013653 /*
13654 * Draw traversal performs several drawing steps which must be executed
13655 * in the appropriate order:
13656 *
13657 * 1. Draw the background
13658 * 2. If necessary, save the canvas' layers to prepare for fading
13659 * 3. Draw view's content
13660 * 4. Draw children
13661 * 5. If necessary, draw the fading edges and restore layers
13662 * 6. Draw decorations (scrollbars for instance)
13663 */
13664
13665 // Step 1, draw the background, if needed
13666 int saveCount;
13667
Romain Guy24443ea2009-05-11 11:56:30 -070013668 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013669 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013670 if (background != null) {
13671 final int scrollX = mScrollX;
13672 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013673
Romain Guy24443ea2009-05-11 11:56:30 -070013674 if (mBackgroundSizeChanged) {
13675 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13676 mBackgroundSizeChanged = false;
13677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678
Romain Guy24443ea2009-05-11 11:56:30 -070013679 if ((scrollX | scrollY) == 0) {
13680 background.draw(canvas);
13681 } else {
13682 canvas.translate(scrollX, scrollY);
13683 background.draw(canvas);
13684 canvas.translate(-scrollX, -scrollY);
13685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013686 }
13687 }
13688
13689 // skip step 2 & 5 if possible (common case)
13690 final int viewFlags = mViewFlags;
13691 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13692 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13693 if (!verticalEdges && !horizontalEdges) {
13694 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013695 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013696
13697 // Step 4, draw the children
13698 dispatchDraw(canvas);
13699
13700 // Step 6, draw decorations (scrollbars)
13701 onDrawScrollBars(canvas);
13702
13703 // we're done...
13704 return;
13705 }
13706
13707 /*
13708 * Here we do the full fledged routine...
13709 * (this is an uncommon case where speed matters less,
13710 * this is why we repeat some of the tests that have been
13711 * done above)
13712 */
13713
13714 boolean drawTop = false;
13715 boolean drawBottom = false;
13716 boolean drawLeft = false;
13717 boolean drawRight = false;
13718
13719 float topFadeStrength = 0.0f;
13720 float bottomFadeStrength = 0.0f;
13721 float leftFadeStrength = 0.0f;
13722 float rightFadeStrength = 0.0f;
13723
13724 // Step 2, save the canvas' layers
13725 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726
13727 final boolean offsetRequired = isPaddingOffsetRequired();
13728 if (offsetRequired) {
13729 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013730 }
13731
13732 int left = mScrollX + paddingLeft;
13733 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013734 int top = mScrollY + getFadeTop(offsetRequired);
13735 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736
13737 if (offsetRequired) {
13738 right += getRightPaddingOffset();
13739 bottom += getBottomPaddingOffset();
13740 }
13741
13742 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013743 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013744 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745
13746 // clip the fade length if top and bottom fades overlap
13747 // overlapping fades produce odd-looking artifacts
13748 if (verticalEdges && (top + length > bottom - length)) {
13749 length = (bottom - top) / 2;
13750 }
13751
13752 // also clip horizontal fades if necessary
13753 if (horizontalEdges && (left + length > right - length)) {
13754 length = (right - left) / 2;
13755 }
13756
13757 if (verticalEdges) {
13758 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013759 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013761 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013762 }
13763
13764 if (horizontalEdges) {
13765 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013766 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013767 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013768 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769 }
13770
13771 saveCount = canvas.getSaveCount();
13772
13773 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013774 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013775 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13776
13777 if (drawTop) {
13778 canvas.saveLayer(left, top, right, top + length, null, flags);
13779 }
13780
13781 if (drawBottom) {
13782 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13783 }
13784
13785 if (drawLeft) {
13786 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13787 }
13788
13789 if (drawRight) {
13790 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13791 }
13792 } else {
13793 scrollabilityCache.setFadeColor(solidColor);
13794 }
13795
13796 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013797 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013798
13799 // Step 4, draw the children
13800 dispatchDraw(canvas);
13801
13802 // Step 5, draw the fade effect and restore layers
13803 final Paint p = scrollabilityCache.paint;
13804 final Matrix matrix = scrollabilityCache.matrix;
13805 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013806
13807 if (drawTop) {
13808 matrix.setScale(1, fadeHeight * topFadeStrength);
13809 matrix.postTranslate(left, top);
13810 fade.setLocalMatrix(matrix);
13811 canvas.drawRect(left, top, right, top + length, p);
13812 }
13813
13814 if (drawBottom) {
13815 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13816 matrix.postRotate(180);
13817 matrix.postTranslate(left, bottom);
13818 fade.setLocalMatrix(matrix);
13819 canvas.drawRect(left, bottom - length, right, bottom, p);
13820 }
13821
13822 if (drawLeft) {
13823 matrix.setScale(1, fadeHeight * leftFadeStrength);
13824 matrix.postRotate(-90);
13825 matrix.postTranslate(left, top);
13826 fade.setLocalMatrix(matrix);
13827 canvas.drawRect(left, top, left + length, bottom, p);
13828 }
13829
13830 if (drawRight) {
13831 matrix.setScale(1, fadeHeight * rightFadeStrength);
13832 matrix.postRotate(90);
13833 matrix.postTranslate(right, top);
13834 fade.setLocalMatrix(matrix);
13835 canvas.drawRect(right - length, top, right, bottom, p);
13836 }
13837
13838 canvas.restoreToCount(saveCount);
13839
13840 // Step 6, draw decorations (scrollbars)
13841 onDrawScrollBars(canvas);
13842 }
13843
13844 /**
13845 * Override this if your view is known to always be drawn on top of a solid color background,
13846 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13847 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13848 * should be set to 0xFF.
13849 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013850 * @see #setVerticalFadingEdgeEnabled(boolean)
13851 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013852 *
13853 * @return The known solid color background for this view, or 0 if the color may vary
13854 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013855 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013856 public int getSolidColor() {
13857 return 0;
13858 }
13859
13860 /**
13861 * Build a human readable string representation of the specified view flags.
13862 *
13863 * @param flags the view flags to convert to a string
13864 * @return a String representing the supplied flags
13865 */
13866 private static String printFlags(int flags) {
13867 String output = "";
13868 int numFlags = 0;
13869 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13870 output += "TAKES_FOCUS";
13871 numFlags++;
13872 }
13873
13874 switch (flags & VISIBILITY_MASK) {
13875 case INVISIBLE:
13876 if (numFlags > 0) {
13877 output += " ";
13878 }
13879 output += "INVISIBLE";
13880 // USELESS HERE numFlags++;
13881 break;
13882 case GONE:
13883 if (numFlags > 0) {
13884 output += " ";
13885 }
13886 output += "GONE";
13887 // USELESS HERE numFlags++;
13888 break;
13889 default:
13890 break;
13891 }
13892 return output;
13893 }
13894
13895 /**
13896 * Build a human readable string representation of the specified private
13897 * view flags.
13898 *
13899 * @param privateFlags the private view flags to convert to a string
13900 * @return a String representing the supplied flags
13901 */
13902 private static String printPrivateFlags(int privateFlags) {
13903 String output = "";
13904 int numFlags = 0;
13905
Dianne Hackborn4702a852012-08-17 15:18:29 -070013906 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907 output += "WANTS_FOCUS";
13908 numFlags++;
13909 }
13910
Dianne Hackborn4702a852012-08-17 15:18:29 -070013911 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013912 if (numFlags > 0) {
13913 output += " ";
13914 }
13915 output += "FOCUSED";
13916 numFlags++;
13917 }
13918
Dianne Hackborn4702a852012-08-17 15:18:29 -070013919 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013920 if (numFlags > 0) {
13921 output += " ";
13922 }
13923 output += "SELECTED";
13924 numFlags++;
13925 }
13926
Dianne Hackborn4702a852012-08-17 15:18:29 -070013927 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013928 if (numFlags > 0) {
13929 output += " ";
13930 }
13931 output += "IS_ROOT_NAMESPACE";
13932 numFlags++;
13933 }
13934
Dianne Hackborn4702a852012-08-17 15:18:29 -070013935 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 if (numFlags > 0) {
13937 output += " ";
13938 }
13939 output += "HAS_BOUNDS";
13940 numFlags++;
13941 }
13942
Dianne Hackborn4702a852012-08-17 15:18:29 -070013943 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 if (numFlags > 0) {
13945 output += " ";
13946 }
13947 output += "DRAWN";
13948 // USELESS HERE numFlags++;
13949 }
13950 return output;
13951 }
13952
13953 /**
13954 * <p>Indicates whether or not this view's layout will be requested during
13955 * the next hierarchy layout pass.</p>
13956 *
13957 * @return true if the layout will be forced during next layout pass
13958 */
13959 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013960 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 }
13962
13963 /**
13964 * Assign a size and position to a view and all of its
13965 * descendants
13966 *
13967 * <p>This is the second phase of the layout mechanism.
13968 * (The first is measuring). In this phase, each parent calls
13969 * layout on all of its children to position them.
13970 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013971 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 *
Chet Haase9c087442011-01-12 16:20:16 -080013973 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 * Derived classes with children should override
13975 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013976 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977 *
13978 * @param l Left position, relative to parent
13979 * @param t Top position, relative to parent
13980 * @param r Right position, relative to parent
13981 * @param b Bottom position, relative to parent
13982 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013983 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013984 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013985 int oldL = mLeft;
13986 int oldT = mTop;
13987 int oldB = mBottom;
13988 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013990 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013991 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013992 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013993
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013994 ListenerInfo li = mListenerInfo;
13995 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013996 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013997 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013998 int numListeners = listenersCopy.size();
13999 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014000 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014001 }
14002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014003 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014004 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014005 }
14006
14007 /**
14008 * Called from layout when this view should
14009 * assign a size and position to each of its children.
14010 *
14011 * Derived classes with children should override
14012 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014013 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 * @param changed This is a new size or position for this view
14015 * @param left Left position, relative to parent
14016 * @param top Top position, relative to parent
14017 * @param right Right position, relative to parent
14018 * @param bottom Bottom position, relative to parent
14019 */
14020 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14021 }
14022
14023 /**
14024 * Assign a size and position to this view.
14025 *
14026 * This is called from layout.
14027 *
14028 * @param left Left position, relative to parent
14029 * @param top Top position, relative to parent
14030 * @param right Right position, relative to parent
14031 * @param bottom Bottom position, relative to parent
14032 * @return true if the new size and position are different than the
14033 * previous ones
14034 * {@hide}
14035 */
14036 protected boolean setFrame(int left, int top, int right, int bottom) {
14037 boolean changed = false;
14038
14039 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014040 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014041 + right + "," + bottom + ")");
14042 }
14043
14044 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14045 changed = true;
14046
14047 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014048 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014050 int oldWidth = mRight - mLeft;
14051 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014052 int newWidth = right - left;
14053 int newHeight = bottom - top;
14054 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14055
14056 // Invalidate our old position
14057 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058
14059 mLeft = left;
14060 mTop = top;
14061 mRight = right;
14062 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014063 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014064 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014066
Dianne Hackborn4702a852012-08-17 15:18:29 -070014067 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069
Chet Haase75755e22011-07-18 17:48:25 -070014070 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014071 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014072 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014073 if (mTransformationInfo != null) {
14074 mTransformationInfo.mMatrixDirty = true;
14075 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014077 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14078 }
14079
14080 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14081 // If we are visible, force the DRAWN bit to on so that
14082 // this invalidate will go through (at least to our parent).
14083 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014084 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014086 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014087 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014088 // parent display list may need to be recreated based on a change in the bounds
14089 // of any child
14090 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 }
14092
14093 // Reset drawn bit to original value (invalidate turns it off)
14094 mPrivateFlags |= drawn;
14095
14096 mBackgroundSizeChanged = true;
14097 }
14098 return changed;
14099 }
14100
14101 /**
14102 * Finalize inflating a view from XML. This is called as the last phase
14103 * of inflation, after all child views have been added.
14104 *
14105 * <p>Even if the subclass overrides onFinishInflate, they should always be
14106 * sure to call the super method, so that we get called.
14107 */
14108 protected void onFinishInflate() {
14109 }
14110
14111 /**
14112 * Returns the resources associated with this view.
14113 *
14114 * @return Resources object.
14115 */
14116 public Resources getResources() {
14117 return mResources;
14118 }
14119
14120 /**
14121 * Invalidates the specified Drawable.
14122 *
14123 * @param drawable the drawable to invalidate
14124 */
14125 public void invalidateDrawable(Drawable drawable) {
14126 if (verifyDrawable(drawable)) {
14127 final Rect dirty = drawable.getBounds();
14128 final int scrollX = mScrollX;
14129 final int scrollY = mScrollY;
14130
14131 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14132 dirty.right + scrollX, dirty.bottom + scrollY);
14133 }
14134 }
14135
14136 /**
14137 * Schedules an action on a drawable to occur at a specified time.
14138 *
14139 * @param who the recipient of the action
14140 * @param what the action to run on the drawable
14141 * @param when the time at which the action must occur. Uses the
14142 * {@link SystemClock#uptimeMillis} timebase.
14143 */
14144 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014145 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014146 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014147 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014148 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14149 Choreographer.CALLBACK_ANIMATION, what, who,
14150 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014151 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014152 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014154 }
14155 }
14156
14157 /**
14158 * Cancels a scheduled action on a drawable.
14159 *
14160 * @param who the recipient of the action
14161 * @param what the action to cancel
14162 */
14163 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014164 if (verifyDrawable(who) && what != null) {
14165 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014166 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14167 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014168 } else {
14169 ViewRootImpl.getRunQueue().removeCallbacks(what);
14170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014171 }
14172 }
14173
14174 /**
14175 * Unschedule any events associated with the given Drawable. This can be
14176 * used when selecting a new Drawable into a view, so that the previous
14177 * one is completely unscheduled.
14178 *
14179 * @param who The Drawable to unschedule.
14180 *
14181 * @see #drawableStateChanged
14182 */
14183 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014184 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014185 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14186 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014187 }
14188 }
14189
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014190 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014191 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14192 * that the View directionality can and will be resolved before its Drawables.
14193 *
14194 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014195 *
14196 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014197 */
14198 public void resolveDrawables() {
14199 if (mBackground != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014200 mBackground.setLayoutDirection(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014201 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014202 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014203 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014204 }
14205
14206 /**
14207 * Called when layout direction has been resolved.
14208 *
14209 * The default implementation does nothing.
14210 *
14211 * @param layoutDirection The resolved layout direction.
14212 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014213 * @see #LAYOUT_DIRECTION_LTR
14214 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014215 *
14216 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014217 */
14218 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014219 }
14220
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014221 private void resetResolvedDrawables() {
14222 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14223 }
14224
14225 private boolean isDrawablesResolved() {
14226 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14227 }
14228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014229 /**
14230 * If your view subclass is displaying its own Drawable objects, it should
14231 * override this function and return true for any Drawable it is
14232 * displaying. This allows animations for those drawables to be
14233 * scheduled.
14234 *
14235 * <p>Be sure to call through to the super class when overriding this
14236 * function.
14237 *
14238 * @param who The Drawable to verify. Return true if it is one you are
14239 * displaying, else return the result of calling through to the
14240 * super class.
14241 *
14242 * @return boolean If true than the Drawable is being displayed in the
14243 * view; else false and it is not allowed to animate.
14244 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014245 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14246 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 */
14248 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014249 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250 }
14251
14252 /**
14253 * This function is called whenever the state of the view changes in such
14254 * a way that it impacts the state of drawables being shown.
14255 *
14256 * <p>Be sure to call through to the superclass when overriding this
14257 * function.
14258 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014259 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014260 */
14261 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014262 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014263 if (d != null && d.isStateful()) {
14264 d.setState(getDrawableState());
14265 }
14266 }
14267
14268 /**
14269 * Call this to force a view to update its drawable state. This will cause
14270 * drawableStateChanged to be called on this view. Views that are interested
14271 * in the new state should call getDrawableState.
14272 *
14273 * @see #drawableStateChanged
14274 * @see #getDrawableState
14275 */
14276 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014277 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014278 drawableStateChanged();
14279
14280 ViewParent parent = mParent;
14281 if (parent != null) {
14282 parent.childDrawableStateChanged(this);
14283 }
14284 }
14285
14286 /**
14287 * Return an array of resource IDs of the drawable states representing the
14288 * current state of the view.
14289 *
14290 * @return The current drawable state
14291 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014292 * @see Drawable#setState(int[])
14293 * @see #drawableStateChanged()
14294 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 */
14296 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014297 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014298 return mDrawableState;
14299 } else {
14300 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014301 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014302 return mDrawableState;
14303 }
14304 }
14305
14306 /**
14307 * Generate the new {@link android.graphics.drawable.Drawable} state for
14308 * this view. This is called by the view
14309 * system when the cached Drawable state is determined to be invalid. To
14310 * retrieve the current state, you should use {@link #getDrawableState}.
14311 *
14312 * @param extraSpace if non-zero, this is the number of extra entries you
14313 * would like in the returned array in which you can place your own
14314 * states.
14315 *
14316 * @return Returns an array holding the current {@link Drawable} state of
14317 * the view.
14318 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014319 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014320 */
14321 protected int[] onCreateDrawableState(int extraSpace) {
14322 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14323 mParent instanceof View) {
14324 return ((View) mParent).onCreateDrawableState(extraSpace);
14325 }
14326
14327 int[] drawableState;
14328
14329 int privateFlags = mPrivateFlags;
14330
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014331 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014332 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014333 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14334 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014335 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014336 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014337 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014338 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14339 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014340 // This is set if HW acceleration is requested, even if the current
14341 // process doesn't allow it. This is just to allow app preview
14342 // windows to better match their app.
14343 viewStateIndex |= VIEW_STATE_ACCELERATED;
14344 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014345 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014346
Christopher Tate3d4bf172011-03-28 16:16:46 -070014347 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014348 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14349 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351 drawableState = VIEW_STATE_SETS[viewStateIndex];
14352
14353 //noinspection ConstantIfStatement
14354 if (false) {
14355 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14356 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014357 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014358 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14359 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014360 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014361 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014362 + ": " + Arrays.toString(drawableState));
14363 }
14364
14365 if (extraSpace == 0) {
14366 return drawableState;
14367 }
14368
14369 final int[] fullState;
14370 if (drawableState != null) {
14371 fullState = new int[drawableState.length + extraSpace];
14372 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14373 } else {
14374 fullState = new int[extraSpace];
14375 }
14376
14377 return fullState;
14378 }
14379
14380 /**
14381 * Merge your own state values in <var>additionalState</var> into the base
14382 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014383 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014384 *
14385 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014386 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014387 * own additional state values.
14388 *
14389 * @param additionalState The additional state values you would like
14390 * added to <var>baseState</var>; this array is not modified.
14391 *
14392 * @return As a convenience, the <var>baseState</var> array you originally
14393 * passed into the function is returned.
14394 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014395 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014396 */
14397 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14398 final int N = baseState.length;
14399 int i = N - 1;
14400 while (i >= 0 && baseState[i] == 0) {
14401 i--;
14402 }
14403 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14404 return baseState;
14405 }
14406
14407 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014408 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14409 * on all Drawable objects associated with this view.
14410 */
14411 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014412 if (mBackground != null) {
14413 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014414 }
14415 }
14416
14417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014418 * Sets the background color for this view.
14419 * @param color the color of the background
14420 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014421 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014423 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014424 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014425 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014426 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014427 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014429 }
14430
14431 /**
14432 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014433 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014434 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014435 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014436 * @attr ref android.R.styleable#View_background
14437 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014438 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014439 public void setBackgroundResource(int resid) {
14440 if (resid != 0 && resid == mBackgroundResource) {
14441 return;
14442 }
14443
14444 Drawable d= null;
14445 if (resid != 0) {
14446 d = mResources.getDrawable(resid);
14447 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014448 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014449
14450 mBackgroundResource = resid;
14451 }
14452
14453 /**
14454 * Set the background to a given Drawable, or remove the background. If the
14455 * background has padding, this View's padding is set to the background's
14456 * padding. However, when a background is removed, this View's padding isn't
14457 * touched. If setting the padding is desired, please use
14458 * {@link #setPadding(int, int, int, int)}.
14459 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014460 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014461 * background
14462 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014463 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014464 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014465 setBackgroundDrawable(background);
14466 }
14467
14468 /**
14469 * @deprecated use {@link #setBackground(Drawable)} instead
14470 */
14471 @Deprecated
14472 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014473 computeOpaqueFlags();
14474
Philip Milne6c8ea062012-04-03 17:38:43 -070014475 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014476 return;
14477 }
14478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014479 boolean requestLayout = false;
14480
14481 mBackgroundResource = 0;
14482
14483 /*
14484 * Regardless of whether we're setting a new background or not, we want
14485 * to clear the previous drawable.
14486 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014487 if (mBackground != null) {
14488 mBackground.setCallback(null);
14489 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014490 }
14491
Philip Milne6c8ea062012-04-03 17:38:43 -070014492 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014493 Rect padding = sThreadLocal.get();
14494 if (padding == null) {
14495 padding = new Rect();
14496 sThreadLocal.set(padding);
14497 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014498 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014499 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014500 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014501 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014502 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014503 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014504 mUserPaddingLeftInitial = padding.right;
14505 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014506 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014507 break;
14508 case LAYOUT_DIRECTION_LTR:
14509 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014510 mUserPaddingLeftInitial = padding.left;
14511 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014512 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014514 }
14515
14516 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14517 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014518 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14519 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014520 requestLayout = true;
14521 }
14522
Philip Milne6c8ea062012-04-03 17:38:43 -070014523 background.setCallback(this);
14524 if (background.isStateful()) {
14525 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014527 background.setVisible(getVisibility() == VISIBLE, false);
14528 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014529
Dianne Hackborn4702a852012-08-17 15:18:29 -070014530 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14531 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14532 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 requestLayout = true;
14534 }
14535 } else {
14536 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014537 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014538
Dianne Hackborn4702a852012-08-17 15:18:29 -070014539 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540 /*
14541 * This view ONLY drew the background before and we're removing
14542 * the background, so now it won't draw anything
14543 * (hence we SKIP_DRAW)
14544 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014545 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14546 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014547 }
14548
14549 /*
14550 * When the background is set, we try to apply its padding to this
14551 * View. When the background is removed, we don't touch this View's
14552 * padding. This is noted in the Javadocs. Hence, we don't need to
14553 * requestLayout(), the invalidate() below is sufficient.
14554 */
14555
14556 // The old background's minimum size could have affected this
14557 // View's layout, so let's requestLayout
14558 requestLayout = true;
14559 }
14560
Romain Guy8f1344f52009-05-15 16:03:59 -070014561 computeOpaqueFlags();
14562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014563 if (requestLayout) {
14564 requestLayout();
14565 }
14566
14567 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014568 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 }
14570
14571 /**
14572 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014574 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014575 *
14576 * @see #setBackground(Drawable)
14577 *
14578 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014579 */
14580 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014581 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014582 }
14583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014584 /**
14585 * Sets the padding. The view may add on the space required to display
14586 * the scrollbars, depending on the style and visibility of the scrollbars.
14587 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14588 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14589 * from the values set in this call.
14590 *
14591 * @attr ref android.R.styleable#View_padding
14592 * @attr ref android.R.styleable#View_paddingBottom
14593 * @attr ref android.R.styleable#View_paddingLeft
14594 * @attr ref android.R.styleable#View_paddingRight
14595 * @attr ref android.R.styleable#View_paddingTop
14596 * @param left the left padding in pixels
14597 * @param top the top padding in pixels
14598 * @param right the right padding in pixels
14599 * @param bottom the bottom padding in pixels
14600 */
14601 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014602 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014603
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014604 mUserPaddingStart = UNDEFINED_PADDING;
14605 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014606
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014607 mUserPaddingLeftInitial = left;
14608 mUserPaddingRightInitial = right;
14609
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014610 internalSetPadding(left, top, right, bottom);
14611 }
14612
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014613 /**
14614 * @hide
14615 */
14616 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014617 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014618 mUserPaddingRight = right;
14619 mUserPaddingBottom = bottom;
14620
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014621 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014622 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014623
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014624 // Common case is there are no scroll bars.
14625 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014626 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014627 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014628 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014629 switch (mVerticalScrollbarPosition) {
14630 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014631 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014632 left += offset;
14633 } else {
14634 right += offset;
14635 }
14636 break;
Adam Powell20232d02010-12-08 21:08:53 -080014637 case SCROLLBAR_POSITION_RIGHT:
14638 right += offset;
14639 break;
14640 case SCROLLBAR_POSITION_LEFT:
14641 left += offset;
14642 break;
14643 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014644 }
Adam Powell20232d02010-12-08 21:08:53 -080014645 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014646 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14647 ? 0 : getHorizontalScrollbarHeight();
14648 }
14649 }
Romain Guy8506ab42009-06-11 17:35:47 -070014650
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014651 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014652 changed = true;
14653 mPaddingLeft = left;
14654 }
14655 if (mPaddingTop != top) {
14656 changed = true;
14657 mPaddingTop = top;
14658 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014659 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014660 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014661 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014662 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014663 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014664 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014665 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014666 }
14667
14668 if (changed) {
14669 requestLayout();
14670 }
14671 }
14672
14673 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014674 * Sets the relative padding. The view may add on the space required to display
14675 * the scrollbars, depending on the style and visibility of the scrollbars.
14676 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14677 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14678 * from the values set in this call.
14679 *
14680 * @attr ref android.R.styleable#View_padding
14681 * @attr ref android.R.styleable#View_paddingBottom
14682 * @attr ref android.R.styleable#View_paddingStart
14683 * @attr ref android.R.styleable#View_paddingEnd
14684 * @attr ref android.R.styleable#View_paddingTop
14685 * @param start the start padding in pixels
14686 * @param top the top padding in pixels
14687 * @param end the end padding in pixels
14688 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014689 */
14690 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014691 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014692
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014693 mUserPaddingStart = start;
14694 mUserPaddingEnd = end;
14695
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014696 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014697 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014698 mUserPaddingLeftInitial = end;
14699 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014700 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014701 break;
14702 case LAYOUT_DIRECTION_LTR:
14703 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014704 mUserPaddingLeftInitial = start;
14705 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014706 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014707 }
14708 }
14709
14710 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014711 * Returns the top padding of this view.
14712 *
14713 * @return the top padding in pixels
14714 */
14715 public int getPaddingTop() {
14716 return mPaddingTop;
14717 }
14718
14719 /**
14720 * Returns the bottom padding of this view. If there are inset and enabled
14721 * scrollbars, this value may include the space required to display the
14722 * scrollbars as well.
14723 *
14724 * @return the bottom padding in pixels
14725 */
14726 public int getPaddingBottom() {
14727 return mPaddingBottom;
14728 }
14729
14730 /**
14731 * Returns the left padding of this view. If there are inset and enabled
14732 * scrollbars, this value may include the space required to display the
14733 * scrollbars as well.
14734 *
14735 * @return the left padding in pixels
14736 */
14737 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014738 if (!isPaddingResolved()) {
14739 resolvePadding();
14740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014741 return mPaddingLeft;
14742 }
14743
14744 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014745 * Returns the start padding of this view depending on its resolved layout direction.
14746 * If there are inset and enabled scrollbars, this value may include the space
14747 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014748 *
14749 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014750 */
14751 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014752 if (!isPaddingResolved()) {
14753 resolvePadding();
14754 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014755 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014756 mPaddingRight : mPaddingLeft;
14757 }
14758
14759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014760 * Returns the right padding of this view. If there are inset and enabled
14761 * scrollbars, this value may include the space required to display the
14762 * scrollbars as well.
14763 *
14764 * @return the right padding in pixels
14765 */
14766 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014767 if (!isPaddingResolved()) {
14768 resolvePadding();
14769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014770 return mPaddingRight;
14771 }
14772
14773 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014774 * Returns the end padding of this view depending on its resolved layout direction.
14775 * If there are inset and enabled scrollbars, this value may include the space
14776 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014777 *
14778 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014779 */
14780 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014781 if (!isPaddingResolved()) {
14782 resolvePadding();
14783 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014784 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014785 mPaddingLeft : mPaddingRight;
14786 }
14787
14788 /**
14789 * Return if the padding as been set thru relative values
14790 * {@link #setPaddingRelative(int, int, int, int)} or thru
14791 * @attr ref android.R.styleable#View_paddingStart or
14792 * @attr ref android.R.styleable#View_paddingEnd
14793 *
14794 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014795 */
14796 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014797 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014798 }
14799
14800 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014801 * @hide
14802 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014803 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014804 if (isRtlCompatibilityMode()) {
14805 mPaddingLeft = mUserPaddingLeftInitial;
14806 mPaddingRight = mUserPaddingRightInitial;
14807 } else {
14808 if (isLayoutRtl()) {
14809 mPaddingLeft = mUserPaddingRightInitial;
14810 mPaddingRight = mUserPaddingLeftInitial;
14811 } else {
14812 mPaddingLeft = mUserPaddingLeftInitial;
14813 mPaddingRight = mUserPaddingRightInitial;
14814 }
14815 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014816 }
14817
14818 /**
14819 * @hide
14820 */
Philip Milne7a23b492012-04-24 22:12:36 -070014821 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014822 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014823 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014824 }
14825 return mLayoutInsets;
14826 }
14827
14828 /**
14829 * @hide
14830 */
14831 public void setLayoutInsets(Insets layoutInsets) {
14832 mLayoutInsets = layoutInsets;
14833 }
14834
14835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014836 * Changes the selection state of this view. A view can be selected or not.
14837 * Note that selection is not the same as focus. Views are typically
14838 * selected in the context of an AdapterView like ListView or GridView;
14839 * the selected view is the view that is highlighted.
14840 *
14841 * @param selected true if the view must be selected, false otherwise
14842 */
14843 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014844 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14845 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014846 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014847 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014848 refreshDrawableState();
14849 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014850 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14851 notifyAccessibilityStateChanged();
14852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014853 }
14854 }
14855
14856 /**
14857 * Dispatch setSelected to all of this View's children.
14858 *
14859 * @see #setSelected(boolean)
14860 *
14861 * @param selected The new selected state
14862 */
14863 protected void dispatchSetSelected(boolean selected) {
14864 }
14865
14866 /**
14867 * Indicates the selection state of this view.
14868 *
14869 * @return true if the view is selected, false otherwise
14870 */
14871 @ViewDebug.ExportedProperty
14872 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014873 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014874 }
14875
14876 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014877 * Changes the activated state of this view. A view can be activated or not.
14878 * Note that activation is not the same as selection. Selection is
14879 * a transient property, representing the view (hierarchy) the user is
14880 * currently interacting with. Activation is a longer-term state that the
14881 * user can move views in and out of. For example, in a list view with
14882 * single or multiple selection enabled, the views in the current selection
14883 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14884 * here.) The activated state is propagated down to children of the view it
14885 * is set on.
14886 *
14887 * @param activated true if the view must be activated, false otherwise
14888 */
14889 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014890 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14891 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014892 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014893 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014894 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014895 }
14896 }
14897
14898 /**
14899 * Dispatch setActivated to all of this View's children.
14900 *
14901 * @see #setActivated(boolean)
14902 *
14903 * @param activated The new activated state
14904 */
14905 protected void dispatchSetActivated(boolean activated) {
14906 }
14907
14908 /**
14909 * Indicates the activation state of this view.
14910 *
14911 * @return true if the view is activated, false otherwise
14912 */
14913 @ViewDebug.ExportedProperty
14914 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014915 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014916 }
14917
14918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014919 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14920 * observer can be used to get notifications when global events, like
14921 * layout, happen.
14922 *
14923 * The returned ViewTreeObserver observer is not guaranteed to remain
14924 * valid for the lifetime of this View. If the caller of this method keeps
14925 * a long-lived reference to ViewTreeObserver, it should always check for
14926 * the return value of {@link ViewTreeObserver#isAlive()}.
14927 *
14928 * @return The ViewTreeObserver for this view's hierarchy.
14929 */
14930 public ViewTreeObserver getViewTreeObserver() {
14931 if (mAttachInfo != null) {
14932 return mAttachInfo.mTreeObserver;
14933 }
14934 if (mFloatingTreeObserver == null) {
14935 mFloatingTreeObserver = new ViewTreeObserver();
14936 }
14937 return mFloatingTreeObserver;
14938 }
14939
14940 /**
14941 * <p>Finds the topmost view in the current view hierarchy.</p>
14942 *
14943 * @return the topmost view containing this view
14944 */
14945 public View getRootView() {
14946 if (mAttachInfo != null) {
14947 final View v = mAttachInfo.mRootView;
14948 if (v != null) {
14949 return v;
14950 }
14951 }
Romain Guy8506ab42009-06-11 17:35:47 -070014952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014953 View parent = this;
14954
14955 while (parent.mParent != null && parent.mParent instanceof View) {
14956 parent = (View) parent.mParent;
14957 }
14958
14959 return parent;
14960 }
14961
14962 /**
14963 * <p>Computes the coordinates of this view on the screen. The argument
14964 * must be an array of two integers. After the method returns, the array
14965 * contains the x and y location in that order.</p>
14966 *
14967 * @param location an array of two integers in which to hold the coordinates
14968 */
14969 public void getLocationOnScreen(int[] location) {
14970 getLocationInWindow(location);
14971
14972 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014973 if (info != null) {
14974 location[0] += info.mWindowLeft;
14975 location[1] += info.mWindowTop;
14976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014977 }
14978
14979 /**
14980 * <p>Computes the coordinates of this view in its window. The argument
14981 * must be an array of two integers. After the method returns, the array
14982 * contains the x and y location in that order.</p>
14983 *
14984 * @param location an array of two integers in which to hold the coordinates
14985 */
14986 public void getLocationInWindow(int[] location) {
14987 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014988 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014989 }
14990
Gilles Debunne6583ce52011-12-06 18:09:02 -080014991 if (mAttachInfo == null) {
14992 // When the view is not attached to a window, this method does not make sense
14993 location[0] = location[1] = 0;
14994 return;
14995 }
14996
Gilles Debunnecea45132011-11-24 02:19:27 +010014997 float[] position = mAttachInfo.mTmpTransformLocation;
14998 position[0] = position[1] = 0.0f;
14999
15000 if (!hasIdentityMatrix()) {
15001 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015003
Gilles Debunnecea45132011-11-24 02:19:27 +010015004 position[0] += mLeft;
15005 position[1] += mTop;
15006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015007 ViewParent viewParent = mParent;
15008 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015009 final View view = (View) viewParent;
15010
15011 position[0] -= view.mScrollX;
15012 position[1] -= view.mScrollY;
15013
15014 if (!view.hasIdentityMatrix()) {
15015 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015016 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015017
15018 position[0] += view.mLeft;
15019 position[1] += view.mTop;
15020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015021 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015022 }
Romain Guy8506ab42009-06-11 17:35:47 -070015023
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015024 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015026 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15027 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015028 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015029
15030 location[0] = (int) (position[0] + 0.5f);
15031 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015032 }
15033
15034 /**
15035 * {@hide}
15036 * @param id the id of the view to be found
15037 * @return the view of the specified id, null if cannot be found
15038 */
15039 protected View findViewTraversal(int id) {
15040 if (id == mID) {
15041 return this;
15042 }
15043 return null;
15044 }
15045
15046 /**
15047 * {@hide}
15048 * @param tag the tag of the view to be found
15049 * @return the view of specified tag, null if cannot be found
15050 */
15051 protected View findViewWithTagTraversal(Object tag) {
15052 if (tag != null && tag.equals(mTag)) {
15053 return this;
15054 }
15055 return null;
15056 }
15057
15058 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015059 * {@hide}
15060 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015061 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015062 * @return The first view that matches the predicate or null.
15063 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015064 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015065 if (predicate.apply(this)) {
15066 return this;
15067 }
15068 return null;
15069 }
15070
15071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015072 * Look for a child view with the given id. If this view has the given
15073 * id, return this view.
15074 *
15075 * @param id The id to search for.
15076 * @return The view that has the given id in the hierarchy or null
15077 */
15078 public final View findViewById(int id) {
15079 if (id < 0) {
15080 return null;
15081 }
15082 return findViewTraversal(id);
15083 }
15084
15085 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015086 * Finds a view by its unuque and stable accessibility id.
15087 *
15088 * @param accessibilityId The searched accessibility id.
15089 * @return The found view.
15090 */
15091 final View findViewByAccessibilityId(int accessibilityId) {
15092 if (accessibilityId < 0) {
15093 return null;
15094 }
15095 return findViewByAccessibilityIdTraversal(accessibilityId);
15096 }
15097
15098 /**
15099 * Performs the traversal to find a view by its unuque and stable accessibility id.
15100 *
15101 * <strong>Note:</strong>This method does not stop at the root namespace
15102 * boundary since the user can touch the screen at an arbitrary location
15103 * potentially crossing the root namespace bounday which will send an
15104 * accessibility event to accessibility services and they should be able
15105 * to obtain the event source. Also accessibility ids are guaranteed to be
15106 * unique in the window.
15107 *
15108 * @param accessibilityId The accessibility id.
15109 * @return The found view.
15110 */
15111 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15112 if (getAccessibilityViewId() == accessibilityId) {
15113 return this;
15114 }
15115 return null;
15116 }
15117
15118 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015119 * Look for a child view with the given tag. If this view has the given
15120 * tag, return this view.
15121 *
15122 * @param tag The tag to search for, using "tag.equals(getTag())".
15123 * @return The View that has the given tag in the hierarchy or null
15124 */
15125 public final View findViewWithTag(Object tag) {
15126 if (tag == null) {
15127 return null;
15128 }
15129 return findViewWithTagTraversal(tag);
15130 }
15131
15132 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015133 * {@hide}
15134 * Look for a child view that matches the specified predicate.
15135 * If this view matches the predicate, return this view.
15136 *
15137 * @param predicate The predicate to evaluate.
15138 * @return The first view that matches the predicate or null.
15139 */
15140 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015141 return findViewByPredicateTraversal(predicate, null);
15142 }
15143
15144 /**
15145 * {@hide}
15146 * Look for a child view that matches the specified predicate,
15147 * starting with the specified view and its descendents and then
15148 * recusively searching the ancestors and siblings of that view
15149 * until this view is reached.
15150 *
15151 * This method is useful in cases where the predicate does not match
15152 * a single unique view (perhaps multiple views use the same id)
15153 * and we are trying to find the view that is "closest" in scope to the
15154 * starting view.
15155 *
15156 * @param start The view to start from.
15157 * @param predicate The predicate to evaluate.
15158 * @return The first view that matches the predicate or null.
15159 */
15160 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15161 View childToSkip = null;
15162 for (;;) {
15163 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15164 if (view != null || start == this) {
15165 return view;
15166 }
15167
15168 ViewParent parent = start.getParent();
15169 if (parent == null || !(parent instanceof View)) {
15170 return null;
15171 }
15172
15173 childToSkip = start;
15174 start = (View) parent;
15175 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015176 }
15177
15178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015179 * Sets the identifier for this view. The identifier does not have to be
15180 * unique in this view's hierarchy. The identifier should be a positive
15181 * number.
15182 *
15183 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015184 * @see #getId()
15185 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015186 *
15187 * @param id a number used to identify the view
15188 *
15189 * @attr ref android.R.styleable#View_id
15190 */
15191 public void setId(int id) {
15192 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015193 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15194 mID = generateViewId();
15195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015196 }
15197
15198 /**
15199 * {@hide}
15200 *
15201 * @param isRoot true if the view belongs to the root namespace, false
15202 * otherwise
15203 */
15204 public void setIsRootNamespace(boolean isRoot) {
15205 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015206 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015207 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015208 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015209 }
15210 }
15211
15212 /**
15213 * {@hide}
15214 *
15215 * @return true if the view belongs to the root namespace, false otherwise
15216 */
15217 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015218 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015219 }
15220
15221 /**
15222 * Returns this view's identifier.
15223 *
15224 * @return a positive integer used to identify the view or {@link #NO_ID}
15225 * if the view has no ID
15226 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015227 * @see #setId(int)
15228 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015229 * @attr ref android.R.styleable#View_id
15230 */
15231 @ViewDebug.CapturedViewProperty
15232 public int getId() {
15233 return mID;
15234 }
15235
15236 /**
15237 * Returns this view's tag.
15238 *
15239 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015240 *
15241 * @see #setTag(Object)
15242 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015243 */
15244 @ViewDebug.ExportedProperty
15245 public Object getTag() {
15246 return mTag;
15247 }
15248
15249 /**
15250 * Sets the tag associated with this view. A tag can be used to mark
15251 * a view in its hierarchy and does not have to be unique within the
15252 * hierarchy. Tags can also be used to store data within a view without
15253 * resorting to another data structure.
15254 *
15255 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015256 *
15257 * @see #getTag()
15258 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015259 */
15260 public void setTag(final Object tag) {
15261 mTag = tag;
15262 }
15263
15264 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015265 * Returns the tag associated with this view and the specified key.
15266 *
15267 * @param key The key identifying the tag
15268 *
15269 * @return the Object stored in this view as a tag
15270 *
15271 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015272 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015273 */
15274 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015275 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015276 return null;
15277 }
15278
15279 /**
15280 * Sets a tag associated with this view and a key. A tag can be used
15281 * to mark a view in its hierarchy and does not have to be unique within
15282 * the hierarchy. Tags can also be used to store data within a view
15283 * without resorting to another data structure.
15284 *
15285 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015286 * application to ensure it is unique (see the <a
15287 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15288 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015289 * the Android framework or not associated with any package will cause
15290 * an {@link IllegalArgumentException} to be thrown.
15291 *
15292 * @param key The key identifying the tag
15293 * @param tag An Object to tag the view with
15294 *
15295 * @throws IllegalArgumentException If they specified key is not valid
15296 *
15297 * @see #setTag(Object)
15298 * @see #getTag(int)
15299 */
15300 public void setTag(int key, final Object tag) {
15301 // If the package id is 0x00 or 0x01, it's either an undefined package
15302 // or a framework id
15303 if ((key >>> 24) < 2) {
15304 throw new IllegalArgumentException("The key must be an application-specific "
15305 + "resource id.");
15306 }
15307
Adam Powell2b2f6d62011-09-23 11:15:39 -070015308 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015309 }
15310
15311 /**
15312 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15313 * framework id.
15314 *
15315 * @hide
15316 */
15317 public void setTagInternal(int key, Object tag) {
15318 if ((key >>> 24) != 0x1) {
15319 throw new IllegalArgumentException("The key must be a framework-specific "
15320 + "resource id.");
15321 }
15322
Adam Powell2b2f6d62011-09-23 11:15:39 -070015323 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015324 }
15325
Adam Powell2b2f6d62011-09-23 11:15:39 -070015326 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015327 if (mKeyedTags == null) {
15328 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015329 }
15330
Adam Powell7db82ac2011-09-22 19:44:04 -070015331 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015332 }
15333
15334 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015335 * Prints information about this view in the log output, with the tag
15336 * {@link #VIEW_LOG_TAG}.
15337 *
15338 * @hide
15339 */
15340 public void debug() {
15341 debug(0);
15342 }
15343
15344 /**
15345 * Prints information about this view in the log output, with the tag
15346 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15347 * indentation defined by the <code>depth</code>.
15348 *
15349 * @param depth the indentation level
15350 *
15351 * @hide
15352 */
15353 protected void debug(int depth) {
15354 String output = debugIndent(depth - 1);
15355
15356 output += "+ " + this;
15357 int id = getId();
15358 if (id != -1) {
15359 output += " (id=" + id + ")";
15360 }
15361 Object tag = getTag();
15362 if (tag != null) {
15363 output += " (tag=" + tag + ")";
15364 }
15365 Log.d(VIEW_LOG_TAG, output);
15366
Dianne Hackborn4702a852012-08-17 15:18:29 -070015367 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015368 output = debugIndent(depth) + " FOCUSED";
15369 Log.d(VIEW_LOG_TAG, output);
15370 }
15371
15372 output = debugIndent(depth);
15373 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15374 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15375 + "} ";
15376 Log.d(VIEW_LOG_TAG, output);
15377
15378 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15379 || mPaddingBottom != 0) {
15380 output = debugIndent(depth);
15381 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15382 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15383 Log.d(VIEW_LOG_TAG, output);
15384 }
15385
15386 output = debugIndent(depth);
15387 output += "mMeasureWidth=" + mMeasuredWidth +
15388 " mMeasureHeight=" + mMeasuredHeight;
15389 Log.d(VIEW_LOG_TAG, output);
15390
15391 output = debugIndent(depth);
15392 if (mLayoutParams == null) {
15393 output += "BAD! no layout params";
15394 } else {
15395 output = mLayoutParams.debug(output);
15396 }
15397 Log.d(VIEW_LOG_TAG, output);
15398
15399 output = debugIndent(depth);
15400 output += "flags={";
15401 output += View.printFlags(mViewFlags);
15402 output += "}";
15403 Log.d(VIEW_LOG_TAG, output);
15404
15405 output = debugIndent(depth);
15406 output += "privateFlags={";
15407 output += View.printPrivateFlags(mPrivateFlags);
15408 output += "}";
15409 Log.d(VIEW_LOG_TAG, output);
15410 }
15411
15412 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015413 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015414 *
15415 * @param depth the indentation level
15416 * @return a String containing (depth * 2 + 3) * 2 white spaces
15417 *
15418 * @hide
15419 */
15420 protected static String debugIndent(int depth) {
15421 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15422 for (int i = 0; i < (depth * 2) + 3; i++) {
15423 spaces.append(' ').append(' ');
15424 }
15425 return spaces.toString();
15426 }
15427
15428 /**
15429 * <p>Return the offset of the widget's text baseline from the widget's top
15430 * boundary. If this widget does not support baseline alignment, this
15431 * method returns -1. </p>
15432 *
15433 * @return the offset of the baseline within the widget's bounds or -1
15434 * if baseline alignment is not supported
15435 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015436 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015437 public int getBaseline() {
15438 return -1;
15439 }
15440
15441 /**
15442 * Call this when something has changed which has invalidated the
15443 * layout of this view. This will schedule a layout pass of the view
15444 * tree.
15445 */
15446 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015447 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15448 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015449
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015450 if (mParent != null && !mParent.isLayoutRequested()) {
15451 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015452 }
15453 }
15454
15455 /**
15456 * Forces this view to be laid out during the next layout pass.
15457 * This method does not call requestLayout() or forceLayout()
15458 * on the parent.
15459 */
15460 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015461 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15462 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015463 }
15464
15465 /**
15466 * <p>
15467 * This is called to find out how big a view should be. The parent
15468 * supplies constraint information in the width and height parameters.
15469 * </p>
15470 *
15471 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015472 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015473 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015474 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015475 * </p>
15476 *
15477 *
15478 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15479 * parent
15480 * @param heightMeasureSpec Vertical space requirements as imposed by the
15481 * parent
15482 *
15483 * @see #onMeasure(int, int)
15484 */
15485 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015486 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015487 widthMeasureSpec != mOldWidthMeasureSpec ||
15488 heightMeasureSpec != mOldHeightMeasureSpec) {
15489
15490 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015491 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015492
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015493 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015495 // measure ourselves, this should set the measured dimension flag back
15496 onMeasure(widthMeasureSpec, heightMeasureSpec);
15497
15498 // flag not set, setMeasuredDimension() was not invoked, we raise
15499 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015500 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015501 throw new IllegalStateException("onMeasure() did not set the"
15502 + " measured dimension by calling"
15503 + " setMeasuredDimension()");
15504 }
15505
Dianne Hackborn4702a852012-08-17 15:18:29 -070015506 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015507 }
15508
15509 mOldWidthMeasureSpec = widthMeasureSpec;
15510 mOldHeightMeasureSpec = heightMeasureSpec;
15511 }
15512
15513 /**
15514 * <p>
15515 * Measure the view and its content to determine the measured width and the
15516 * measured height. This method is invoked by {@link #measure(int, int)} and
15517 * should be overriden by subclasses to provide accurate and efficient
15518 * measurement of their contents.
15519 * </p>
15520 *
15521 * <p>
15522 * <strong>CONTRACT:</strong> When overriding this method, you
15523 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15524 * measured width and height of this view. Failure to do so will trigger an
15525 * <code>IllegalStateException</code>, thrown by
15526 * {@link #measure(int, int)}. Calling the superclass'
15527 * {@link #onMeasure(int, int)} is a valid use.
15528 * </p>
15529 *
15530 * <p>
15531 * The base class implementation of measure defaults to the background size,
15532 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15533 * override {@link #onMeasure(int, int)} to provide better measurements of
15534 * their content.
15535 * </p>
15536 *
15537 * <p>
15538 * If this method is overridden, it is the subclass's responsibility to make
15539 * sure the measured height and width are at least the view's minimum height
15540 * and width ({@link #getSuggestedMinimumHeight()} and
15541 * {@link #getSuggestedMinimumWidth()}).
15542 * </p>
15543 *
15544 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15545 * The requirements are encoded with
15546 * {@link android.view.View.MeasureSpec}.
15547 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15548 * The requirements are encoded with
15549 * {@link android.view.View.MeasureSpec}.
15550 *
15551 * @see #getMeasuredWidth()
15552 * @see #getMeasuredHeight()
15553 * @see #setMeasuredDimension(int, int)
15554 * @see #getSuggestedMinimumHeight()
15555 * @see #getSuggestedMinimumWidth()
15556 * @see android.view.View.MeasureSpec#getMode(int)
15557 * @see android.view.View.MeasureSpec#getSize(int)
15558 */
15559 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15560 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15561 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15562 }
15563
15564 /**
15565 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15566 * measured width and measured height. Failing to do so will trigger an
15567 * exception at measurement time.</p>
15568 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015569 * @param measuredWidth The measured width of this view. May be a complex
15570 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15571 * {@link #MEASURED_STATE_TOO_SMALL}.
15572 * @param measuredHeight The measured height of this view. May be a complex
15573 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15574 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015575 */
15576 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15577 mMeasuredWidth = measuredWidth;
15578 mMeasuredHeight = measuredHeight;
15579
Dianne Hackborn4702a852012-08-17 15:18:29 -070015580 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015581 }
15582
15583 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015584 * Merge two states as returned by {@link #getMeasuredState()}.
15585 * @param curState The current state as returned from a view or the result
15586 * of combining multiple views.
15587 * @param newState The new view state to combine.
15588 * @return Returns a new integer reflecting the combination of the two
15589 * states.
15590 */
15591 public static int combineMeasuredStates(int curState, int newState) {
15592 return curState | newState;
15593 }
15594
15595 /**
15596 * Version of {@link #resolveSizeAndState(int, int, int)}
15597 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15598 */
15599 public static int resolveSize(int size, int measureSpec) {
15600 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15601 }
15602
15603 /**
15604 * Utility to reconcile a desired size and state, with constraints imposed
15605 * by a MeasureSpec. Will take the desired size, unless a different size
15606 * is imposed by the constraints. The returned value is a compound integer,
15607 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15608 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15609 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015610 *
15611 * @param size How big the view wants to be
15612 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015613 * @return Size information bit mask as defined by
15614 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015615 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015616 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015617 int result = size;
15618 int specMode = MeasureSpec.getMode(measureSpec);
15619 int specSize = MeasureSpec.getSize(measureSpec);
15620 switch (specMode) {
15621 case MeasureSpec.UNSPECIFIED:
15622 result = size;
15623 break;
15624 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015625 if (specSize < size) {
15626 result = specSize | MEASURED_STATE_TOO_SMALL;
15627 } else {
15628 result = size;
15629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015630 break;
15631 case MeasureSpec.EXACTLY:
15632 result = specSize;
15633 break;
15634 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015635 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015636 }
15637
15638 /**
15639 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015640 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015641 * by the MeasureSpec.
15642 *
15643 * @param size Default size for this view
15644 * @param measureSpec Constraints imposed by the parent
15645 * @return The size this view should be.
15646 */
15647 public static int getDefaultSize(int size, int measureSpec) {
15648 int result = size;
15649 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015650 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015651
15652 switch (specMode) {
15653 case MeasureSpec.UNSPECIFIED:
15654 result = size;
15655 break;
15656 case MeasureSpec.AT_MOST:
15657 case MeasureSpec.EXACTLY:
15658 result = specSize;
15659 break;
15660 }
15661 return result;
15662 }
15663
15664 /**
15665 * Returns the suggested minimum height that the view should use. This
15666 * returns the maximum of the view's minimum height
15667 * and the background's minimum height
15668 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15669 * <p>
15670 * When being used in {@link #onMeasure(int, int)}, the caller should still
15671 * ensure the returned height is within the requirements of the parent.
15672 *
15673 * @return The suggested minimum height of the view.
15674 */
15675 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015676 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015678 }
15679
15680 /**
15681 * Returns the suggested minimum width that the view should use. This
15682 * returns the maximum of the view's minimum width)
15683 * and the background's minimum width
15684 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15685 * <p>
15686 * When being used in {@link #onMeasure(int, int)}, the caller should still
15687 * ensure the returned width is within the requirements of the parent.
15688 *
15689 * @return The suggested minimum width of the view.
15690 */
15691 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015692 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015694
Philip Milne6c8ea062012-04-03 17:38:43 -070015695 /**
15696 * Returns the minimum height of the view.
15697 *
15698 * @return the minimum height the view will try to be.
15699 *
15700 * @see #setMinimumHeight(int)
15701 *
15702 * @attr ref android.R.styleable#View_minHeight
15703 */
15704 public int getMinimumHeight() {
15705 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015706 }
15707
15708 /**
15709 * Sets the minimum height of the view. It is not guaranteed the view will
15710 * be able to achieve this minimum height (for example, if its parent layout
15711 * constrains it with less available height).
15712 *
15713 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015714 *
15715 * @see #getMinimumHeight()
15716 *
15717 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015718 */
15719 public void setMinimumHeight(int minHeight) {
15720 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015721 requestLayout();
15722 }
15723
15724 /**
15725 * Returns the minimum width of the view.
15726 *
15727 * @return the minimum width the view will try to be.
15728 *
15729 * @see #setMinimumWidth(int)
15730 *
15731 * @attr ref android.R.styleable#View_minWidth
15732 */
15733 public int getMinimumWidth() {
15734 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015735 }
15736
15737 /**
15738 * Sets the minimum width of the view. It is not guaranteed the view will
15739 * be able to achieve this minimum width (for example, if its parent layout
15740 * constrains it with less available width).
15741 *
15742 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015743 *
15744 * @see #getMinimumWidth()
15745 *
15746 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015747 */
15748 public void setMinimumWidth(int minWidth) {
15749 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015750 requestLayout();
15751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015752 }
15753
15754 /**
15755 * Get the animation currently associated with this view.
15756 *
15757 * @return The animation that is currently playing or
15758 * scheduled to play for this view.
15759 */
15760 public Animation getAnimation() {
15761 return mCurrentAnimation;
15762 }
15763
15764 /**
15765 * Start the specified animation now.
15766 *
15767 * @param animation the animation to start now
15768 */
15769 public void startAnimation(Animation animation) {
15770 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15771 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015772 invalidateParentCaches();
15773 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015774 }
15775
15776 /**
15777 * Cancels any animations for this view.
15778 */
15779 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015780 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015781 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015783 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015784 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015785 }
15786
15787 /**
15788 * Sets the next animation to play for this view.
15789 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015790 * {@link #startAnimation(android.view.animation.Animation)} instead.
15791 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015792 * control over the start time and invalidation, but you
15793 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015794 * 2) the view's parent (which controls animations on its children)
15795 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015796 * start.
15797 *
15798 * @param animation The next animation, or null.
15799 */
15800 public void setAnimation(Animation animation) {
15801 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015803 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015804 // If the screen is off assume the animation start time is now instead of
15805 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15806 // would cause the animation to start when the screen turns back on
15807 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15808 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15809 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015811 animation.reset();
15812 }
15813 }
15814
15815 /**
15816 * Invoked by a parent ViewGroup to notify the start of the animation
15817 * currently associated with this view. If you override this method,
15818 * always call super.onAnimationStart();
15819 *
15820 * @see #setAnimation(android.view.animation.Animation)
15821 * @see #getAnimation()
15822 */
15823 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015824 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015825 }
15826
15827 /**
15828 * Invoked by a parent ViewGroup to notify the end of the animation
15829 * currently associated with this view. If you override this method,
15830 * always call super.onAnimationEnd();
15831 *
15832 * @see #setAnimation(android.view.animation.Animation)
15833 * @see #getAnimation()
15834 */
15835 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015836 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015837 }
15838
15839 /**
15840 * Invoked if there is a Transform that involves alpha. Subclass that can
15841 * draw themselves with the specified alpha should return true, and then
15842 * respect that alpha when their onDraw() is called. If this returns false
15843 * then the view may be redirected to draw into an offscreen buffer to
15844 * fulfill the request, which will look fine, but may be slower than if the
15845 * subclass handles it internally. The default implementation returns false.
15846 *
15847 * @param alpha The alpha (0..255) to apply to the view's drawing
15848 * @return true if the view can draw with the specified alpha.
15849 */
15850 protected boolean onSetAlpha(int alpha) {
15851 return false;
15852 }
15853
15854 /**
15855 * This is used by the RootView to perform an optimization when
15856 * the view hierarchy contains one or several SurfaceView.
15857 * SurfaceView is always considered transparent, but its children are not,
15858 * therefore all View objects remove themselves from the global transparent
15859 * region (passed as a parameter to this function).
15860 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015861 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015862 *
15863 * @return Returns true if the effective visibility of the view at this
15864 * point is opaque, regardless of the transparent region; returns false
15865 * if it is possible for underlying windows to be seen behind the view.
15866 *
15867 * {@hide}
15868 */
15869 public boolean gatherTransparentRegion(Region region) {
15870 final AttachInfo attachInfo = mAttachInfo;
15871 if (region != null && attachInfo != null) {
15872 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015873 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015874 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15875 // remove it from the transparent region.
15876 final int[] location = attachInfo.mTransparentLocation;
15877 getLocationInWindow(location);
15878 region.op(location[0], location[1], location[0] + mRight - mLeft,
15879 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015880 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015881 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15882 // exists, so we remove the background drawable's non-transparent
15883 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015884 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015885 }
15886 }
15887 return true;
15888 }
15889
15890 /**
15891 * Play a sound effect for this view.
15892 *
15893 * <p>The framework will play sound effects for some built in actions, such as
15894 * clicking, but you may wish to play these effects in your widget,
15895 * for instance, for internal navigation.
15896 *
15897 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15898 * {@link #isSoundEffectsEnabled()} is true.
15899 *
15900 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15901 */
15902 public void playSoundEffect(int soundConstant) {
15903 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15904 return;
15905 }
15906 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15907 }
15908
15909 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015910 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015911 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015912 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015913 *
15914 * <p>The framework will provide haptic feedback for some built in actions,
15915 * such as long presses, but you may wish to provide feedback for your
15916 * own widget.
15917 *
15918 * <p>The feedback will only be performed if
15919 * {@link #isHapticFeedbackEnabled()} is true.
15920 *
15921 * @param feedbackConstant One of the constants defined in
15922 * {@link HapticFeedbackConstants}
15923 */
15924 public boolean performHapticFeedback(int feedbackConstant) {
15925 return performHapticFeedback(feedbackConstant, 0);
15926 }
15927
15928 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015929 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015930 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015931 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015932 *
15933 * @param feedbackConstant One of the constants defined in
15934 * {@link HapticFeedbackConstants}
15935 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15936 */
15937 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15938 if (mAttachInfo == null) {
15939 return false;
15940 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015941 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015942 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015943 && !isHapticFeedbackEnabled()) {
15944 return false;
15945 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015946 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15947 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015948 }
15949
15950 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015951 * Request that the visibility of the status bar or other screen/window
15952 * decorations be changed.
15953 *
15954 * <p>This method is used to put the over device UI into temporary modes
15955 * where the user's attention is focused more on the application content,
15956 * by dimming or hiding surrounding system affordances. This is typically
15957 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15958 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15959 * to be placed behind the action bar (and with these flags other system
15960 * affordances) so that smooth transitions between hiding and showing them
15961 * can be done.
15962 *
15963 * <p>Two representative examples of the use of system UI visibility is
15964 * implementing a content browsing application (like a magazine reader)
15965 * and a video playing application.
15966 *
15967 * <p>The first code shows a typical implementation of a View in a content
15968 * browsing application. In this implementation, the application goes
15969 * into a content-oriented mode by hiding the status bar and action bar,
15970 * and putting the navigation elements into lights out mode. The user can
15971 * then interact with content while in this mode. Such an application should
15972 * provide an easy way for the user to toggle out of the mode (such as to
15973 * check information in the status bar or access notifications). In the
15974 * implementation here, this is done simply by tapping on the content.
15975 *
15976 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15977 * content}
15978 *
15979 * <p>This second code sample shows a typical implementation of a View
15980 * in a video playing application. In this situation, while the video is
15981 * playing the application would like to go into a complete full-screen mode,
15982 * to use as much of the display as possible for the video. When in this state
15983 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015984 * touching on the screen to pop the UI out of full screen mode. See
15985 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015986 *
15987 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15988 * content}
15989 *
15990 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15991 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15992 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15993 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015994 */
15995 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015996 if (visibility != mSystemUiVisibility) {
15997 mSystemUiVisibility = visibility;
15998 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15999 mParent.recomputeViewAttributes(this);
16000 }
Joe Onorato664644d2011-01-23 17:53:23 -080016001 }
16002 }
16003
16004 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016005 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16006 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16007 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16008 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16009 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016010 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016011 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016012 return mSystemUiVisibility;
16013 }
16014
Scott Mainec6331b2011-05-24 16:55:56 -070016015 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016016 * Returns the current system UI visibility that is currently set for
16017 * the entire window. This is the combination of the
16018 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16019 * views in the window.
16020 */
16021 public int getWindowSystemUiVisibility() {
16022 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16023 }
16024
16025 /**
16026 * Override to find out when the window's requested system UI visibility
16027 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16028 * This is different from the callbacks recieved through
16029 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16030 * in that this is only telling you about the local request of the window,
16031 * not the actual values applied by the system.
16032 */
16033 public void onWindowSystemUiVisibilityChanged(int visible) {
16034 }
16035
16036 /**
16037 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16038 * the view hierarchy.
16039 */
16040 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16041 onWindowSystemUiVisibilityChanged(visible);
16042 }
16043
16044 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016045 * Set a listener to receive callbacks when the visibility of the system bar changes.
16046 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16047 */
Joe Onorato664644d2011-01-23 17:53:23 -080016048 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016049 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016050 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16051 mParent.recomputeViewAttributes(this);
16052 }
16053 }
16054
16055 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016056 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16057 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016058 */
16059 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016060 ListenerInfo li = mListenerInfo;
16061 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16062 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016063 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016064 }
16065 }
16066
Dianne Hackborncf675782012-05-10 15:07:24 -070016067 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016068 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16069 if (val != mSystemUiVisibility) {
16070 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016071 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016072 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016073 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016074 }
16075
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016076 /** @hide */
16077 public void setDisabledSystemUiVisibility(int flags) {
16078 if (mAttachInfo != null) {
16079 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16080 mAttachInfo.mDisabledSystemUiVisibility = flags;
16081 if (mParent != null) {
16082 mParent.recomputeViewAttributes(this);
16083 }
16084 }
16085 }
16086 }
16087
Joe Onorato664644d2011-01-23 17:53:23 -080016088 /**
Joe Malin32736f02011-01-19 16:14:20 -080016089 * Creates an image that the system displays during the drag and drop
16090 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16091 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16092 * appearance as the given View. The default also positions the center of the drag shadow
16093 * directly under the touch point. If no View is provided (the constructor with no parameters
16094 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16095 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16096 * default is an invisible drag shadow.
16097 * <p>
16098 * You are not required to use the View you provide to the constructor as the basis of the
16099 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16100 * anything you want as the drag shadow.
16101 * </p>
16102 * <p>
16103 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16104 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16105 * size and position of the drag shadow. It uses this data to construct a
16106 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16107 * so that your application can draw the shadow image in the Canvas.
16108 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016109 *
16110 * <div class="special reference">
16111 * <h3>Developer Guides</h3>
16112 * <p>For a guide to implementing drag and drop features, read the
16113 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16114 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016115 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016116 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016117 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016118
16119 /**
Joe Malin32736f02011-01-19 16:14:20 -080016120 * Constructs a shadow image builder based on a View. By default, the resulting drag
16121 * shadow will have the same appearance and dimensions as the View, with the touch point
16122 * over the center of the View.
16123 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016124 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016125 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016126 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016127 }
16128
Christopher Tate17ed60c2011-01-18 12:50:26 -080016129 /**
16130 * Construct a shadow builder object with no associated View. This
16131 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16132 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16133 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016134 * reference to any View object. If they are not overridden, then the result is an
16135 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016136 */
16137 public DragShadowBuilder() {
16138 mView = new WeakReference<View>(null);
16139 }
16140
16141 /**
16142 * Returns the View object that had been passed to the
16143 * {@link #View.DragShadowBuilder(View)}
16144 * constructor. If that View parameter was {@code null} or if the
16145 * {@link #View.DragShadowBuilder()}
16146 * constructor was used to instantiate the builder object, this method will return
16147 * null.
16148 *
16149 * @return The View object associate with this builder object.
16150 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016151 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016152 final public View getView() {
16153 return mView.get();
16154 }
16155
Christopher Tate2c095f32010-10-04 14:13:40 -070016156 /**
Joe Malin32736f02011-01-19 16:14:20 -080016157 * Provides the metrics for the shadow image. These include the dimensions of
16158 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016159 * be centered under the touch location while dragging.
16160 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016161 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016162 * same as the dimensions of the View itself and centers the shadow under
16163 * the touch point.
16164 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016165 *
Joe Malin32736f02011-01-19 16:14:20 -080016166 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16167 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16168 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16169 * image.
16170 *
16171 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16172 * shadow image that should be underneath the touch point during the drag and drop
16173 * operation. Your application must set {@link android.graphics.Point#x} to the
16174 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016175 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016176 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016177 final View view = mView.get();
16178 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016179 shadowSize.set(view.getWidth(), view.getHeight());
16180 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016181 } else {
16182 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16183 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016184 }
16185
16186 /**
Joe Malin32736f02011-01-19 16:14:20 -080016187 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16188 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016189 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016190 *
Joe Malin32736f02011-01-19 16:14:20 -080016191 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016192 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016193 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016194 final View view = mView.get();
16195 if (view != null) {
16196 view.draw(canvas);
16197 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016198 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016199 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016200 }
16201 }
16202
16203 /**
Joe Malin32736f02011-01-19 16:14:20 -080016204 * Starts a drag and drop operation. When your application calls this method, it passes a
16205 * {@link android.view.View.DragShadowBuilder} object to the system. The
16206 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16207 * to get metrics for the drag shadow, and then calls the object's
16208 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16209 * <p>
16210 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16211 * drag events to all the View objects in your application that are currently visible. It does
16212 * this either by calling the View object's drag listener (an implementation of
16213 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16214 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16215 * Both are passed a {@link android.view.DragEvent} object that has a
16216 * {@link android.view.DragEvent#getAction()} value of
16217 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16218 * </p>
16219 * <p>
16220 * Your application can invoke startDrag() on any attached View object. The View object does not
16221 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16222 * be related to the View the user selected for dragging.
16223 * </p>
16224 * @param data A {@link android.content.ClipData} object pointing to the data to be
16225 * transferred by the drag and drop operation.
16226 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16227 * drag shadow.
16228 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16229 * drop operation. This Object is put into every DragEvent object sent by the system during the
16230 * current drag.
16231 * <p>
16232 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16233 * to the target Views. For example, it can contain flags that differentiate between a
16234 * a copy operation and a move operation.
16235 * </p>
16236 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16237 * so the parameter should be set to 0.
16238 * @return {@code true} if the method completes successfully, or
16239 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16240 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016241 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016242 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016243 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016244 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016245 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016246 }
16247 boolean okay = false;
16248
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016249 Point shadowSize = new Point();
16250 Point shadowTouchPoint = new Point();
16251 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016252
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016253 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16254 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16255 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016256 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016257
Chris Tatea32dcf72010-10-14 12:13:50 -070016258 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016259 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16260 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016261 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016262 Surface surface = new Surface();
16263 try {
16264 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016265 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016266 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016267 + " surface=" + surface);
16268 if (token != null) {
16269 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016270 try {
Chris Tate6b391282010-10-14 15:48:59 -070016271 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016272 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016273 } finally {
16274 surface.unlockCanvasAndPost(canvas);
16275 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016276
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016277 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016278
16279 // Cache the local state object for delivery with DragEvents
16280 root.setLocalDragState(myLocalState);
16281
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016282 // repurpose 'shadowSize' for the last touch point
16283 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016284
Christopher Tatea53146c2010-09-07 11:57:52 -070016285 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016286 shadowSize.x, shadowSize.y,
16287 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016288 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016289
16290 // Off and running! Release our local surface instance; the drag
16291 // shadow surface is now managed by the system process.
16292 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016293 }
16294 } catch (Exception e) {
16295 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16296 surface.destroy();
16297 }
16298
16299 return okay;
16300 }
16301
Christopher Tatea53146c2010-09-07 11:57:52 -070016302 /**
Joe Malin32736f02011-01-19 16:14:20 -080016303 * Handles drag events sent by the system following a call to
16304 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16305 *<p>
16306 * When the system calls this method, it passes a
16307 * {@link android.view.DragEvent} object. A call to
16308 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16309 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16310 * operation.
16311 * @param event The {@link android.view.DragEvent} sent by the system.
16312 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16313 * in DragEvent, indicating the type of drag event represented by this object.
16314 * @return {@code true} if the method was successful, otherwise {@code false}.
16315 * <p>
16316 * The method should return {@code true} in response to an action type of
16317 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16318 * operation.
16319 * </p>
16320 * <p>
16321 * The method should also return {@code true} in response to an action type of
16322 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16323 * {@code false} if it didn't.
16324 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016325 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016326 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016327 return false;
16328 }
16329
16330 /**
Joe Malin32736f02011-01-19 16:14:20 -080016331 * Detects if this View is enabled and has a drag event listener.
16332 * If both are true, then it calls the drag event listener with the
16333 * {@link android.view.DragEvent} it received. If the drag event listener returns
16334 * {@code true}, then dispatchDragEvent() returns {@code true}.
16335 * <p>
16336 * For all other cases, the method calls the
16337 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16338 * method and returns its result.
16339 * </p>
16340 * <p>
16341 * This ensures that a drag event is always consumed, even if the View does not have a drag
16342 * event listener. However, if the View has a listener and the listener returns true, then
16343 * onDragEvent() is not called.
16344 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016345 */
16346 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016347 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016348 ListenerInfo li = mListenerInfo;
16349 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16350 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016351 return true;
16352 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016353 return onDragEvent(event);
16354 }
16355
Christopher Tate3d4bf172011-03-28 16:16:46 -070016356 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016357 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016358 }
16359
Christopher Tatea53146c2010-09-07 11:57:52 -070016360 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016361 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16362 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016363 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016364 */
16365 public void onCloseSystemDialogs(String reason) {
16366 }
Joe Malin32736f02011-01-19 16:14:20 -080016367
Dianne Hackbornffa42482009-09-23 22:20:11 -070016368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016369 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016370 * update a Region being computed for
16371 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016372 * that any non-transparent parts of the Drawable are removed from the
16373 * given transparent region.
16374 *
16375 * @param dr The Drawable whose transparency is to be applied to the region.
16376 * @param region A Region holding the current transparency information,
16377 * where any parts of the region that are set are considered to be
16378 * transparent. On return, this region will be modified to have the
16379 * transparency information reduced by the corresponding parts of the
16380 * Drawable that are not transparent.
16381 * {@hide}
16382 */
16383 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16384 if (DBG) {
16385 Log.i("View", "Getting transparent region for: " + this);
16386 }
16387 final Region r = dr.getTransparentRegion();
16388 final Rect db = dr.getBounds();
16389 final AttachInfo attachInfo = mAttachInfo;
16390 if (r != null && attachInfo != null) {
16391 final int w = getRight()-getLeft();
16392 final int h = getBottom()-getTop();
16393 if (db.left > 0) {
16394 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16395 r.op(0, 0, db.left, h, Region.Op.UNION);
16396 }
16397 if (db.right < w) {
16398 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16399 r.op(db.right, 0, w, h, Region.Op.UNION);
16400 }
16401 if (db.top > 0) {
16402 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16403 r.op(0, 0, w, db.top, Region.Op.UNION);
16404 }
16405 if (db.bottom < h) {
16406 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16407 r.op(0, db.bottom, w, h, Region.Op.UNION);
16408 }
16409 final int[] location = attachInfo.mTransparentLocation;
16410 getLocationInWindow(location);
16411 r.translate(location[0], location[1]);
16412 region.op(r, Region.Op.INTERSECT);
16413 } else {
16414 region.op(db, Region.Op.DIFFERENCE);
16415 }
16416 }
16417
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016418 private void checkForLongClick(int delayOffset) {
16419 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16420 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016421
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016422 if (mPendingCheckForLongPress == null) {
16423 mPendingCheckForLongPress = new CheckForLongPress();
16424 }
16425 mPendingCheckForLongPress.rememberWindowAttachCount();
16426 postDelayed(mPendingCheckForLongPress,
16427 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016429 }
16430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016431 /**
16432 * Inflate a view from an XML resource. This convenience method wraps the {@link
16433 * LayoutInflater} class, which provides a full range of options for view inflation.
16434 *
16435 * @param context The Context object for your activity or application.
16436 * @param resource The resource ID to inflate
16437 * @param root A view group that will be the parent. Used to properly inflate the
16438 * layout_* parameters.
16439 * @see LayoutInflater
16440 */
16441 public static View inflate(Context context, int resource, ViewGroup root) {
16442 LayoutInflater factory = LayoutInflater.from(context);
16443 return factory.inflate(resource, root);
16444 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016446 /**
Adam Powell637d3372010-08-25 14:37:03 -070016447 * Scroll the view with standard behavior for scrolling beyond the normal
16448 * content boundaries. Views that call this method should override
16449 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16450 * results of an over-scroll operation.
16451 *
16452 * Views can use this method to handle any touch or fling-based scrolling.
16453 *
16454 * @param deltaX Change in X in pixels
16455 * @param deltaY Change in Y in pixels
16456 * @param scrollX Current X scroll value in pixels before applying deltaX
16457 * @param scrollY Current Y scroll value in pixels before applying deltaY
16458 * @param scrollRangeX Maximum content scroll range along the X axis
16459 * @param scrollRangeY Maximum content scroll range along the Y axis
16460 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16461 * along the X axis.
16462 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16463 * along the Y axis.
16464 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16465 * @return true if scrolling was clamped to an over-scroll boundary along either
16466 * axis, false otherwise.
16467 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016468 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016469 protected boolean overScrollBy(int deltaX, int deltaY,
16470 int scrollX, int scrollY,
16471 int scrollRangeX, int scrollRangeY,
16472 int maxOverScrollX, int maxOverScrollY,
16473 boolean isTouchEvent) {
16474 final int overScrollMode = mOverScrollMode;
16475 final boolean canScrollHorizontal =
16476 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16477 final boolean canScrollVertical =
16478 computeVerticalScrollRange() > computeVerticalScrollExtent();
16479 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16480 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16481 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16482 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16483
16484 int newScrollX = scrollX + deltaX;
16485 if (!overScrollHorizontal) {
16486 maxOverScrollX = 0;
16487 }
16488
16489 int newScrollY = scrollY + deltaY;
16490 if (!overScrollVertical) {
16491 maxOverScrollY = 0;
16492 }
16493
16494 // Clamp values if at the limits and record
16495 final int left = -maxOverScrollX;
16496 final int right = maxOverScrollX + scrollRangeX;
16497 final int top = -maxOverScrollY;
16498 final int bottom = maxOverScrollY + scrollRangeY;
16499
16500 boolean clampedX = false;
16501 if (newScrollX > right) {
16502 newScrollX = right;
16503 clampedX = true;
16504 } else if (newScrollX < left) {
16505 newScrollX = left;
16506 clampedX = true;
16507 }
16508
16509 boolean clampedY = false;
16510 if (newScrollY > bottom) {
16511 newScrollY = bottom;
16512 clampedY = true;
16513 } else if (newScrollY < top) {
16514 newScrollY = top;
16515 clampedY = true;
16516 }
16517
16518 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16519
16520 return clampedX || clampedY;
16521 }
16522
16523 /**
16524 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16525 * respond to the results of an over-scroll operation.
16526 *
16527 * @param scrollX New X scroll value in pixels
16528 * @param scrollY New Y scroll value in pixels
16529 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16530 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16531 */
16532 protected void onOverScrolled(int scrollX, int scrollY,
16533 boolean clampedX, boolean clampedY) {
16534 // Intentionally empty.
16535 }
16536
16537 /**
16538 * Returns the over-scroll mode for this view. The result will be
16539 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16540 * (allow over-scrolling only if the view content is larger than the container),
16541 * or {@link #OVER_SCROLL_NEVER}.
16542 *
16543 * @return This view's over-scroll mode.
16544 */
16545 public int getOverScrollMode() {
16546 return mOverScrollMode;
16547 }
16548
16549 /**
16550 * Set the over-scroll mode for this view. Valid over-scroll modes are
16551 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16552 * (allow over-scrolling only if the view content is larger than the container),
16553 * or {@link #OVER_SCROLL_NEVER}.
16554 *
16555 * Setting the over-scroll mode of a view will have an effect only if the
16556 * view is capable of scrolling.
16557 *
16558 * @param overScrollMode The new over-scroll mode for this view.
16559 */
16560 public void setOverScrollMode(int overScrollMode) {
16561 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16562 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16563 overScrollMode != OVER_SCROLL_NEVER) {
16564 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16565 }
16566 mOverScrollMode = overScrollMode;
16567 }
16568
16569 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016570 * Gets a scale factor that determines the distance the view should scroll
16571 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16572 * @return The vertical scroll scale factor.
16573 * @hide
16574 */
16575 protected float getVerticalScrollFactor() {
16576 if (mVerticalScrollFactor == 0) {
16577 TypedValue outValue = new TypedValue();
16578 if (!mContext.getTheme().resolveAttribute(
16579 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16580 throw new IllegalStateException(
16581 "Expected theme to define listPreferredItemHeight.");
16582 }
16583 mVerticalScrollFactor = outValue.getDimension(
16584 mContext.getResources().getDisplayMetrics());
16585 }
16586 return mVerticalScrollFactor;
16587 }
16588
16589 /**
16590 * Gets a scale factor that determines the distance the view should scroll
16591 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16592 * @return The horizontal scroll scale factor.
16593 * @hide
16594 */
16595 protected float getHorizontalScrollFactor() {
16596 // TODO: Should use something else.
16597 return getVerticalScrollFactor();
16598 }
16599
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016600 /**
16601 * Return the value specifying the text direction or policy that was set with
16602 * {@link #setTextDirection(int)}.
16603 *
16604 * @return the defined text direction. It can be one of:
16605 *
16606 * {@link #TEXT_DIRECTION_INHERIT},
16607 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16608 * {@link #TEXT_DIRECTION_ANY_RTL},
16609 * {@link #TEXT_DIRECTION_LTR},
16610 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016611 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016612 *
16613 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016614 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016615 @ViewDebug.ExportedProperty(category = "text", mapping = {
16616 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16617 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16618 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16619 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16620 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16621 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16622 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016623 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016624 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016625 }
16626
16627 /**
16628 * Set the text direction.
16629 *
16630 * @param textDirection the direction to set. Should be one of:
16631 *
16632 * {@link #TEXT_DIRECTION_INHERIT},
16633 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16634 * {@link #TEXT_DIRECTION_ANY_RTL},
16635 * {@link #TEXT_DIRECTION_LTR},
16636 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016637 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016638 *
16639 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16640 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16641 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016642 */
16643 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016644 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016645 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016646 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016647 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016648 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016649 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016650 // Do resolution
16651 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016652 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016653 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016654 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016655 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016656 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016657 }
16658 }
16659
16660 /**
16661 * Return the resolved text direction.
16662 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016663 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016664 *
Doug Feltcb3791202011-07-07 11:57:48 -070016665 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16666 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016667 * {@link #TEXT_DIRECTION_LTR},
16668 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016669 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016670 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016671 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016672 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016673 }
16674
16675 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016676 * Resolve the text direction.
16677 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016678 * @return true if resolution has been done, false otherwise.
16679 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016680 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016681 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016682 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016683 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016684 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016685
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016686 if (hasRtlSupport()) {
16687 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016688 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016689 switch(textDirection) {
16690 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016691 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016692 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016693 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016694 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016695 return false;
16696 }
16697
16698 View parent = ((View) mParent);
16699 // Parent has not yet resolved, so we still return the default
16700 if (!parent.isTextDirectionResolved()) {
16701 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16702 // Resolution will need to happen again later
16703 return false;
16704 }
16705
16706 // Set current resolved direction to the same value as the parent's one
16707 final int parentResolvedDirection = parent.getTextDirection();
16708 switch (parentResolvedDirection) {
16709 case TEXT_DIRECTION_FIRST_STRONG:
16710 case TEXT_DIRECTION_ANY_RTL:
16711 case TEXT_DIRECTION_LTR:
16712 case TEXT_DIRECTION_RTL:
16713 case TEXT_DIRECTION_LOCALE:
16714 mPrivateFlags2 |=
16715 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16716 break;
16717 default:
16718 // Default resolved direction is "first strong" heuristic
16719 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016720 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016721 break;
16722 case TEXT_DIRECTION_FIRST_STRONG:
16723 case TEXT_DIRECTION_ANY_RTL:
16724 case TEXT_DIRECTION_LTR:
16725 case TEXT_DIRECTION_RTL:
16726 case TEXT_DIRECTION_LOCALE:
16727 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016728 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016729 break;
16730 default:
16731 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016732 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016733 }
16734 } else {
16735 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016736 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016737 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016738
16739 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016740 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016741 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016742 }
16743
16744 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016745 * Check if text direction resolution can be done.
16746 *
16747 * @return true if text direction resolution can be done otherwise return false.
16748 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016749 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016750 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016751 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016752 return (mParent != null) && (mParent instanceof View) &&
16753 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016754 default:
16755 return true;
16756 }
16757 }
16758
16759 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016760 * Reset resolved text direction. Text direction will be resolved during a call to
16761 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016762 *
16763 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016764 */
16765 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016766 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016767 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016768 // Set to default value
16769 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016770 }
16771
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016772 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016773 * @return true if text direction is inherited.
16774 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016775 * @hide
16776 */
16777 public boolean isTextDirectionInherited() {
16778 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16779 }
16780
16781 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016782 * @return true if text direction is resolved.
16783 */
16784 private boolean isTextDirectionResolved() {
16785 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16786 }
16787
16788 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016789 * Return the value specifying the text alignment or policy that was set with
16790 * {@link #setTextAlignment(int)}.
16791 *
16792 * @return the defined text alignment. It can be one of:
16793 *
16794 * {@link #TEXT_ALIGNMENT_INHERIT},
16795 * {@link #TEXT_ALIGNMENT_GRAVITY},
16796 * {@link #TEXT_ALIGNMENT_CENTER},
16797 * {@link #TEXT_ALIGNMENT_TEXT_START},
16798 * {@link #TEXT_ALIGNMENT_TEXT_END},
16799 * {@link #TEXT_ALIGNMENT_VIEW_START},
16800 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016801 *
16802 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016803 */
16804 @ViewDebug.ExportedProperty(category = "text", mapping = {
16805 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16806 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16807 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16808 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16809 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16810 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16811 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16812 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016813 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016814 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016815 }
16816
16817 /**
16818 * Set the text alignment.
16819 *
16820 * @param textAlignment The text alignment to set. Should be one of
16821 *
16822 * {@link #TEXT_ALIGNMENT_INHERIT},
16823 * {@link #TEXT_ALIGNMENT_GRAVITY},
16824 * {@link #TEXT_ALIGNMENT_CENTER},
16825 * {@link #TEXT_ALIGNMENT_TEXT_START},
16826 * {@link #TEXT_ALIGNMENT_TEXT_END},
16827 * {@link #TEXT_ALIGNMENT_VIEW_START},
16828 * {@link #TEXT_ALIGNMENT_VIEW_END}
16829 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016830 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16831 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16832 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16833 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016834 * @attr ref android.R.styleable#View_textAlignment
16835 */
16836 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016837 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016838 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016839 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016840 resetResolvedTextAlignment();
16841 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016842 mPrivateFlags2 |=
16843 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16844 // Do resolution
16845 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016846 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016847 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016848 // Refresh
16849 requestLayout();
16850 invalidate(true);
16851 }
16852 }
16853
16854 /**
16855 * Return the resolved text alignment.
16856 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016857 * @return the resolved text alignment. Returns one of:
16858 *
16859 * {@link #TEXT_ALIGNMENT_GRAVITY},
16860 * {@link #TEXT_ALIGNMENT_CENTER},
16861 * {@link #TEXT_ALIGNMENT_TEXT_START},
16862 * {@link #TEXT_ALIGNMENT_TEXT_END},
16863 * {@link #TEXT_ALIGNMENT_VIEW_START},
16864 * {@link #TEXT_ALIGNMENT_VIEW_END}
16865 */
16866 @ViewDebug.ExportedProperty(category = "text", mapping = {
16867 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16868 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16869 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16870 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16871 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16872 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16873 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16874 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016875 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016876 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16877 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016878 }
16879
16880 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016881 * Resolve the text alignment.
16882 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016883 * @return true if resolution has been done, false otherwise.
16884 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016885 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016886 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016887 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016888 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016889 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016890
16891 if (hasRtlSupport()) {
16892 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016893 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016894 switch (textAlignment) {
16895 case TEXT_ALIGNMENT_INHERIT:
16896 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016897 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016898 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016899 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016900 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016901 return false;
16902 }
16903 View parent = (View) mParent;
16904
16905 // Parent has not yet resolved, so we still return the default
16906 if (!parent.isTextAlignmentResolved()) {
16907 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16908 // Resolution will need to happen again later
16909 return false;
16910 }
16911
16912 final int parentResolvedTextAlignment = parent.getTextAlignment();
16913 switch (parentResolvedTextAlignment) {
16914 case TEXT_ALIGNMENT_GRAVITY:
16915 case TEXT_ALIGNMENT_TEXT_START:
16916 case TEXT_ALIGNMENT_TEXT_END:
16917 case TEXT_ALIGNMENT_CENTER:
16918 case TEXT_ALIGNMENT_VIEW_START:
16919 case TEXT_ALIGNMENT_VIEW_END:
16920 // Resolved text alignment is the same as the parent resolved
16921 // text alignment
16922 mPrivateFlags2 |=
16923 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16924 break;
16925 default:
16926 // Use default resolved text alignment
16927 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016928 }
16929 break;
16930 case TEXT_ALIGNMENT_GRAVITY:
16931 case TEXT_ALIGNMENT_TEXT_START:
16932 case TEXT_ALIGNMENT_TEXT_END:
16933 case TEXT_ALIGNMENT_CENTER:
16934 case TEXT_ALIGNMENT_VIEW_START:
16935 case TEXT_ALIGNMENT_VIEW_END:
16936 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016937 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016938 break;
16939 default:
16940 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016941 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016942 }
16943 } else {
16944 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016945 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016946 }
16947
16948 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016949 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016950 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016951 }
16952
16953 /**
16954 * Check if text alignment resolution can be done.
16955 *
16956 * @return true if text alignment resolution can be done otherwise return false.
16957 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016958 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016959 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016960 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016961 return (mParent != null) && (mParent instanceof View) &&
16962 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016963 default:
16964 return true;
16965 }
16966 }
16967
16968 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016969 * Reset resolved text alignment. Text alignment will be resolved during a call to
16970 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016971 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016972 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016973 */
16974 public void resetResolvedTextAlignment() {
16975 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016976 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016977 // Set to default
16978 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016979 }
16980
Adam Powella9108a22012-07-18 11:18:09 -070016981 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016982 * @return true if text alignment is inherited.
16983 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016984 * @hide
16985 */
16986 public boolean isTextAlignmentInherited() {
16987 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16988 }
16989
16990 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016991 * @return true if text alignment is resolved.
16992 */
16993 private boolean isTextAlignmentResolved() {
16994 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
16995 }
16996
16997 /**
Adam Powella9108a22012-07-18 11:18:09 -070016998 * Generate a value suitable for use in {@link #setId(int)}.
16999 * This value will not collide with ID values generated at build time by aapt for R.id.
17000 *
17001 * @return a generated ID value
17002 */
17003 public static int generateViewId() {
17004 for (;;) {
17005 final int result = sNextGeneratedId.get();
17006 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17007 int newValue = result + 1;
17008 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17009 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17010 return result;
17011 }
17012 }
17013 }
17014
Chet Haaseb39f0512011-05-24 14:36:40 -070017015 //
17016 // Properties
17017 //
17018 /**
17019 * A Property wrapper around the <code>alpha</code> functionality handled by the
17020 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17021 */
Chet Haased47f1532011-12-16 11:18:52 -080017022 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017023 @Override
17024 public void setValue(View object, float value) {
17025 object.setAlpha(value);
17026 }
17027
17028 @Override
17029 public Float get(View object) {
17030 return object.getAlpha();
17031 }
17032 };
17033
17034 /**
17035 * A Property wrapper around the <code>translationX</code> functionality handled by the
17036 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17037 */
Chet Haased47f1532011-12-16 11:18:52 -080017038 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017039 @Override
17040 public void setValue(View object, float value) {
17041 object.setTranslationX(value);
17042 }
17043
17044 @Override
17045 public Float get(View object) {
17046 return object.getTranslationX();
17047 }
17048 };
17049
17050 /**
17051 * A Property wrapper around the <code>translationY</code> functionality handled by the
17052 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17053 */
Chet Haased47f1532011-12-16 11:18:52 -080017054 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017055 @Override
17056 public void setValue(View object, float value) {
17057 object.setTranslationY(value);
17058 }
17059
17060 @Override
17061 public Float get(View object) {
17062 return object.getTranslationY();
17063 }
17064 };
17065
17066 /**
17067 * A Property wrapper around the <code>x</code> functionality handled by the
17068 * {@link View#setX(float)} and {@link View#getX()} methods.
17069 */
Chet Haased47f1532011-12-16 11:18:52 -080017070 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017071 @Override
17072 public void setValue(View object, float value) {
17073 object.setX(value);
17074 }
17075
17076 @Override
17077 public Float get(View object) {
17078 return object.getX();
17079 }
17080 };
17081
17082 /**
17083 * A Property wrapper around the <code>y</code> functionality handled by the
17084 * {@link View#setY(float)} and {@link View#getY()} methods.
17085 */
Chet Haased47f1532011-12-16 11:18:52 -080017086 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017087 @Override
17088 public void setValue(View object, float value) {
17089 object.setY(value);
17090 }
17091
17092 @Override
17093 public Float get(View object) {
17094 return object.getY();
17095 }
17096 };
17097
17098 /**
17099 * A Property wrapper around the <code>rotation</code> functionality handled by the
17100 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17101 */
Chet Haased47f1532011-12-16 11:18:52 -080017102 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017103 @Override
17104 public void setValue(View object, float value) {
17105 object.setRotation(value);
17106 }
17107
17108 @Override
17109 public Float get(View object) {
17110 return object.getRotation();
17111 }
17112 };
17113
17114 /**
17115 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17116 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17117 */
Chet Haased47f1532011-12-16 11:18:52 -080017118 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017119 @Override
17120 public void setValue(View object, float value) {
17121 object.setRotationX(value);
17122 }
17123
17124 @Override
17125 public Float get(View object) {
17126 return object.getRotationX();
17127 }
17128 };
17129
17130 /**
17131 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17132 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17133 */
Chet Haased47f1532011-12-16 11:18:52 -080017134 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017135 @Override
17136 public void setValue(View object, float value) {
17137 object.setRotationY(value);
17138 }
17139
17140 @Override
17141 public Float get(View object) {
17142 return object.getRotationY();
17143 }
17144 };
17145
17146 /**
17147 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17148 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17149 */
Chet Haased47f1532011-12-16 11:18:52 -080017150 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017151 @Override
17152 public void setValue(View object, float value) {
17153 object.setScaleX(value);
17154 }
17155
17156 @Override
17157 public Float get(View object) {
17158 return object.getScaleX();
17159 }
17160 };
17161
17162 /**
17163 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17164 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17165 */
Chet Haased47f1532011-12-16 11:18:52 -080017166 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017167 @Override
17168 public void setValue(View object, float value) {
17169 object.setScaleY(value);
17170 }
17171
17172 @Override
17173 public Float get(View object) {
17174 return object.getScaleY();
17175 }
17176 };
17177
Jeff Brown33bbfd22011-02-24 20:55:35 -080017178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017179 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17180 * Each MeasureSpec represents a requirement for either the width or the height.
17181 * A MeasureSpec is comprised of a size and a mode. There are three possible
17182 * modes:
17183 * <dl>
17184 * <dt>UNSPECIFIED</dt>
17185 * <dd>
17186 * The parent has not imposed any constraint on the child. It can be whatever size
17187 * it wants.
17188 * </dd>
17189 *
17190 * <dt>EXACTLY</dt>
17191 * <dd>
17192 * The parent has determined an exact size for the child. The child is going to be
17193 * given those bounds regardless of how big it wants to be.
17194 * </dd>
17195 *
17196 * <dt>AT_MOST</dt>
17197 * <dd>
17198 * The child can be as large as it wants up to the specified size.
17199 * </dd>
17200 * </dl>
17201 *
17202 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17203 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17204 */
17205 public static class MeasureSpec {
17206 private static final int MODE_SHIFT = 30;
17207 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17208
17209 /**
17210 * Measure specification mode: The parent has not imposed any constraint
17211 * on the child. It can be whatever size it wants.
17212 */
17213 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17214
17215 /**
17216 * Measure specification mode: The parent has determined an exact size
17217 * for the child. The child is going to be given those bounds regardless
17218 * of how big it wants to be.
17219 */
17220 public static final int EXACTLY = 1 << MODE_SHIFT;
17221
17222 /**
17223 * Measure specification mode: The child can be as large as it wants up
17224 * to the specified size.
17225 */
17226 public static final int AT_MOST = 2 << MODE_SHIFT;
17227
17228 /**
17229 * Creates a measure specification based on the supplied size and mode.
17230 *
17231 * The mode must always be one of the following:
17232 * <ul>
17233 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17234 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17235 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17236 * </ul>
17237 *
17238 * @param size the size of the measure specification
17239 * @param mode the mode of the measure specification
17240 * @return the measure specification based on size and mode
17241 */
17242 public static int makeMeasureSpec(int size, int mode) {
Adam Powell946d05b2012-10-01 15:06:20 -070017243 return size + mode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017244 }
17245
17246 /**
17247 * Extracts the mode from the supplied measure specification.
17248 *
17249 * @param measureSpec the measure specification to extract the mode from
17250 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17251 * {@link android.view.View.MeasureSpec#AT_MOST} or
17252 * {@link android.view.View.MeasureSpec#EXACTLY}
17253 */
17254 public static int getMode(int measureSpec) {
17255 return (measureSpec & MODE_MASK);
17256 }
17257
17258 /**
17259 * Extracts the size from the supplied measure specification.
17260 *
17261 * @param measureSpec the measure specification to extract the size from
17262 * @return the size in pixels defined in the supplied measure specification
17263 */
17264 public static int getSize(int measureSpec) {
17265 return (measureSpec & ~MODE_MASK);
17266 }
17267
17268 /**
17269 * Returns a String representation of the specified measure
17270 * specification.
17271 *
17272 * @param measureSpec the measure specification to convert to a String
17273 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17274 */
17275 public static String toString(int measureSpec) {
17276 int mode = getMode(measureSpec);
17277 int size = getSize(measureSpec);
17278
17279 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17280
17281 if (mode == UNSPECIFIED)
17282 sb.append("UNSPECIFIED ");
17283 else if (mode == EXACTLY)
17284 sb.append("EXACTLY ");
17285 else if (mode == AT_MOST)
17286 sb.append("AT_MOST ");
17287 else
17288 sb.append(mode).append(" ");
17289
17290 sb.append(size);
17291 return sb.toString();
17292 }
17293 }
17294
17295 class CheckForLongPress implements Runnable {
17296
17297 private int mOriginalWindowAttachCount;
17298
17299 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017300 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017301 && mOriginalWindowAttachCount == mWindowAttachCount) {
17302 if (performLongClick()) {
17303 mHasPerformedLongPress = true;
17304 }
17305 }
17306 }
17307
17308 public void rememberWindowAttachCount() {
17309 mOriginalWindowAttachCount = mWindowAttachCount;
17310 }
17311 }
Joe Malin32736f02011-01-19 16:14:20 -080017312
Adam Powelle14579b2009-12-16 18:39:52 -080017313 private final class CheckForTap implements Runnable {
17314 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017315 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017316 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017317 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017318 }
17319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017320
Adam Powella35d7682010-03-12 14:48:13 -080017321 private final class PerformClick implements Runnable {
17322 public void run() {
17323 performClick();
17324 }
17325 }
17326
Dianne Hackborn63042d62011-01-26 18:56:29 -080017327 /** @hide */
17328 public void hackTurnOffWindowResizeAnim(boolean off) {
17329 mAttachInfo.mTurnOffWindowResizeAnim = off;
17330 }
Joe Malin32736f02011-01-19 16:14:20 -080017331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017332 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017333 * This method returns a ViewPropertyAnimator object, which can be used to animate
17334 * specific properties on this View.
17335 *
17336 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17337 */
17338 public ViewPropertyAnimator animate() {
17339 if (mAnimator == null) {
17340 mAnimator = new ViewPropertyAnimator(this);
17341 }
17342 return mAnimator;
17343 }
17344
17345 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017346 * Interface definition for a callback to be invoked when a hardware key event is
17347 * dispatched to this view. The callback will be invoked before the key event is
17348 * given to the view. This is only useful for hardware keyboards; a software input
17349 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017350 */
17351 public interface OnKeyListener {
17352 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017353 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017354 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017355 * <p>Key presses in software keyboards will generally NOT trigger this method,
17356 * although some may elect to do so in some situations. Do not assume a
17357 * software input method has to be key-based; even if it is, it may use key presses
17358 * in a different way than you expect, so there is no way to reliably catch soft
17359 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017360 *
17361 * @param v The view the key has been dispatched to.
17362 * @param keyCode The code for the physical key that was pressed
17363 * @param event The KeyEvent object containing full information about
17364 * the event.
17365 * @return True if the listener has consumed the event, false otherwise.
17366 */
17367 boolean onKey(View v, int keyCode, KeyEvent event);
17368 }
17369
17370 /**
17371 * Interface definition for a callback to be invoked when a touch event is
17372 * dispatched to this view. The callback will be invoked before the touch
17373 * event is given to the view.
17374 */
17375 public interface OnTouchListener {
17376 /**
17377 * Called when a touch event is dispatched to a view. This allows listeners to
17378 * get a chance to respond before the target view.
17379 *
17380 * @param v The view the touch event has been dispatched to.
17381 * @param event The MotionEvent object containing full information about
17382 * the event.
17383 * @return True if the listener has consumed the event, false otherwise.
17384 */
17385 boolean onTouch(View v, MotionEvent event);
17386 }
17387
17388 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017389 * Interface definition for a callback to be invoked when a hover event is
17390 * dispatched to this view. The callback will be invoked before the hover
17391 * event is given to the view.
17392 */
17393 public interface OnHoverListener {
17394 /**
17395 * Called when a hover event is dispatched to a view. This allows listeners to
17396 * get a chance to respond before the target view.
17397 *
17398 * @param v The view the hover event has been dispatched to.
17399 * @param event The MotionEvent object containing full information about
17400 * the event.
17401 * @return True if the listener has consumed the event, false otherwise.
17402 */
17403 boolean onHover(View v, MotionEvent event);
17404 }
17405
17406 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017407 * Interface definition for a callback to be invoked when a generic motion event is
17408 * dispatched to this view. The callback will be invoked before the generic motion
17409 * event is given to the view.
17410 */
17411 public interface OnGenericMotionListener {
17412 /**
17413 * Called when a generic motion event is dispatched to a view. This allows listeners to
17414 * get a chance to respond before the target view.
17415 *
17416 * @param v The view the generic motion event has been dispatched to.
17417 * @param event The MotionEvent object containing full information about
17418 * the event.
17419 * @return True if the listener has consumed the event, false otherwise.
17420 */
17421 boolean onGenericMotion(View v, MotionEvent event);
17422 }
17423
17424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017425 * Interface definition for a callback to be invoked when a view has been clicked and held.
17426 */
17427 public interface OnLongClickListener {
17428 /**
17429 * Called when a view has been clicked and held.
17430 *
17431 * @param v The view that was clicked and held.
17432 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017433 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017434 */
17435 boolean onLongClick(View v);
17436 }
17437
17438 /**
Chris Tate32affef2010-10-18 15:29:21 -070017439 * Interface definition for a callback to be invoked when a drag is being dispatched
17440 * to this view. The callback will be invoked before the hosting view's own
17441 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17442 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017443 *
17444 * <div class="special reference">
17445 * <h3>Developer Guides</h3>
17446 * <p>For a guide to implementing drag and drop features, read the
17447 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17448 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017449 */
17450 public interface OnDragListener {
17451 /**
17452 * Called when a drag event is dispatched to a view. This allows listeners
17453 * to get a chance to override base View behavior.
17454 *
Joe Malin32736f02011-01-19 16:14:20 -080017455 * @param v The View that received the drag event.
17456 * @param event The {@link android.view.DragEvent} object for the drag event.
17457 * @return {@code true} if the drag event was handled successfully, or {@code false}
17458 * if the drag event was not handled. Note that {@code false} will trigger the View
17459 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017460 */
17461 boolean onDrag(View v, DragEvent event);
17462 }
17463
17464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017465 * Interface definition for a callback to be invoked when the focus state of
17466 * a view changed.
17467 */
17468 public interface OnFocusChangeListener {
17469 /**
17470 * Called when the focus state of a view has changed.
17471 *
17472 * @param v The view whose state has changed.
17473 * @param hasFocus The new focus state of v.
17474 */
17475 void onFocusChange(View v, boolean hasFocus);
17476 }
17477
17478 /**
17479 * Interface definition for a callback to be invoked when a view is clicked.
17480 */
17481 public interface OnClickListener {
17482 /**
17483 * Called when a view has been clicked.
17484 *
17485 * @param v The view that was clicked.
17486 */
17487 void onClick(View v);
17488 }
17489
17490 /**
17491 * Interface definition for a callback to be invoked when the context menu
17492 * for this view is being built.
17493 */
17494 public interface OnCreateContextMenuListener {
17495 /**
17496 * Called when the context menu for this view is being built. It is not
17497 * safe to hold onto the menu after this method returns.
17498 *
17499 * @param menu The context menu that is being built
17500 * @param v The view for which the context menu is being built
17501 * @param menuInfo Extra information about the item for which the
17502 * context menu should be shown. This information will vary
17503 * depending on the class of v.
17504 */
17505 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17506 }
17507
Joe Onorato664644d2011-01-23 17:53:23 -080017508 /**
17509 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017510 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017511 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017512 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017513 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017514 */
17515 public interface OnSystemUiVisibilityChangeListener {
17516 /**
17517 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017518 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017519 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017520 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17521 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17522 * This tells you the <strong>global</strong> state of these UI visibility
17523 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017524 */
17525 public void onSystemUiVisibilityChange(int visibility);
17526 }
17527
Adam Powell4afd62b2011-02-18 15:02:18 -080017528 /**
17529 * Interface definition for a callback to be invoked when this view is attached
17530 * or detached from its window.
17531 */
17532 public interface OnAttachStateChangeListener {
17533 /**
17534 * Called when the view is attached to a window.
17535 * @param v The view that was attached
17536 */
17537 public void onViewAttachedToWindow(View v);
17538 /**
17539 * Called when the view is detached from a window.
17540 * @param v The view that was detached
17541 */
17542 public void onViewDetachedFromWindow(View v);
17543 }
17544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017545 private final class UnsetPressedState implements Runnable {
17546 public void run() {
17547 setPressed(false);
17548 }
17549 }
17550
17551 /**
17552 * Base class for derived classes that want to save and restore their own
17553 * state in {@link android.view.View#onSaveInstanceState()}.
17554 */
17555 public static class BaseSavedState extends AbsSavedState {
17556 /**
17557 * Constructor used when reading from a parcel. Reads the state of the superclass.
17558 *
17559 * @param source
17560 */
17561 public BaseSavedState(Parcel source) {
17562 super(source);
17563 }
17564
17565 /**
17566 * Constructor called by derived classes when creating their SavedState objects
17567 *
17568 * @param superState The state of the superclass of this view
17569 */
17570 public BaseSavedState(Parcelable superState) {
17571 super(superState);
17572 }
17573
17574 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17575 new Parcelable.Creator<BaseSavedState>() {
17576 public BaseSavedState createFromParcel(Parcel in) {
17577 return new BaseSavedState(in);
17578 }
17579
17580 public BaseSavedState[] newArray(int size) {
17581 return new BaseSavedState[size];
17582 }
17583 };
17584 }
17585
17586 /**
17587 * A set of information given to a view when it is attached to its parent
17588 * window.
17589 */
17590 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017591 interface Callbacks {
17592 void playSoundEffect(int effectId);
17593 boolean performHapticFeedback(int effectId, boolean always);
17594 }
17595
17596 /**
17597 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17598 * to a Handler. This class contains the target (View) to invalidate and
17599 * the coordinates of the dirty rectangle.
17600 *
17601 * For performance purposes, this class also implements a pool of up to
17602 * POOL_LIMIT objects that get reused. This reduces memory allocations
17603 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017604 */
Romain Guyd928d682009-03-31 17:52:16 -070017605 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017606 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017607 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17608 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017609 public InvalidateInfo newInstance() {
17610 return new InvalidateInfo();
17611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017612
Romain Guyd928d682009-03-31 17:52:16 -070017613 public void onAcquired(InvalidateInfo element) {
17614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017615
Romain Guyd928d682009-03-31 17:52:16 -070017616 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017617 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017618 }
17619 }, POOL_LIMIT)
17620 );
17621
17622 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017623 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017624
17625 View target;
17626
17627 int left;
17628 int top;
17629 int right;
17630 int bottom;
17631
Romain Guyd928d682009-03-31 17:52:16 -070017632 public void setNextPoolable(InvalidateInfo element) {
17633 mNext = element;
17634 }
17635
17636 public InvalidateInfo getNextPoolable() {
17637 return mNext;
17638 }
17639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017640 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017641 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017642 }
17643
17644 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017645 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017646 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017647
17648 public boolean isPooled() {
17649 return mIsPooled;
17650 }
17651
17652 public void setPooled(boolean isPooled) {
17653 mIsPooled = isPooled;
17654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017655 }
17656
17657 final IWindowSession mSession;
17658
17659 final IWindow mWindow;
17660
17661 final IBinder mWindowToken;
17662
Jeff Brown98365d72012-08-19 20:30:52 -070017663 final Display mDisplay;
17664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017665 final Callbacks mRootCallbacks;
17666
Romain Guy59a12ca2011-06-09 17:48:21 -070017667 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017669 /**
17670 * The top view of the hierarchy.
17671 */
17672 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017674 IBinder mPanelParentWindowToken;
17675 Surface mSurface;
17676
Romain Guyb051e892010-09-28 19:09:36 -070017677 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017678 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017679 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017680
Romain Guy7e4e5612012-03-05 14:37:29 -080017681 boolean mScreenOn;
17682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017683 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017684 * Scale factor used by the compatibility mode
17685 */
17686 float mApplicationScale;
17687
17688 /**
17689 * Indicates whether the application is in compatibility mode
17690 */
17691 boolean mScalingRequired;
17692
17693 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017694 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017695 */
17696 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017697
Dianne Hackborn63042d62011-01-26 18:56:29 -080017698 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017699 * Left position of this view's window
17700 */
17701 int mWindowLeft;
17702
17703 /**
17704 * Top position of this view's window
17705 */
17706 int mWindowTop;
17707
17708 /**
Adam Powell26153a32010-11-08 15:22:27 -080017709 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017710 */
Adam Powell26153a32010-11-08 15:22:27 -080017711 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017712
17713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017714 * For windows that are full-screen but using insets to layout inside
17715 * of the screen decorations, these are the current insets for the
17716 * content of the window.
17717 */
17718 final Rect mContentInsets = new Rect();
17719
17720 /**
17721 * For windows that are full-screen but using insets to layout inside
17722 * of the screen decorations, these are the current insets for the
17723 * actual visible parts of the window.
17724 */
17725 final Rect mVisibleInsets = new Rect();
17726
17727 /**
17728 * The internal insets given by this window. This value is
17729 * supplied by the client (through
17730 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17731 * be given to the window manager when changed to be used in laying
17732 * out windows behind it.
17733 */
17734 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17735 = new ViewTreeObserver.InternalInsetsInfo();
17736
17737 /**
17738 * All views in the window's hierarchy that serve as scroll containers,
17739 * used to determine if the window can be resized or must be panned
17740 * to adjust for a soft input area.
17741 */
17742 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17743
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017744 final KeyEvent.DispatcherState mKeyDispatchState
17745 = new KeyEvent.DispatcherState();
17746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017747 /**
17748 * Indicates whether the view's window currently has the focus.
17749 */
17750 boolean mHasWindowFocus;
17751
17752 /**
17753 * The current visibility of the window.
17754 */
17755 int mWindowVisibility;
17756
17757 /**
17758 * Indicates the time at which drawing started to occur.
17759 */
17760 long mDrawingTime;
17761
17762 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017763 * Indicates whether or not ignoring the DIRTY_MASK flags.
17764 */
17765 boolean mIgnoreDirtyState;
17766
17767 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017768 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17769 * to avoid clearing that flag prematurely.
17770 */
17771 boolean mSetIgnoreDirtyState = false;
17772
17773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017774 * Indicates whether the view's window is currently in touch mode.
17775 */
17776 boolean mInTouchMode;
17777
17778 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017779 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017780 * the next time it performs a traversal
17781 */
17782 boolean mRecomputeGlobalAttributes;
17783
17784 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017785 * Always report new attributes at next traversal.
17786 */
17787 boolean mForceReportNewAttributes;
17788
17789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017790 * Set during a traveral if any views want to keep the screen on.
17791 */
17792 boolean mKeepScreenOn;
17793
17794 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017795 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17796 */
17797 int mSystemUiVisibility;
17798
17799 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017800 * Hack to force certain system UI visibility flags to be cleared.
17801 */
17802 int mDisabledSystemUiVisibility;
17803
17804 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017805 * Last global system UI visibility reported by the window manager.
17806 */
17807 int mGlobalSystemUiVisibility;
17808
17809 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017810 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17811 * attached.
17812 */
17813 boolean mHasSystemUiListeners;
17814
17815 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017816 * Set if the visibility of any views has changed.
17817 */
17818 boolean mViewVisibilityChanged;
17819
17820 /**
17821 * Set to true if a view has been scrolled.
17822 */
17823 boolean mViewScrollChanged;
17824
17825 /**
17826 * Global to the view hierarchy used as a temporary for dealing with
17827 * x/y points in the transparent region computations.
17828 */
17829 final int[] mTransparentLocation = new int[2];
17830
17831 /**
17832 * Global to the view hierarchy used as a temporary for dealing with
17833 * x/y points in the ViewGroup.invalidateChild implementation.
17834 */
17835 final int[] mInvalidateChildLocation = new int[2];
17836
Chet Haasec3aa3612010-06-17 08:50:37 -070017837
17838 /**
17839 * Global to the view hierarchy used as a temporary for dealing with
17840 * x/y location when view is transformed.
17841 */
17842 final float[] mTmpTransformLocation = new float[2];
17843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017844 /**
17845 * The view tree observer used to dispatch global events like
17846 * layout, pre-draw, touch mode change, etc.
17847 */
17848 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17849
17850 /**
17851 * A Canvas used by the view hierarchy to perform bitmap caching.
17852 */
17853 Canvas mCanvas;
17854
17855 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017856 * The view root impl.
17857 */
17858 final ViewRootImpl mViewRootImpl;
17859
17860 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017861 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017862 * handler can be used to pump events in the UI events queue.
17863 */
17864 final Handler mHandler;
17865
17866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017867 * Temporary for use in computing invalidate rectangles while
17868 * calling up the hierarchy.
17869 */
17870 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017871
17872 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017873 * Temporary for use in computing hit areas with transformed views
17874 */
17875 final RectF mTmpTransformRect = new RectF();
17876
17877 /**
Chet Haase599913d2012-07-23 16:22:05 -070017878 * Temporary for use in transforming invalidation rect
17879 */
17880 final Matrix mTmpMatrix = new Matrix();
17881
17882 /**
17883 * Temporary for use in transforming invalidation rect
17884 */
17885 final Transformation mTmpTransformation = new Transformation();
17886
17887 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017888 * Temporary list for use in collecting focusable descendents of a view.
17889 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017890 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017892 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017893 * The id of the window for accessibility purposes.
17894 */
17895 int mAccessibilityWindowId = View.NO_ID;
17896
17897 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017898 * Whether to ingore not exposed for accessibility Views when
17899 * reporting the view tree to accessibility services.
17900 */
17901 boolean mIncludeNotImportantViews;
17902
17903 /**
17904 * The drawable for highlighting accessibility focus.
17905 */
17906 Drawable mAccessibilityFocusDrawable;
17907
17908 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017909 * Show where the margins, bounds and layout bounds are for each view.
17910 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017911 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017912
17913 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017914 * Point used to compute visible regions.
17915 */
17916 final Point mPoint = new Point();
17917
17918 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017919 * Creates a new set of attachment information with the specified
17920 * events handler and thread.
17921 *
17922 * @param handler the events handler the view must use
17923 */
Jeff Brown98365d72012-08-19 20:30:52 -070017924 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017925 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017926 mSession = session;
17927 mWindow = window;
17928 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017929 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017930 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017931 mHandler = handler;
17932 mRootCallbacks = effectPlayer;
17933 }
17934 }
17935
17936 /**
17937 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17938 * is supported. This avoids keeping too many unused fields in most
17939 * instances of View.</p>
17940 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017941 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017942
Mike Cleronf116bf82009-09-27 19:14:12 -070017943 /**
17944 * Scrollbars are not visible
17945 */
17946 public static final int OFF = 0;
17947
17948 /**
17949 * Scrollbars are visible
17950 */
17951 public static final int ON = 1;
17952
17953 /**
17954 * Scrollbars are fading away
17955 */
17956 public static final int FADING = 2;
17957
17958 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017960 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017961 public int scrollBarDefaultDelayBeforeFade;
17962 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017963
17964 public int scrollBarSize;
17965 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017966 public float[] interpolatorValues;
17967 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017968
17969 public final Paint paint;
17970 public final Matrix matrix;
17971 public Shader shader;
17972
Mike Cleronf116bf82009-09-27 19:14:12 -070017973 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17974
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017975 private static final float[] OPAQUE = { 255 };
17976 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017977
Mike Cleronf116bf82009-09-27 19:14:12 -070017978 /**
17979 * When fading should start. This time moves into the future every time
17980 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17981 */
17982 public long fadeStartTime;
17983
17984
17985 /**
17986 * The current state of the scrollbars: ON, OFF, or FADING
17987 */
17988 public int state = OFF;
17989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017990 private int mLastColor;
17991
Mike Cleronf116bf82009-09-27 19:14:12 -070017992 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017993 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17994 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017995 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17996 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017997
17998 paint = new Paint();
17999 matrix = new Matrix();
18000 // use use a height of 1, and then wack the matrix each time we
18001 // actually use it.
18002 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018003 paint.setShader(shader);
18004 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018005
Mike Cleronf116bf82009-09-27 19:14:12 -070018006 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018007 }
Romain Guy8506ab42009-06-11 17:35:47 -070018008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018009 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018010 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018011 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018012
Romain Guyd679b572012-08-29 21:49:00 -070018013 if (color != 0) {
18014 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18015 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18016 paint.setShader(shader);
18017 // Restore the default transfer mode (src_over)
18018 paint.setXfermode(null);
18019 } else {
18020 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18021 paint.setShader(shader);
18022 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018024 }
18025 }
Joe Malin32736f02011-01-19 16:14:20 -080018026
Mike Cleronf116bf82009-09-27 19:14:12 -070018027 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018028 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018029 if (now >= fadeStartTime) {
18030
18031 // the animation fades the scrollbars out by changing
18032 // the opacity (alpha) from fully opaque to fully
18033 // transparent
18034 int nextFrame = (int) now;
18035 int framesCount = 0;
18036
18037 Interpolator interpolator = scrollBarInterpolator;
18038
18039 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018040 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018041
18042 // End transparent
18043 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018044 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018045
18046 state = FADING;
18047
18048 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018049 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018050 }
18051 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018052 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018053
Svetoslav Ganova0156172011-06-26 17:55:44 -070018054 /**
18055 * Resuable callback for sending
18056 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18057 */
18058 private class SendViewScrolledAccessibilityEvent implements Runnable {
18059 public volatile boolean mIsPending;
18060
18061 public void run() {
18062 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18063 mIsPending = false;
18064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018065 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018066
18067 /**
18068 * <p>
18069 * This class represents a delegate that can be registered in a {@link View}
18070 * to enhance accessibility support via composition rather via inheritance.
18071 * It is specifically targeted to widget developers that extend basic View
18072 * classes i.e. classes in package android.view, that would like their
18073 * applications to be backwards compatible.
18074 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018075 * <div class="special reference">
18076 * <h3>Developer Guides</h3>
18077 * <p>For more information about making applications accessible, read the
18078 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18079 * developer guide.</p>
18080 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018081 * <p>
18082 * A scenario in which a developer would like to use an accessibility delegate
18083 * is overriding a method introduced in a later API version then the minimal API
18084 * version supported by the application. For example, the method
18085 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18086 * in API version 4 when the accessibility APIs were first introduced. If a
18087 * developer would like his application to run on API version 4 devices (assuming
18088 * all other APIs used by the application are version 4 or lower) and take advantage
18089 * of this method, instead of overriding the method which would break the application's
18090 * backwards compatibility, he can override the corresponding method in this
18091 * delegate and register the delegate in the target View if the API version of
18092 * the system is high enough i.e. the API version is same or higher to the API
18093 * version that introduced
18094 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18095 * </p>
18096 * <p>
18097 * Here is an example implementation:
18098 * </p>
18099 * <code><pre><p>
18100 * if (Build.VERSION.SDK_INT >= 14) {
18101 * // If the API version is equal of higher than the version in
18102 * // which onInitializeAccessibilityNodeInfo was introduced we
18103 * // register a delegate with a customized implementation.
18104 * View view = findViewById(R.id.view_id);
18105 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18106 * public void onInitializeAccessibilityNodeInfo(View host,
18107 * AccessibilityNodeInfo info) {
18108 * // Let the default implementation populate the info.
18109 * super.onInitializeAccessibilityNodeInfo(host, info);
18110 * // Set some other information.
18111 * info.setEnabled(host.isEnabled());
18112 * }
18113 * });
18114 * }
18115 * </code></pre></p>
18116 * <p>
18117 * This delegate contains methods that correspond to the accessibility methods
18118 * in View. If a delegate has been specified the implementation in View hands
18119 * off handling to the corresponding method in this delegate. The default
18120 * implementation the delegate methods behaves exactly as the corresponding
18121 * method in View for the case of no accessibility delegate been set. Hence,
18122 * to customize the behavior of a View method, clients can override only the
18123 * corresponding delegate method without altering the behavior of the rest
18124 * accessibility related methods of the host view.
18125 * </p>
18126 */
18127 public static class AccessibilityDelegate {
18128
18129 /**
18130 * Sends an accessibility event of the given type. If accessibility is not
18131 * enabled this method has no effect.
18132 * <p>
18133 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18134 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18135 * been set.
18136 * </p>
18137 *
18138 * @param host The View hosting the delegate.
18139 * @param eventType The type of the event to send.
18140 *
18141 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18142 */
18143 public void sendAccessibilityEvent(View host, int eventType) {
18144 host.sendAccessibilityEventInternal(eventType);
18145 }
18146
18147 /**
alanv8eeefef2012-05-07 16:57:53 -070018148 * Performs the specified accessibility action on the view. For
18149 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18150 * <p>
18151 * The default implementation behaves as
18152 * {@link View#performAccessibilityAction(int, Bundle)
18153 * View#performAccessibilityAction(int, Bundle)} for the case of
18154 * no accessibility delegate been set.
18155 * </p>
18156 *
18157 * @param action The action to perform.
18158 * @return Whether the action was performed.
18159 *
18160 * @see View#performAccessibilityAction(int, Bundle)
18161 * View#performAccessibilityAction(int, Bundle)
18162 */
18163 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18164 return host.performAccessibilityActionInternal(action, args);
18165 }
18166
18167 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018168 * Sends an accessibility event. This method behaves exactly as
18169 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18170 * empty {@link AccessibilityEvent} and does not perform a check whether
18171 * accessibility is enabled.
18172 * <p>
18173 * The default implementation behaves as
18174 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18175 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18176 * the case of no accessibility delegate been set.
18177 * </p>
18178 *
18179 * @param host The View hosting the delegate.
18180 * @param event The event to send.
18181 *
18182 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18183 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18184 */
18185 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18186 host.sendAccessibilityEventUncheckedInternal(event);
18187 }
18188
18189 /**
18190 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18191 * to its children for adding their text content to the event.
18192 * <p>
18193 * The default implementation behaves as
18194 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18195 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18196 * the case of no accessibility delegate been set.
18197 * </p>
18198 *
18199 * @param host The View hosting the delegate.
18200 * @param event The event.
18201 * @return True if the event population was completed.
18202 *
18203 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18204 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18205 */
18206 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18207 return host.dispatchPopulateAccessibilityEventInternal(event);
18208 }
18209
18210 /**
18211 * Gives a chance to the host View to populate the accessibility event with its
18212 * text content.
18213 * <p>
18214 * The default implementation behaves as
18215 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18216 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18217 * the case of no accessibility delegate been set.
18218 * </p>
18219 *
18220 * @param host The View hosting the delegate.
18221 * @param event The accessibility event which to populate.
18222 *
18223 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18224 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18225 */
18226 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18227 host.onPopulateAccessibilityEventInternal(event);
18228 }
18229
18230 /**
18231 * Initializes an {@link AccessibilityEvent} with information about the
18232 * the host View which is the event source.
18233 * <p>
18234 * The default implementation behaves as
18235 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18236 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18237 * the case of no accessibility delegate been set.
18238 * </p>
18239 *
18240 * @param host The View hosting the delegate.
18241 * @param event The event to initialize.
18242 *
18243 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18244 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18245 */
18246 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18247 host.onInitializeAccessibilityEventInternal(event);
18248 }
18249
18250 /**
18251 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18252 * <p>
18253 * The default implementation behaves as
18254 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18255 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18256 * the case of no accessibility delegate been set.
18257 * </p>
18258 *
18259 * @param host The View hosting the delegate.
18260 * @param info The instance to initialize.
18261 *
18262 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18263 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18264 */
18265 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18266 host.onInitializeAccessibilityNodeInfoInternal(info);
18267 }
18268
18269 /**
18270 * Called when a child of the host View has requested sending an
18271 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18272 * to augment the event.
18273 * <p>
18274 * The default implementation behaves as
18275 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18276 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18277 * the case of no accessibility delegate been set.
18278 * </p>
18279 *
18280 * @param host The View hosting the delegate.
18281 * @param child The child which requests sending the event.
18282 * @param event The event to be sent.
18283 * @return True if the event should be sent
18284 *
18285 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18286 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18287 */
18288 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18289 AccessibilityEvent event) {
18290 return host.onRequestSendAccessibilityEventInternal(child, event);
18291 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018292
18293 /**
18294 * Gets the provider for managing a virtual view hierarchy rooted at this View
18295 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18296 * that explore the window content.
18297 * <p>
18298 * The default implementation behaves as
18299 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18300 * the case of no accessibility delegate been set.
18301 * </p>
18302 *
18303 * @return The provider.
18304 *
18305 * @see AccessibilityNodeProvider
18306 */
18307 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18308 return null;
18309 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018310 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018311
18312 private class MatchIdPredicate implements Predicate<View> {
18313 public int mId;
18314
18315 @Override
18316 public boolean apply(View view) {
18317 return (view.mID == mId);
18318 }
18319 }
18320
18321 private class MatchLabelForPredicate implements Predicate<View> {
18322 private int mLabeledId;
18323
18324 @Override
18325 public boolean apply(View view) {
18326 return (view.mLabelForId == mLabeledId);
18327 }
18328 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018329
18330 /**
18331 * Dump all private flags in readable format, useful for documentation and
18332 * sanity checking.
18333 */
18334 private static void dumpFlags() {
18335 final HashMap<String, String> found = Maps.newHashMap();
18336 try {
18337 for (Field field : View.class.getDeclaredFields()) {
18338 final int modifiers = field.getModifiers();
18339 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18340 if (field.getType().equals(int.class)) {
18341 final int value = field.getInt(null);
18342 dumpFlag(found, field.getName(), value);
18343 } else if (field.getType().equals(int[].class)) {
18344 final int[] values = (int[]) field.get(null);
18345 for (int i = 0; i < values.length; i++) {
18346 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18347 }
18348 }
18349 }
18350 }
18351 } catch (IllegalAccessException e) {
18352 throw new RuntimeException(e);
18353 }
18354
18355 final ArrayList<String> keys = Lists.newArrayList();
18356 keys.addAll(found.keySet());
18357 Collections.sort(keys);
18358 for (String key : keys) {
18359 Log.d(VIEW_LOG_TAG, found.get(key));
18360 }
18361 }
18362
18363 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18364 // Sort flags by prefix, then by bits, always keeping unique keys
18365 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18366 final int prefix = name.indexOf('_');
18367 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18368 final String output = bits + " " + name;
18369 found.put(key, output);
18370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018371}