blob: d5e1ed3015f39cdc92174a7ccd5d829c74987d94 [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.
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001867 */
1868 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001869
Adam Powell539ee872012-02-03 19:00:49 -08001870 /**
1871 * Indicates that the view is tracking some sort of transient state
1872 * that the app should not need to be aware of, but that the framework
1873 * should take special care to preserve.
1874 *
1875 * @hide
1876 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001877 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001878
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001879 /**
1880 * Text direction is inherited thru {@link ViewGroup}
1881 */
1882 public static final int TEXT_DIRECTION_INHERIT = 0;
1883
1884 /**
1885 * Text direction is using "first strong algorithm". The first strong directional character
1886 * determines the paragraph direction. If there is no strong directional character, the
1887 * paragraph direction is the view's resolved layout direction.
1888 */
1889 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1890
1891 /**
1892 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1893 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1894 * If there are neither, the paragraph direction is the view's resolved layout direction.
1895 */
1896 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1897
1898 /**
1899 * Text direction is forced to LTR.
1900 */
1901 public static final int TEXT_DIRECTION_LTR = 3;
1902
1903 /**
1904 * Text direction is forced to RTL.
1905 */
1906 public static final int TEXT_DIRECTION_RTL = 4;
1907
1908 /**
1909 * Text direction is coming from the system Locale.
1910 */
1911 public static final int TEXT_DIRECTION_LOCALE = 5;
1912
1913 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001914 * Default text direction is inherited
1915 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07001916 private static final int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001917
1918 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001919 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1920 * @hide
1921 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001922 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001923
1924 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001925 * Mask for use with private flags indicating bits used for text direction.
1926 * @hide
1927 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001928 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1929 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001930
1931 /**
1932 * Array of text direction flags for mapping attribute "textDirection" to correct
1933 * flag value.
1934 * @hide
1935 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001936 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1937 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1938 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001943 };
1944
1945 /**
1946 * Indicates whether the view text direction has been resolved.
1947 * @hide
1948 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001949 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1950 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001951
1952 /**
1953 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1954 * @hide
1955 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001956 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001957
1958 /**
1959 * Mask for use with private flags indicating bits used for resolved text direction.
1960 * @hide
1961 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001962 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1963 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001964
1965 /**
1966 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1967 * @hide
1968 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001969 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1970 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001971
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001972 /*
1973 * Default text alignment. The text alignment of this View is inherited from its parent.
1974 * Use with {@link #setTextAlignment(int)}
1975 */
1976 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1977
1978 /**
1979 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1980 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1981 *
1982 * Use with {@link #setTextAlignment(int)}
1983 */
1984 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1985
1986 /**
1987 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1988 *
1989 * Use with {@link #setTextAlignment(int)}
1990 */
1991 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1992
1993 /**
1994 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1995 *
1996 * Use with {@link #setTextAlignment(int)}
1997 */
1998 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1999
2000 /**
2001 * Center the paragraph, e.g. ALIGN_CENTER.
2002 *
2003 * Use with {@link #setTextAlignment(int)}
2004 */
2005 public static final int TEXT_ALIGNMENT_CENTER = 4;
2006
2007 /**
2008 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2009 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2010 *
2011 * Use with {@link #setTextAlignment(int)}
2012 */
2013 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2014
2015 /**
2016 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2017 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2018 *
2019 * Use with {@link #setTextAlignment(int)}
2020 */
2021 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2022
2023 /**
2024 * Default text alignment is inherited
2025 */
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07002026 private static final int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002027
2028 /**
2029 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2030 * @hide
2031 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002032 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002033
2034 /**
2035 * Mask for use with private flags indicating bits used for text alignment.
2036 * @hide
2037 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002038 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002039
2040 /**
2041 * Array of text direction flags for mapping attribute "textAlignment" to correct
2042 * flag value.
2043 * @hide
2044 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002045 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2046 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2047 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002053 };
2054
2055 /**
2056 * Indicates whether the view text alignment has been resolved.
2057 * @hide
2058 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002059 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002060
2061 /**
2062 * Bit shift to get the resolved text alignment.
2063 * @hide
2064 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002065 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002066
2067 /**
2068 * Mask for use with private flags indicating bits used for text alignment.
2069 * @hide
2070 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002071 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2072 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002073
2074 /**
2075 * Indicates whether if the view text alignment has been resolved to gravity
2076 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002077 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2078 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002079
Svetoslav Ganov42138042012-03-20 11:51:39 -07002080 // Accessiblity constants for mPrivateFlags2
2081
2082 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002083 * Shift for the bits in {@link #mPrivateFlags2} related to the
2084 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002085 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002086 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002087
2088 /**
2089 * Automatically determine whether a view is important for accessibility.
2090 */
2091 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2092
2093 /**
2094 * The view is important for accessibility.
2095 */
2096 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2097
2098 /**
2099 * The view is not important for accessibility.
2100 */
2101 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2102
2103 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002104 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002105 */
2106 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2107
2108 /**
2109 * Mask for obtainig the bits which specify how to determine
2110 * whether a view is important for accessibility.
2111 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002112 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002113 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002114 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002115
2116 /**
2117 * Flag indicating whether a view has accessibility focus.
2118 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002119 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002120
2121 /**
2122 * Flag indicating whether a view state for accessibility has changed.
2123 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002124 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2125 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002126
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002127 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002128 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2129 * is used to check whether later changes to the view's transform should invalidate the
2130 * view to force the quickReject test to run again.
2131 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002132 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002133
Adam Powell0090f202012-08-07 17:15:30 -07002134 /**
2135 * Flag indicating that start/end padding has been resolved into left/right padding
2136 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2137 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2138 * during measurement. In some special cases this is required such as when an adapter-based
2139 * view measures prospective children without attaching them to a window.
2140 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002141 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002142
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002143 /**
2144 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2145 */
2146 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2147
2148 /**
2149 * Group of bits indicating that RTL properties resolution is done.
2150 */
2151 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002152 PFLAG2_TEXT_DIRECTION_RESOLVED |
2153 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2154 PFLAG2_PADDING_RESOLVED |
2155 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002156
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002157 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002158
Christopher Tate3d4bf172011-03-28 16:16:46 -07002159 /* End of masks for mPrivateFlags2 */
2160
Chet Haase21433372012-06-05 07:54:09 -07002161 /* Masks for mPrivateFlags3 */
2162
2163 /**
2164 * Flag indicating that view has a transform animation set on it. This is used to track whether
2165 * an animation is cleared between successive frames, in order to tell the associated
2166 * DisplayList to clear its animation matrix.
2167 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002168 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002169
2170 /**
2171 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2172 * animation is cleared between successive frames, in order to tell the associated
2173 * DisplayList to restore its alpha value.
2174 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002175 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002176
2177
2178 /* End of masks for mPrivateFlags3 */
2179
Dianne Hackborn4702a852012-08-17 15:18:29 -07002180 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002181
Chet Haasedaf98e92011-01-10 14:10:36 -08002182 /**
Adam Powell637d3372010-08-25 14:37:03 -07002183 * Always allow a user to over-scroll this view, provided it is a
2184 * view that can scroll.
2185 *
2186 * @see #getOverScrollMode()
2187 * @see #setOverScrollMode(int)
2188 */
2189 public static final int OVER_SCROLL_ALWAYS = 0;
2190
2191 /**
2192 * Allow a user to over-scroll this view only if the content is large
2193 * enough to meaningfully scroll, provided it is a view that can scroll.
2194 *
2195 * @see #getOverScrollMode()
2196 * @see #setOverScrollMode(int)
2197 */
2198 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2199
2200 /**
2201 * Never allow a user to over-scroll this view.
2202 *
2203 * @see #getOverScrollMode()
2204 * @see #setOverScrollMode(int)
2205 */
2206 public static final int OVER_SCROLL_NEVER = 2;
2207
2208 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002209 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2210 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002211 *
Joe Malin32736f02011-01-19 16:14:20 -08002212 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002213 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002214 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002215
2216 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002217 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2218 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002219 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002220 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002221 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002222 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002223 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002224 *
Joe Malin32736f02011-01-19 16:14:20 -08002225 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002226 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002227 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2228
2229 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002230 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2231 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002232 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002233 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002234 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2235 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2236 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002237 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002238 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2239 * window flags) for displaying content using every last pixel on the display.
2240 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002241 * <p>There is a limitation: because navigation controls are so important, the least user
2242 * interaction will cause them to reappear immediately. When this happens, both
2243 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2244 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002245 *
2246 * @see #setSystemUiVisibility(int)
2247 */
2248 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2249
2250 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002251 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2252 * into the normal fullscreen mode so that its content can take over the screen
2253 * while still allowing the user to interact with the application.
2254 *
2255 * <p>This has the same visual effect as
2256 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2257 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2258 * meaning that non-critical screen decorations (such as the status bar) will be
2259 * hidden while the user is in the View's window, focusing the experience on
2260 * that content. Unlike the window flag, if you are using ActionBar in
2261 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2262 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2263 * hide the action bar.
2264 *
2265 * <p>This approach to going fullscreen is best used over the window flag when
2266 * it is a transient state -- that is, the application does this at certain
2267 * points in its user interaction where it wants to allow the user to focus
2268 * on content, but not as a continuous state. For situations where the application
2269 * would like to simply stay full screen the entire time (such as a game that
2270 * wants to take over the screen), the
2271 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2272 * is usually a better approach. The state set here will be removed by the system
2273 * in various situations (such as the user moving to another application) like
2274 * the other system UI states.
2275 *
2276 * <p>When using this flag, the application should provide some easy facility
2277 * for the user to go out of it. A common example would be in an e-book
2278 * reader, where tapping on the screen brings back whatever screen and UI
2279 * decorations that had been hidden while the user was immersed in reading
2280 * the book.
2281 *
2282 * @see #setSystemUiVisibility(int)
2283 */
2284 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2285
2286 /**
2287 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2288 * flags, we would like a stable view of the content insets given to
2289 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2290 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002291 * as a continuous state. In the stock Android UI this is the space for
2292 * the system bar, nav bar, and status bar, but not more transient elements
2293 * such as an input method.
2294 *
2295 * The stable layout your UI sees is based on the system UI modes you can
2296 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2297 * then you will get a stable layout for changes of the
2298 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2299 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2300 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2301 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2302 * with a stable layout. (Note that you should avoid using
2303 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2304 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002305 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002306 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2307 * then a hidden status bar will be considered a "stable" state for purposes
2308 * here. This allows your UI to continually hide the status bar, while still
2309 * using the system UI flags to hide the action bar while still retaining
2310 * a stable layout. Note that changing the window fullscreen flag will never
2311 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002312 *
2313 * <p>If you are using ActionBar in
2314 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2315 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2316 * insets it adds to those given to the application.
2317 */
2318 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2319
2320 /**
2321 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2322 * to be layed out as if it has requested
2323 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2324 * allows it to avoid artifacts when switching in and out of that mode, at
2325 * the expense that some of its user interface may be covered by screen
2326 * decorations when they are shown. You can perform layout of your inner
2327 * UI elements to account for the navagation system UI through the
2328 * {@link #fitSystemWindows(Rect)} method.
2329 */
2330 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2331
2332 /**
2333 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2334 * to be layed out as if it has requested
2335 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2336 * allows it to avoid artifacts when switching in and out of that mode, at
2337 * the expense that some of its user interface may be covered by screen
2338 * decorations when they are shown. You can perform layout of your inner
2339 * UI elements to account for non-fullscreen system UI through the
2340 * {@link #fitSystemWindows(Rect)} method.
2341 */
2342 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2343
2344 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002345 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2346 */
2347 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2348
2349 /**
2350 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2351 */
2352 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002353
2354 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002355 * @hide
2356 *
2357 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2358 * out of the public fields to keep the undefined bits out of the developer's way.
2359 *
2360 * Flag to make the status bar not expandable. Unless you also
2361 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2362 */
2363 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2364
2365 /**
2366 * @hide
2367 *
2368 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2369 * out of the public fields to keep the undefined bits out of the developer's way.
2370 *
2371 * Flag to hide notification icons and scrolling ticker text.
2372 */
2373 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2374
2375 /**
2376 * @hide
2377 *
2378 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2379 * out of the public fields to keep the undefined bits out of the developer's way.
2380 *
2381 * Flag to disable incoming notification alerts. This will not block
2382 * icons, but it will block sound, vibrating and other visual or aural notifications.
2383 */
2384 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2385
2386 /**
2387 * @hide
2388 *
2389 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2390 * out of the public fields to keep the undefined bits out of the developer's way.
2391 *
2392 * Flag to hide only the scrolling ticker. Note that
2393 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2394 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2395 */
2396 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2397
2398 /**
2399 * @hide
2400 *
2401 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2402 * out of the public fields to keep the undefined bits out of the developer's way.
2403 *
2404 * Flag to hide the center system info area.
2405 */
2406 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2407
2408 /**
2409 * @hide
2410 *
2411 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2412 * out of the public fields to keep the undefined bits out of the developer's way.
2413 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002414 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002415 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2416 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002417 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002418
2419 /**
2420 * @hide
2421 *
2422 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2423 * out of the public fields to keep the undefined bits out of the developer's way.
2424 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002425 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002426 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2427 */
2428 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2429
2430 /**
2431 * @hide
2432 *
2433 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2434 * out of the public fields to keep the undefined bits out of the developer's way.
2435 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002436 * Flag to hide only the clock. You might use this if your activity has
2437 * its own clock making the status bar's clock redundant.
2438 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002439 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2440
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002441 /**
2442 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002443 *
2444 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2445 * out of the public fields to keep the undefined bits out of the developer's way.
2446 *
2447 * Flag to hide only the recent apps button. Don't use this
2448 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2449 */
2450 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2451
2452 /**
2453 * @hide
Daniel Sandlerd5483c32012-10-19 16:44:15 -04002454 *
2455 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2456 * out of the public fields to keep the undefined bits out of the developer's way.
2457 *
2458 * Flag to disable the global search gesture. Don't use this
2459 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2460 */
2461 public static final int STATUS_BAR_DISABLE_SEARCH = 0x02000000;
2462
2463 /**
2464 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002465 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002466 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002467
2468 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002469 * These are the system UI flags that can be cleared by events outside
2470 * of an application. Currently this is just the ability to tap on the
2471 * screen while hiding the navigation bar to have it return.
2472 * @hide
2473 */
2474 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002475 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2476 | SYSTEM_UI_FLAG_FULLSCREEN;
2477
2478 /**
2479 * Flags that can impact the layout in relation to system UI.
2480 */
2481 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2482 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2483 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002484
2485 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002486 * Find views that render the specified text.
2487 *
2488 * @see #findViewsWithText(ArrayList, CharSequence, int)
2489 */
2490 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2491
2492 /**
2493 * Find find views that contain the specified content description.
2494 *
2495 * @see #findViewsWithText(ArrayList, CharSequence, int)
2496 */
2497 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2498
2499 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002500 * Find views that contain {@link AccessibilityNodeProvider}. Such
2501 * a View is a root of virtual view hierarchy and may contain the searched
2502 * text. If this flag is set Views with providers are automatically
2503 * added and it is a responsibility of the client to call the APIs of
2504 * the provider to determine whether the virtual tree rooted at this View
2505 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2506 * represeting the virtual views with this text.
2507 *
2508 * @see #findViewsWithText(ArrayList, CharSequence, int)
2509 *
2510 * @hide
2511 */
2512 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2513
2514 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002515 * The undefined cursor position.
2516 */
2517 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2518
2519 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002520 * Indicates that the screen has changed state and is now off.
2521 *
2522 * @see #onScreenStateChanged(int)
2523 */
2524 public static final int SCREEN_STATE_OFF = 0x0;
2525
2526 /**
2527 * Indicates that the screen has changed state and is now on.
2528 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002529 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002530 */
2531 public static final int SCREEN_STATE_ON = 0x1;
2532
2533 /**
Adam Powell637d3372010-08-25 14:37:03 -07002534 * Controls the over-scroll mode for this view.
2535 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2536 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2537 * and {@link #OVER_SCROLL_NEVER}.
2538 */
2539 private int mOverScrollMode;
2540
2541 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 * The parent this view is attached to.
2543 * {@hide}
2544 *
2545 * @see #getParent()
2546 */
2547 protected ViewParent mParent;
2548
2549 /**
2550 * {@hide}
2551 */
2552 AttachInfo mAttachInfo;
2553
2554 /**
2555 * {@hide}
2556 */
Romain Guy809a7f62009-05-14 15:44:42 -07002557 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002558 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002559 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002560 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002561 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002562 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002563 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002564 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2565 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2566 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2567 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002568 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002570 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002571 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572
2573 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002574 * This view's request for the visibility of the status bar.
2575 * @hide
2576 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002577 @ViewDebug.ExportedProperty(flagMapping = {
2578 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2579 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2580 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2581 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2582 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2583 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2584 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2585 equals = SYSTEM_UI_FLAG_VISIBLE,
2586 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2587 })
Joe Onorato664644d2011-01-23 17:53:23 -08002588 int mSystemUiVisibility;
2589
2590 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002591 * Reference count for transient state.
2592 * @see #setHasTransientState(boolean)
2593 */
2594 int mTransientStateCount = 0;
2595
2596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * Count of how many windows this view has been attached to.
2598 */
2599 int mWindowAttachCount;
2600
2601 /**
2602 * The layout parameters associated with this view and used by the parent
2603 * {@link android.view.ViewGroup} to determine how this view should be
2604 * laid out.
2605 * {@hide}
2606 */
2607 protected ViewGroup.LayoutParams mLayoutParams;
2608
2609 /**
2610 * The view flags hold various views states.
2611 * {@hide}
2612 */
2613 @ViewDebug.ExportedProperty
2614 int mViewFlags;
2615
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002616 static class TransformationInfo {
2617 /**
2618 * The transform matrix for the View. This transform is calculated internally
2619 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2620 * is used by default. Do *not* use this variable directly; instead call
2621 * getMatrix(), which will automatically recalculate the matrix if necessary
2622 * to get the correct matrix based on the latest rotation and scale properties.
2623 */
2624 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002625
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002626 /**
2627 * The transform matrix for the View. This transform is calculated internally
2628 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2629 * is used by default. Do *not* use this variable directly; instead call
2630 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2631 * to get the correct matrix based on the latest rotation and scale properties.
2632 */
2633 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002634
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002635 /**
2636 * An internal variable that tracks whether we need to recalculate the
2637 * transform matrix, based on whether the rotation or scaleX/Y properties
2638 * have changed since the matrix was last calculated.
2639 */
2640 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002641
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002642 /**
2643 * An internal variable that tracks whether we need to recalculate the
2644 * transform matrix, based on whether the rotation or scaleX/Y properties
2645 * have changed since the matrix was last calculated.
2646 */
2647 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002648
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002649 /**
2650 * A variable that tracks whether we need to recalculate the
2651 * transform matrix, based on whether the rotation or scaleX/Y properties
2652 * have changed since the matrix was last calculated. This variable
2653 * is only valid after a call to updateMatrix() or to a function that
2654 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2655 */
2656 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002658 /**
2659 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2660 */
2661 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002662
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002663 /**
2664 * This matrix is used when computing the matrix for 3D rotations.
2665 */
2666 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002667
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002668 /**
2669 * These prev values are used to recalculate a centered pivot point when necessary. The
2670 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2671 * set), so thes values are only used then as well.
2672 */
2673 private int mPrevWidth = -1;
2674 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002675
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002676 /**
2677 * The degrees rotation around the vertical axis through the pivot point.
2678 */
2679 @ViewDebug.ExportedProperty
2680 float mRotationY = 0f;
2681
2682 /**
2683 * The degrees rotation around the horizontal axis through the pivot point.
2684 */
2685 @ViewDebug.ExportedProperty
2686 float mRotationX = 0f;
2687
2688 /**
2689 * The degrees rotation around the pivot point.
2690 */
2691 @ViewDebug.ExportedProperty
2692 float mRotation = 0f;
2693
2694 /**
2695 * The amount of translation of the object away from its left property (post-layout).
2696 */
2697 @ViewDebug.ExportedProperty
2698 float mTranslationX = 0f;
2699
2700 /**
2701 * The amount of translation of the object away from its top property (post-layout).
2702 */
2703 @ViewDebug.ExportedProperty
2704 float mTranslationY = 0f;
2705
2706 /**
2707 * The amount of scale in the x direction around the pivot point. A
2708 * value of 1 means no scaling is applied.
2709 */
2710 @ViewDebug.ExportedProperty
2711 float mScaleX = 1f;
2712
2713 /**
2714 * The amount of scale in the y direction around the pivot point. A
2715 * value of 1 means no scaling is applied.
2716 */
2717 @ViewDebug.ExportedProperty
2718 float mScaleY = 1f;
2719
2720 /**
Chet Haasea33de552012-02-03 16:28:24 -08002721 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002722 */
2723 @ViewDebug.ExportedProperty
2724 float mPivotX = 0f;
2725
2726 /**
Chet Haasea33de552012-02-03 16:28:24 -08002727 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002728 */
2729 @ViewDebug.ExportedProperty
2730 float mPivotY = 0f;
2731
2732 /**
2733 * The opacity of the View. This is a value from 0 to 1, where 0 means
2734 * completely transparent and 1 means completely opaque.
2735 */
2736 @ViewDebug.ExportedProperty
2737 float mAlpha = 1f;
2738 }
2739
2740 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002741
Joe Malin32736f02011-01-19 16:14:20 -08002742 private boolean mLastIsOpaque;
2743
Chet Haasefd2b0022010-08-06 13:08:56 -07002744 /**
2745 * Convenience value to check for float values that are close enough to zero to be considered
2746 * zero.
2747 */
Romain Guy2542d192010-08-18 11:47:12 -07002748 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002749
2750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 * The distance in pixels from the left edge of this view's parent
2752 * to the left edge of this view.
2753 * {@hide}
2754 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002755 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 protected int mLeft;
2757 /**
2758 * The distance in pixels from the left edge of this view's parent
2759 * to the right edge of this view.
2760 * {@hide}
2761 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002762 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 protected int mRight;
2764 /**
2765 * The distance in pixels from the top edge of this view's parent
2766 * to the top edge of this view.
2767 * {@hide}
2768 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002769 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 protected int mTop;
2771 /**
2772 * The distance in pixels from the top edge of this view's parent
2773 * to the bottom edge of this view.
2774 * {@hide}
2775 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002776 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 protected int mBottom;
2778
2779 /**
2780 * The offset, in pixels, by which the content of this view is scrolled
2781 * horizontally.
2782 * {@hide}
2783 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002784 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 protected int mScrollX;
2786 /**
2787 * The offset, in pixels, by which the content of this view is scrolled
2788 * vertically.
2789 * {@hide}
2790 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002791 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 protected int mScrollY;
2793
2794 /**
2795 * The left padding in pixels, that is the distance in pixels between the
2796 * left edge of this view and the left edge of its content.
2797 * {@hide}
2798 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002799 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002800 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 /**
2802 * The right padding in pixels, that is the distance in pixels between the
2803 * right edge of this view and the right edge of its content.
2804 * {@hide}
2805 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002806 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002807 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 /**
2809 * The top padding in pixels, that is the distance in pixels between the
2810 * top edge of this view and the top edge of its content.
2811 * {@hide}
2812 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002813 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 protected int mPaddingTop;
2815 /**
2816 * The bottom padding in pixels, that is the distance in pixels between the
2817 * bottom edge of this view and the bottom edge of its content.
2818 * {@hide}
2819 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002820 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 protected int mPaddingBottom;
2822
2823 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002824 * The layout insets in pixels, that is the distance in pixels between the
2825 * visible edges of this view its bounds.
2826 */
2827 private Insets mLayoutInsets;
2828
2829 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002830 * Briefly describes the view and is primarily used for accessibility support.
2831 */
2832 private CharSequence mContentDescription;
2833
2834 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002835 * Specifies the id of a view for which this view serves as a label for
2836 * accessibility purposes.
2837 */
2838 private int mLabelForId = View.NO_ID;
2839
2840 /**
2841 * Predicate for matching labeled view id with its label for
2842 * accessibility purposes.
2843 */
2844 private MatchLabelForPredicate mMatchLabelForPredicate;
2845
2846 /**
2847 * Predicate for matching a view by its id.
2848 */
2849 private MatchIdPredicate mMatchIdPredicate;
2850
2851 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002853 *
2854 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002856 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002857 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858
2859 /**
2860 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002861 *
2862 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002864 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002865 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002867 /**
Adam Powell20232d02010-12-08 21:08:53 -08002868 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002869 *
2870 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002871 */
2872 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002873 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002874
2875 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002876 * Cache the paddingStart set by the user to append to the scrollbar's size.
2877 *
2878 */
2879 @ViewDebug.ExportedProperty(category = "padding")
2880 int mUserPaddingStart;
2881
2882 /**
2883 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2884 *
2885 */
2886 @ViewDebug.ExportedProperty(category = "padding")
2887 int mUserPaddingEnd;
2888
2889 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002890 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002891 *
2892 * @hide
2893 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002894 int mUserPaddingLeftInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002895
2896 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002897 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002898 *
2899 * @hide
2900 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002901 int mUserPaddingRightInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002902
2903 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002904 * Default undefined padding
2905 */
2906 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2907
2908 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002909 * @hide
2910 */
2911 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2912 /**
2913 * @hide
2914 */
2915 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916
Romain Guydfab3632012-10-03 14:53:25 -07002917 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002918 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002919
2920 private int mBackgroundResource;
2921 private boolean mBackgroundSizeChanged;
2922
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002923 static class ListenerInfo {
2924 /**
2925 * Listener used to dispatch focus change events.
2926 * This field should be made private, so it is hidden from the SDK.
2927 * {@hide}
2928 */
2929 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002931 /**
2932 * Listeners for layout change events.
2933 */
2934 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002935
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002936 /**
2937 * Listeners for attach events.
2938 */
2939 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002940
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002941 /**
2942 * Listener used to dispatch click events.
2943 * This field should be made private, so it is hidden from the SDK.
2944 * {@hide}
2945 */
2946 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002948 /**
2949 * Listener used to dispatch long click events.
2950 * This field should be made private, so it is hidden from the SDK.
2951 * {@hide}
2952 */
2953 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002955 /**
2956 * Listener used to build the context menu.
2957 * This field should be made private, so it is hidden from the SDK.
2958 * {@hide}
2959 */
2960 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002962 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002964 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002966 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002967
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002968 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002969
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002970 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002971
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002972 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2973 }
2974
2975 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 /**
2978 * The application environment this view lives in.
2979 * This field should be made private, so it is hidden from the SDK.
2980 * {@hide}
2981 */
2982 protected Context mContext;
2983
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002984 private final Resources mResources;
2985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 private ScrollabilityCache mScrollCache;
2987
2988 private int[] mDrawableState = null;
2989
Romain Guy0211a0a2011-02-14 16:34:59 -08002990 /**
2991 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002992 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002993 * @hide
2994 */
2995 public boolean mCachingFailed;
2996
Romain Guy02890fd2010-08-06 17:58:44 -07002997 private Bitmap mDrawingCache;
2998 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002999 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07003000 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001
3002 /**
3003 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
3004 * the user may specify which view to go to next.
3005 */
3006 private int mNextFocusLeftId = View.NO_ID;
3007
3008 /**
3009 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3010 * the user may specify which view to go to next.
3011 */
3012 private int mNextFocusRightId = View.NO_ID;
3013
3014 /**
3015 * When this view has focus and the next focus is {@link #FOCUS_UP},
3016 * the user may specify which view to go to next.
3017 */
3018 private int mNextFocusUpId = View.NO_ID;
3019
3020 /**
3021 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3022 * the user may specify which view to go to next.
3023 */
3024 private int mNextFocusDownId = View.NO_ID;
3025
Jeff Brown4e6319b2010-12-13 10:36:51 -08003026 /**
3027 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3028 * the user may specify which view to go to next.
3029 */
3030 int mNextFocusForwardId = View.NO_ID;
3031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003033 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003034 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003035 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 private UnsetPressedState mUnsetPressedState;
3038
3039 /**
3040 * Whether the long press's action has been invoked. The tap's action is invoked on the
3041 * up event while a long press is invoked as soon as the long press duration is reached, so
3042 * a long press could be performed before the tap is checked, in which case the tap's action
3043 * should not be invoked.
3044 */
3045 private boolean mHasPerformedLongPress;
3046
3047 /**
3048 * The minimum height of the view. We'll try our best to have the height
3049 * of this view to at least this amount.
3050 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003051 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003052 private int mMinHeight;
3053
3054 /**
3055 * The minimum width of the view. We'll try our best to have the width
3056 * of this view to at least this amount.
3057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003058 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 private int mMinWidth;
3060
3061 /**
3062 * The delegate to handle touch events that are physically in this view
3063 * but should be handled by another view.
3064 */
3065 private TouchDelegate mTouchDelegate = null;
3066
3067 /**
3068 * Solid color to use as a background when creating the drawing cache. Enables
3069 * the cache to use 16 bit bitmaps instead of 32 bit.
3070 */
3071 private int mDrawingCacheBackgroundColor = 0;
3072
3073 /**
3074 * Special tree observer used when mAttachInfo is null.
3075 */
3076 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003077
Adam Powelle14579b2009-12-16 18:39:52 -08003078 /**
3079 * Cache the touch slop from the context that created the view.
3080 */
3081 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003084 * Object that handles automatic animation of view properties.
3085 */
3086 private ViewPropertyAnimator mAnimator = null;
3087
3088 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003089 * Flag indicating that a drag can cross window boundaries. When
3090 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3091 * with this flag set, all visible applications will be able to participate
3092 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003093 *
3094 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003095 */
3096 public static final int DRAG_FLAG_GLOBAL = 1;
3097
3098 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003099 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3100 */
3101 private float mVerticalScrollFactor;
3102
3103 /**
Adam Powell20232d02010-12-08 21:08:53 -08003104 * Position of the vertical scroll bar.
3105 */
3106 private int mVerticalScrollbarPosition;
3107
3108 /**
3109 * Position the scroll bar at the default position as determined by the system.
3110 */
3111 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3112
3113 /**
3114 * Position the scroll bar along the left edge.
3115 */
3116 public static final int SCROLLBAR_POSITION_LEFT = 1;
3117
3118 /**
3119 * Position the scroll bar along the right edge.
3120 */
3121 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3122
3123 /**
Romain Guy171c5922011-01-06 10:04:23 -08003124 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003125 *
3126 * @see #getLayerType()
3127 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003128 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003129 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003130 */
3131 public static final int LAYER_TYPE_NONE = 0;
3132
3133 /**
3134 * <p>Indicates that the view has a software layer. A software layer is backed
3135 * by a bitmap and causes the view to be rendered using Android's software
3136 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003137 *
Romain Guy171c5922011-01-06 10:04:23 -08003138 * <p>Software layers have various usages:</p>
3139 * <p>When the application is not using hardware acceleration, a software layer
3140 * is useful to apply a specific color filter and/or blending mode and/or
3141 * translucency to a view and all its children.</p>
3142 * <p>When the application is using hardware acceleration, a software layer
3143 * is useful to render drawing primitives not supported by the hardware
3144 * accelerated pipeline. It can also be used to cache a complex view tree
3145 * into a texture and reduce the complexity of drawing operations. For instance,
3146 * when animating a complex view tree with a translation, a software layer can
3147 * be used to render the view tree only once.</p>
3148 * <p>Software layers should be avoided when the affected view tree updates
3149 * often. Every update will require to re-render the software layer, which can
3150 * potentially be slow (particularly when hardware acceleration is turned on
3151 * since the layer will have to be uploaded into a hardware texture after every
3152 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003153 *
3154 * @see #getLayerType()
3155 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003156 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003157 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003158 */
3159 public static final int LAYER_TYPE_SOFTWARE = 1;
3160
3161 /**
3162 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3163 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3164 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3165 * rendering pipeline, but only if hardware acceleration is turned on for the
3166 * view hierarchy. When hardware acceleration is turned off, hardware layers
3167 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003168 *
Romain Guy171c5922011-01-06 10:04:23 -08003169 * <p>A hardware layer is useful to apply a specific color filter and/or
3170 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003171 * <p>A hardware layer can be used to cache a complex view tree into a
3172 * texture and reduce the complexity of drawing operations. For instance,
3173 * when animating a complex view tree with a translation, a hardware layer can
3174 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003175 * <p>A hardware layer can also be used to increase the rendering quality when
3176 * rotation transformations are applied on a view. It can also be used to
3177 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003178 *
3179 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003180 * @see #setLayerType(int, android.graphics.Paint)
3181 * @see #LAYER_TYPE_NONE
3182 * @see #LAYER_TYPE_SOFTWARE
3183 */
3184 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003185
Romain Guy3aaff3a2011-01-12 14:18:47 -08003186 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3187 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3188 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3189 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3190 })
Romain Guy171c5922011-01-06 10:04:23 -08003191 int mLayerType = LAYER_TYPE_NONE;
3192 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003193 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003194
3195 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003196 * Set to true when the view is sending hover accessibility events because it
3197 * is the innermost hovered view.
3198 */
3199 private boolean mSendingHoverAccessibilityEvents;
3200
Dianne Hackborn4702a852012-08-17 15:18:29 -07003201 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003202 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003203 */
3204 AccessibilityDelegate mAccessibilityDelegate;
3205
3206 /**
3207 * Consistency verifier for debugging purposes.
3208 * @hide
3209 */
3210 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3211 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3212 new InputEventConsistencyVerifier(this, 0) : null;
3213
Adam Powella9108a22012-07-18 11:18:09 -07003214 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3215
Jeff Brown87b7f802011-06-21 18:35:45 -07003216 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 * Simple constructor to use when creating a view from code.
3218 *
3219 * @param context The Context the view is running in, through which it can
3220 * access the current theme, resources, etc.
3221 */
3222 public View(Context context) {
3223 mContext = context;
3224 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003225 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Meglio72898e92012-10-25 11:22:39 -07003226 // Set some flags defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003227 mPrivateFlags2 =
3228 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003229 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003230 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003231 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003232 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003233 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003234 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003235 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003236 mUserPaddingStart = UNDEFINED_PADDING;
3237 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239
3240 /**
3241 * Constructor that is called when inflating a view from XML. This is called
3242 * when a view is being constructed from an XML file, supplying attributes
3243 * that were specified in the XML file. This version uses a default style of
3244 * 0, so the only attribute values applied are those in the Context's Theme
3245 * and the given AttributeSet.
3246 *
3247 * <p>
3248 * The method onFinishInflate() will be called after all children have been
3249 * added.
3250 *
3251 * @param context The Context the view is running in, through which it can
3252 * access the current theme, resources, etc.
3253 * @param attrs The attributes of the XML tag that is inflating the view.
3254 * @see #View(Context, AttributeSet, int)
3255 */
3256 public View(Context context, AttributeSet attrs) {
3257 this(context, attrs, 0);
3258 }
3259
3260 /**
3261 * Perform inflation from XML and apply a class-specific base style. This
3262 * constructor of View allows subclasses to use their own base style when
3263 * they are inflating. For example, a Button class's constructor would call
3264 * this version of the super class constructor and supply
3265 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3266 * the theme's button style to modify all of the base view attributes (in
3267 * particular its background) as well as the Button class's attributes.
3268 *
3269 * @param context The Context the view is running in, through which it can
3270 * access the current theme, resources, etc.
3271 * @param attrs The attributes of the XML tag that is inflating the view.
3272 * @param defStyle The default style to apply to this view. If 0, no style
3273 * will be applied (beyond what is included in the theme). This may
3274 * either be an attribute resource, whose value will be retrieved
3275 * from the current theme, or an explicit style resource.
3276 * @see #View(Context, AttributeSet)
3277 */
3278 public View(Context context, AttributeSet attrs, int defStyle) {
3279 this(context);
3280
3281 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3282 defStyle, 0);
3283
3284 Drawable background = null;
3285
3286 int leftPadding = -1;
3287 int topPadding = -1;
3288 int rightPadding = -1;
3289 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003290 int startPadding = UNDEFINED_PADDING;
3291 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292
3293 int padding = -1;
3294
3295 int viewFlagValues = 0;
3296 int viewFlagMasks = 0;
3297
3298 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 int x = 0;
3301 int y = 0;
3302
Chet Haase73066682010-11-29 15:55:32 -08003303 float tx = 0;
3304 float ty = 0;
3305 float rotation = 0;
3306 float rotationX = 0;
3307 float rotationY = 0;
3308 float sx = 1f;
3309 float sy = 1f;
3310 boolean transformSet = false;
3311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003313 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003314 boolean initializeScrollbars = false;
3315
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003316 boolean leftPaddingDefined = false;
3317 boolean rightPaddingDefined = false;
3318 boolean startPaddingDefined = false;
3319 boolean endPaddingDefined = false;
3320
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003321 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 final int N = a.getIndexCount();
3324 for (int i = 0; i < N; i++) {
3325 int attr = a.getIndex(i);
3326 switch (attr) {
3327 case com.android.internal.R.styleable.View_background:
3328 background = a.getDrawable(attr);
3329 break;
3330 case com.android.internal.R.styleable.View_padding:
3331 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003332 mUserPaddingLeftInitial = padding;
3333 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003334 leftPaddingDefined = true;
3335 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 break;
3337 case com.android.internal.R.styleable.View_paddingLeft:
3338 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003339 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003340 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 break;
3342 case com.android.internal.R.styleable.View_paddingTop:
3343 topPadding = a.getDimensionPixelSize(attr, -1);
3344 break;
3345 case com.android.internal.R.styleable.View_paddingRight:
3346 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003347 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003348 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 break;
3350 case com.android.internal.R.styleable.View_paddingBottom:
3351 bottomPadding = a.getDimensionPixelSize(attr, -1);
3352 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003353 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003354 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003355 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003356 break;
3357 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003358 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003359 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003360 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 case com.android.internal.R.styleable.View_scrollX:
3362 x = a.getDimensionPixelOffset(attr, 0);
3363 break;
3364 case com.android.internal.R.styleable.View_scrollY:
3365 y = a.getDimensionPixelOffset(attr, 0);
3366 break;
Chet Haase73066682010-11-29 15:55:32 -08003367 case com.android.internal.R.styleable.View_alpha:
3368 setAlpha(a.getFloat(attr, 1f));
3369 break;
3370 case com.android.internal.R.styleable.View_transformPivotX:
3371 setPivotX(a.getDimensionPixelOffset(attr, 0));
3372 break;
3373 case com.android.internal.R.styleable.View_transformPivotY:
3374 setPivotY(a.getDimensionPixelOffset(attr, 0));
3375 break;
3376 case com.android.internal.R.styleable.View_translationX:
3377 tx = a.getDimensionPixelOffset(attr, 0);
3378 transformSet = true;
3379 break;
3380 case com.android.internal.R.styleable.View_translationY:
3381 ty = a.getDimensionPixelOffset(attr, 0);
3382 transformSet = true;
3383 break;
3384 case com.android.internal.R.styleable.View_rotation:
3385 rotation = a.getFloat(attr, 0);
3386 transformSet = true;
3387 break;
3388 case com.android.internal.R.styleable.View_rotationX:
3389 rotationX = a.getFloat(attr, 0);
3390 transformSet = true;
3391 break;
3392 case com.android.internal.R.styleable.View_rotationY:
3393 rotationY = a.getFloat(attr, 0);
3394 transformSet = true;
3395 break;
3396 case com.android.internal.R.styleable.View_scaleX:
3397 sx = a.getFloat(attr, 1f);
3398 transformSet = true;
3399 break;
3400 case com.android.internal.R.styleable.View_scaleY:
3401 sy = a.getFloat(attr, 1f);
3402 transformSet = true;
3403 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 case com.android.internal.R.styleable.View_id:
3405 mID = a.getResourceId(attr, NO_ID);
3406 break;
3407 case com.android.internal.R.styleable.View_tag:
3408 mTag = a.getText(attr);
3409 break;
3410 case com.android.internal.R.styleable.View_fitsSystemWindows:
3411 if (a.getBoolean(attr, false)) {
3412 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3413 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3414 }
3415 break;
3416 case com.android.internal.R.styleable.View_focusable:
3417 if (a.getBoolean(attr, false)) {
3418 viewFlagValues |= FOCUSABLE;
3419 viewFlagMasks |= FOCUSABLE_MASK;
3420 }
3421 break;
3422 case com.android.internal.R.styleable.View_focusableInTouchMode:
3423 if (a.getBoolean(attr, false)) {
3424 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3425 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3426 }
3427 break;
3428 case com.android.internal.R.styleable.View_clickable:
3429 if (a.getBoolean(attr, false)) {
3430 viewFlagValues |= CLICKABLE;
3431 viewFlagMasks |= CLICKABLE;
3432 }
3433 break;
3434 case com.android.internal.R.styleable.View_longClickable:
3435 if (a.getBoolean(attr, false)) {
3436 viewFlagValues |= LONG_CLICKABLE;
3437 viewFlagMasks |= LONG_CLICKABLE;
3438 }
3439 break;
3440 case com.android.internal.R.styleable.View_saveEnabled:
3441 if (!a.getBoolean(attr, true)) {
3442 viewFlagValues |= SAVE_DISABLED;
3443 viewFlagMasks |= SAVE_DISABLED_MASK;
3444 }
3445 break;
3446 case com.android.internal.R.styleable.View_duplicateParentState:
3447 if (a.getBoolean(attr, false)) {
3448 viewFlagValues |= DUPLICATE_PARENT_STATE;
3449 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3450 }
3451 break;
3452 case com.android.internal.R.styleable.View_visibility:
3453 final int visibility = a.getInt(attr, 0);
3454 if (visibility != 0) {
3455 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3456 viewFlagMasks |= VISIBILITY_MASK;
3457 }
3458 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003459 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003460 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003461 mPrivateFlags2 &=
3462 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003463 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003464 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003465 final int value = (layoutDirection != -1) ?
3466 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003467 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003468 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 case com.android.internal.R.styleable.View_drawingCacheQuality:
3470 final int cacheQuality = a.getInt(attr, 0);
3471 if (cacheQuality != 0) {
3472 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3473 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3474 }
3475 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003476 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003477 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003478 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003479 case com.android.internal.R.styleable.View_labelFor:
3480 setLabelFor(a.getResourceId(attr, NO_ID));
3481 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3483 if (!a.getBoolean(attr, true)) {
3484 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3485 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3486 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003487 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3489 if (!a.getBoolean(attr, true)) {
3490 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3491 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3492 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003493 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 case R.styleable.View_scrollbars:
3495 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3496 if (scrollbars != SCROLLBARS_NONE) {
3497 viewFlagValues |= scrollbars;
3498 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003499 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 }
3501 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003502 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003504 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003505 // Ignore the attribute starting with ICS
3506 break;
3507 }
3508 // With builds < ICS, fall through and apply fading edges
3509 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3511 if (fadingEdge != FADING_EDGE_NONE) {
3512 viewFlagValues |= fadingEdge;
3513 viewFlagMasks |= FADING_EDGE_MASK;
3514 initializeFadingEdge(a);
3515 }
3516 break;
3517 case R.styleable.View_scrollbarStyle:
3518 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3519 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3520 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3521 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3522 }
3523 break;
3524 case R.styleable.View_isScrollContainer:
3525 setScrollContainer = true;
3526 if (a.getBoolean(attr, false)) {
3527 setScrollContainer(true);
3528 }
3529 break;
3530 case com.android.internal.R.styleable.View_keepScreenOn:
3531 if (a.getBoolean(attr, false)) {
3532 viewFlagValues |= KEEP_SCREEN_ON;
3533 viewFlagMasks |= KEEP_SCREEN_ON;
3534 }
3535 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003536 case R.styleable.View_filterTouchesWhenObscured:
3537 if (a.getBoolean(attr, false)) {
3538 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3539 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3540 }
3541 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 case R.styleable.View_nextFocusLeft:
3543 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3544 break;
3545 case R.styleable.View_nextFocusRight:
3546 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3547 break;
3548 case R.styleable.View_nextFocusUp:
3549 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3550 break;
3551 case R.styleable.View_nextFocusDown:
3552 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3553 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003554 case R.styleable.View_nextFocusForward:
3555 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3556 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 case R.styleable.View_minWidth:
3558 mMinWidth = a.getDimensionPixelSize(attr, 0);
3559 break;
3560 case R.styleable.View_minHeight:
3561 mMinHeight = a.getDimensionPixelSize(attr, 0);
3562 break;
Romain Guy9a817362009-05-01 10:57:14 -07003563 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003564 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003565 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003566 + "be used within a restricted context");
3567 }
3568
Romain Guy9a817362009-05-01 10:57:14 -07003569 final String handlerName = a.getString(attr);
3570 if (handlerName != null) {
3571 setOnClickListener(new OnClickListener() {
3572 private Method mHandler;
3573
3574 public void onClick(View v) {
3575 if (mHandler == null) {
3576 try {
3577 mHandler = getContext().getClass().getMethod(handlerName,
3578 View.class);
3579 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003580 int id = getId();
3581 String idText = id == NO_ID ? "" : " with id '"
3582 + getContext().getResources().getResourceEntryName(
3583 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003584 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003585 handlerName + "(View) in the activity "
3586 + getContext().getClass() + " for onClick handler"
3587 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003588 }
3589 }
3590
3591 try {
3592 mHandler.invoke(getContext(), View.this);
3593 } catch (IllegalAccessException e) {
3594 throw new IllegalStateException("Could not execute non "
3595 + "public method of the activity", e);
3596 } catch (InvocationTargetException e) {
3597 throw new IllegalStateException("Could not execute "
3598 + "method of the activity", e);
3599 }
3600 }
3601 });
3602 }
3603 break;
Adam Powell637d3372010-08-25 14:37:03 -07003604 case R.styleable.View_overScrollMode:
3605 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3606 break;
Adam Powell20232d02010-12-08 21:08:53 -08003607 case R.styleable.View_verticalScrollbarPosition:
3608 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3609 break;
Romain Guy171c5922011-01-06 10:04:23 -08003610 case R.styleable.View_layerType:
3611 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3612 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003613 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003614 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003615 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003616 // Set the text direction flags depending on the value of the attribute
3617 final int textDirection = a.getInt(attr, -1);
3618 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003619 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003620 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003621 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003622 case R.styleable.View_textAlignment:
3623 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003624 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003625 // Set the text alignment flag depending on the value of the attribute
3626 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003627 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003628 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003629 case R.styleable.View_importantForAccessibility:
3630 setImportantForAccessibility(a.getInt(attr,
3631 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003632 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 }
3634 }
3635
Adam Powell637d3372010-08-25 14:37:03 -07003636 setOverScrollMode(overScrollMode);
3637
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003638 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3639 // the resolved layout direction). Those cached values will be used later during padding
3640 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003641 mUserPaddingStart = startPadding;
3642 mUserPaddingEnd = endPadding;
3643
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003644 if (background != null) {
3645 setBackground(background);
3646 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 if (padding >= 0) {
3649 leftPadding = padding;
3650 topPadding = padding;
3651 rightPadding = padding;
3652 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003653 mUserPaddingLeftInitial = padding;
3654 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
3656
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003657 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003658 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3659 // left / right padding are used if defined (meaning here nothing to do). If they are not
3660 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3661 // start / end and resolve them as left / right (layout direction is not taken into account).
3662 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3663 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3664 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003665 if (!leftPaddingDefined && startPaddingDefined) {
3666 leftPadding = startPadding;
3667 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003668 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003669 if (!rightPaddingDefined && endPaddingDefined) {
3670 rightPadding = endPadding;
3671 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003672 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003673 } else {
3674 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3675 // values defined. Otherwise, left /right values are used.
3676 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3677 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3678 // defined.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003679 if (leftPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003680 mUserPaddingLeftInitial = leftPadding;
3681 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003682 if (rightPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003683 mUserPaddingRightInitial = rightPadding;
3684 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003685 }
3686
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003687 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003688 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003690 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3692
3693 if (viewFlagMasks != 0) {
3694 setFlags(viewFlagValues, viewFlagMasks);
3695 }
3696
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003697 if (initializeScrollbars) {
3698 initializeScrollbars(a);
3699 }
3700
3701 a.recycle();
3702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 // Needs to be called after mViewFlags is set
3704 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3705 recomputePadding();
3706 }
3707
3708 if (x != 0 || y != 0) {
3709 scrollTo(x, y);
3710 }
3711
Chet Haase73066682010-11-29 15:55:32 -08003712 if (transformSet) {
3713 setTranslationX(tx);
3714 setTranslationY(ty);
3715 setRotation(rotation);
3716 setRotationX(rotationX);
3717 setRotationY(rotationY);
3718 setScaleX(sx);
3719 setScaleY(sy);
3720 }
3721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3723 setScrollContainer(true);
3724 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003725
3726 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 }
3728
3729 /**
3730 * Non-public constructor for use in testing
3731 */
3732 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003733 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 }
3735
Dianne Hackborn4702a852012-08-17 15:18:29 -07003736 public String toString() {
3737 StringBuilder out = new StringBuilder(128);
3738 out.append(getClass().getName());
3739 out.append('{');
3740 out.append(Integer.toHexString(System.identityHashCode(this)));
3741 out.append(' ');
3742 switch (mViewFlags&VISIBILITY_MASK) {
3743 case VISIBLE: out.append('V'); break;
3744 case INVISIBLE: out.append('I'); break;
3745 case GONE: out.append('G'); break;
3746 default: out.append('.'); break;
3747 }
3748 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3749 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3750 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3751 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3752 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3753 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3754 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3755 out.append(' ');
3756 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3757 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3758 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3759 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3760 out.append('p');
3761 } else {
3762 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3763 }
3764 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3765 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3766 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3767 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3768 out.append(' ');
3769 out.append(mLeft);
3770 out.append(',');
3771 out.append(mTop);
3772 out.append('-');
3773 out.append(mRight);
3774 out.append(',');
3775 out.append(mBottom);
3776 final int id = getId();
3777 if (id != NO_ID) {
3778 out.append(" #");
3779 out.append(Integer.toHexString(id));
3780 final Resources r = mResources;
3781 if (id != 0 && r != null) {
3782 try {
3783 String pkgname;
3784 switch (id&0xff000000) {
3785 case 0x7f000000:
3786 pkgname="app";
3787 break;
3788 case 0x01000000:
3789 pkgname="android";
3790 break;
3791 default:
3792 pkgname = r.getResourcePackageName(id);
3793 break;
3794 }
3795 String typename = r.getResourceTypeName(id);
3796 String entryname = r.getResourceEntryName(id);
3797 out.append(" ");
3798 out.append(pkgname);
3799 out.append(":");
3800 out.append(typename);
3801 out.append("/");
3802 out.append(entryname);
3803 } catch (Resources.NotFoundException e) {
3804 }
3805 }
3806 }
3807 out.append("}");
3808 return out.toString();
3809 }
3810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 /**
3812 * <p>
3813 * Initializes the fading edges from a given set of styled attributes. This
3814 * method should be called by subclasses that need fading edges and when an
3815 * instance of these subclasses is created programmatically rather than
3816 * being inflated from XML. This method is automatically called when the XML
3817 * is inflated.
3818 * </p>
3819 *
3820 * @param a the styled attributes set to initialize the fading edges from
3821 */
3822 protected void initializeFadingEdge(TypedArray a) {
3823 initScrollCache();
3824
3825 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3826 R.styleable.View_fadingEdgeLength,
3827 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3828 }
3829
3830 /**
3831 * Returns the size of the vertical faded edges used to indicate that more
3832 * content in this view is visible.
3833 *
3834 * @return The size in pixels of the vertical faded edge or 0 if vertical
3835 * faded edges are not enabled for this view.
3836 * @attr ref android.R.styleable#View_fadingEdgeLength
3837 */
3838 public int getVerticalFadingEdgeLength() {
3839 if (isVerticalFadingEdgeEnabled()) {
3840 ScrollabilityCache cache = mScrollCache;
3841 if (cache != null) {
3842 return cache.fadingEdgeLength;
3843 }
3844 }
3845 return 0;
3846 }
3847
3848 /**
3849 * Set the size of the faded edge used to indicate that more content in this
3850 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003851 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3852 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3853 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 *
3855 * @param length The size in pixels of the faded edge used to indicate that more
3856 * content in this view is visible.
3857 */
3858 public void setFadingEdgeLength(int length) {
3859 initScrollCache();
3860 mScrollCache.fadingEdgeLength = length;
3861 }
3862
3863 /**
3864 * Returns the size of the horizontal faded edges used to indicate that more
3865 * content in this view is visible.
3866 *
3867 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3868 * faded edges are not enabled for this view.
3869 * @attr ref android.R.styleable#View_fadingEdgeLength
3870 */
3871 public int getHorizontalFadingEdgeLength() {
3872 if (isHorizontalFadingEdgeEnabled()) {
3873 ScrollabilityCache cache = mScrollCache;
3874 if (cache != null) {
3875 return cache.fadingEdgeLength;
3876 }
3877 }
3878 return 0;
3879 }
3880
3881 /**
3882 * Returns the width of the vertical scrollbar.
3883 *
3884 * @return The width in pixels of the vertical scrollbar or 0 if there
3885 * is no vertical scrollbar.
3886 */
3887 public int getVerticalScrollbarWidth() {
3888 ScrollabilityCache cache = mScrollCache;
3889 if (cache != null) {
3890 ScrollBarDrawable scrollBar = cache.scrollBar;
3891 if (scrollBar != null) {
3892 int size = scrollBar.getSize(true);
3893 if (size <= 0) {
3894 size = cache.scrollBarSize;
3895 }
3896 return size;
3897 }
3898 return 0;
3899 }
3900 return 0;
3901 }
3902
3903 /**
3904 * Returns the height of the horizontal scrollbar.
3905 *
3906 * @return The height in pixels of the horizontal scrollbar or 0 if
3907 * there is no horizontal scrollbar.
3908 */
3909 protected int getHorizontalScrollbarHeight() {
3910 ScrollabilityCache cache = mScrollCache;
3911 if (cache != null) {
3912 ScrollBarDrawable scrollBar = cache.scrollBar;
3913 if (scrollBar != null) {
3914 int size = scrollBar.getSize(false);
3915 if (size <= 0) {
3916 size = cache.scrollBarSize;
3917 }
3918 return size;
3919 }
3920 return 0;
3921 }
3922 return 0;
3923 }
3924
3925 /**
3926 * <p>
3927 * Initializes the scrollbars from a given set of styled attributes. This
3928 * method should be called by subclasses that need scrollbars and when an
3929 * instance of these subclasses is created programmatically rather than
3930 * being inflated from XML. This method is automatically called when the XML
3931 * is inflated.
3932 * </p>
3933 *
3934 * @param a the styled attributes set to initialize the scrollbars from
3935 */
3936 protected void initializeScrollbars(TypedArray a) {
3937 initScrollCache();
3938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003940
Mike Cleronf116bf82009-09-27 19:14:12 -07003941 if (scrollabilityCache.scrollBar == null) {
3942 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3943 }
Joe Malin32736f02011-01-19 16:14:20 -08003944
Romain Guy8bda2482010-03-02 11:42:11 -08003945 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946
Mike Cleronf116bf82009-09-27 19:14:12 -07003947 if (!fadeScrollbars) {
3948 scrollabilityCache.state = ScrollabilityCache.ON;
3949 }
3950 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003951
3952
Mike Cleronf116bf82009-09-27 19:14:12 -07003953 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3954 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3955 .getScrollBarFadeDuration());
3956 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3957 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3958 ViewConfiguration.getScrollDefaultDelay());
3959
Joe Malin32736f02011-01-19 16:14:20 -08003960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003961 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3962 com.android.internal.R.styleable.View_scrollbarSize,
3963 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3964
3965 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3966 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3967
3968 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3969 if (thumb != null) {
3970 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3971 }
3972
3973 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3974 false);
3975 if (alwaysDraw) {
3976 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3977 }
3978
3979 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3980 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3981
3982 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3983 if (thumb != null) {
3984 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3985 }
3986
3987 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3988 false);
3989 if (alwaysDraw) {
3990 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3991 }
3992
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003993 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003994 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003995 if (track != null) {
3996 track.setLayoutDirection(layoutDirection);
3997 }
3998 if (thumb != null) {
3999 thumb.setLayoutDirection(layoutDirection);
4000 }
4001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004003 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005
4006 /**
4007 * <p>
4008 * Initalizes the scrollability cache if necessary.
4009 * </p>
4010 */
4011 private void initScrollCache() {
4012 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004013 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 }
4015 }
4016
Philip Milne6c8ea062012-04-03 17:38:43 -07004017 private ScrollabilityCache getScrollCache() {
4018 initScrollCache();
4019 return mScrollCache;
4020 }
4021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 /**
Adam Powell20232d02010-12-08 21:08:53 -08004023 * Set the position of the vertical scroll bar. Should be one of
4024 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4025 * {@link #SCROLLBAR_POSITION_RIGHT}.
4026 *
4027 * @param position Where the vertical scroll bar should be positioned.
4028 */
4029 public void setVerticalScrollbarPosition(int position) {
4030 if (mVerticalScrollbarPosition != position) {
4031 mVerticalScrollbarPosition = position;
4032 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004033 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004034 }
4035 }
4036
4037 /**
4038 * @return The position where the vertical scroll bar will show, if applicable.
4039 * @see #setVerticalScrollbarPosition(int)
4040 */
4041 public int getVerticalScrollbarPosition() {
4042 return mVerticalScrollbarPosition;
4043 }
4044
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004045 ListenerInfo getListenerInfo() {
4046 if (mListenerInfo != null) {
4047 return mListenerInfo;
4048 }
4049 mListenerInfo = new ListenerInfo();
4050 return mListenerInfo;
4051 }
4052
Adam Powell20232d02010-12-08 21:08:53 -08004053 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 * Register a callback to be invoked when focus of this view changed.
4055 *
4056 * @param l The callback that will run.
4057 */
4058 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004059 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 }
4061
4062 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004063 * Add a listener that will be called when the bounds of the view change due to
4064 * layout processing.
4065 *
4066 * @param listener The listener that will be called when layout bounds change.
4067 */
4068 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004069 ListenerInfo li = getListenerInfo();
4070 if (li.mOnLayoutChangeListeners == null) {
4071 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004072 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004073 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4074 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004075 }
Chet Haase21cd1382010-09-01 17:42:29 -07004076 }
4077
4078 /**
4079 * Remove a listener for layout changes.
4080 *
4081 * @param listener The listener for layout bounds change.
4082 */
4083 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004084 ListenerInfo li = mListenerInfo;
4085 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004086 return;
4087 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004088 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004089 }
4090
4091 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004092 * Add a listener for attach state changes.
4093 *
4094 * This listener will be called whenever this view is attached or detached
4095 * from a window. Remove the listener using
4096 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4097 *
4098 * @param listener Listener to attach
4099 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4100 */
4101 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004102 ListenerInfo li = getListenerInfo();
4103 if (li.mOnAttachStateChangeListeners == null) {
4104 li.mOnAttachStateChangeListeners
4105 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004106 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004107 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004108 }
4109
4110 /**
4111 * Remove a listener for attach state changes. The listener will receive no further
4112 * notification of window attach/detach events.
4113 *
4114 * @param listener Listener to remove
4115 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4116 */
4117 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004118 ListenerInfo li = mListenerInfo;
4119 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004120 return;
4121 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004122 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004123 }
4124
4125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 * Returns the focus-change callback registered for this view.
4127 *
4128 * @return The callback, or null if one is not registered.
4129 */
4130 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004131 ListenerInfo li = mListenerInfo;
4132 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
4134
4135 /**
4136 * Register a callback to be invoked when this view is clicked. If this view is not
4137 * clickable, it becomes clickable.
4138 *
4139 * @param l The callback that will run
4140 *
4141 * @see #setClickable(boolean)
4142 */
4143 public void setOnClickListener(OnClickListener l) {
4144 if (!isClickable()) {
4145 setClickable(true);
4146 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004147 getListenerInfo().mOnClickListener = l;
4148 }
4149
4150 /**
4151 * Return whether this view has an attached OnClickListener. Returns
4152 * true if there is a listener, false if there is none.
4153 */
4154 public boolean hasOnClickListeners() {
4155 ListenerInfo li = mListenerInfo;
4156 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 }
4158
4159 /**
4160 * Register a callback to be invoked when this view is clicked and held. If this view is not
4161 * long clickable, it becomes long clickable.
4162 *
4163 * @param l The callback that will run
4164 *
4165 * @see #setLongClickable(boolean)
4166 */
4167 public void setOnLongClickListener(OnLongClickListener l) {
4168 if (!isLongClickable()) {
4169 setLongClickable(true);
4170 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004171 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173
4174 /**
4175 * Register a callback to be invoked when the context menu for this view is
4176 * being built. If this view is not long clickable, it becomes long clickable.
4177 *
4178 * @param l The callback that will run
4179 *
4180 */
4181 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4182 if (!isLongClickable()) {
4183 setLongClickable(true);
4184 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004185 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 }
4187
4188 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004189 * Call this view's OnClickListener, if it is defined. Performs all normal
4190 * actions associated with clicking: reporting accessibility event, playing
4191 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 *
4193 * @return True there was an assigned OnClickListener that was called, false
4194 * otherwise is returned.
4195 */
4196 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004197 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4198
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004199 ListenerInfo li = mListenerInfo;
4200 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004202 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 return true;
4204 }
4205
4206 return false;
4207 }
4208
4209 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004210 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4211 * this only calls the listener, and does not do any associated clicking
4212 * actions like reporting an accessibility event.
4213 *
4214 * @return True there was an assigned OnClickListener that was called, false
4215 * otherwise is returned.
4216 */
4217 public boolean callOnClick() {
4218 ListenerInfo li = mListenerInfo;
4219 if (li != null && li.mOnClickListener != null) {
4220 li.mOnClickListener.onClick(this);
4221 return true;
4222 }
4223 return false;
4224 }
4225
4226 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004227 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4228 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004230 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 */
4232 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004233 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004236 ListenerInfo li = mListenerInfo;
4237 if (li != null && li.mOnLongClickListener != null) {
4238 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 }
4240 if (!handled) {
4241 handled = showContextMenu();
4242 }
4243 if (handled) {
4244 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4245 }
4246 return handled;
4247 }
4248
4249 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004250 * Performs button-related actions during a touch down event.
4251 *
4252 * @param event The event.
4253 * @return True if the down was consumed.
4254 *
4255 * @hide
4256 */
4257 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4258 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4259 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4260 return true;
4261 }
4262 }
4263 return false;
4264 }
4265
4266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 * Bring up the context menu for this view.
4268 *
4269 * @return Whether a context menu was displayed.
4270 */
4271 public boolean showContextMenu() {
4272 return getParent().showContextMenuForChild(this);
4273 }
4274
4275 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004276 * Bring up the context menu for this view, referring to the item under the specified point.
4277 *
4278 * @param x The referenced x coordinate.
4279 * @param y The referenced y coordinate.
4280 * @param metaState The keyboard modifiers that were pressed.
4281 * @return Whether a context menu was displayed.
4282 *
4283 * @hide
4284 */
4285 public boolean showContextMenu(float x, float y, int metaState) {
4286 return showContextMenu();
4287 }
4288
4289 /**
Adam Powell6e346362010-07-23 10:18:23 -07004290 * Start an action mode.
4291 *
4292 * @param callback Callback that will control the lifecycle of the action mode
4293 * @return The new action mode if it is started, null otherwise
4294 *
4295 * @see ActionMode
4296 */
4297 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004298 ViewParent parent = getParent();
4299 if (parent == null) return null;
4300 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004301 }
4302
4303 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004304 * Register a callback to be invoked when a hardware key is pressed in this view.
4305 * Key presses in software input methods will generally not trigger the methods of
4306 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 * @param l the key listener to attach to this view
4308 */
4309 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004310 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 }
4312
4313 /**
4314 * Register a callback to be invoked when a touch event is sent to this view.
4315 * @param l the touch listener to attach to this view
4316 */
4317 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004318 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 }
4320
4321 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004322 * Register a callback to be invoked when a generic motion event is sent to this view.
4323 * @param l the generic motion listener to attach to this view
4324 */
4325 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004326 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004327 }
4328
4329 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004330 * Register a callback to be invoked when a hover event is sent to this view.
4331 * @param l the hover listener to attach to this view
4332 */
4333 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004334 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004335 }
4336
4337 /**
Joe Malin32736f02011-01-19 16:14:20 -08004338 * Register a drag event listener callback object for this View. The parameter is
4339 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4340 * View, the system calls the
4341 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4342 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004343 */
4344 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004345 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004346 }
4347
4348 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004349 * Give this view focus. This will cause
4350 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 *
4352 * Note: this does not check whether this {@link View} should get focus, it just
4353 * gives it focus no matter what. It should only be called internally by framework
4354 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4355 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004356 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4357 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 * focus moved when requestFocus() is called. It may not always
4359 * apply, in which case use the default View.FOCUS_DOWN.
4360 * @param previouslyFocusedRect The rectangle of the view that had focus
4361 * prior in this View's coordinate system.
4362 */
4363 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4364 if (DBG) {
4365 System.out.println(this + " requestFocus()");
4366 }
4367
Dianne Hackborn4702a852012-08-17 15:18:29 -07004368 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4369 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370
4371 if (mParent != null) {
4372 mParent.requestChildFocus(this, this);
4373 }
4374
4375 onFocusChanged(true, direction, previouslyFocusedRect);
4376 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004377
4378 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4379 notifyAccessibilityStateChanged();
4380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 }
4382 }
4383
4384 /**
4385 * Request that a rectangle of this view be visible on the screen,
4386 * scrolling if necessary just enough.
4387 *
4388 * <p>A View should call this if it maintains some notion of which part
4389 * of its content is interesting. For example, a text editing view
4390 * should call this when its cursor moves.
4391 *
4392 * @param rectangle The rectangle.
4393 * @return Whether any parent scrolled.
4394 */
4395 public boolean requestRectangleOnScreen(Rect rectangle) {
4396 return requestRectangleOnScreen(rectangle, false);
4397 }
4398
4399 /**
4400 * Request that a rectangle of this view be visible on the screen,
4401 * scrolling if necessary just enough.
4402 *
4403 * <p>A View should call this if it maintains some notion of which part
4404 * of its content is interesting. For example, a text editing view
4405 * should call this when its cursor moves.
4406 *
4407 * <p>When <code>immediate</code> is set to true, scrolling will not be
4408 * animated.
4409 *
4410 * @param rectangle The rectangle.
4411 * @param immediate True to forbid animated scrolling, false otherwise
4412 * @return Whether any parent scrolled.
4413 */
4414 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004415 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004416 return false;
4417 }
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004420
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004421 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004422 position.set(rectangle);
4423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 ViewParent parent = mParent;
4425 boolean scrolled = false;
4426 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004427 rectangle.set((int) position.left, (int) position.top,
4428 (int) position.right, (int) position.bottom);
4429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 scrolled |= parent.requestChildRectangleOnScreen(child,
4431 rectangle, immediate);
4432
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004433 if (!child.hasIdentityMatrix()) {
4434 child.getMatrix().mapRect(position);
4435 }
4436
4437 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438
4439 if (!(parent instanceof View)) {
4440 break;
4441 }
Romain Guy8506ab42009-06-11 17:35:47 -07004442
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004443 View parentView = (View) parent;
4444
4445 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4446
4447 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 parent = child.getParent();
4449 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 return scrolled;
4452 }
4453
4454 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004455 * Called when this view wants to give up focus. If focus is cleared
4456 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4457 * <p>
4458 * <strong>Note:</strong> When a View clears focus the framework is trying
4459 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004460 * View is the first from the top that can take focus, then all callbacks
4461 * related to clearing focus will be invoked after wich the framework will
4462 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004463 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 */
4465 public void clearFocus() {
4466 if (DBG) {
4467 System.out.println(this + " clearFocus()");
4468 }
4469
Dianne Hackborn4702a852012-08-17 15:18:29 -07004470 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4471 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472
4473 if (mParent != null) {
4474 mParent.clearChildFocus(this);
4475 }
4476
4477 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004480
4481 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004482
4483 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4484 notifyAccessibilityStateChanged();
4485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 }
4487 }
4488
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004489 void ensureInputFocusOnFirstFocusable() {
4490 View root = getRootView();
4491 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004492 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004493 }
4494 }
4495
4496 /**
4497 * Called internally by the view system when a new view is getting focus.
4498 * This is what clears the old focus.
4499 */
4500 void unFocus() {
4501 if (DBG) {
4502 System.out.println(this + " unFocus()");
4503 }
4504
Dianne Hackborn4702a852012-08-17 15:18:29 -07004505 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4506 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507
4508 onFocusChanged(false, 0, null);
4509 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004510
4511 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4512 notifyAccessibilityStateChanged();
4513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 }
4515 }
4516
4517 /**
4518 * Returns true if this view has focus iteself, or is the ancestor of the
4519 * view that has focus.
4520 *
4521 * @return True if this view has or contains focus, false otherwise.
4522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004523 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004525 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 }
4527
4528 /**
4529 * Returns true if this view is focusable or if it contains a reachable View
4530 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4531 * is a View whose parents do not block descendants focus.
4532 *
4533 * Only {@link #VISIBLE} views are considered focusable.
4534 *
4535 * @return True if the view is focusable or if the view contains a focusable
4536 * View, false otherwise.
4537 *
4538 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4539 */
4540 public boolean hasFocusable() {
4541 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4542 }
4543
4544 /**
4545 * Called by the view system when the focus state of this view changes.
4546 * When the focus change event is caused by directional navigation, direction
4547 * and previouslyFocusedRect provide insight into where the focus is coming from.
4548 * When overriding, be sure to call up through to the super class so that
4549 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004550 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 * @param gainFocus True if the View has focus; false otherwise.
4552 * @param direction The direction focus has moved when requestFocus()
4553 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004554 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4555 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4556 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4558 * system, of the previously focused view. If applicable, this will be
4559 * passed in as finer grained information about where the focus is coming
4560 * from (in addition to direction). Will be <code>null</code> otherwise.
4561 */
4562 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004563 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004564 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4565 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004566 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004567 }
4568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 InputMethodManager imm = InputMethodManager.peekInstance();
4570 if (!gainFocus) {
4571 if (isPressed()) {
4572 setPressed(false);
4573 }
4574 if (imm != null && mAttachInfo != null
4575 && mAttachInfo.mHasWindowFocus) {
4576 imm.focusOut(this);
4577 }
Romain Guya2431d02009-04-30 16:30:00 -07004578 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 } else if (imm != null && mAttachInfo != null
4580 && mAttachInfo.mHasWindowFocus) {
4581 imm.focusIn(this);
4582 }
Romain Guy8506ab42009-06-11 17:35:47 -07004583
Romain Guy0fd89bf2011-01-26 15:41:30 -08004584 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004585 ListenerInfo li = mListenerInfo;
4586 if (li != null && li.mOnFocusChangeListener != null) {
4587 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 }
Joe Malin32736f02011-01-19 16:14:20 -08004589
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004590 if (mAttachInfo != null) {
4591 mAttachInfo.mKeyDispatchState.reset(this);
4592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 }
4594
4595 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004596 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004597 * not enabled this method has no effect. The default implementation calls
4598 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4599 * to populate information about the event source (this View), then calls
4600 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4601 * populate the text content of the event source including its descendants,
4602 * and last calls
4603 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4604 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004605 * <p>
4606 * If an {@link AccessibilityDelegate} has been specified via calling
4607 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4608 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4609 * responsible for handling this call.
4610 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004611 *
Scott Mainb303d832011-10-12 16:45:18 -07004612 * @param eventType The type of the event to send, as defined by several types from
4613 * {@link android.view.accessibility.AccessibilityEvent}, such as
4614 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4615 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004616 *
4617 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4618 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4619 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004620 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004621 */
4622 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004623 if (mAccessibilityDelegate != null) {
4624 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4625 } else {
4626 sendAccessibilityEventInternal(eventType);
4627 }
4628 }
4629
4630 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004631 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4632 * {@link AccessibilityEvent} to make an announcement which is related to some
4633 * sort of a context change for which none of the events representing UI transitions
4634 * is a good fit. For example, announcing a new page in a book. If accessibility
4635 * is not enabled this method does nothing.
4636 *
4637 * @param text The announcement text.
4638 */
4639 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004640 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004641 AccessibilityEvent event = AccessibilityEvent.obtain(
4642 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004643 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004644 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004645 event.setContentDescription(null);
4646 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004647 }
4648 }
4649
4650 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004651 * @see #sendAccessibilityEvent(int)
4652 *
4653 * Note: Called from the default {@link AccessibilityDelegate}.
4654 */
4655 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004656 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4657 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4658 }
4659 }
4660
4661 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004662 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4663 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004664 * perform a check whether accessibility is enabled.
4665 * <p>
4666 * If an {@link AccessibilityDelegate} has been specified via calling
4667 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4668 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4669 * is responsible for handling this call.
4670 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004671 *
4672 * @param event The event to send.
4673 *
4674 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004675 */
4676 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004677 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004678 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004679 } else {
4680 sendAccessibilityEventUncheckedInternal(event);
4681 }
4682 }
4683
4684 /**
4685 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4686 *
4687 * Note: Called from the default {@link AccessibilityDelegate}.
4688 */
4689 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004690 if (!isShown()) {
4691 return;
4692 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004693 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004694 // Only a subset of accessibility events populates text content.
4695 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4696 dispatchPopulateAccessibilityEvent(event);
4697 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004698 // In the beginning we called #isShown(), so we know that getParent() is not null.
4699 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004700 }
4701
4702 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004703 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4704 * to its children for adding their text content to the event. Note that the
4705 * event text is populated in a separate dispatch path since we add to the
4706 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004707 * A typical implementation will call
4708 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4709 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4710 * on each child. Override this method if custom population of the event text
4711 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004712 * <p>
4713 * If an {@link AccessibilityDelegate} has been specified via calling
4714 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4715 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4716 * is responsible for handling this call.
4717 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004718 * <p>
4719 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4720 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4721 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004722 *
4723 * @param event The event.
4724 *
4725 * @return True if the event population was completed.
4726 */
4727 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004728 if (mAccessibilityDelegate != null) {
4729 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4730 } else {
4731 return dispatchPopulateAccessibilityEventInternal(event);
4732 }
4733 }
4734
4735 /**
4736 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4737 *
4738 * Note: Called from the default {@link AccessibilityDelegate}.
4739 */
4740 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004741 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004742 return false;
4743 }
4744
4745 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004746 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004747 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004748 * text content. While this method is free to modify event
4749 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004750 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4751 * <p>
4752 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004753 * to the text added by the super implementation:
4754 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004755 * super.onPopulateAccessibilityEvent(event);
4756 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4757 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4758 * mCurrentDate.getTimeInMillis(), flags);
4759 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004760 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004761 * <p>
4762 * If an {@link AccessibilityDelegate} has been specified via calling
4763 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4764 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4765 * is responsible for handling this call.
4766 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004767 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4768 * information to the event, in case the default implementation has basic information to add.
4769 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004770 *
4771 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004772 *
4773 * @see #sendAccessibilityEvent(int)
4774 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004775 */
4776 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004777 if (mAccessibilityDelegate != null) {
4778 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4779 } else {
4780 onPopulateAccessibilityEventInternal(event);
4781 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004782 }
4783
4784 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004785 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4786 *
4787 * Note: Called from the default {@link AccessibilityDelegate}.
4788 */
4789 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4790
4791 }
4792
4793 /**
4794 * Initializes an {@link AccessibilityEvent} with information about
4795 * this View which is the event source. In other words, the source of
4796 * an accessibility event is the view whose state change triggered firing
4797 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004798 * <p>
4799 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004800 * to properties set by the super implementation:
4801 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4802 * super.onInitializeAccessibilityEvent(event);
4803 * event.setPassword(true);
4804 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004805 * <p>
4806 * If an {@link AccessibilityDelegate} has been specified via calling
4807 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4808 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4809 * is responsible for handling this call.
4810 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004811 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4812 * information to the event, in case the default implementation has basic information to add.
4813 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004814 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004815 *
4816 * @see #sendAccessibilityEvent(int)
4817 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4818 */
4819 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004820 if (mAccessibilityDelegate != null) {
4821 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4822 } else {
4823 onInitializeAccessibilityEventInternal(event);
4824 }
4825 }
4826
4827 /**
4828 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4829 *
4830 * Note: Called from the default {@link AccessibilityDelegate}.
4831 */
4832 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004833 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004834 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004835 event.setPackageName(getContext().getPackageName());
4836 event.setEnabled(isEnabled());
4837 event.setContentDescription(mContentDescription);
4838
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004839 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004840 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004841 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4842 FOCUSABLES_ALL);
4843 event.setItemCount(focusablesTempList.size());
4844 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4845 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004846 }
4847 }
4848
4849 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004850 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4851 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4852 * This method is responsible for obtaining an accessibility node info from a
4853 * pool of reusable instances and calling
4854 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4855 * initialize the former.
4856 * <p>
4857 * Note: The client is responsible for recycling the obtained instance by calling
4858 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4859 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004860 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004861 * @return A populated {@link AccessibilityNodeInfo}.
4862 *
4863 * @see AccessibilityNodeInfo
4864 */
4865 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004866 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4867 if (provider != null) {
4868 return provider.createAccessibilityNodeInfo(View.NO_ID);
4869 } else {
4870 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4871 onInitializeAccessibilityNodeInfo(info);
4872 return info;
4873 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004874 }
4875
4876 /**
4877 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4878 * The base implementation sets:
4879 * <ul>
4880 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004881 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4882 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004883 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4884 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4885 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4886 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4887 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4888 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4889 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4890 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4891 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4892 * </ul>
4893 * <p>
4894 * Subclasses should override this method, call the super implementation,
4895 * and set additional attributes.
4896 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004897 * <p>
4898 * If an {@link AccessibilityDelegate} has been specified via calling
4899 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4900 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4901 * is responsible for handling this call.
4902 * </p>
4903 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004904 * @param info The instance to initialize.
4905 */
4906 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004907 if (mAccessibilityDelegate != null) {
4908 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4909 } else {
4910 onInitializeAccessibilityNodeInfoInternal(info);
4911 }
4912 }
4913
4914 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004915 * Gets the location of this view in screen coordintates.
4916 *
4917 * @param outRect The output location
4918 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004919 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004920 if (mAttachInfo == null) {
4921 return;
4922 }
4923
4924 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004925 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004926
4927 if (!hasIdentityMatrix()) {
4928 getMatrix().mapRect(position);
4929 }
4930
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004931 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004932
4933 ViewParent parent = mParent;
4934 while (parent instanceof View) {
4935 View parentView = (View) parent;
4936
4937 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4938
4939 if (!parentView.hasIdentityMatrix()) {
4940 parentView.getMatrix().mapRect(position);
4941 }
4942
4943 position.offset(parentView.mLeft, parentView.mTop);
4944
4945 parent = parentView.mParent;
4946 }
4947
4948 if (parent instanceof ViewRootImpl) {
4949 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4950 position.offset(0, -viewRootImpl.mCurScrollY);
4951 }
4952
4953 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4954
4955 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4956 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4957 }
4958
4959 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004960 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4961 *
4962 * Note: Called from the default {@link AccessibilityDelegate}.
4963 */
4964 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004965 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004966
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004967 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004968 info.setBoundsInParent(bounds);
4969
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004970 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004971 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004972
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004973 ViewParent parent = getParentForAccessibility();
4974 if (parent instanceof View) {
4975 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004976 }
4977
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004978 if (mID != View.NO_ID) {
4979 View rootView = getRootView();
4980 if (rootView == null) {
4981 rootView = this;
4982 }
4983 View label = rootView.findLabelForView(this, mID);
4984 if (label != null) {
4985 info.setLabeledBy(label);
4986 }
4987 }
4988
4989 if (mLabelForId != View.NO_ID) {
4990 View rootView = getRootView();
4991 if (rootView == null) {
4992 rootView = this;
4993 }
4994 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4995 if (labeled != null) {
4996 info.setLabelFor(labeled);
4997 }
4998 }
4999
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005000 info.setVisibleToUser(isVisibleToUser());
5001
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005002 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08005003 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005004 info.setContentDescription(getContentDescription());
5005
5006 info.setEnabled(isEnabled());
5007 info.setClickable(isClickable());
5008 info.setFocusable(isFocusable());
5009 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005010 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005011 info.setSelected(isSelected());
5012 info.setLongClickable(isLongClickable());
5013
5014 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005015 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005016 // we should report as selectable view in an AdapterView.
5017 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5018 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5019
5020 if (isFocusable()) {
5021 if (isFocused()) {
5022 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5023 } else {
5024 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5025 }
5026 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005027
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005028 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005029 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005030 } else {
5031 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5032 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005033
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005034 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005035 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5036 }
5037
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005038 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005039 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5040 }
5041
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005042 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005043 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5044 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5045 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005046 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5047 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005048 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005049 }
5050
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005051 private View findLabelForView(View view, int labeledId) {
5052 if (mMatchLabelForPredicate == null) {
5053 mMatchLabelForPredicate = new MatchLabelForPredicate();
5054 }
5055 mMatchLabelForPredicate.mLabeledId = labeledId;
5056 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5057 }
5058
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005059 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005060 * Computes whether this view is visible to the user. Such a view is
5061 * attached, visible, all its predecessors are visible, it is not clipped
5062 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005063 *
5064 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005065 *
5066 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005067 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005068 protected boolean isVisibleToUser() {
5069 return isVisibleToUser(null);
5070 }
5071
5072 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005073 * Computes whether the given portion of this view is visible to the user.
5074 * Such a view is attached, visible, all its predecessors are visible,
5075 * has an alpha greater than zero, and the specified portion is not
5076 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005077 *
5078 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5079 * <code>null</code>, and the entire view will be tested in this case.
5080 * When <code>true</code> is returned by the function, the actual visible
5081 * region will be stored in this parameter; that is, if boundInView is fully
5082 * contained within the view, no modification will be made, otherwise regions
5083 * outside of the visible area of the view will be clipped.
5084 *
5085 * @return Whether the specified portion of the view is visible on the screen.
5086 *
5087 * @hide
5088 */
5089 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005090 if (mAttachInfo != null) {
5091 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5092 Point offset = mAttachInfo.mPoint;
5093 // The first two checks are made also made by isShown() which
5094 // however traverses the tree up to the parent to catch that.
5095 // Therefore, we do some fail fast check to minimize the up
5096 // tree traversal.
5097 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
5098 && getAlpha() > 0
5099 && isShown()
5100 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005101 if (isVisible && boundInView != null) {
5102 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07005103 // isVisible is always true here, use a simple assignment
5104 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005105 }
5106 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07005107 }
5108
5109 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005110 }
5111
5112 /**
alanvb72fe7a2012-08-27 16:44:25 -07005113 * Returns the delegate for implementing accessibility support via
5114 * composition. For more details see {@link AccessibilityDelegate}.
5115 *
5116 * @return The delegate, or null if none set.
5117 *
5118 * @hide
5119 */
5120 public AccessibilityDelegate getAccessibilityDelegate() {
5121 return mAccessibilityDelegate;
5122 }
5123
5124 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005125 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005126 * opposed to inheritance. The delegate's primary use is for implementing
5127 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5128 *
5129 * @param delegate The delegate instance.
5130 *
5131 * @see AccessibilityDelegate
5132 */
5133 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5134 mAccessibilityDelegate = delegate;
5135 }
5136
5137 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005138 * Gets the provider for managing a virtual view hierarchy rooted at this View
5139 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5140 * that explore the window content.
5141 * <p>
5142 * If this method returns an instance, this instance is responsible for managing
5143 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5144 * View including the one representing the View itself. Similarly the returned
5145 * instance is responsible for performing accessibility actions on any virtual
5146 * view or the root view itself.
5147 * </p>
5148 * <p>
5149 * If an {@link AccessibilityDelegate} has been specified via calling
5150 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5151 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5152 * is responsible for handling this call.
5153 * </p>
5154 *
5155 * @return The provider.
5156 *
5157 * @see AccessibilityNodeProvider
5158 */
5159 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5160 if (mAccessibilityDelegate != null) {
5161 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5162 } else {
5163 return null;
5164 }
5165 }
5166
5167 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005168 * Gets the unique identifier of this view on the screen for accessibility purposes.
5169 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5170 *
5171 * @return The view accessibility id.
5172 *
5173 * @hide
5174 */
5175 public int getAccessibilityViewId() {
5176 if (mAccessibilityViewId == NO_ID) {
5177 mAccessibilityViewId = sNextAccessibilityViewId++;
5178 }
5179 return mAccessibilityViewId;
5180 }
5181
5182 /**
5183 * Gets the unique identifier of the window in which this View reseides.
5184 *
5185 * @return The window accessibility id.
5186 *
5187 * @hide
5188 */
5189 public int getAccessibilityWindowId() {
5190 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5191 }
5192
5193 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005194 * Gets the {@link View} description. It briefly describes the view and is
5195 * primarily used for accessibility support. Set this property to enable
5196 * better accessibility support for your application. This is especially
5197 * true for views that do not have textual representation (For example,
5198 * ImageButton).
5199 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005200 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005201 *
5202 * @attr ref android.R.styleable#View_contentDescription
5203 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005204 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005205 public CharSequence getContentDescription() {
5206 return mContentDescription;
5207 }
5208
5209 /**
5210 * Sets the {@link View} description. It briefly describes the view and is
5211 * primarily used for accessibility support. Set this property to enable
5212 * better accessibility support for your application. This is especially
5213 * true for views that do not have textual representation (For example,
5214 * ImageButton).
5215 *
5216 * @param contentDescription The content description.
5217 *
5218 * @attr ref android.R.styleable#View_contentDescription
5219 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005220 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005221 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005222 if (mContentDescription == null) {
5223 if (contentDescription == null) {
5224 return;
5225 }
5226 } else if (mContentDescription.equals(contentDescription)) {
5227 return;
5228 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005229 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005230 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5231 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5232 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5233 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005234 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005235 }
5236
5237 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005238 * Gets the id of a view for which this view serves as a label for
5239 * accessibility purposes.
5240 *
5241 * @return The labeled view id.
5242 */
5243 @ViewDebug.ExportedProperty(category = "accessibility")
5244 public int getLabelFor() {
5245 return mLabelForId;
5246 }
5247
5248 /**
5249 * Sets the id of a view for which this view serves as a label for
5250 * accessibility purposes.
5251 *
5252 * @param id The labeled view id.
5253 */
5254 @RemotableViewMethod
5255 public void setLabelFor(int id) {
5256 mLabelForId = id;
5257 if (mLabelForId != View.NO_ID
5258 && mID == View.NO_ID) {
5259 mID = generateViewId();
5260 }
5261 }
5262
5263 /**
Romain Guya2431d02009-04-30 16:30:00 -07005264 * Invoked whenever this view loses focus, either by losing window focus or by losing
5265 * focus within its window. This method can be used to clear any state tied to the
5266 * focus. For instance, if a button is held pressed with the trackball and the window
5267 * loses focus, this method can be used to cancel the press.
5268 *
5269 * Subclasses of View overriding this method should always call super.onFocusLost().
5270 *
5271 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005272 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005273 *
5274 * @hide pending API council approval
5275 */
5276 protected void onFocusLost() {
5277 resetPressedState();
5278 }
5279
5280 private void resetPressedState() {
5281 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5282 return;
5283 }
5284
5285 if (isPressed()) {
5286 setPressed(false);
5287
5288 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005289 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005290 }
5291 }
5292 }
5293
5294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295 * Returns true if this view has focus
5296 *
5297 * @return True if this view has focus, false otherwise.
5298 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005299 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005301 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005302 }
5303
5304 /**
5305 * Find the view in the hierarchy rooted at this view that currently has
5306 * focus.
5307 *
5308 * @return The view that currently has focus, or null if no focused view can
5309 * be found.
5310 */
5311 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005312 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 }
5314
5315 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005316 * Indicates whether this view is one of the set of scrollable containers in
5317 * its window.
5318 *
5319 * @return whether this view is one of the set of scrollable containers in
5320 * its window
5321 *
5322 * @attr ref android.R.styleable#View_isScrollContainer
5323 */
5324 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005325 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005326 }
5327
5328 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 * Change whether this view is one of the set of scrollable containers in
5330 * its window. This will be used to determine whether the window can
5331 * resize or must pan when a soft input area is open -- scrollable
5332 * containers allow the window to use resize mode since the container
5333 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005334 *
5335 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 */
5337 public void setScrollContainer(boolean isScrollContainer) {
5338 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005339 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005341 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005343 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005345 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005346 mAttachInfo.mScrollContainers.remove(this);
5347 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005348 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 }
5350 }
5351
5352 /**
5353 * Returns the quality of the drawing cache.
5354 *
5355 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5356 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5357 *
5358 * @see #setDrawingCacheQuality(int)
5359 * @see #setDrawingCacheEnabled(boolean)
5360 * @see #isDrawingCacheEnabled()
5361 *
5362 * @attr ref android.R.styleable#View_drawingCacheQuality
5363 */
5364 public int getDrawingCacheQuality() {
5365 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5366 }
5367
5368 /**
5369 * Set the drawing cache quality of this view. This value is used only when the
5370 * drawing cache is enabled
5371 *
5372 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5373 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5374 *
5375 * @see #getDrawingCacheQuality()
5376 * @see #setDrawingCacheEnabled(boolean)
5377 * @see #isDrawingCacheEnabled()
5378 *
5379 * @attr ref android.R.styleable#View_drawingCacheQuality
5380 */
5381 public void setDrawingCacheQuality(int quality) {
5382 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5383 }
5384
5385 /**
5386 * Returns whether the screen should remain on, corresponding to the current
5387 * value of {@link #KEEP_SCREEN_ON}.
5388 *
5389 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5390 *
5391 * @see #setKeepScreenOn(boolean)
5392 *
5393 * @attr ref android.R.styleable#View_keepScreenOn
5394 */
5395 public boolean getKeepScreenOn() {
5396 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5397 }
5398
5399 /**
5400 * Controls whether the screen should remain on, modifying the
5401 * value of {@link #KEEP_SCREEN_ON}.
5402 *
5403 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5404 *
5405 * @see #getKeepScreenOn()
5406 *
5407 * @attr ref android.R.styleable#View_keepScreenOn
5408 */
5409 public void setKeepScreenOn(boolean keepScreenOn) {
5410 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5411 }
5412
5413 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005414 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5415 * @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 -08005416 *
5417 * @attr ref android.R.styleable#View_nextFocusLeft
5418 */
5419 public int getNextFocusLeftId() {
5420 return mNextFocusLeftId;
5421 }
5422
5423 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005424 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5425 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5426 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005427 *
5428 * @attr ref android.R.styleable#View_nextFocusLeft
5429 */
5430 public void setNextFocusLeftId(int nextFocusLeftId) {
5431 mNextFocusLeftId = nextFocusLeftId;
5432 }
5433
5434 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005435 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5436 * @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 -08005437 *
5438 * @attr ref android.R.styleable#View_nextFocusRight
5439 */
5440 public int getNextFocusRightId() {
5441 return mNextFocusRightId;
5442 }
5443
5444 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005445 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5446 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5447 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005448 *
5449 * @attr ref android.R.styleable#View_nextFocusRight
5450 */
5451 public void setNextFocusRightId(int nextFocusRightId) {
5452 mNextFocusRightId = nextFocusRightId;
5453 }
5454
5455 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005456 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5457 * @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 -08005458 *
5459 * @attr ref android.R.styleable#View_nextFocusUp
5460 */
5461 public int getNextFocusUpId() {
5462 return mNextFocusUpId;
5463 }
5464
5465 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005466 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5467 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5468 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 *
5470 * @attr ref android.R.styleable#View_nextFocusUp
5471 */
5472 public void setNextFocusUpId(int nextFocusUpId) {
5473 mNextFocusUpId = nextFocusUpId;
5474 }
5475
5476 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005477 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5478 * @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 -08005479 *
5480 * @attr ref android.R.styleable#View_nextFocusDown
5481 */
5482 public int getNextFocusDownId() {
5483 return mNextFocusDownId;
5484 }
5485
5486 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005487 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5488 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5489 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 *
5491 * @attr ref android.R.styleable#View_nextFocusDown
5492 */
5493 public void setNextFocusDownId(int nextFocusDownId) {
5494 mNextFocusDownId = nextFocusDownId;
5495 }
5496
5497 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005498 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5499 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5500 *
5501 * @attr ref android.R.styleable#View_nextFocusForward
5502 */
5503 public int getNextFocusForwardId() {
5504 return mNextFocusForwardId;
5505 }
5506
5507 /**
5508 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5509 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5510 * decide automatically.
5511 *
5512 * @attr ref android.R.styleable#View_nextFocusForward
5513 */
5514 public void setNextFocusForwardId(int nextFocusForwardId) {
5515 mNextFocusForwardId = nextFocusForwardId;
5516 }
5517
5518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 * Returns the visibility of this view and all of its ancestors
5520 *
5521 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5522 */
5523 public boolean isShown() {
5524 View current = this;
5525 //noinspection ConstantConditions
5526 do {
5527 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5528 return false;
5529 }
5530 ViewParent parent = current.mParent;
5531 if (parent == null) {
5532 return false; // We are not attached to the view root
5533 }
5534 if (!(parent instanceof View)) {
5535 return true;
5536 }
5537 current = (View) parent;
5538 } while (current != null);
5539
5540 return false;
5541 }
5542
5543 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005544 * Called by the view hierarchy when the content insets for a window have
5545 * changed, to allow it to adjust its content to fit within those windows.
5546 * The content insets tell you the space that the status bar, input method,
5547 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005549 * <p>You do not normally need to deal with this function, since the default
5550 * window decoration given to applications takes care of applying it to the
5551 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5552 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5553 * and your content can be placed under those system elements. You can then
5554 * use this method within your view hierarchy if you have parts of your UI
5555 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005557 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005558 * inset's to the view's padding, consuming that content (modifying the
5559 * insets to be 0), and returning true. This behavior is off by default, but can
5560 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5561 *
5562 * <p>This function's traversal down the hierarchy is depth-first. The same content
5563 * insets object is propagated down the hierarchy, so any changes made to it will
5564 * be seen by all following views (including potentially ones above in
5565 * the hierarchy since this is a depth-first traversal). The first view
5566 * that returns true will abort the entire traversal.
5567 *
5568 * <p>The default implementation works well for a situation where it is
5569 * used with a container that covers the entire window, allowing it to
5570 * apply the appropriate insets to its content on all edges. If you need
5571 * a more complicated layout (such as two different views fitting system
5572 * windows, one on the top of the window, and one on the bottom),
5573 * you can override the method and handle the insets however you would like.
5574 * Note that the insets provided by the framework are always relative to the
5575 * far edges of the window, not accounting for the location of the called view
5576 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005577 * where the layout will place the view, as it is done before layout happens.)
5578 *
5579 * <p>Note: unlike many View methods, there is no dispatch phase to this
5580 * call. If you are overriding it in a ViewGroup and want to allow the
5581 * call to continue to your children, you must be sure to call the super
5582 * implementation.
5583 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005584 * <p>Here is a sample layout that makes use of fitting system windows
5585 * to have controls for a video view placed inside of the window decorations
5586 * that it hides and shows. This can be used with code like the second
5587 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5588 *
5589 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5590 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005591 * @param insets Current content insets of the window. Prior to
5592 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5593 * the insets or else you and Android will be unhappy.
5594 *
5595 * @return Return true if this view applied the insets and it should not
5596 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005597 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005598 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005599 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 */
5601 protected boolean fitSystemWindows(Rect insets) {
5602 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005603 mUserPaddingStart = UNDEFINED_PADDING;
5604 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005605 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5606 || mAttachInfo == null
5607 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5608 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5609 return true;
5610 } else {
5611 internalSetPadding(0, 0, 0, 0);
5612 return false;
5613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 }
5615 return false;
5616 }
5617
5618 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005619 * Sets whether or not this view should account for system screen decorations
5620 * such as the status bar and inset its content; that is, controlling whether
5621 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5622 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005623 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005624 * <p>Note that if you are providing your own implementation of
5625 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5626 * flag to true -- your implementation will be overriding the default
5627 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005628 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005629 * @param fitSystemWindows If true, then the default implementation of
5630 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005631 *
5632 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005633 * @see #getFitsSystemWindows()
5634 * @see #fitSystemWindows(Rect)
5635 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005636 */
5637 public void setFitsSystemWindows(boolean fitSystemWindows) {
5638 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5639 }
5640
5641 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005642 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005643 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5644 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005645 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005646 * @return Returns true if the default implementation of
5647 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005648 *
5649 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005650 * @see #setFitsSystemWindows()
5651 * @see #fitSystemWindows(Rect)
5652 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005653 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005654 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005655 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5656 }
5657
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005658 /** @hide */
5659 public boolean fitsSystemWindows() {
5660 return getFitsSystemWindows();
5661 }
5662
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005663 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005664 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5665 */
5666 public void requestFitSystemWindows() {
5667 if (mParent != null) {
5668 mParent.requestFitSystemWindows();
5669 }
5670 }
5671
5672 /**
5673 * For use by PhoneWindow to make its own system window fitting optional.
5674 * @hide
5675 */
5676 public void makeOptionalFitsSystemWindows() {
5677 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5678 }
5679
5680 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 * Returns the visibility status for this view.
5682 *
5683 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5684 * @attr ref android.R.styleable#View_visibility
5685 */
5686 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005687 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5688 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5689 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 })
5691 public int getVisibility() {
5692 return mViewFlags & VISIBILITY_MASK;
5693 }
5694
5695 /**
5696 * Set the enabled state of this view.
5697 *
5698 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5699 * @attr ref android.R.styleable#View_visibility
5700 */
5701 @RemotableViewMethod
5702 public void setVisibility(int visibility) {
5703 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005704 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 }
5706
5707 /**
5708 * Returns the enabled status for this view. The interpretation of the
5709 * enabled state varies by subclass.
5710 *
5711 * @return True if this view is enabled, false otherwise.
5712 */
5713 @ViewDebug.ExportedProperty
5714 public boolean isEnabled() {
5715 return (mViewFlags & ENABLED_MASK) == ENABLED;
5716 }
5717
5718 /**
5719 * Set the enabled state of this view. The interpretation of the enabled
5720 * state varies by subclass.
5721 *
5722 * @param enabled True if this view is enabled, false otherwise.
5723 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005724 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005725 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005726 if (enabled == isEnabled()) return;
5727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5729
5730 /*
5731 * The View most likely has to change its appearance, so refresh
5732 * the drawable state.
5733 */
5734 refreshDrawableState();
5735
5736 // Invalidate too, since the default behavior for views is to be
5737 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005738 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 }
5740
5741 /**
5742 * Set whether this view can receive the focus.
5743 *
5744 * Setting this to false will also ensure that this view is not focusable
5745 * in touch mode.
5746 *
5747 * @param focusable If true, this view can receive the focus.
5748 *
5749 * @see #setFocusableInTouchMode(boolean)
5750 * @attr ref android.R.styleable#View_focusable
5751 */
5752 public void setFocusable(boolean focusable) {
5753 if (!focusable) {
5754 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5755 }
5756 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5757 }
5758
5759 /**
5760 * Set whether this view can receive focus while in touch mode.
5761 *
5762 * Setting this to true will also ensure that this view is focusable.
5763 *
5764 * @param focusableInTouchMode If true, this view can receive the focus while
5765 * in touch mode.
5766 *
5767 * @see #setFocusable(boolean)
5768 * @attr ref android.R.styleable#View_focusableInTouchMode
5769 */
5770 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5771 // Focusable in touch mode should always be set before the focusable flag
5772 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5773 // which, in touch mode, will not successfully request focus on this view
5774 // because the focusable in touch mode flag is not set
5775 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5776 if (focusableInTouchMode) {
5777 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5778 }
5779 }
5780
5781 /**
5782 * Set whether this view should have sound effects enabled for events such as
5783 * clicking and touching.
5784 *
5785 * <p>You may wish to disable sound effects for a view if you already play sounds,
5786 * for instance, a dial key that plays dtmf tones.
5787 *
5788 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5789 * @see #isSoundEffectsEnabled()
5790 * @see #playSoundEffect(int)
5791 * @attr ref android.R.styleable#View_soundEffectsEnabled
5792 */
5793 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5794 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5795 }
5796
5797 /**
5798 * @return whether this view should have sound effects enabled for events such as
5799 * clicking and touching.
5800 *
5801 * @see #setSoundEffectsEnabled(boolean)
5802 * @see #playSoundEffect(int)
5803 * @attr ref android.R.styleable#View_soundEffectsEnabled
5804 */
5805 @ViewDebug.ExportedProperty
5806 public boolean isSoundEffectsEnabled() {
5807 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5808 }
5809
5810 /**
5811 * Set whether this view should have haptic feedback for events such as
5812 * long presses.
5813 *
5814 * <p>You may wish to disable haptic feedback if your view already controls
5815 * its own haptic feedback.
5816 *
5817 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5818 * @see #isHapticFeedbackEnabled()
5819 * @see #performHapticFeedback(int)
5820 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5821 */
5822 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5823 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5824 }
5825
5826 /**
5827 * @return whether this view should have haptic feedback enabled for events
5828 * long presses.
5829 *
5830 * @see #setHapticFeedbackEnabled(boolean)
5831 * @see #performHapticFeedback(int)
5832 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5833 */
5834 @ViewDebug.ExportedProperty
5835 public boolean isHapticFeedbackEnabled() {
5836 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5837 }
5838
5839 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005840 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005841 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005842 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5843 * {@link #LAYOUT_DIRECTION_RTL},
5844 * {@link #LAYOUT_DIRECTION_INHERIT} or
5845 * {@link #LAYOUT_DIRECTION_LOCALE}.
5846 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005847 *
5848 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005849 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005850 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005851 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5852 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5853 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5854 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005855 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005856 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005857 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005858 }
5859
5860 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005861 * Set the layout direction for this view. This will propagate a reset of layout direction
5862 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005863 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005864 * @param layoutDirection the layout direction to set. Should be one of:
5865 *
5866 * {@link #LAYOUT_DIRECTION_LTR},
5867 * {@link #LAYOUT_DIRECTION_RTL},
5868 * {@link #LAYOUT_DIRECTION_INHERIT},
5869 * {@link #LAYOUT_DIRECTION_LOCALE}.
5870 *
5871 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5872 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5873 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005874 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005875 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005876 */
5877 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005878 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005879 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005880 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005881 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005882 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005883 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005884 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005885 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005886 // We need to resolve all RTL properties as they all depend on layout direction
5887 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005888 requestLayout();
5889 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005890 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005891 }
5892
5893 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005894 * Returns the resolved layout direction for this view.
5895 *
5896 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005897 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005898 *
5899 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5900 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005901 */
5902 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005903 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5904 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005905 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005906 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005907 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5908 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005909 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005910 return LAYOUT_DIRECTION_LTR;
5911 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005912 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5913 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005914 }
5915
5916 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005917 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5918 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005919 *
5920 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005921 *
5922 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005923 */
5924 @ViewDebug.ExportedProperty(category = "layout")
5925 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005926 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005927 }
5928
5929 /**
Adam Powell539ee872012-02-03 19:00:49 -08005930 * Indicates whether the view is currently tracking transient state that the
5931 * app should not need to concern itself with saving and restoring, but that
5932 * the framework should take special note to preserve when possible.
5933 *
Adam Powell785c4472012-05-02 21:25:39 -07005934 * <p>A view with transient state cannot be trivially rebound from an external
5935 * data source, such as an adapter binding item views in a list. This may be
5936 * because the view is performing an animation, tracking user selection
5937 * of content, or similar.</p>
5938 *
Adam Powell539ee872012-02-03 19:00:49 -08005939 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005940 */
5941 @ViewDebug.ExportedProperty(category = "layout")
5942 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005943 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005944 }
5945
5946 /**
5947 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005948 * framework should attempt to preserve when possible. This flag is reference counted,
5949 * so every call to setHasTransientState(true) should be paired with a later call
5950 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005951 *
Adam Powell785c4472012-05-02 21:25:39 -07005952 * <p>A view with transient state cannot be trivially rebound from an external
5953 * data source, such as an adapter binding item views in a list. This may be
5954 * because the view is performing an animation, tracking user selection
5955 * of content, or similar.</p>
5956 *
Adam Powell539ee872012-02-03 19:00:49 -08005957 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005958 */
5959 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005960 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5961 mTransientStateCount - 1;
5962 if (mTransientStateCount < 0) {
5963 mTransientStateCount = 0;
5964 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5965 "unmatched pair of setHasTransientState calls");
5966 }
5967 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005968 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005969 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005970 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5971 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005972 if (mParent != null) {
5973 try {
5974 mParent.childHasTransientStateChanged(this, hasTransientState);
5975 } catch (AbstractMethodError e) {
5976 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5977 " does not fully implement ViewParent", e);
5978 }
Adam Powell539ee872012-02-03 19:00:49 -08005979 }
5980 }
5981 }
5982
5983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 * If this view doesn't do any drawing on its own, set this flag to
5985 * allow further optimizations. By default, this flag is not set on
5986 * View, but could be set on some View subclasses such as ViewGroup.
5987 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005988 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5989 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 *
5991 * @param willNotDraw whether or not this View draw on its own
5992 */
5993 public void setWillNotDraw(boolean willNotDraw) {
5994 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5995 }
5996
5997 /**
5998 * Returns whether or not this View draws on its own.
5999 *
6000 * @return true if this view has nothing to draw, false otherwise
6001 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006002 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 public boolean willNotDraw() {
6004 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
6005 }
6006
6007 /**
6008 * When a View's drawing cache is enabled, drawing is redirected to an
6009 * offscreen bitmap. Some views, like an ImageView, must be able to
6010 * bypass this mechanism if they already draw a single bitmap, to avoid
6011 * unnecessary usage of the memory.
6012 *
6013 * @param willNotCacheDrawing true if this view does not cache its
6014 * drawing, false otherwise
6015 */
6016 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6017 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6018 }
6019
6020 /**
6021 * Returns whether or not this View can cache its drawing or not.
6022 *
6023 * @return true if this view does not cache its drawing, false otherwise
6024 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006025 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 public boolean willNotCacheDrawing() {
6027 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6028 }
6029
6030 /**
6031 * Indicates whether this view reacts to click events or not.
6032 *
6033 * @return true if the view is clickable, false otherwise
6034 *
6035 * @see #setClickable(boolean)
6036 * @attr ref android.R.styleable#View_clickable
6037 */
6038 @ViewDebug.ExportedProperty
6039 public boolean isClickable() {
6040 return (mViewFlags & CLICKABLE) == CLICKABLE;
6041 }
6042
6043 /**
6044 * Enables or disables click events for this view. When a view
6045 * is clickable it will change its state to "pressed" on every click.
6046 * Subclasses should set the view clickable to visually react to
6047 * user's clicks.
6048 *
6049 * @param clickable true to make the view clickable, false otherwise
6050 *
6051 * @see #isClickable()
6052 * @attr ref android.R.styleable#View_clickable
6053 */
6054 public void setClickable(boolean clickable) {
6055 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6056 }
6057
6058 /**
6059 * Indicates whether this view reacts to long click events or not.
6060 *
6061 * @return true if the view is long clickable, false otherwise
6062 *
6063 * @see #setLongClickable(boolean)
6064 * @attr ref android.R.styleable#View_longClickable
6065 */
6066 public boolean isLongClickable() {
6067 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6068 }
6069
6070 /**
6071 * Enables or disables long click events for this view. When a view is long
6072 * clickable it reacts to the user holding down the button for a longer
6073 * duration than a tap. This event can either launch the listener or a
6074 * context menu.
6075 *
6076 * @param longClickable true to make the view long clickable, false otherwise
6077 * @see #isLongClickable()
6078 * @attr ref android.R.styleable#View_longClickable
6079 */
6080 public void setLongClickable(boolean longClickable) {
6081 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6082 }
6083
6084 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006085 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 *
6087 * @see #isClickable()
6088 * @see #setClickable(boolean)
6089 *
6090 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6091 * the View's internal state from a previously set "pressed" state.
6092 */
6093 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006094 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006096 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006097 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006099 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006101
6102 if (needsRefresh) {
6103 refreshDrawableState();
6104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 dispatchSetPressed(pressed);
6106 }
6107
6108 /**
6109 * Dispatch setPressed to all of this View's children.
6110 *
6111 * @see #setPressed(boolean)
6112 *
6113 * @param pressed The new pressed state
6114 */
6115 protected void dispatchSetPressed(boolean pressed) {
6116 }
6117
6118 /**
6119 * Indicates whether the view is currently in pressed state. Unless
6120 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6121 * the pressed state.
6122 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006123 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 * @see #isClickable()
6125 * @see #setClickable(boolean)
6126 *
6127 * @return true if the view is currently pressed, false otherwise
6128 */
6129 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006130 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 }
6132
6133 /**
6134 * Indicates whether this view will save its state (that is,
6135 * whether its {@link #onSaveInstanceState} method will be called).
6136 *
6137 * @return Returns true if the view state saving is enabled, else false.
6138 *
6139 * @see #setSaveEnabled(boolean)
6140 * @attr ref android.R.styleable#View_saveEnabled
6141 */
6142 public boolean isSaveEnabled() {
6143 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6144 }
6145
6146 /**
6147 * Controls whether the saving of this view's state is
6148 * enabled (that is, whether its {@link #onSaveInstanceState} method
6149 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006150 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 * for its state to be saved. This flag can only disable the
6152 * saving of this view; any child views may still have their state saved.
6153 *
6154 * @param enabled Set to false to <em>disable</em> state saving, or true
6155 * (the default) to allow it.
6156 *
6157 * @see #isSaveEnabled()
6158 * @see #setId(int)
6159 * @see #onSaveInstanceState()
6160 * @attr ref android.R.styleable#View_saveEnabled
6161 */
6162 public void setSaveEnabled(boolean enabled) {
6163 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6164 }
6165
Jeff Brown85a31762010-09-01 17:01:00 -07006166 /**
6167 * Gets whether the framework should discard touches when the view's
6168 * window is obscured by another visible window.
6169 * Refer to the {@link View} security documentation for more details.
6170 *
6171 * @return True if touch filtering is enabled.
6172 *
6173 * @see #setFilterTouchesWhenObscured(boolean)
6174 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6175 */
6176 @ViewDebug.ExportedProperty
6177 public boolean getFilterTouchesWhenObscured() {
6178 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6179 }
6180
6181 /**
6182 * Sets whether the framework should discard touches when the view's
6183 * window is obscured by another visible window.
6184 * Refer to the {@link View} security documentation for more details.
6185 *
6186 * @param enabled True if touch filtering should be enabled.
6187 *
6188 * @see #getFilterTouchesWhenObscured
6189 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6190 */
6191 public void setFilterTouchesWhenObscured(boolean enabled) {
6192 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6193 FILTER_TOUCHES_WHEN_OBSCURED);
6194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195
6196 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006197 * Indicates whether the entire hierarchy under this view will save its
6198 * state when a state saving traversal occurs from its parent. The default
6199 * is true; if false, these views will not be saved unless
6200 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6201 *
6202 * @return Returns true if the view state saving from parent is enabled, else false.
6203 *
6204 * @see #setSaveFromParentEnabled(boolean)
6205 */
6206 public boolean isSaveFromParentEnabled() {
6207 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6208 }
6209
6210 /**
6211 * Controls whether the entire hierarchy under this view will save its
6212 * state when a state saving traversal occurs from its parent. The default
6213 * is true; if false, these views will not be saved unless
6214 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6215 *
6216 * @param enabled Set to false to <em>disable</em> state saving, or true
6217 * (the default) to allow it.
6218 *
6219 * @see #isSaveFromParentEnabled()
6220 * @see #setId(int)
6221 * @see #onSaveInstanceState()
6222 */
6223 public void setSaveFromParentEnabled(boolean enabled) {
6224 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6225 }
6226
6227
6228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 * Returns whether this View is able to take focus.
6230 *
6231 * @return True if this view can take focus, or false otherwise.
6232 * @attr ref android.R.styleable#View_focusable
6233 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006234 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 public final boolean isFocusable() {
6236 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6237 }
6238
6239 /**
6240 * When a view is focusable, it may not want to take focus when in touch mode.
6241 * For example, a button would like focus when the user is navigating via a D-pad
6242 * so that the user can click on it, but once the user starts touching the screen,
6243 * the button shouldn't take focus
6244 * @return Whether the view is focusable in touch mode.
6245 * @attr ref android.R.styleable#View_focusableInTouchMode
6246 */
6247 @ViewDebug.ExportedProperty
6248 public final boolean isFocusableInTouchMode() {
6249 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6250 }
6251
6252 /**
6253 * Find the nearest view in the specified direction that can take focus.
6254 * This does not actually give focus to that view.
6255 *
6256 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6257 *
6258 * @return The nearest focusable in the specified direction, or null if none
6259 * can be found.
6260 */
6261 public View focusSearch(int direction) {
6262 if (mParent != null) {
6263 return mParent.focusSearch(this, direction);
6264 } else {
6265 return null;
6266 }
6267 }
6268
6269 /**
6270 * This method is the last chance for the focused view and its ancestors to
6271 * respond to an arrow key. This is called when the focused view did not
6272 * consume the key internally, nor could the view system find a new view in
6273 * the requested direction to give focus to.
6274 *
6275 * @param focused The currently focused view.
6276 * @param direction The direction focus wants to move. One of FOCUS_UP,
6277 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6278 * @return True if the this view consumed this unhandled move.
6279 */
6280 public boolean dispatchUnhandledMove(View focused, int direction) {
6281 return false;
6282 }
6283
6284 /**
6285 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006286 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006288 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6289 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 * @return The user specified next view, or null if there is none.
6291 */
6292 View findUserSetNextFocus(View root, int direction) {
6293 switch (direction) {
6294 case FOCUS_LEFT:
6295 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006296 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006297 case FOCUS_RIGHT:
6298 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006299 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006300 case FOCUS_UP:
6301 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006302 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006303 case FOCUS_DOWN:
6304 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006305 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006306 case FOCUS_FORWARD:
6307 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006308 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006309 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006310 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006311 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006312 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006313 @Override
6314 public boolean apply(View t) {
6315 return t.mNextFocusForwardId == id;
6316 }
6317 });
6318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 }
6320 return null;
6321 }
6322
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006323 private View findViewInsideOutShouldExist(View root, int id) {
6324 if (mMatchIdPredicate == null) {
6325 mMatchIdPredicate = new MatchIdPredicate();
6326 }
6327 mMatchIdPredicate.mId = id;
6328 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006330 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 }
6332 return result;
6333 }
6334
6335 /**
6336 * Find and return all focusable views that are descendants of this view,
6337 * possibly including this view if it is focusable itself.
6338 *
6339 * @param direction The direction of the focus
6340 * @return A list of focusable views
6341 */
6342 public ArrayList<View> getFocusables(int direction) {
6343 ArrayList<View> result = new ArrayList<View>(24);
6344 addFocusables(result, direction);
6345 return result;
6346 }
6347
6348 /**
6349 * Add any focusable views that are descendants of this view (possibly
6350 * including this view if it is focusable itself) to views. If we are in touch mode,
6351 * only add views that are also focusable in touch mode.
6352 *
6353 * @param views Focusable views found so far
6354 * @param direction The direction of the focus
6355 */
6356 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006357 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006359
svetoslavganov75986cf2009-05-14 22:28:01 -07006360 /**
6361 * Adds any focusable views that are descendants of this view (possibly
6362 * including this view if it is focusable itself) to views. This method
6363 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006364 * only views focusable in touch mode if we are in touch mode or
6365 * only views that can take accessibility focus if accessibility is enabeld
6366 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006367 *
6368 * @param views Focusable views found so far or null if all we are interested is
6369 * the number of focusables.
6370 * @param direction The direction of the focus.
6371 * @param focusableMode The type of focusables to be added.
6372 *
6373 * @see #FOCUSABLES_ALL
6374 * @see #FOCUSABLES_TOUCH_MODE
6375 */
6376 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006377 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006378 return;
6379 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006380 if (!isFocusable()) {
6381 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006382 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006383 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6384 && isInTouchMode() && !isFocusableInTouchMode()) {
6385 return;
6386 }
6387 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006388 }
6389
6390 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006391 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006392 * The search is performed by either the text that the View renders or the content
6393 * description that describes the view for accessibility purposes and the view does
6394 * not render or both. Clients can specify how the search is to be performed via
6395 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6396 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006397 *
6398 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006399 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006400 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006401 * @see #FIND_VIEWS_WITH_TEXT
6402 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6403 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006404 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006405 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006406 if (getAccessibilityNodeProvider() != null) {
6407 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6408 outViews.add(this);
6409 }
6410 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006411 && (searched != null && searched.length() > 0)
6412 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006413 String searchedLowerCase = searched.toString().toLowerCase();
6414 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6415 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6416 outViews.add(this);
6417 }
6418 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006419 }
6420
6421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006422 * Find and return all touchable views that are descendants of this view,
6423 * possibly including this view if it is touchable itself.
6424 *
6425 * @return A list of touchable views
6426 */
6427 public ArrayList<View> getTouchables() {
6428 ArrayList<View> result = new ArrayList<View>();
6429 addTouchables(result);
6430 return result;
6431 }
6432
6433 /**
6434 * Add any touchable views that are descendants of this view (possibly
6435 * including this view if it is touchable itself) to views.
6436 *
6437 * @param views Touchable views found so far
6438 */
6439 public void addTouchables(ArrayList<View> views) {
6440 final int viewFlags = mViewFlags;
6441
6442 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6443 && (viewFlags & ENABLED_MASK) == ENABLED) {
6444 views.add(this);
6445 }
6446 }
6447
6448 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006449 * Returns whether this View is accessibility focused.
6450 *
6451 * @return True if this View is accessibility focused.
6452 */
6453 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006454 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006455 }
6456
6457 /**
6458 * Call this to try to give accessibility focus to this view.
6459 *
6460 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6461 * returns false or the view is no visible or the view already has accessibility
6462 * focus.
6463 *
6464 * See also {@link #focusSearch(int)}, which is what you call to say that you
6465 * have focus, and you want your parent to look for the next one.
6466 *
6467 * @return Whether this view actually took accessibility focus.
6468 *
6469 * @hide
6470 */
6471 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006472 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6473 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006474 return false;
6475 }
6476 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6477 return false;
6478 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006479 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6480 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006481 ViewRootImpl viewRootImpl = getViewRootImpl();
6482 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006483 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006484 }
6485 invalidate();
6486 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6487 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006488 return true;
6489 }
6490 return false;
6491 }
6492
6493 /**
6494 * Call this to try to clear accessibility focus of this view.
6495 *
6496 * See also {@link #focusSearch(int)}, which is what you call to say that you
6497 * have focus, and you want your parent to look for the next one.
6498 *
6499 * @hide
6500 */
6501 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006502 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6503 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006504 invalidate();
6505 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6506 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006508 // Clear the global reference of accessibility focus if this
6509 // view or any of its descendants had accessibility focus.
6510 ViewRootImpl viewRootImpl = getViewRootImpl();
6511 if (viewRootImpl != null) {
6512 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6513 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006514 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006515 }
6516 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006517 }
6518
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006519 private void sendAccessibilityHoverEvent(int eventType) {
6520 // Since we are not delivering to a client accessibility events from not
6521 // important views (unless the clinet request that) we need to fire the
6522 // event from the deepest view exposed to the client. As a consequence if
6523 // the user crosses a not exposed view the client will see enter and exit
6524 // of the exposed predecessor followed by and enter and exit of that same
6525 // predecessor when entering and exiting the not exposed descendant. This
6526 // is fine since the client has a clear idea which view is hovered at the
6527 // price of a couple more events being sent. This is a simple and
6528 // working solution.
6529 View source = this;
6530 while (true) {
6531 if (source.includeForAccessibility()) {
6532 source.sendAccessibilityEvent(eventType);
6533 return;
6534 }
6535 ViewParent parent = source.getParent();
6536 if (parent instanceof View) {
6537 source = (View) parent;
6538 } else {
6539 return;
6540 }
6541 }
6542 }
6543
Svetoslav Ganov42138042012-03-20 11:51:39 -07006544 /**
6545 * Clears accessibility focus without calling any callback methods
6546 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6547 * is used for clearing accessibility focus when giving this focus to
6548 * another view.
6549 */
6550 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006551 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6552 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006553 invalidate();
6554 }
6555 }
6556
6557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 * Call this to try to give focus to a specific view or to one of its
6559 * descendants.
6560 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006561 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6562 * false), or if it is focusable and it is not focusable in touch mode
6563 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006565 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 * have focus, and you want your parent to look for the next one.
6567 *
6568 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6569 * {@link #FOCUS_DOWN} and <code>null</code>.
6570 *
6571 * @return Whether this view or one of its descendants actually took focus.
6572 */
6573 public final boolean requestFocus() {
6574 return requestFocus(View.FOCUS_DOWN);
6575 }
6576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006577 /**
6578 * Call this to try to give focus to a specific view or to one of its
6579 * descendants and give it a hint about what direction focus is heading.
6580 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006581 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6582 * false), or if it is focusable and it is not focusable in touch mode
6583 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006585 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 * have focus, and you want your parent to look for the next one.
6587 *
6588 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6589 * <code>null</code> set for the previously focused rectangle.
6590 *
6591 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6592 * @return Whether this view or one of its descendants actually took focus.
6593 */
6594 public final boolean requestFocus(int direction) {
6595 return requestFocus(direction, null);
6596 }
6597
6598 /**
6599 * Call this to try to give focus to a specific view or to one of its descendants
6600 * and give it hints about the direction and a specific rectangle that the focus
6601 * is coming from. The rectangle can help give larger views a finer grained hint
6602 * about where focus is coming from, and therefore, where to show selection, or
6603 * forward focus change internally.
6604 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006605 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6606 * false), or if it is focusable and it is not focusable in touch mode
6607 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 *
6609 * A View will not take focus if it is not visible.
6610 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006611 * A View will not take focus if one of its parents has
6612 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6613 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006615 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 * have focus, and you want your parent to look for the next one.
6617 *
6618 * You may wish to override this method if your custom {@link View} has an internal
6619 * {@link View} that it wishes to forward the request to.
6620 *
6621 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6622 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6623 * to give a finer grained hint about where focus is coming from. May be null
6624 * if there is no hint.
6625 * @return Whether this view or one of its descendants actually took focus.
6626 */
6627 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006628 return requestFocusNoSearch(direction, previouslyFocusedRect);
6629 }
6630
6631 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 // need to be focusable
6633 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6634 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6635 return false;
6636 }
6637
6638 // need to be focusable in touch mode if in touch mode
6639 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006640 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6641 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 }
6643
6644 // need to not have any parents blocking us
6645 if (hasAncestorThatBlocksDescendantFocus()) {
6646 return false;
6647 }
6648
6649 handleFocusGainInternal(direction, previouslyFocusedRect);
6650 return true;
6651 }
6652
6653 /**
6654 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6655 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6656 * touch mode to request focus when they are touched.
6657 *
6658 * @return Whether this view or one of its descendants actually took focus.
6659 *
6660 * @see #isInTouchMode()
6661 *
6662 */
6663 public final boolean requestFocusFromTouch() {
6664 // Leave touch mode if we need to
6665 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006666 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006667 if (viewRoot != null) {
6668 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
6670 }
6671 return requestFocus(View.FOCUS_DOWN);
6672 }
6673
6674 /**
6675 * @return Whether any ancestor of this view blocks descendant focus.
6676 */
6677 private boolean hasAncestorThatBlocksDescendantFocus() {
6678 ViewParent ancestor = mParent;
6679 while (ancestor instanceof ViewGroup) {
6680 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6681 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6682 return true;
6683 } else {
6684 ancestor = vgAncestor.getParent();
6685 }
6686 }
6687 return false;
6688 }
6689
6690 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006691 * Gets the mode for determining whether this View is important for accessibility
6692 * which is if it fires accessibility events and if it is reported to
6693 * accessibility services that query the screen.
6694 *
6695 * @return The mode for determining whether a View is important for accessibility.
6696 *
6697 * @attr ref android.R.styleable#View_importantForAccessibility
6698 *
6699 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6700 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6701 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6702 */
6703 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006704 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6705 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6706 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006707 })
6708 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006709 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6710 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006711 }
6712
6713 /**
6714 * Sets how to determine whether this view is important for accessibility
6715 * which is if it fires accessibility events and if it is reported to
6716 * accessibility services that query the screen.
6717 *
6718 * @param mode How to determine whether this view is important for accessibility.
6719 *
6720 * @attr ref android.R.styleable#View_importantForAccessibility
6721 *
6722 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6723 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6724 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6725 */
6726 public void setImportantForAccessibility(int mode) {
6727 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006728 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6729 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6730 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006731 notifyAccessibilityStateChanged();
6732 }
6733 }
6734
6735 /**
6736 * Gets whether this view should be exposed for accessibility.
6737 *
6738 * @return Whether the view is exposed for accessibility.
6739 *
6740 * @hide
6741 */
6742 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006743 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6744 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006745 switch (mode) {
6746 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6747 return true;
6748 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6749 return false;
6750 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006751 return isActionableForAccessibility() || hasListenersForAccessibility()
6752 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006753 default:
6754 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6755 + mode);
6756 }
6757 }
6758
6759 /**
6760 * Gets the parent for accessibility purposes. Note that the parent for
6761 * accessibility is not necessary the immediate parent. It is the first
6762 * predecessor that is important for accessibility.
6763 *
6764 * @return The parent for accessibility purposes.
6765 */
6766 public ViewParent getParentForAccessibility() {
6767 if (mParent instanceof View) {
6768 View parentView = (View) mParent;
6769 if (parentView.includeForAccessibility()) {
6770 return mParent;
6771 } else {
6772 return mParent.getParentForAccessibility();
6773 }
6774 }
6775 return null;
6776 }
6777
6778 /**
6779 * Adds the children of a given View for accessibility. Since some Views are
6780 * not important for accessibility the children for accessibility are not
6781 * necessarily direct children of the riew, rather they are the first level of
6782 * descendants important for accessibility.
6783 *
6784 * @param children The list of children for accessibility.
6785 */
6786 public void addChildrenForAccessibility(ArrayList<View> children) {
6787 if (includeForAccessibility()) {
6788 children.add(this);
6789 }
6790 }
6791
6792 /**
6793 * Whether to regard this view for accessibility. A view is regarded for
6794 * accessibility if it is important for accessibility or the querying
6795 * accessibility service has explicitly requested that view not
6796 * important for accessibility are regarded.
6797 *
6798 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006799 *
6800 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006801 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006802 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006803 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006804 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006805 }
6806 return false;
6807 }
6808
6809 /**
6810 * Returns whether the View is considered actionable from
6811 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006812 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006813 *
6814 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006815 *
6816 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006817 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006818 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006819 return (isClickable() || isLongClickable() || isFocusable());
6820 }
6821
6822 /**
6823 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006824 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006825 *
6826 * @return True if the view is actionable for accessibility.
6827 */
6828 private boolean hasListenersForAccessibility() {
6829 ListenerInfo info = getListenerInfo();
6830 return mTouchDelegate != null || info.mOnKeyListener != null
6831 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6832 || info.mOnHoverListener != null || info.mOnDragListener != null;
6833 }
6834
6835 /**
6836 * Notifies accessibility services that some view's important for
6837 * accessibility state has changed. Note that such notifications
6838 * are made at most once every
6839 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6840 * to avoid unnecessary load to the system. Also once a view has
6841 * made a notifucation this method is a NOP until the notification has
6842 * been sent to clients.
6843 *
6844 * @hide
6845 *
6846 * TODO: Makse sure this method is called for any view state change
6847 * that is interesting for accessilility purposes.
6848 */
6849 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006850 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6851 return;
6852 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006853 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6854 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006855 if (mParent != null) {
6856 mParent.childAccessibilityStateChanged(this);
6857 }
6858 }
6859 }
6860
6861 /**
6862 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006863 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006864 *
6865 * @hide
6866 */
6867 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006868 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006869 }
6870
6871 /**
6872 * Performs the specified accessibility action on the view. For
6873 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006874 * <p>
6875 * If an {@link AccessibilityDelegate} has been specified via calling
6876 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6877 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6878 * is responsible for handling this call.
6879 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006880 *
6881 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006882 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006883 * @return Whether the action was performed.
6884 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006885 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006886 if (mAccessibilityDelegate != null) {
6887 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6888 } else {
6889 return performAccessibilityActionInternal(action, arguments);
6890 }
6891 }
6892
6893 /**
6894 * @see #performAccessibilityAction(int, Bundle)
6895 *
6896 * Note: Called from the default {@link AccessibilityDelegate}.
6897 */
6898 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006899 switch (action) {
6900 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006901 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006902 performClick();
6903 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006904 }
6905 } break;
6906 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6907 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006908 performLongClick();
6909 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006910 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006911 } break;
6912 case AccessibilityNodeInfo.ACTION_FOCUS: {
6913 if (!hasFocus()) {
6914 // Get out of touch mode since accessibility
6915 // wants to move focus around.
6916 getViewRootImpl().ensureTouchMode(false);
6917 return requestFocus();
6918 }
6919 } break;
6920 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6921 if (hasFocus()) {
6922 clearFocus();
6923 return !isFocused();
6924 }
6925 } break;
6926 case AccessibilityNodeInfo.ACTION_SELECT: {
6927 if (!isSelected()) {
6928 setSelected(true);
6929 return isSelected();
6930 }
6931 } break;
6932 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6933 if (isSelected()) {
6934 setSelected(false);
6935 return !isSelected();
6936 }
6937 } break;
6938 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006939 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006940 return requestAccessibilityFocus();
6941 }
6942 } break;
6943 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6944 if (isAccessibilityFocused()) {
6945 clearAccessibilityFocus();
6946 return true;
6947 }
6948 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006949 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6950 if (arguments != null) {
6951 final int granularity = arguments.getInt(
6952 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6953 return nextAtGranularity(granularity);
6954 }
6955 } break;
6956 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6957 if (arguments != null) {
6958 final int granularity = arguments.getInt(
6959 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6960 return previousAtGranularity(granularity);
6961 }
6962 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006963 }
6964 return false;
6965 }
6966
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006967 private boolean nextAtGranularity(int granularity) {
6968 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006969 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006970 return false;
6971 }
6972 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6973 if (iterator == null) {
6974 return false;
6975 }
6976 final int current = getAccessibilityCursorPosition();
6977 final int[] range = iterator.following(current);
6978 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006979 return false;
6980 }
6981 final int start = range[0];
6982 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006983 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006984 sendViewTextTraversedAtGranularityEvent(
6985 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6986 granularity, start, end);
6987 return true;
6988 }
6989
6990 private boolean previousAtGranularity(int granularity) {
6991 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006992 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006993 return false;
6994 }
6995 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6996 if (iterator == null) {
6997 return false;
6998 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006999 int current = getAccessibilityCursorPosition();
7000 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
7001 current = text.length();
7002 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7003 // When traversing by character we always put the cursor after the character
7004 // to ease edit and have to compensate before asking the for previous segment.
7005 current--;
7006 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007007 final int[] range = iterator.preceding(current);
7008 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007009 return false;
7010 }
7011 final int start = range[0];
7012 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007013 // Always put the cursor after the character to ease edit.
7014 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7015 setAccessibilityCursorPosition(end);
7016 } else {
7017 setAccessibilityCursorPosition(start);
7018 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007019 sendViewTextTraversedAtGranularityEvent(
7020 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7021 granularity, start, end);
7022 return true;
7023 }
7024
7025 /**
7026 * Gets the text reported for accessibility purposes.
7027 *
7028 * @return The accessibility text.
7029 *
7030 * @hide
7031 */
7032 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007033 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007034 }
7035
7036 /**
7037 * @hide
7038 */
7039 public int getAccessibilityCursorPosition() {
7040 return mAccessibilityCursorPosition;
7041 }
7042
7043 /**
7044 * @hide
7045 */
7046 public void setAccessibilityCursorPosition(int position) {
7047 mAccessibilityCursorPosition = position;
7048 }
7049
7050 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7051 int fromIndex, int toIndex) {
7052 if (mParent == null) {
7053 return;
7054 }
7055 AccessibilityEvent event = AccessibilityEvent.obtain(
7056 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7057 onInitializeAccessibilityEvent(event);
7058 onPopulateAccessibilityEvent(event);
7059 event.setFromIndex(fromIndex);
7060 event.setToIndex(toIndex);
7061 event.setAction(action);
7062 event.setMovementGranularity(granularity);
7063 mParent.requestSendAccessibilityEvent(this, event);
7064 }
7065
7066 /**
7067 * @hide
7068 */
7069 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7070 switch (granularity) {
7071 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7072 CharSequence text = getIterableTextForAccessibility();
7073 if (text != null && text.length() > 0) {
7074 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007075 CharacterTextSegmentIterator.getInstance(
7076 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007077 iterator.initialize(text.toString());
7078 return iterator;
7079 }
7080 } break;
7081 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7082 CharSequence text = getIterableTextForAccessibility();
7083 if (text != null && text.length() > 0) {
7084 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007085 WordTextSegmentIterator.getInstance(
7086 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007087 iterator.initialize(text.toString());
7088 return iterator;
7089 }
7090 } break;
7091 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH: {
7092 CharSequence text = getIterableTextForAccessibility();
7093 if (text != null && text.length() > 0) {
7094 ParagraphTextSegmentIterator iterator =
7095 ParagraphTextSegmentIterator.getInstance();
7096 iterator.initialize(text.toString());
7097 return iterator;
7098 }
7099 } break;
7100 }
7101 return null;
7102 }
7103
Svetoslav Ganov42138042012-03-20 11:51:39 -07007104 /**
Romain Guya440b002010-02-24 15:57:54 -08007105 * @hide
7106 */
7107 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007108 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007109 clearDisplayList();
7110
Romain Guya440b002010-02-24 15:57:54 -08007111 onStartTemporaryDetach();
7112 }
7113
7114 /**
7115 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007116 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7117 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007118 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 */
7120 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007121 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007122 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007123 }
7124
7125 /**
7126 * @hide
7127 */
7128 public void dispatchFinishTemporaryDetach() {
7129 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007130 }
Romain Guy8506ab42009-06-11 17:35:47 -07007131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 /**
7133 * Called after {@link #onStartTemporaryDetach} when the container is done
7134 * changing the view.
7135 */
7136 public void onFinishTemporaryDetach() {
7137 }
Romain Guy8506ab42009-06-11 17:35:47 -07007138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007140 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7141 * for this view's window. Returns null if the view is not currently attached
7142 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007143 * just use the standard high-level event callbacks like
7144 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007145 */
7146 public KeyEvent.DispatcherState getKeyDispatcherState() {
7147 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7148 }
Joe Malin32736f02011-01-19 16:14:20 -08007149
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 * Dispatch a key event before it is processed by any input method
7152 * associated with the view hierarchy. This can be used to intercept
7153 * key events in special situations before the IME consumes them; a
7154 * typical example would be handling the BACK key to update the application's
7155 * UI instead of allowing the IME to see it and close itself.
7156 *
7157 * @param event The key event to be dispatched.
7158 * @return True if the event was handled, false otherwise.
7159 */
7160 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7161 return onKeyPreIme(event.getKeyCode(), event);
7162 }
7163
7164 /**
7165 * Dispatch a key event to the next view on the focus path. This path runs
7166 * from the top of the view tree down to the currently focused view. If this
7167 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7168 * the next node down the focus path. This method also fires any key
7169 * listeners.
7170 *
7171 * @param event The key event to be dispatched.
7172 * @return True if the event was handled, false otherwise.
7173 */
7174 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007175 if (mInputEventConsistencyVerifier != null) {
7176 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178
Jeff Brown21bc5c92011-02-28 18:27:14 -08007179 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007180 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007181 ListenerInfo li = mListenerInfo;
7182 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7183 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 return true;
7185 }
7186
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007187 if (event.dispatch(this, mAttachInfo != null
7188 ? mAttachInfo.mKeyDispatchState : null, this)) {
7189 return true;
7190 }
7191
7192 if (mInputEventConsistencyVerifier != null) {
7193 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7194 }
7195 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007196 }
7197
7198 /**
7199 * Dispatches a key shortcut event.
7200 *
7201 * @param event The key event to be dispatched.
7202 * @return True if the event was handled by the view, false otherwise.
7203 */
7204 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7205 return onKeyShortcut(event.getKeyCode(), event);
7206 }
7207
7208 /**
7209 * Pass the touch screen motion event down to the target view, or this
7210 * view if it is the target.
7211 *
7212 * @param event The motion event to be dispatched.
7213 * @return True if the event was handled by the view, false otherwise.
7214 */
7215 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007216 if (mInputEventConsistencyVerifier != null) {
7217 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7218 }
7219
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007220 if (onFilterTouchEventForSecurity(event)) {
7221 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007222 ListenerInfo li = mListenerInfo;
7223 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7224 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007225 return true;
7226 }
7227
7228 if (onTouchEvent(event)) {
7229 return true;
7230 }
Jeff Brown85a31762010-09-01 17:01:00 -07007231 }
7232
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007233 if (mInputEventConsistencyVerifier != null) {
7234 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007236 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 }
7238
7239 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007240 * Filter the touch event to apply security policies.
7241 *
7242 * @param event The motion event to be filtered.
7243 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007244 *
Jeff Brown85a31762010-09-01 17:01:00 -07007245 * @see #getFilterTouchesWhenObscured
7246 */
7247 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007248 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007249 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7250 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7251 // Window is obscured, drop this touch.
7252 return false;
7253 }
7254 return true;
7255 }
7256
7257 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 * Pass a trackball motion event down to the focused view.
7259 *
7260 * @param event The motion event to be dispatched.
7261 * @return True if the event was handled by the view, false otherwise.
7262 */
7263 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007264 if (mInputEventConsistencyVerifier != null) {
7265 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7266 }
7267
Romain Guy02ccac62011-06-24 13:20:23 -07007268 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007269 }
7270
7271 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007272 * Dispatch a generic motion event.
7273 * <p>
7274 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7275 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007276 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007277 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007278 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007279 *
7280 * @param event The motion event to be dispatched.
7281 * @return True if the event was handled by the view, false otherwise.
7282 */
7283 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007284 if (mInputEventConsistencyVerifier != null) {
7285 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7286 }
7287
Jeff Browna032cc02011-03-07 16:56:21 -08007288 final int source = event.getSource();
7289 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7290 final int action = event.getAction();
7291 if (action == MotionEvent.ACTION_HOVER_ENTER
7292 || action == MotionEvent.ACTION_HOVER_MOVE
7293 || action == MotionEvent.ACTION_HOVER_EXIT) {
7294 if (dispatchHoverEvent(event)) {
7295 return true;
7296 }
7297 } else if (dispatchGenericPointerEvent(event)) {
7298 return true;
7299 }
7300 } else if (dispatchGenericFocusedEvent(event)) {
7301 return true;
7302 }
7303
Jeff Brown10b62902011-06-20 16:40:37 -07007304 if (dispatchGenericMotionEventInternal(event)) {
7305 return true;
7306 }
7307
7308 if (mInputEventConsistencyVerifier != null) {
7309 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7310 }
7311 return false;
7312 }
7313
7314 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007315 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007316 ListenerInfo li = mListenerInfo;
7317 if (li != null && li.mOnGenericMotionListener != null
7318 && (mViewFlags & ENABLED_MASK) == ENABLED
7319 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007320 return true;
7321 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007322
7323 if (onGenericMotionEvent(event)) {
7324 return true;
7325 }
7326
7327 if (mInputEventConsistencyVerifier != null) {
7328 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7329 }
7330 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007331 }
7332
7333 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007334 * Dispatch a hover event.
7335 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007336 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007337 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007338 * </p>
7339 *
7340 * @param event The motion event to be dispatched.
7341 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007342 */
7343 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007344 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007345 ListenerInfo li = mListenerInfo;
7346 if (li != null && li.mOnHoverListener != null
7347 && (mViewFlags & ENABLED_MASK) == ENABLED
7348 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007349 return true;
7350 }
7351
Jeff Browna032cc02011-03-07 16:56:21 -08007352 return onHoverEvent(event);
7353 }
7354
7355 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007356 * Returns true if the view has a child to which it has recently sent
7357 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7358 * it does not have a hovered child, then it must be the innermost hovered view.
7359 * @hide
7360 */
7361 protected boolean hasHoveredChild() {
7362 return false;
7363 }
7364
7365 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007366 * Dispatch a generic motion event to the view under the first pointer.
7367 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007368 * Do not call this method directly.
7369 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007370 * </p>
7371 *
7372 * @param event The motion event to be dispatched.
7373 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007374 */
7375 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7376 return false;
7377 }
7378
7379 /**
7380 * Dispatch a generic motion event to the currently focused view.
7381 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007382 * Do not call this method directly.
7383 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007384 * </p>
7385 *
7386 * @param event The motion event to be dispatched.
7387 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007388 */
7389 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7390 return false;
7391 }
7392
7393 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007394 * Dispatch a pointer event.
7395 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007396 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7397 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7398 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007399 * and should not be expected to handle other pointing device features.
7400 * </p>
7401 *
7402 * @param event The motion event to be dispatched.
7403 * @return True if the event was handled by the view, false otherwise.
7404 * @hide
7405 */
7406 public final boolean dispatchPointerEvent(MotionEvent event) {
7407 if (event.isTouchEvent()) {
7408 return dispatchTouchEvent(event);
7409 } else {
7410 return dispatchGenericMotionEvent(event);
7411 }
7412 }
7413
7414 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 * Called when the window containing this view gains or loses window focus.
7416 * ViewGroups should override to route to their children.
7417 *
7418 * @param hasFocus True if the window containing this view now has focus,
7419 * false otherwise.
7420 */
7421 public void dispatchWindowFocusChanged(boolean hasFocus) {
7422 onWindowFocusChanged(hasFocus);
7423 }
7424
7425 /**
7426 * Called when the window containing this view gains or loses focus. Note
7427 * that this is separate from view focus: to receive key events, both
7428 * your view and its window must have focus. If a window is displayed
7429 * on top of yours that takes input focus, then your own window will lose
7430 * focus but the view focus will remain unchanged.
7431 *
7432 * @param hasWindowFocus True if the window containing this view now has
7433 * focus, false otherwise.
7434 */
7435 public void onWindowFocusChanged(boolean hasWindowFocus) {
7436 InputMethodManager imm = InputMethodManager.peekInstance();
7437 if (!hasWindowFocus) {
7438 if (isPressed()) {
7439 setPressed(false);
7440 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007441 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 imm.focusOut(this);
7443 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007444 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007445 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007446 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007447 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 imm.focusIn(this);
7449 }
7450 refreshDrawableState();
7451 }
7452
7453 /**
7454 * Returns true if this view is in a window that currently has window focus.
7455 * Note that this is not the same as the view itself having focus.
7456 *
7457 * @return True if this view is in a window that currently has window focus.
7458 */
7459 public boolean hasWindowFocus() {
7460 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7461 }
7462
7463 /**
Adam Powell326d8082009-12-09 15:10:07 -08007464 * Dispatch a view visibility change down the view hierarchy.
7465 * ViewGroups should override to route to their children.
7466 * @param changedView The view whose visibility changed. Could be 'this' or
7467 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007468 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7469 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007470 */
7471 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7472 onVisibilityChanged(changedView, visibility);
7473 }
7474
7475 /**
7476 * Called when the visibility of the view or an ancestor of the view is changed.
7477 * @param changedView The view whose visibility changed. Could be 'this' or
7478 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007479 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7480 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007481 */
7482 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007483 if (visibility == VISIBLE) {
7484 if (mAttachInfo != null) {
7485 initialAwakenScrollBars();
7486 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007487 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007488 }
7489 }
Adam Powell326d8082009-12-09 15:10:07 -08007490 }
7491
7492 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007493 * Dispatch a hint about whether this view is displayed. For instance, when
7494 * a View moves out of the screen, it might receives a display hint indicating
7495 * the view is not displayed. Applications should not <em>rely</em> on this hint
7496 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007497 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007498 * @param hint A hint about whether or not this view is displayed:
7499 * {@link #VISIBLE} or {@link #INVISIBLE}.
7500 */
7501 public void dispatchDisplayHint(int hint) {
7502 onDisplayHint(hint);
7503 }
7504
7505 /**
7506 * Gives this view a hint about whether is displayed or not. For instance, when
7507 * a View moves out of the screen, it might receives a display hint indicating
7508 * the view is not displayed. Applications should not <em>rely</em> on this hint
7509 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007510 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007511 * @param hint A hint about whether or not this view is displayed:
7512 * {@link #VISIBLE} or {@link #INVISIBLE}.
7513 */
7514 protected void onDisplayHint(int hint) {
7515 }
7516
7517 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 * Dispatch a window visibility change down the view hierarchy.
7519 * ViewGroups should override to route to their children.
7520 *
7521 * @param visibility The new visibility of the window.
7522 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007523 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007524 */
7525 public void dispatchWindowVisibilityChanged(int visibility) {
7526 onWindowVisibilityChanged(visibility);
7527 }
7528
7529 /**
7530 * Called when the window containing has change its visibility
7531 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7532 * that this tells you whether or not your window is being made visible
7533 * to the window manager; this does <em>not</em> tell you whether or not
7534 * your window is obscured by other windows on the screen, even if it
7535 * is itself visible.
7536 *
7537 * @param visibility The new visibility of the window.
7538 */
7539 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007540 if (visibility == VISIBLE) {
7541 initialAwakenScrollBars();
7542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 }
7544
7545 /**
7546 * Returns the current visibility of the window this view is attached to
7547 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7548 *
7549 * @return Returns the current visibility of the view's window.
7550 */
7551 public int getWindowVisibility() {
7552 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7553 }
7554
7555 /**
7556 * Retrieve the overall visible display size in which the window this view is
7557 * attached to has been positioned in. This takes into account screen
7558 * decorations above the window, for both cases where the window itself
7559 * is being position inside of them or the window is being placed under
7560 * then and covered insets are used for the window to position its content
7561 * inside. In effect, this tells you the available area where content can
7562 * be placed and remain visible to users.
7563 *
7564 * <p>This function requires an IPC back to the window manager to retrieve
7565 * the requested information, so should not be used in performance critical
7566 * code like drawing.
7567 *
7568 * @param outRect Filled in with the visible display frame. If the view
7569 * is not attached to a window, this is simply the raw display size.
7570 */
7571 public void getWindowVisibleDisplayFrame(Rect outRect) {
7572 if (mAttachInfo != null) {
7573 try {
7574 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7575 } catch (RemoteException e) {
7576 return;
7577 }
7578 // XXX This is really broken, and probably all needs to be done
7579 // in the window manager, and we need to know more about whether
7580 // we want the area behind or in front of the IME.
7581 final Rect insets = mAttachInfo.mVisibleInsets;
7582 outRect.left += insets.left;
7583 outRect.top += insets.top;
7584 outRect.right -= insets.right;
7585 outRect.bottom -= insets.bottom;
7586 return;
7587 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007588 // The view is not attached to a display so we don't have a context.
7589 // Make a best guess about the display size.
7590 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007591 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 }
7593
7594 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007595 * Dispatch a notification about a resource configuration change down
7596 * the view hierarchy.
7597 * ViewGroups should override to route to their children.
7598 *
7599 * @param newConfig The new resource configuration.
7600 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007601 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007602 */
7603 public void dispatchConfigurationChanged(Configuration newConfig) {
7604 onConfigurationChanged(newConfig);
7605 }
7606
7607 /**
7608 * Called when the current configuration of the resources being used
7609 * by the application have changed. You can use this to decide when
7610 * to reload resources that can changed based on orientation and other
7611 * configuration characterstics. You only need to use this if you are
7612 * not relying on the normal {@link android.app.Activity} mechanism of
7613 * recreating the activity instance upon a configuration change.
7614 *
7615 * @param newConfig The new resource configuration.
7616 */
7617 protected void onConfigurationChanged(Configuration newConfig) {
7618 }
7619
7620 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621 * Private function to aggregate all per-view attributes in to the view
7622 * root.
7623 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007624 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7625 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 }
7627
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007628 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7629 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007630 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007631 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007632 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007633 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007634 ListenerInfo li = mListenerInfo;
7635 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007636 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007638 }
7639 }
7640
7641 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007642 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007643 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007644 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7645 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 ai.mRecomputeGlobalAttributes = true;
7647 }
7648 }
7649 }
7650
7651 /**
7652 * Returns whether the device is currently in touch mode. Touch mode is entered
7653 * once the user begins interacting with the device by touch, and affects various
7654 * things like whether focus is always visible to the user.
7655 *
7656 * @return Whether the device is in touch mode.
7657 */
7658 @ViewDebug.ExportedProperty
7659 public boolean isInTouchMode() {
7660 if (mAttachInfo != null) {
7661 return mAttachInfo.mInTouchMode;
7662 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007663 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664 }
7665 }
7666
7667 /**
7668 * Returns the context the view is running in, through which it can
7669 * access the current theme, resources, etc.
7670 *
7671 * @return The view's Context.
7672 */
7673 @ViewDebug.CapturedViewProperty
7674 public final Context getContext() {
7675 return mContext;
7676 }
7677
7678 /**
7679 * Handle a key event before it is processed by any input method
7680 * associated with the view hierarchy. This can be used to intercept
7681 * key events in special situations before the IME consumes them; a
7682 * typical example would be handling the BACK key to update the application's
7683 * UI instead of allowing the IME to see it and close itself.
7684 *
7685 * @param keyCode The value in event.getKeyCode().
7686 * @param event Description of the key event.
7687 * @return If you handled the event, return true. If you want to allow the
7688 * event to be handled by the next receiver, return false.
7689 */
7690 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7691 return false;
7692 }
7693
7694 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007695 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7696 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7698 * is released, if the view is enabled and clickable.
7699 *
Jean Chalard405bc512012-05-29 19:12:34 +09007700 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7701 * although some may elect to do so in some situations. Do not rely on this to
7702 * catch software key presses.
7703 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 * @param keyCode A key code that represents the button pressed, from
7705 * {@link android.view.KeyEvent}.
7706 * @param event The KeyEvent object that defines the button action.
7707 */
7708 public boolean onKeyDown(int keyCode, KeyEvent event) {
7709 boolean result = false;
7710
7711 switch (keyCode) {
7712 case KeyEvent.KEYCODE_DPAD_CENTER:
7713 case KeyEvent.KEYCODE_ENTER: {
7714 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7715 return true;
7716 }
7717 // Long clickable items don't necessarily have to be clickable
7718 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7719 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7720 (event.getRepeatCount() == 0)) {
7721 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007722 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 return true;
7724 }
7725 break;
7726 }
7727 }
7728 return result;
7729 }
7730
7731 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007732 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7733 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7734 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007735 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7736 * although some may elect to do so in some situations. Do not rely on this to
7737 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007738 */
7739 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7740 return false;
7741 }
7742
7743 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007744 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7745 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007746 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7747 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007748 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7749 * although some may elect to do so in some situations. Do not rely on this to
7750 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 *
7752 * @param keyCode A key code that represents the button pressed, from
7753 * {@link android.view.KeyEvent}.
7754 * @param event The KeyEvent object that defines the button action.
7755 */
7756 public boolean onKeyUp(int keyCode, KeyEvent event) {
7757 boolean result = false;
7758
7759 switch (keyCode) {
7760 case KeyEvent.KEYCODE_DPAD_CENTER:
7761 case KeyEvent.KEYCODE_ENTER: {
7762 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7763 return true;
7764 }
7765 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7766 setPressed(false);
7767
7768 if (!mHasPerformedLongPress) {
7769 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007770 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007771
7772 result = performClick();
7773 }
7774 }
7775 break;
7776 }
7777 }
7778 return result;
7779 }
7780
7781 /**
7782 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7783 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7784 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007785 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7786 * although some may elect to do so in some situations. Do not rely on this to
7787 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 *
7789 * @param keyCode A key code that represents the button pressed, from
7790 * {@link android.view.KeyEvent}.
7791 * @param repeatCount The number of times the action was made.
7792 * @param event The KeyEvent object that defines the button action.
7793 */
7794 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7795 return false;
7796 }
7797
7798 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007799 * Called on the focused view when a key shortcut event is not handled.
7800 * Override this method to implement local key shortcuts for the View.
7801 * Key shortcuts can also be implemented by setting the
7802 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 *
7804 * @param keyCode The value in event.getKeyCode().
7805 * @param event Description of the key event.
7806 * @return If you handled the event, return true. If you want to allow the
7807 * event to be handled by the next receiver, return false.
7808 */
7809 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7810 return false;
7811 }
7812
7813 /**
7814 * Check whether the called view is a text editor, in which case it
7815 * would make sense to automatically display a soft input window for
7816 * it. Subclasses should override this if they implement
7817 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007818 * a call on that method would return a non-null InputConnection, and
7819 * they are really a first-class editor that the user would normally
7820 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007821 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007822 * <p>The default implementation always returns false. This does
7823 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7824 * will not be called or the user can not otherwise perform edits on your
7825 * view; it is just a hint to the system that this is not the primary
7826 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 * @return Returns true if this view is a text editor, else false.
7829 */
7830 public boolean onCheckIsTextEditor() {
7831 return false;
7832 }
Romain Guy8506ab42009-06-11 17:35:47 -07007833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007834 /**
7835 * Create a new InputConnection for an InputMethod to interact
7836 * with the view. The default implementation returns null, since it doesn't
7837 * support input methods. You can override this to implement such support.
7838 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007839 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 * <p>When implementing this, you probably also want to implement
7841 * {@link #onCheckIsTextEditor()} to indicate you will return a
7842 * non-null InputConnection.
7843 *
7844 * @param outAttrs Fill in with attribute information about the connection.
7845 */
7846 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7847 return null;
7848 }
7849
7850 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007851 * Called by the {@link android.view.inputmethod.InputMethodManager}
7852 * when a view who is not the current
7853 * input connection target is trying to make a call on the manager. The
7854 * default implementation returns false; you can override this to return
7855 * true for certain views if you are performing InputConnection proxying
7856 * to them.
7857 * @param view The View that is making the InputMethodManager call.
7858 * @return Return true to allow the call, false to reject.
7859 */
7860 public boolean checkInputConnectionProxy(View view) {
7861 return false;
7862 }
Romain Guy8506ab42009-06-11 17:35:47 -07007863
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 * Show the context menu for this view. It is not safe to hold on to the
7866 * menu after returning from this method.
7867 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007868 * You should normally not overload this method. Overload
7869 * {@link #onCreateContextMenu(ContextMenu)} or define an
7870 * {@link OnCreateContextMenuListener} to add items to the context menu.
7871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 * @param menu The context menu to populate
7873 */
7874 public void createContextMenu(ContextMenu menu) {
7875 ContextMenuInfo menuInfo = getContextMenuInfo();
7876
7877 // Sets the current menu info so all items added to menu will have
7878 // my extra info set.
7879 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7880
7881 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007882 ListenerInfo li = mListenerInfo;
7883 if (li != null && li.mOnCreateContextMenuListener != null) {
7884 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 }
7886
7887 // Clear the extra information so subsequent items that aren't mine don't
7888 // have my extra info.
7889 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7890
7891 if (mParent != null) {
7892 mParent.createContextMenu(menu);
7893 }
7894 }
7895
7896 /**
7897 * Views should implement this if they have extra information to associate
7898 * with the context menu. The return result is supplied as a parameter to
7899 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7900 * callback.
7901 *
7902 * @return Extra information about the item for which the context menu
7903 * should be shown. This information will vary across different
7904 * subclasses of View.
7905 */
7906 protected ContextMenuInfo getContextMenuInfo() {
7907 return null;
7908 }
7909
7910 /**
7911 * Views should implement this if the view itself is going to add items to
7912 * the context menu.
7913 *
7914 * @param menu the context menu to populate
7915 */
7916 protected void onCreateContextMenu(ContextMenu menu) {
7917 }
7918
7919 /**
7920 * Implement this method to handle trackball motion events. The
7921 * <em>relative</em> movement of the trackball since the last event
7922 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7923 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7924 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7925 * they will often be fractional values, representing the more fine-grained
7926 * movement information available from a trackball).
7927 *
7928 * @param event The motion event.
7929 * @return True if the event was handled, false otherwise.
7930 */
7931 public boolean onTrackballEvent(MotionEvent event) {
7932 return false;
7933 }
7934
7935 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007936 * Implement this method to handle generic motion events.
7937 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007938 * Generic motion events describe joystick movements, mouse hovers, track pad
7939 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007940 * {@link MotionEvent#getSource() source} of the motion event specifies
7941 * the class of input that was received. Implementations of this method
7942 * must examine the bits in the source before processing the event.
7943 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007944 * </p><p>
7945 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7946 * are delivered to the view under the pointer. All other generic motion events are
7947 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007948 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007949 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007950 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007951 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7952 * // process the joystick movement...
7953 * return true;
7954 * }
7955 * }
7956 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7957 * switch (event.getAction()) {
7958 * case MotionEvent.ACTION_HOVER_MOVE:
7959 * // process the mouse hover movement...
7960 * return true;
7961 * case MotionEvent.ACTION_SCROLL:
7962 * // process the scroll wheel movement...
7963 * return true;
7964 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007965 * }
7966 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007967 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007968 *
7969 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007970 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007971 */
7972 public boolean onGenericMotionEvent(MotionEvent event) {
7973 return false;
7974 }
7975
7976 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007977 * Implement this method to handle hover events.
7978 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007979 * This method is called whenever a pointer is hovering into, over, or out of the
7980 * bounds of a view and the view is not currently being touched.
7981 * Hover events are represented as pointer events with action
7982 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7983 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7984 * </p>
7985 * <ul>
7986 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7987 * when the pointer enters the bounds of the view.</li>
7988 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7989 * when the pointer has already entered the bounds of the view and has moved.</li>
7990 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7991 * when the pointer has exited the bounds of the view or when the pointer is
7992 * about to go down due to a button click, tap, or similar user action that
7993 * causes the view to be touched.</li>
7994 * </ul>
7995 * <p>
7996 * The view should implement this method to return true to indicate that it is
7997 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007998 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007999 * The default implementation calls {@link #setHovered} to update the hovered state
8000 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07008001 * is enabled and is clickable. The default implementation also sends hover
8002 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08008003 * </p>
8004 *
8005 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07008006 * @return True if the view handled the hover event.
8007 *
8008 * @see #isHovered
8009 * @see #setHovered
8010 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008011 */
8012 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008013 // The root view may receive hover (or touch) events that are outside the bounds of
8014 // the window. This code ensures that we only send accessibility events for
8015 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008016 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008017 if (!mSendingHoverAccessibilityEvents) {
8018 if ((action == MotionEvent.ACTION_HOVER_ENTER
8019 || action == MotionEvent.ACTION_HOVER_MOVE)
8020 && !hasHoveredChild()
8021 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008022 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008023 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008024 }
8025 } else {
8026 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008027 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008028 && !pointInView(event.getX(), event.getY()))) {
8029 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008030 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008031 // If the window does not have input focus we take away accessibility
8032 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008033 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008034 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008035 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008036 }
Jeff Browna1b24182011-07-28 13:38:24 -07008037 }
8038
Jeff Brown87b7f802011-06-21 18:35:45 -07008039 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008040 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008041 case MotionEvent.ACTION_HOVER_ENTER:
8042 setHovered(true);
8043 break;
8044 case MotionEvent.ACTION_HOVER_EXIT:
8045 setHovered(false);
8046 break;
8047 }
Jeff Browna1b24182011-07-28 13:38:24 -07008048
8049 // Dispatch the event to onGenericMotionEvent before returning true.
8050 // This is to provide compatibility with existing applications that
8051 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8052 // break because of the new default handling for hoverable views
8053 // in onHoverEvent.
8054 // Note that onGenericMotionEvent will be called by default when
8055 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8056 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008057 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008058 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008059
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008060 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008061 }
8062
8063 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008064 * Returns true if the view should handle {@link #onHoverEvent}
8065 * by calling {@link #setHovered} to change its hovered state.
8066 *
8067 * @return True if the view is hoverable.
8068 */
8069 private boolean isHoverable() {
8070 final int viewFlags = mViewFlags;
8071 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8072 return false;
8073 }
8074
8075 return (viewFlags & CLICKABLE) == CLICKABLE
8076 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8077 }
8078
8079 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008080 * Returns true if the view is currently hovered.
8081 *
8082 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008083 *
8084 * @see #setHovered
8085 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008086 */
Jeff Brown10b62902011-06-20 16:40:37 -07008087 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008088 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008089 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008090 }
8091
8092 /**
8093 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008094 * <p>
8095 * Calling this method also changes the drawable state of the view. This
8096 * enables the view to react to hover by using different drawable resources
8097 * to change its appearance.
8098 * </p><p>
8099 * The {@link #onHoverChanged} method is called when the hovered state changes.
8100 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008101 *
8102 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008103 *
8104 * @see #isHovered
8105 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008106 */
8107 public void setHovered(boolean hovered) {
8108 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008109 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8110 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008111 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008112 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008113 }
8114 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008115 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8116 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008117 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008118 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008119 }
8120 }
8121 }
8122
8123 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008124 * Implement this method to handle hover state changes.
8125 * <p>
8126 * This method is called whenever the hover state changes as a result of a
8127 * call to {@link #setHovered}.
8128 * </p>
8129 *
8130 * @param hovered The current hover state, as returned by {@link #isHovered}.
8131 *
8132 * @see #isHovered
8133 * @see #setHovered
8134 */
8135 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008136 }
8137
8138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 * Implement this method to handle touch screen motion events.
8140 *
8141 * @param event The motion event.
8142 * @return True if the event was handled, false otherwise.
8143 */
8144 public boolean onTouchEvent(MotionEvent event) {
8145 final int viewFlags = mViewFlags;
8146
8147 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008148 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008149 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008151 // A disabled view that is clickable still consumes the touch
8152 // events, it just doesn't respond to them.
8153 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8154 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8155 }
8156
8157 if (mTouchDelegate != null) {
8158 if (mTouchDelegate.onTouchEvent(event)) {
8159 return true;
8160 }
8161 }
8162
8163 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8164 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8165 switch (event.getAction()) {
8166 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008167 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8168 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169 // take focus if we don't have it already and we should in
8170 // touch mode.
8171 boolean focusTaken = false;
8172 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8173 focusTaken = requestFocus();
8174 }
8175
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008176 if (prepressed) {
8177 // The button is being released before we actually
8178 // showed it as pressed. Make it show the pressed
8179 // state now (before scheduling the click) to ensure
8180 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008181 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008182 }
Joe Malin32736f02011-01-19 16:14:20 -08008183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 if (!mHasPerformedLongPress) {
8185 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008186 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008187
8188 // Only perform take click actions if we were in the pressed state
8189 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008190 // Use a Runnable and post this rather than calling
8191 // performClick directly. This lets other visual state
8192 // of the view update before click actions start.
8193 if (mPerformClick == null) {
8194 mPerformClick = new PerformClick();
8195 }
8196 if (!post(mPerformClick)) {
8197 performClick();
8198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 }
8200 }
8201
8202 if (mUnsetPressedState == null) {
8203 mUnsetPressedState = new UnsetPressedState();
8204 }
8205
Adam Powelle14579b2009-12-16 18:39:52 -08008206 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008207 postDelayed(mUnsetPressedState,
8208 ViewConfiguration.getPressedStateDuration());
8209 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 // If the post failed, unpress right now
8211 mUnsetPressedState.run();
8212 }
Adam Powelle14579b2009-12-16 18:39:52 -08008213 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 }
8215 break;
8216
8217 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008218 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008219
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008220 if (performButtonActionOnTouchDown(event)) {
8221 break;
8222 }
8223
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008224 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008225 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008226
8227 // For views inside a scrolling container, delay the pressed feedback for
8228 // a short period in case this is a scroll.
8229 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008230 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008231 if (mPendingCheckForTap == null) {
8232 mPendingCheckForTap = new CheckForTap();
8233 }
8234 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8235 } else {
8236 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008237 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008238 checkForLongClick(0);
8239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008240 break;
8241
8242 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008243 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008244 removeTapCallback();
Adam Powell5311c442012-10-22 12:15:49 -07008245 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 break;
8247
8248 case MotionEvent.ACTION_MOVE:
8249 final int x = (int) event.getX();
8250 final int y = (int) event.getY();
8251
8252 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008253 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008255 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008256 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008257 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008258 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259
Adam Powell4d6f0662012-02-21 15:11:11 -08008260 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 }
8263 break;
8264 }
8265 return true;
8266 }
8267
8268 return false;
8269 }
8270
8271 /**
Adam Powell10298662011-08-14 18:26:30 -07008272 * @hide
8273 */
8274 public boolean isInScrollingContainer() {
8275 ViewParent p = getParent();
8276 while (p != null && p instanceof ViewGroup) {
8277 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8278 return true;
8279 }
8280 p = p.getParent();
8281 }
8282 return false;
8283 }
8284
8285 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008286 * Remove the longpress detection timer.
8287 */
8288 private void removeLongPressCallback() {
8289 if (mPendingCheckForLongPress != null) {
8290 removeCallbacks(mPendingCheckForLongPress);
8291 }
8292 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008293
8294 /**
8295 * Remove the pending click action
8296 */
8297 private void removePerformClickCallback() {
8298 if (mPerformClick != null) {
8299 removeCallbacks(mPerformClick);
8300 }
8301 }
8302
Adam Powelle14579b2009-12-16 18:39:52 -08008303 /**
Romain Guya440b002010-02-24 15:57:54 -08008304 * Remove the prepress detection timer.
8305 */
8306 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008307 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008308 setPressed(false);
8309 removeCallbacks(mUnsetPressedState);
8310 }
8311 }
8312
8313 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008314 * Remove the tap detection timer.
8315 */
8316 private void removeTapCallback() {
8317 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008318 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008319 removeCallbacks(mPendingCheckForTap);
8320 }
8321 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008322
8323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 * Cancels a pending long press. Your subclass can use this if you
8325 * want the context menu to come up if the user presses and holds
8326 * at the same place, but you don't want it to come up if they press
8327 * and then move around enough to cause scrolling.
8328 */
8329 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008330 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008331
8332 /*
8333 * The prepressed state handled by the tap callback is a display
8334 * construct, but the tap callback will post a long press callback
8335 * less its own timeout. Remove it here.
8336 */
8337 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 }
8339
8340 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008341 * Remove the pending callback for sending a
8342 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8343 */
8344 private void removeSendViewScrolledAccessibilityEventCallback() {
8345 if (mSendViewScrolledAccessibilityEvent != null) {
8346 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008347 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008348 }
8349 }
8350
8351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008352 * Sets the TouchDelegate for this View.
8353 */
8354 public void setTouchDelegate(TouchDelegate delegate) {
8355 mTouchDelegate = delegate;
8356 }
8357
8358 /**
8359 * Gets the TouchDelegate for this View.
8360 */
8361 public TouchDelegate getTouchDelegate() {
8362 return mTouchDelegate;
8363 }
8364
8365 /**
8366 * Set flags controlling behavior of this view.
8367 *
8368 * @param flags Constant indicating the value which should be set
8369 * @param mask Constant indicating the bit range that should be changed
8370 */
8371 void setFlags(int flags, int mask) {
8372 int old = mViewFlags;
8373 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8374
8375 int changed = mViewFlags ^ old;
8376 if (changed == 0) {
8377 return;
8378 }
8379 int privateFlags = mPrivateFlags;
8380
8381 /* Check if the FOCUSABLE bit has changed */
8382 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008383 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008385 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 /* Give up focus if we are no longer focusable */
8387 clearFocus();
8388 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008389 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 /*
8391 * Tell the view system that we are now available to take focus
8392 * if no one else already has it.
8393 */
8394 if (mParent != null) mParent.focusableViewAvailable(this);
8395 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008396 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8397 notifyAccessibilityStateChanged();
8398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 }
8400
8401 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8402 if ((changed & VISIBILITY_MASK) != 0) {
8403 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008404 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008405 * it was not visible. Marking it drawn ensures that the invalidation will
8406 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008408 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008409 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410
8411 needGlobalAttributesUpdate(true);
8412
8413 // a view becoming visible is worth notifying the parent
8414 // about in case nothing has focus. even if this specific view
8415 // isn't focusable, it may contain something that is, so let
8416 // the root view try to give this focus if nothing else does.
8417 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8418 mParent.focusableViewAvailable(this);
8419 }
8420 }
8421 }
8422
8423 /* Check if the GONE bit has changed */
8424 if ((changed & GONE) != 0) {
8425 needGlobalAttributesUpdate(false);
8426 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008427
Romain Guyecd80ee2009-12-03 17:13:02 -08008428 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8429 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008430 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008431 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008432 if (mParent instanceof View) {
8433 // GONE views noop invalidation, so invalidate the parent
8434 ((View) mParent).invalidate(true);
8435 }
8436 // Mark the view drawn to ensure that it gets invalidated properly the next
8437 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008438 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008439 }
8440 if (mAttachInfo != null) {
8441 mAttachInfo.mViewVisibilityChanged = true;
8442 }
8443 }
8444
8445 /* Check if the VISIBLE bit has changed */
8446 if ((changed & INVISIBLE) != 0) {
8447 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008448 /*
8449 * If this view is becoming invisible, set the DRAWN flag so that
8450 * the next invalidate() will not be skipped.
8451 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008452 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008453
8454 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008455 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 if (getRootView() != this) {
8457 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008458 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 }
8460 }
8461 if (mAttachInfo != null) {
8462 mAttachInfo.mViewVisibilityChanged = true;
8463 }
8464 }
8465
Adam Powell326d8082009-12-09 15:10:07 -08008466 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008467 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008468 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8469 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008470 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008471 } else if (mParent != null) {
8472 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008473 }
Adam Powell326d8082009-12-09 15:10:07 -08008474 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8475 }
8476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8478 destroyDrawingCache();
8479 }
8480
8481 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8482 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008483 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008484 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008485 }
8486
8487 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8488 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008489 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 }
8491
8492 if ((changed & DRAW_MASK) != 0) {
8493 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008494 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008495 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8496 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008498 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008499 }
8500 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008501 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 }
8503 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008504 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 }
8506
8507 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008508 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 mParent.recomputeViewAttributes(this);
8510 }
8511 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008512
8513 if (AccessibilityManager.getInstance(mContext).isEnabled()
8514 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8515 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8516 notifyAccessibilityStateChanged();
8517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 }
8519
8520 /**
8521 * Change the view's z order in the tree, so it's on top of other sibling
8522 * views
8523 */
8524 public void bringToFront() {
8525 if (mParent != null) {
8526 mParent.bringChildToFront(this);
8527 }
8528 }
8529
8530 /**
8531 * This is called in response to an internal scroll in this view (i.e., the
8532 * view scrolled its own contents). This is typically as a result of
8533 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8534 * called.
8535 *
8536 * @param l Current horizontal scroll origin.
8537 * @param t Current vertical scroll origin.
8538 * @param oldl Previous horizontal scroll origin.
8539 * @param oldt Previous vertical scroll origin.
8540 */
8541 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008542 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8543 postSendViewScrolledAccessibilityEventCallback();
8544 }
8545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 mBackgroundSizeChanged = true;
8547
8548 final AttachInfo ai = mAttachInfo;
8549 if (ai != null) {
8550 ai.mViewScrollChanged = true;
8551 }
8552 }
8553
8554 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008555 * Interface definition for a callback to be invoked when the layout bounds of a view
8556 * changes due to layout processing.
8557 */
8558 public interface OnLayoutChangeListener {
8559 /**
8560 * Called when the focus state of a view has changed.
8561 *
8562 * @param v The view whose state has changed.
8563 * @param left The new value of the view's left property.
8564 * @param top The new value of the view's top property.
8565 * @param right The new value of the view's right property.
8566 * @param bottom The new value of the view's bottom property.
8567 * @param oldLeft The previous value of the view's left property.
8568 * @param oldTop The previous value of the view's top property.
8569 * @param oldRight The previous value of the view's right property.
8570 * @param oldBottom The previous value of the view's bottom property.
8571 */
8572 void onLayoutChange(View v, int left, int top, int right, int bottom,
8573 int oldLeft, int oldTop, int oldRight, int oldBottom);
8574 }
8575
8576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 * This is called during layout when the size of this view has changed. If
8578 * you were just added to the view hierarchy, you're called with the old
8579 * values of 0.
8580 *
8581 * @param w Current width of this view.
8582 * @param h Current height of this view.
8583 * @param oldw Old width of this view.
8584 * @param oldh Old height of this view.
8585 */
8586 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8587 }
8588
8589 /**
8590 * Called by draw to draw the child views. This may be overridden
8591 * by derived classes to gain control just before its children are drawn
8592 * (but after its own view has been drawn).
8593 * @param canvas the canvas on which to draw the view
8594 */
8595 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 }
8598
8599 /**
8600 * Gets the parent of this view. Note that the parent is a
8601 * ViewParent and not necessarily a View.
8602 *
8603 * @return Parent of this view.
8604 */
8605 public final ViewParent getParent() {
8606 return mParent;
8607 }
8608
8609 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008610 * Set the horizontal scrolled position of your view. This will cause a call to
8611 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8612 * invalidated.
8613 * @param value the x position to scroll to
8614 */
8615 public void setScrollX(int value) {
8616 scrollTo(value, mScrollY);
8617 }
8618
8619 /**
8620 * Set the vertical scrolled position of your view. This will cause a call to
8621 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8622 * invalidated.
8623 * @param value the y position to scroll to
8624 */
8625 public void setScrollY(int value) {
8626 scrollTo(mScrollX, value);
8627 }
8628
8629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008630 * Return the scrolled left position of this view. This is the left edge of
8631 * the displayed part of your view. You do not need to draw any pixels
8632 * farther left, since those are outside of the frame of your view on
8633 * screen.
8634 *
8635 * @return The left edge of the displayed part of your view, in pixels.
8636 */
8637 public final int getScrollX() {
8638 return mScrollX;
8639 }
8640
8641 /**
8642 * Return the scrolled top position of this view. This is the top edge of
8643 * the displayed part of your view. You do not need to draw any pixels above
8644 * it, since those are outside of the frame of your view on screen.
8645 *
8646 * @return The top edge of the displayed part of your view, in pixels.
8647 */
8648 public final int getScrollY() {
8649 return mScrollY;
8650 }
8651
8652 /**
8653 * Return the width of the your view.
8654 *
8655 * @return The width of your view, in pixels.
8656 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008657 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008658 public final int getWidth() {
8659 return mRight - mLeft;
8660 }
8661
8662 /**
8663 * Return the height of your view.
8664 *
8665 * @return The height of your view, in pixels.
8666 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008667 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 public final int getHeight() {
8669 return mBottom - mTop;
8670 }
8671
8672 /**
8673 * Return the visible drawing bounds of your view. Fills in the output
8674 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008675 * getWidth(), and getHeight(). These bounds do not account for any
8676 * transformation properties currently set on the view, such as
8677 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 *
8679 * @param outRect The (scrolled) drawing bounds of the view.
8680 */
8681 public void getDrawingRect(Rect outRect) {
8682 outRect.left = mScrollX;
8683 outRect.top = mScrollY;
8684 outRect.right = mScrollX + (mRight - mLeft);
8685 outRect.bottom = mScrollY + (mBottom - mTop);
8686 }
8687
8688 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008689 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8690 * raw width component (that is the result is masked by
8691 * {@link #MEASURED_SIZE_MASK}).
8692 *
8693 * @return The raw measured width of this view.
8694 */
8695 public final int getMeasuredWidth() {
8696 return mMeasuredWidth & MEASURED_SIZE_MASK;
8697 }
8698
8699 /**
8700 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008701 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008702 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 * This should be used during measurement and layout calculations only. Use
8704 * {@link #getWidth()} to see how wide a view is after layout.
8705 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008706 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008708 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 return mMeasuredWidth;
8710 }
8711
8712 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008713 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8714 * raw width component (that is the result is masked by
8715 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008716 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008717 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 */
8719 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008720 return mMeasuredHeight & MEASURED_SIZE_MASK;
8721 }
8722
8723 /**
8724 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008725 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008726 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8727 * This should be used during measurement and layout calculations only. Use
8728 * {@link #getHeight()} to see how wide a view is after layout.
8729 *
8730 * @return The measured width of this view as a bit mask.
8731 */
8732 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 return mMeasuredHeight;
8734 }
8735
8736 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008737 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8738 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8739 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8740 * and the height component is at the shifted bits
8741 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8742 */
8743 public final int getMeasuredState() {
8744 return (mMeasuredWidth&MEASURED_STATE_MASK)
8745 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8746 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8747 }
8748
8749 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008750 * The transform matrix of this view, which is calculated based on the current
8751 * roation, scale, and pivot properties.
8752 *
8753 * @see #getRotation()
8754 * @see #getScaleX()
8755 * @see #getScaleY()
8756 * @see #getPivotX()
8757 * @see #getPivotY()
8758 * @return The current transform matrix for the view
8759 */
8760 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008761 if (mTransformationInfo != null) {
8762 updateMatrix();
8763 return mTransformationInfo.mMatrix;
8764 }
8765 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008766 }
8767
8768 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008769 * Utility function to determine if the value is far enough away from zero to be
8770 * considered non-zero.
8771 * @param value A floating point value to check for zero-ness
8772 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8773 */
8774 private static boolean nonzero(float value) {
8775 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8776 }
8777
8778 /**
Jeff Brown86671742010-09-30 20:00:15 -07008779 * Returns true if the transform matrix is the identity matrix.
8780 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008781 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008782 * @return True if the transform matrix is the identity matrix, false otherwise.
8783 */
Jeff Brown86671742010-09-30 20:00:15 -07008784 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008785 if (mTransformationInfo != null) {
8786 updateMatrix();
8787 return mTransformationInfo.mMatrixIsIdentity;
8788 }
8789 return true;
8790 }
8791
8792 void ensureTransformationInfo() {
8793 if (mTransformationInfo == null) {
8794 mTransformationInfo = new TransformationInfo();
8795 }
Jeff Brown86671742010-09-30 20:00:15 -07008796 }
8797
8798 /**
8799 * Recomputes the transform matrix if necessary.
8800 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008801 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008802 final TransformationInfo info = mTransformationInfo;
8803 if (info == null) {
8804 return;
8805 }
8806 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008807 // transform-related properties have changed since the last time someone
8808 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008809
8810 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008811 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008812 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8813 info.mPrevWidth = mRight - mLeft;
8814 info.mPrevHeight = mBottom - mTop;
8815 info.mPivotX = info.mPrevWidth / 2f;
8816 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008817 }
8818 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008819 info.mMatrix.reset();
8820 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8821 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8822 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8823 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008824 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008825 if (info.mCamera == null) {
8826 info.mCamera = new Camera();
8827 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008828 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008829 info.mCamera.save();
8830 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8831 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8832 info.mCamera.getMatrix(info.matrix3D);
8833 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8834 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8835 info.mPivotY + info.mTranslationY);
8836 info.mMatrix.postConcat(info.matrix3D);
8837 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008838 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008839 info.mMatrixDirty = false;
8840 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8841 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008842 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008843 }
8844
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008846 * Utility method to retrieve the inverse of the current mMatrix property.
8847 * We cache the matrix to avoid recalculating it when transform properties
8848 * have not changed.
8849 *
8850 * @return The inverse of the current matrix of this view.
8851 */
Jeff Brown86671742010-09-30 20:00:15 -07008852 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008853 final TransformationInfo info = mTransformationInfo;
8854 if (info != null) {
8855 updateMatrix();
8856 if (info.mInverseMatrixDirty) {
8857 if (info.mInverseMatrix == null) {
8858 info.mInverseMatrix = new Matrix();
8859 }
8860 info.mMatrix.invert(info.mInverseMatrix);
8861 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008862 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008863 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008864 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008865 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008866 }
8867
8868 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008869 * Gets the distance along the Z axis from the camera to this view.
8870 *
8871 * @see #setCameraDistance(float)
8872 *
8873 * @return The distance along the Z axis.
8874 */
8875 public float getCameraDistance() {
8876 ensureTransformationInfo();
8877 final float dpi = mResources.getDisplayMetrics().densityDpi;
8878 final TransformationInfo info = mTransformationInfo;
8879 if (info.mCamera == null) {
8880 info.mCamera = new Camera();
8881 info.matrix3D = new Matrix();
8882 }
8883 return -(info.mCamera.getLocationZ() * dpi);
8884 }
8885
8886 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008887 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8888 * views are drawn) from the camera to this view. The camera's distance
8889 * affects 3D transformations, for instance rotations around the X and Y
8890 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008891 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008892 * use a camera distance that's greater than the height (X axis rotation) or
8893 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008894 *
Romain Guya5364ee2011-02-24 14:46:04 -08008895 * <p>The distance of the camera from the view plane can have an affect on the
8896 * perspective distortion of the view when it is rotated around the x or y axis.
8897 * For example, a large distance will result in a large viewing angle, and there
8898 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008899 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008900 * also result in some drawing artifacts if the rotated view ends up partially
8901 * behind the camera (which is why the recommendation is to use a distance at
8902 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008903 *
Romain Guya5364ee2011-02-24 14:46:04 -08008904 * <p>The distance is expressed in "depth pixels." The default distance depends
8905 * on the screen density. For instance, on a medium density display, the
8906 * default distance is 1280. On a high density display, the default distance
8907 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008908 *
Romain Guya5364ee2011-02-24 14:46:04 -08008909 * <p>If you want to specify a distance that leads to visually consistent
8910 * results across various densities, use the following formula:</p>
8911 * <pre>
8912 * float scale = context.getResources().getDisplayMetrics().density;
8913 * view.setCameraDistance(distance * scale);
8914 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008915 *
Romain Guya5364ee2011-02-24 14:46:04 -08008916 * <p>The density scale factor of a high density display is 1.5,
8917 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008918 *
Romain Guya5364ee2011-02-24 14:46:04 -08008919 * @param distance The distance in "depth pixels", if negative the opposite
8920 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008921 *
8922 * @see #setRotationX(float)
8923 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008924 */
8925 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008926 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008927
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008928 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008929 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008930 final TransformationInfo info = mTransformationInfo;
8931 if (info.mCamera == null) {
8932 info.mCamera = new Camera();
8933 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008934 }
8935
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008936 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8937 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008938
Chet Haase9d1992d2012-03-13 11:03:25 -07008939 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008940 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008941 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008942 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008943 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008944 // View was rejected last time it was drawn by its parent; this may have changed
8945 invalidateParentIfNeeded();
8946 }
Romain Guya5364ee2011-02-24 14:46:04 -08008947 }
8948
8949 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 * The degrees that the view is rotated around the pivot point.
8951 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008952 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008953 * @see #getPivotX()
8954 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008955 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008956 * @return The degrees of rotation.
8957 */
Chet Haasea5531132012-02-02 13:41:44 -08008958 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008959 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008960 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008961 }
8962
8963 /**
Chet Haase897247b2010-09-09 14:54:47 -07008964 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8965 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008966 *
8967 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008968 *
8969 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008970 * @see #getPivotX()
8971 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008972 * @see #setRotationX(float)
8973 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008974 *
8975 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008976 */
8977 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008978 ensureTransformationInfo();
8979 final TransformationInfo info = mTransformationInfo;
8980 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008981 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008982 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008983 info.mRotation = rotation;
8984 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008985 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008986 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008987 mDisplayList.setRotation(rotation);
8988 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008989 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008990 // View was rejected last time it was drawn by its parent; this may have changed
8991 invalidateParentIfNeeded();
8992 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008993 }
8994 }
8995
8996 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008997 * The degrees that the view is rotated around the vertical axis through the pivot point.
8998 *
8999 * @see #getPivotX()
9000 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009001 * @see #setRotationY(float)
9002 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009003 * @return The degrees of Y rotation.
9004 */
Chet Haasea5531132012-02-02 13:41:44 -08009005 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009006 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009007 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009008 }
9009
9010 /**
Chet Haase897247b2010-09-09 14:54:47 -07009011 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9012 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9013 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009014 *
Romain Guya5364ee2011-02-24 14:46:04 -08009015 * When rotating large views, it is recommended to adjust the camera distance
9016 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009017 *
9018 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009019 *
9020 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009021 * @see #getPivotX()
9022 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009023 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009024 * @see #setRotationX(float)
9025 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009026 *
9027 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009028 */
9029 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009030 ensureTransformationInfo();
9031 final TransformationInfo info = mTransformationInfo;
9032 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009033 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009034 info.mRotationY = rotationY;
9035 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009036 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009037 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009038 mDisplayList.setRotationY(rotationY);
9039 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009040 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009041 // View was rejected last time it was drawn by its parent; this may have changed
9042 invalidateParentIfNeeded();
9043 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009044 }
9045 }
9046
9047 /**
9048 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9049 *
9050 * @see #getPivotX()
9051 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009052 * @see #setRotationX(float)
9053 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009054 * @return The degrees of X rotation.
9055 */
Chet Haasea5531132012-02-02 13:41:44 -08009056 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009057 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009058 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009059 }
9060
9061 /**
Chet Haase897247b2010-09-09 14:54:47 -07009062 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9063 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9064 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009065 *
Romain Guya5364ee2011-02-24 14:46:04 -08009066 * When rotating large views, it is recommended to adjust the camera distance
9067 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009068 *
9069 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009070 *
9071 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009072 * @see #getPivotX()
9073 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009074 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009075 * @see #setRotationY(float)
9076 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009077 *
9078 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009079 */
9080 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009081 ensureTransformationInfo();
9082 final TransformationInfo info = mTransformationInfo;
9083 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009084 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009085 info.mRotationX = rotationX;
9086 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009087 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009088 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009089 mDisplayList.setRotationX(rotationX);
9090 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009091 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009092 // View was rejected last time it was drawn by its parent; this may have changed
9093 invalidateParentIfNeeded();
9094 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009095 }
9096 }
9097
9098 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009099 * The amount that the view is scaled in x around the pivot point, as a proportion of
9100 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9101 *
Joe Onorato93162322010-09-16 15:42:01 -04009102 * <p>By default, this is 1.0f.
9103 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009104 * @see #getPivotX()
9105 * @see #getPivotY()
9106 * @return The scaling factor.
9107 */
Chet Haasea5531132012-02-02 13:41:44 -08009108 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009109 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009110 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009111 }
9112
9113 /**
9114 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9115 * the view's unscaled width. A value of 1 means that no scaling is applied.
9116 *
9117 * @param scaleX The scaling factor.
9118 * @see #getPivotX()
9119 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009120 *
9121 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009122 */
9123 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009124 ensureTransformationInfo();
9125 final TransformationInfo info = mTransformationInfo;
9126 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009127 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009128 info.mScaleX = scaleX;
9129 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009130 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009131 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009132 mDisplayList.setScaleX(scaleX);
9133 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009134 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009135 // View was rejected last time it was drawn by its parent; this may have changed
9136 invalidateParentIfNeeded();
9137 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009138 }
9139 }
9140
9141 /**
9142 * The amount that the view is scaled in y around the pivot point, as a proportion of
9143 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9144 *
Joe Onorato93162322010-09-16 15:42:01 -04009145 * <p>By default, this is 1.0f.
9146 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009147 * @see #getPivotX()
9148 * @see #getPivotY()
9149 * @return The scaling factor.
9150 */
Chet Haasea5531132012-02-02 13:41:44 -08009151 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009152 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009153 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009154 }
9155
9156 /**
9157 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9158 * the view's unscaled width. A value of 1 means that no scaling is applied.
9159 *
9160 * @param scaleY The scaling factor.
9161 * @see #getPivotX()
9162 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009163 *
9164 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009165 */
9166 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009167 ensureTransformationInfo();
9168 final TransformationInfo info = mTransformationInfo;
9169 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009170 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009171 info.mScaleY = scaleY;
9172 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009173 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009174 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009175 mDisplayList.setScaleY(scaleY);
9176 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009177 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009178 // View was rejected last time it was drawn by its parent; this may have changed
9179 invalidateParentIfNeeded();
9180 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009181 }
9182 }
9183
9184 /**
9185 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9186 * and {@link #setScaleX(float) scaled}.
9187 *
9188 * @see #getRotation()
9189 * @see #getScaleX()
9190 * @see #getScaleY()
9191 * @see #getPivotY()
9192 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009193 *
9194 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009195 */
Chet Haasea5531132012-02-02 13:41:44 -08009196 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009197 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009198 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009199 }
9200
9201 /**
9202 * Sets the x location of the point around which the view is
9203 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009204 * By default, the pivot point is centered on the object.
9205 * Setting this property disables this behavior and causes the view to use only the
9206 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009207 *
9208 * @param pivotX The x location of the pivot point.
9209 * @see #getRotation()
9210 * @see #getScaleX()
9211 * @see #getScaleY()
9212 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009213 *
9214 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009215 */
9216 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009217 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009218 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009219 final TransformationInfo info = mTransformationInfo;
9220 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009221 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009222 info.mPivotX = pivotX;
9223 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009224 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009225 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009226 mDisplayList.setPivotX(pivotX);
9227 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009228 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009229 // View was rejected last time it was drawn by its parent; this may have changed
9230 invalidateParentIfNeeded();
9231 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009232 }
9233 }
9234
9235 /**
9236 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9237 * and {@link #setScaleY(float) scaled}.
9238 *
9239 * @see #getRotation()
9240 * @see #getScaleX()
9241 * @see #getScaleY()
9242 * @see #getPivotY()
9243 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009244 *
9245 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009246 */
Chet Haasea5531132012-02-02 13:41:44 -08009247 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009248 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009249 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009250 }
9251
9252 /**
9253 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009254 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9255 * Setting this property disables this behavior and causes the view to use only the
9256 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009257 *
9258 * @param pivotY The y location of the pivot point.
9259 * @see #getRotation()
9260 * @see #getScaleX()
9261 * @see #getScaleY()
9262 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009263 *
9264 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009265 */
9266 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009267 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009268 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009269 final TransformationInfo info = mTransformationInfo;
9270 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009271 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009272 info.mPivotY = pivotY;
9273 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009274 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009275 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009276 mDisplayList.setPivotY(pivotY);
9277 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009278 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009279 // View was rejected last time it was drawn by its parent; this may have changed
9280 invalidateParentIfNeeded();
9281 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009282 }
9283 }
9284
9285 /**
9286 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9287 * completely transparent and 1 means the view is completely opaque.
9288 *
Joe Onorato93162322010-09-16 15:42:01 -04009289 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009290 * @return The opacity of the view.
9291 */
Chet Haasea5531132012-02-02 13:41:44 -08009292 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009293 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009294 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009295 }
9296
9297 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009298 * Returns whether this View has content which overlaps. This function, intended to be
9299 * overridden by specific View types, is an optimization when alpha is set on a view. If
9300 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9301 * and then composited it into place, which can be expensive. If the view has no overlapping
9302 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9303 * An example of overlapping rendering is a TextView with a background image, such as a
9304 * Button. An example of non-overlapping rendering is a TextView with no background, or
9305 * an ImageView with only the foreground image. The default implementation returns true;
9306 * subclasses should override if they have cases which can be optimized.
9307 *
9308 * @return true if the content in this view might overlap, false otherwise.
9309 */
9310 public boolean hasOverlappingRendering() {
9311 return true;
9312 }
9313
9314 /**
Romain Guy171c5922011-01-06 10:04:23 -08009315 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9316 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009317 *
Romain Guy171c5922011-01-06 10:04:23 -08009318 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9319 * responsible for applying the opacity itself. Otherwise, calling this method is
9320 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009321 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009322 *
Chet Haasea5531132012-02-02 13:41:44 -08009323 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9324 * performance implications. It is generally best to use the alpha property sparingly and
9325 * transiently, as in the case of fading animations.</p>
9326 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009327 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009328 *
Joe Malin32736f02011-01-19 16:14:20 -08009329 * @see #setLayerType(int, android.graphics.Paint)
9330 *
Chet Haase73066682010-11-29 15:55:32 -08009331 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009332 */
9333 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009334 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009335 if (mTransformationInfo.mAlpha != alpha) {
9336 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009337 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009338 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009339 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009340 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009341 invalidate(true);
9342 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009343 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009344 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009345 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009346 mDisplayList.setAlpha(alpha);
9347 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009348 }
Chet Haaseed032702010-10-01 14:05:54 -07009349 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009350 }
9351
9352 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009353 * Faster version of setAlpha() which performs the same steps except there are
9354 * no calls to invalidate(). The caller of this function should perform proper invalidation
9355 * on the parent and this object. The return value indicates whether the subclass handles
9356 * alpha (the return value for onSetAlpha()).
9357 *
9358 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009359 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9360 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009361 */
9362 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009363 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009364 if (mTransformationInfo.mAlpha != alpha) {
9365 mTransformationInfo.mAlpha = alpha;
9366 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9367 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009368 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009369 return true;
9370 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009371 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009372 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009373 mDisplayList.setAlpha(alpha);
9374 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009375 }
Chet Haasea00f3862011-02-22 06:34:40 -08009376 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009377 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009378 }
9379
9380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 * Top position of this view relative to its parent.
9382 *
9383 * @return The top of this view, in pixels.
9384 */
9385 @ViewDebug.CapturedViewProperty
9386 public final int getTop() {
9387 return mTop;
9388 }
9389
9390 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009391 * Sets the top position of this view relative to its parent. This method is meant to be called
9392 * by the layout system and should not generally be called otherwise, because the property
9393 * may be changed at any time by the layout.
9394 *
9395 * @param top The top of this view, in pixels.
9396 */
9397 public final void setTop(int top) {
9398 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009399 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009400 final boolean matrixIsIdentity = mTransformationInfo == null
9401 || mTransformationInfo.mMatrixIsIdentity;
9402 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009403 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009404 int minTop;
9405 int yLoc;
9406 if (top < mTop) {
9407 minTop = top;
9408 yLoc = top - mTop;
9409 } else {
9410 minTop = mTop;
9411 yLoc = 0;
9412 }
Chet Haasee9140a72011-02-16 16:23:29 -08009413 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009414 }
9415 } else {
9416 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009417 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009418 }
9419
Chet Haaseed032702010-10-01 14:05:54 -07009420 int width = mRight - mLeft;
9421 int oldHeight = mBottom - mTop;
9422
Chet Haase21cd1382010-09-01 17:42:29 -07009423 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009424 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009425 mDisplayList.setTop(mTop);
9426 }
Chet Haase21cd1382010-09-01 17:42:29 -07009427
Chet Haaseed032702010-10-01 14:05:54 -07009428 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9429
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009430 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009431 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009432 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009433 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009434 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009435 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009436 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009437 }
Chet Haase55dbb652010-12-21 20:15:08 -08009438 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009439 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009440 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009441 // View was rejected last time it was drawn by its parent; this may have changed
9442 invalidateParentIfNeeded();
9443 }
Chet Haase21cd1382010-09-01 17:42:29 -07009444 }
9445 }
9446
9447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 * Bottom position of this view relative to its parent.
9449 *
9450 * @return The bottom of this view, in pixels.
9451 */
9452 @ViewDebug.CapturedViewProperty
9453 public final int getBottom() {
9454 return mBottom;
9455 }
9456
9457 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009458 * True if this view has changed since the last time being drawn.
9459 *
9460 * @return The dirty state of this view.
9461 */
9462 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009463 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009464 }
9465
9466 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009467 * Sets the bottom position of this view relative to its parent. This method is meant to be
9468 * called by the layout system and should not generally be called otherwise, because the
9469 * property may be changed at any time by the layout.
9470 *
9471 * @param bottom The bottom of this view, in pixels.
9472 */
9473 public final void setBottom(int bottom) {
9474 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009475 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009476 final boolean matrixIsIdentity = mTransformationInfo == null
9477 || mTransformationInfo.mMatrixIsIdentity;
9478 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009479 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009480 int maxBottom;
9481 if (bottom < mBottom) {
9482 maxBottom = mBottom;
9483 } else {
9484 maxBottom = bottom;
9485 }
Chet Haasee9140a72011-02-16 16:23:29 -08009486 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009487 }
9488 } else {
9489 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009490 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009491 }
9492
Chet Haaseed032702010-10-01 14:05:54 -07009493 int width = mRight - mLeft;
9494 int oldHeight = mBottom - mTop;
9495
Chet Haase21cd1382010-09-01 17:42:29 -07009496 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009497 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009498 mDisplayList.setBottom(mBottom);
9499 }
Chet Haase21cd1382010-09-01 17:42:29 -07009500
Chet Haaseed032702010-10-01 14:05:54 -07009501 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9502
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009503 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009504 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009505 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009506 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009507 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009508 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009509 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009510 }
Chet Haase55dbb652010-12-21 20:15:08 -08009511 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009512 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009513 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009514 // View was rejected last time it was drawn by its parent; this may have changed
9515 invalidateParentIfNeeded();
9516 }
Chet Haase21cd1382010-09-01 17:42:29 -07009517 }
9518 }
9519
9520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009521 * Left position of this view relative to its parent.
9522 *
9523 * @return The left edge of this view, in pixels.
9524 */
9525 @ViewDebug.CapturedViewProperty
9526 public final int getLeft() {
9527 return mLeft;
9528 }
9529
9530 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009531 * Sets the left position of this view relative to its parent. This method is meant to be called
9532 * by the layout system and should not generally be called otherwise, because the property
9533 * may be changed at any time by the layout.
9534 *
9535 * @param left The bottom of this view, in pixels.
9536 */
9537 public final void setLeft(int left) {
9538 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009539 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009540 final boolean matrixIsIdentity = mTransformationInfo == null
9541 || mTransformationInfo.mMatrixIsIdentity;
9542 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009543 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009544 int minLeft;
9545 int xLoc;
9546 if (left < mLeft) {
9547 minLeft = left;
9548 xLoc = left - mLeft;
9549 } else {
9550 minLeft = mLeft;
9551 xLoc = 0;
9552 }
Chet Haasee9140a72011-02-16 16:23:29 -08009553 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009554 }
9555 } else {
9556 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009557 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009558 }
9559
Chet Haaseed032702010-10-01 14:05:54 -07009560 int oldWidth = mRight - mLeft;
9561 int height = mBottom - mTop;
9562
Chet Haase21cd1382010-09-01 17:42:29 -07009563 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009564 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009565 mDisplayList.setLeft(left);
9566 }
Chet Haase21cd1382010-09-01 17:42:29 -07009567
Chet Haaseed032702010-10-01 14:05:54 -07009568 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9569
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009570 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009571 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009572 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009573 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009574 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009575 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009576 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009577 }
Chet Haase55dbb652010-12-21 20:15:08 -08009578 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009579 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009580 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009581 // View was rejected last time it was drawn by its parent; this may have changed
9582 invalidateParentIfNeeded();
9583 }
Chet Haase21cd1382010-09-01 17:42:29 -07009584 }
9585 }
9586
9587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 * Right position of this view relative to its parent.
9589 *
9590 * @return The right edge of this view, in pixels.
9591 */
9592 @ViewDebug.CapturedViewProperty
9593 public final int getRight() {
9594 return mRight;
9595 }
9596
9597 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009598 * Sets the right position of this view relative to its parent. This method is meant to be called
9599 * by the layout system and should not generally be called otherwise, because the property
9600 * may be changed at any time by the layout.
9601 *
9602 * @param right The bottom of this view, in pixels.
9603 */
9604 public final void setRight(int right) {
9605 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009606 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009607 final boolean matrixIsIdentity = mTransformationInfo == null
9608 || mTransformationInfo.mMatrixIsIdentity;
9609 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009610 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009611 int maxRight;
9612 if (right < mRight) {
9613 maxRight = mRight;
9614 } else {
9615 maxRight = right;
9616 }
Chet Haasee9140a72011-02-16 16:23:29 -08009617 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009618 }
9619 } else {
9620 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009621 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009622 }
9623
Chet Haaseed032702010-10-01 14:05:54 -07009624 int oldWidth = mRight - mLeft;
9625 int height = mBottom - mTop;
9626
Chet Haase21cd1382010-09-01 17:42:29 -07009627 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009628 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009629 mDisplayList.setRight(mRight);
9630 }
Chet Haase21cd1382010-09-01 17:42:29 -07009631
Chet Haaseed032702010-10-01 14:05:54 -07009632 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9633
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009634 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009635 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009636 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009637 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009638 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009639 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009640 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009641 }
Chet Haase55dbb652010-12-21 20:15:08 -08009642 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009643 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009644 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009645 // View was rejected last time it was drawn by its parent; this may have changed
9646 invalidateParentIfNeeded();
9647 }
Chet Haase21cd1382010-09-01 17:42:29 -07009648 }
9649 }
9650
9651 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009652 * The visual x position of this view, in pixels. This is equivalent to the
9653 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009654 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009655 *
Chet Haasedf030d22010-07-30 17:22:38 -07009656 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009657 */
Chet Haasea5531132012-02-02 13:41:44 -08009658 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009659 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009660 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009661 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009662
Chet Haasedf030d22010-07-30 17:22:38 -07009663 /**
9664 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9665 * {@link #setTranslationX(float) translationX} property to be the difference between
9666 * the x value passed in and the current {@link #getLeft() left} property.
9667 *
9668 * @param x The visual x position of this view, in pixels.
9669 */
9670 public void setX(float x) {
9671 setTranslationX(x - mLeft);
9672 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009673
Chet Haasedf030d22010-07-30 17:22:38 -07009674 /**
9675 * The visual y position of this view, in pixels. This is equivalent to the
9676 * {@link #setTranslationY(float) translationY} property plus the current
9677 * {@link #getTop() top} property.
9678 *
9679 * @return The visual y position of this view, in pixels.
9680 */
Chet Haasea5531132012-02-02 13:41:44 -08009681 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009682 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009683 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009684 }
9685
9686 /**
9687 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9688 * {@link #setTranslationY(float) translationY} property to be the difference between
9689 * the y value passed in and the current {@link #getTop() top} property.
9690 *
9691 * @param y The visual y position of this view, in pixels.
9692 */
9693 public void setY(float y) {
9694 setTranslationY(y - mTop);
9695 }
9696
9697
9698 /**
9699 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9700 * This position is post-layout, in addition to wherever the object's
9701 * layout placed it.
9702 *
9703 * @return The horizontal position of this view relative to its left position, in pixels.
9704 */
Chet Haasea5531132012-02-02 13:41:44 -08009705 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009706 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009707 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009708 }
9709
9710 /**
9711 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9712 * This effectively positions the object post-layout, in addition to wherever the object's
9713 * layout placed it.
9714 *
9715 * @param translationX The horizontal position of this view relative to its left position,
9716 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009717 *
9718 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009719 */
9720 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009721 ensureTransformationInfo();
9722 final TransformationInfo info = mTransformationInfo;
9723 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009724 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009725 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009726 info.mTranslationX = translationX;
9727 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009728 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009729 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009730 mDisplayList.setTranslationX(translationX);
9731 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009732 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009733 // View was rejected last time it was drawn by its parent; this may have changed
9734 invalidateParentIfNeeded();
9735 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009736 }
9737 }
9738
9739 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009740 * The horizontal location of this view relative to its {@link #getTop() top} position.
9741 * This position is post-layout, in addition to wherever the object's
9742 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009743 *
Chet Haasedf030d22010-07-30 17:22:38 -07009744 * @return The vertical position of this view relative to its top position,
9745 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009746 */
Chet Haasea5531132012-02-02 13:41:44 -08009747 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009748 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009749 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009750 }
9751
9752 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009753 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9754 * This effectively positions the object post-layout, in addition to wherever the object's
9755 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009756 *
Chet Haasedf030d22010-07-30 17:22:38 -07009757 * @param translationY The vertical position of this view relative to its top position,
9758 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009759 *
9760 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009761 */
Chet Haasedf030d22010-07-30 17:22:38 -07009762 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009763 ensureTransformationInfo();
9764 final TransformationInfo info = mTransformationInfo;
9765 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009766 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009767 info.mTranslationY = translationY;
9768 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009769 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009770 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009771 mDisplayList.setTranslationY(translationY);
9772 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009773 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009774 // View was rejected last time it was drawn by its parent; this may have changed
9775 invalidateParentIfNeeded();
9776 }
Chet Haasedf030d22010-07-30 17:22:38 -07009777 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009778 }
9779
9780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009781 * Hit rectangle in parent's coordinates
9782 *
9783 * @param outRect The hit rectangle of the view.
9784 */
9785 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009786 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009787 final TransformationInfo info = mTransformationInfo;
9788 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009789 outRect.set(mLeft, mTop, mRight, mBottom);
9790 } else {
9791 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009792 tmpRect.set(-info.mPivotX, -info.mPivotY,
9793 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9794 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009795 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9796 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009797 }
9798 }
9799
9800 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009801 * Determines whether the given point, in local coordinates is inside the view.
9802 */
9803 /*package*/ final boolean pointInView(float localX, float localY) {
9804 return localX >= 0 && localX < (mRight - mLeft)
9805 && localY >= 0 && localY < (mBottom - mTop);
9806 }
9807
9808 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009809 * Utility method to determine whether the given point, in local coordinates,
9810 * is inside the view, where the area of the view is expanded by the slop factor.
9811 * This method is called while processing touch-move events to determine if the event
9812 * is still within the view.
9813 */
9814 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009815 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009816 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 }
9818
9819 /**
9820 * When a view has focus and the user navigates away from it, the next view is searched for
9821 * starting from the rectangle filled in by this method.
9822 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009823 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009824 * of the view. However, if your view maintains some idea of internal selection,
9825 * such as a cursor, or a selected row or column, you should override this method and
9826 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 *
9828 * @param r The rectangle to fill in, in this view's coordinates.
9829 */
9830 public void getFocusedRect(Rect r) {
9831 getDrawingRect(r);
9832 }
9833
9834 /**
9835 * If some part of this view is not clipped by any of its parents, then
9836 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009837 * coordinates (without taking possible View rotations into account), offset
9838 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9839 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 *
9841 * @param r If true is returned, r holds the global coordinates of the
9842 * visible portion of this view.
9843 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9844 * between this view and its root. globalOffet may be null.
9845 * @return true if r is non-empty (i.e. part of the view is visible at the
9846 * root level.
9847 */
9848 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9849 int width = mRight - mLeft;
9850 int height = mBottom - mTop;
9851 if (width > 0 && height > 0) {
9852 r.set(0, 0, width, height);
9853 if (globalOffset != null) {
9854 globalOffset.set(-mScrollX, -mScrollY);
9855 }
9856 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9857 }
9858 return false;
9859 }
9860
9861 public final boolean getGlobalVisibleRect(Rect r) {
9862 return getGlobalVisibleRect(r, null);
9863 }
9864
9865 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009866 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 if (getGlobalVisibleRect(r, offset)) {
9868 r.offset(-offset.x, -offset.y); // make r local
9869 return true;
9870 }
9871 return false;
9872 }
9873
9874 /**
9875 * Offset this view's vertical location by the specified number of pixels.
9876 *
9877 * @param offset the number of pixels to offset the view by
9878 */
9879 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009880 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009881 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009882 final boolean matrixIsIdentity = mTransformationInfo == null
9883 || mTransformationInfo.mMatrixIsIdentity;
9884 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009885 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009886 invalidateViewProperty(false, false);
9887 } else {
9888 final ViewParent p = mParent;
9889 if (p != null && mAttachInfo != null) {
9890 final Rect r = mAttachInfo.mTmpInvalRect;
9891 int minTop;
9892 int maxBottom;
9893 int yLoc;
9894 if (offset < 0) {
9895 minTop = mTop + offset;
9896 maxBottom = mBottom;
9897 yLoc = offset;
9898 } else {
9899 minTop = mTop;
9900 maxBottom = mBottom + offset;
9901 yLoc = 0;
9902 }
9903 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9904 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009905 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009906 }
9907 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009908 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009909 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009910
Chet Haasec3aa3612010-06-17 08:50:37 -07009911 mTop += offset;
9912 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009913 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009914 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009915 invalidateViewProperty(false, false);
9916 } else {
9917 if (!matrixIsIdentity) {
9918 invalidateViewProperty(false, true);
9919 }
9920 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009921 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 }
9924
9925 /**
9926 * Offset this view's horizontal location by the specified amount of pixels.
9927 *
9928 * @param offset the numer of pixels to offset the view by
9929 */
9930 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009931 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009932 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009933 final boolean matrixIsIdentity = mTransformationInfo == null
9934 || mTransformationInfo.mMatrixIsIdentity;
9935 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009936 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009937 invalidateViewProperty(false, false);
9938 } else {
9939 final ViewParent p = mParent;
9940 if (p != null && mAttachInfo != null) {
9941 final Rect r = mAttachInfo.mTmpInvalRect;
9942 int minLeft;
9943 int maxRight;
9944 if (offset < 0) {
9945 minLeft = mLeft + offset;
9946 maxRight = mRight;
9947 } else {
9948 minLeft = mLeft;
9949 maxRight = mRight + offset;
9950 }
9951 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9952 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009953 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009954 }
9955 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009956 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009957 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009958
Chet Haasec3aa3612010-06-17 08:50:37 -07009959 mLeft += offset;
9960 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009961 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009962 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009963 invalidateViewProperty(false, false);
9964 } else {
9965 if (!matrixIsIdentity) {
9966 invalidateViewProperty(false, true);
9967 }
9968 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009969 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 }
9972
9973 /**
9974 * Get the LayoutParams associated with this view. All views should have
9975 * layout parameters. These supply parameters to the <i>parent</i> of this
9976 * view specifying how it should be arranged. There are many subclasses of
9977 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9978 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009979 *
9980 * This method may return null if this View is not attached to a parent
9981 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9982 * was not invoked successfully. When a View is attached to a parent
9983 * ViewGroup, this method must not return null.
9984 *
9985 * @return The LayoutParams associated with this view, or null if no
9986 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009988 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009989 public ViewGroup.LayoutParams getLayoutParams() {
9990 return mLayoutParams;
9991 }
9992
9993 /**
9994 * Set the layout parameters associated with this view. These supply
9995 * parameters to the <i>parent</i> of this view specifying how it should be
9996 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9997 * correspond to the different subclasses of ViewGroup that are responsible
9998 * for arranging their children.
9999 *
Romain Guy01c174b2011-02-22 11:51:06 -080010000 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010001 */
10002 public void setLayoutParams(ViewGroup.LayoutParams params) {
10003 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -080010004 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 }
10006 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010007 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -080010008 if (mParent instanceof ViewGroup) {
10009 ((ViewGroup) mParent).onSetLayoutParams(this, params);
10010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 requestLayout();
10012 }
10013
10014 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010015 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010016 *
10017 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010018 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010019 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010020 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010021 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010022 }
10023 }
10024
10025 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 * Set the scrolled position of your view. This will cause a call to
10027 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10028 * invalidated.
10029 * @param x the x position to scroll to
10030 * @param y the y position to scroll to
10031 */
10032 public void scrollTo(int x, int y) {
10033 if (mScrollX != x || mScrollY != y) {
10034 int oldX = mScrollX;
10035 int oldY = mScrollY;
10036 mScrollX = x;
10037 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010038 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010040 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010041 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010043 }
10044 }
10045
10046 /**
10047 * Move the scrolled position of your view. This will cause a call to
10048 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10049 * invalidated.
10050 * @param x the amount of pixels to scroll by horizontally
10051 * @param y the amount of pixels to scroll by vertically
10052 */
10053 public void scrollBy(int x, int y) {
10054 scrollTo(mScrollX + x, mScrollY + y);
10055 }
10056
10057 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010058 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10059 * animation to fade the scrollbars out after a default delay. If a subclass
10060 * provides animated scrolling, the start delay should equal the duration
10061 * of the scrolling animation.</p>
10062 *
10063 * <p>The animation starts only if at least one of the scrollbars is
10064 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10065 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10066 * this method returns true, and false otherwise. If the animation is
10067 * started, this method calls {@link #invalidate()}; in that case the
10068 * caller should not call {@link #invalidate()}.</p>
10069 *
10070 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010071 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010072 *
10073 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10074 * and {@link #scrollTo(int, int)}.</p>
10075 *
10076 * @return true if the animation is played, false otherwise
10077 *
10078 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010079 * @see #scrollBy(int, int)
10080 * @see #scrollTo(int, int)
10081 * @see #isHorizontalScrollBarEnabled()
10082 * @see #isVerticalScrollBarEnabled()
10083 * @see #setHorizontalScrollBarEnabled(boolean)
10084 * @see #setVerticalScrollBarEnabled(boolean)
10085 */
10086 protected boolean awakenScrollBars() {
10087 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010088 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010089 }
10090
10091 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010092 * Trigger the scrollbars to draw.
10093 * This method differs from awakenScrollBars() only in its default duration.
10094 * initialAwakenScrollBars() will show the scroll bars for longer than
10095 * usual to give the user more of a chance to notice them.
10096 *
10097 * @return true if the animation is played, false otherwise.
10098 */
10099 private boolean initialAwakenScrollBars() {
10100 return mScrollCache != null &&
10101 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10102 }
10103
10104 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010105 * <p>
10106 * Trigger the scrollbars to draw. When invoked this method starts an
10107 * animation to fade the scrollbars out after a fixed delay. If a subclass
10108 * provides animated scrolling, the start delay should equal the duration of
10109 * the scrolling animation.
10110 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010111 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010112 * <p>
10113 * The animation starts only if at least one of the scrollbars is enabled,
10114 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10115 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10116 * this method returns true, and false otherwise. If the animation is
10117 * started, this method calls {@link #invalidate()}; in that case the caller
10118 * should not call {@link #invalidate()}.
10119 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010120 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010121 * <p>
10122 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010123 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010124 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010125 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010126 * @param startDelay the delay, in milliseconds, after which the animation
10127 * should start; when the delay is 0, the animation starts
10128 * immediately
10129 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010130 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010131 * @see #scrollBy(int, int)
10132 * @see #scrollTo(int, int)
10133 * @see #isHorizontalScrollBarEnabled()
10134 * @see #isVerticalScrollBarEnabled()
10135 * @see #setHorizontalScrollBarEnabled(boolean)
10136 * @see #setVerticalScrollBarEnabled(boolean)
10137 */
10138 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010139 return awakenScrollBars(startDelay, true);
10140 }
Joe Malin32736f02011-01-19 16:14:20 -080010141
Mike Cleron290947b2009-09-29 18:34:32 -070010142 /**
10143 * <p>
10144 * Trigger the scrollbars to draw. When invoked this method starts an
10145 * animation to fade the scrollbars out after a fixed delay. If a subclass
10146 * provides animated scrolling, the start delay should equal the duration of
10147 * the scrolling animation.
10148 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010149 *
Mike Cleron290947b2009-09-29 18:34:32 -070010150 * <p>
10151 * The animation starts only if at least one of the scrollbars is enabled,
10152 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10153 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10154 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010155 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010156 * is set to true; in that case the caller
10157 * should not call {@link #invalidate()}.
10158 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010159 *
Mike Cleron290947b2009-09-29 18:34:32 -070010160 * <p>
10161 * This method should be invoked everytime a subclass directly updates the
10162 * scroll parameters.
10163 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010164 *
Mike Cleron290947b2009-09-29 18:34:32 -070010165 * @param startDelay the delay, in milliseconds, after which the animation
10166 * should start; when the delay is 0, the animation starts
10167 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010168 *
Mike Cleron290947b2009-09-29 18:34:32 -070010169 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010170 *
Mike Cleron290947b2009-09-29 18:34:32 -070010171 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010172 *
Mike Cleron290947b2009-09-29 18:34:32 -070010173 * @see #scrollBy(int, int)
10174 * @see #scrollTo(int, int)
10175 * @see #isHorizontalScrollBarEnabled()
10176 * @see #isVerticalScrollBarEnabled()
10177 * @see #setHorizontalScrollBarEnabled(boolean)
10178 * @see #setVerticalScrollBarEnabled(boolean)
10179 */
10180 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010181 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010182
Mike Cleronf116bf82009-09-27 19:14:12 -070010183 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10184 return false;
10185 }
10186
10187 if (scrollCache.scrollBar == null) {
10188 scrollCache.scrollBar = new ScrollBarDrawable();
10189 }
10190
10191 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10192
Mike Cleron290947b2009-09-29 18:34:32 -070010193 if (invalidate) {
10194 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010195 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010196 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010197
10198 if (scrollCache.state == ScrollabilityCache.OFF) {
10199 // FIXME: this is copied from WindowManagerService.
10200 // We should get this value from the system when it
10201 // is possible to do so.
10202 final int KEY_REPEAT_FIRST_DELAY = 750;
10203 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10204 }
10205
10206 // Tell mScrollCache when we should start fading. This may
10207 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010208 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010209 scrollCache.fadeStartTime = fadeStartTime;
10210 scrollCache.state = ScrollabilityCache.ON;
10211
10212 // Schedule our fader to run, unscheduling any old ones first
10213 if (mAttachInfo != null) {
10214 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10215 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10216 }
10217
10218 return true;
10219 }
10220
10221 return false;
10222 }
10223
10224 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010225 * Do not invalidate views which are not visible and which are not running an animation. They
10226 * will not get drawn and they should not set dirty flags as if they will be drawn
10227 */
10228 private boolean skipInvalidate() {
10229 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10230 (!(mParent instanceof ViewGroup) ||
10231 !((ViewGroup) mParent).isViewTransitioning(this));
10232 }
10233 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010234 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010235 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10236 * in the future. This must be called from a UI thread. To call from a non-UI
10237 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 *
10239 * WARNING: This method is destructive to dirty.
10240 * @param dirty the rectangle representing the bounds of the dirty region
10241 */
10242 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010243 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010244 return;
10245 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010246 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10247 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10248 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10249 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10250 mPrivateFlags |= PFLAG_INVALIDATED;
10251 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252 final ViewParent p = mParent;
10253 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010254 //noinspection PointlessBooleanExpression,ConstantConditions
10255 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10256 if (p != null && ai != null && ai.mHardwareAccelerated) {
10257 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010258 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010259 p.invalidateChild(this, null);
10260 return;
10261 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 if (p != null && ai != null) {
10264 final int scrollX = mScrollX;
10265 final int scrollY = mScrollY;
10266 final Rect r = ai.mTmpInvalRect;
10267 r.set(dirty.left - scrollX, dirty.top - scrollY,
10268 dirty.right - scrollX, dirty.bottom - scrollY);
10269 mParent.invalidateChild(this, r);
10270 }
10271 }
10272 }
10273
10274 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010275 * 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 -080010276 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010277 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10278 * will be called at some point in the future. This must be called from
10279 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010280 * @param l the left position of the dirty region
10281 * @param t the top position of the dirty region
10282 * @param r the right position of the dirty region
10283 * @param b the bottom position of the dirty region
10284 */
10285 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010286 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010287 return;
10288 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010289 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10290 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10291 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10292 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10293 mPrivateFlags |= PFLAG_INVALIDATED;
10294 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 final ViewParent p = mParent;
10296 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010297 //noinspection PointlessBooleanExpression,ConstantConditions
10298 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10299 if (p != null && ai != null && ai.mHardwareAccelerated) {
10300 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010301 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010302 p.invalidateChild(this, null);
10303 return;
10304 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 if (p != null && ai != null && l < r && t < b) {
10307 final int scrollX = mScrollX;
10308 final int scrollY = mScrollY;
10309 final Rect tmpr = ai.mTmpInvalRect;
10310 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10311 p.invalidateChild(this, tmpr);
10312 }
10313 }
10314 }
10315
10316 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010317 * Invalidate the whole view. If the view is visible,
10318 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10319 * the future. This must be called from a UI thread. To call from a non-UI thread,
10320 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 */
10322 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010323 invalidate(true);
10324 }
Joe Malin32736f02011-01-19 16:14:20 -080010325
Chet Haaseed032702010-10-01 14:05:54 -070010326 /**
10327 * This is where the invalidate() work actually happens. A full invalidate()
10328 * causes the drawing cache to be invalidated, but this function can be called with
10329 * invalidateCache set to false to skip that invalidation step for cases that do not
10330 * need it (for example, a component that remains at the same dimensions with the same
10331 * content).
10332 *
10333 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10334 * well. This is usually true for a full invalidate, but may be set to false if the
10335 * View's contents or dimensions have not changed.
10336 */
Romain Guy849d0a32011-02-01 17:20:48 -080010337 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010338 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010339 return;
10340 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010341 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10342 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10343 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010344 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010345 mPrivateFlags &= ~PFLAG_DRAWN;
10346 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010347 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010348 mPrivateFlags |= PFLAG_INVALIDATED;
10349 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010352 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010353 //noinspection PointlessBooleanExpression,ConstantConditions
10354 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10355 if (p != null && ai != null && ai.mHardwareAccelerated) {
10356 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010357 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010358 p.invalidateChild(this, null);
10359 return;
10360 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010361 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 if (p != null && ai != null) {
10364 final Rect r = ai.mTmpInvalRect;
10365 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10366 // Don't call invalidate -- we don't want to internally scroll
10367 // our own bounds
10368 p.invalidateChild(this, r);
10369 }
10370 }
10371 }
10372
10373 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010374 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10375 * set any flags or handle all of the cases handled by the default invalidation methods.
10376 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10377 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10378 * walk up the hierarchy, transforming the dirty rect as necessary.
10379 *
10380 * The method also handles normal invalidation logic if display list properties are not
10381 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10382 * backup approach, to handle these cases used in the various property-setting methods.
10383 *
10384 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10385 * are not being used in this view
10386 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10387 * list properties are not being used in this view
10388 */
10389 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010390 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010391 if (invalidateParent) {
10392 invalidateParentCaches();
10393 }
10394 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010395 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010396 }
10397 invalidate(false);
10398 } else {
10399 final AttachInfo ai = mAttachInfo;
10400 final ViewParent p = mParent;
10401 if (p != null && ai != null) {
10402 final Rect r = ai.mTmpInvalRect;
10403 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10404 if (mParent instanceof ViewGroup) {
10405 ((ViewGroup) mParent).invalidateChildFast(this, r);
10406 } else {
10407 mParent.invalidateChild(this, r);
10408 }
10409 }
10410 }
10411 }
10412
10413 /**
10414 * Utility method to transform a given Rect by the current matrix of this view.
10415 */
10416 void transformRect(final Rect rect) {
10417 if (!getMatrix().isIdentity()) {
10418 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10419 boundingRect.set(rect);
10420 getMatrix().mapRect(boundingRect);
10421 rect.set((int) (boundingRect.left - 0.5f),
10422 (int) (boundingRect.top - 0.5f),
10423 (int) (boundingRect.right + 0.5f),
10424 (int) (boundingRect.bottom + 0.5f));
10425 }
10426 }
10427
10428 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010429 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010430 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10431 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010432 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10433 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010434 *
10435 * @hide
10436 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010437 protected void invalidateParentCaches() {
10438 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010439 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010440 }
10441 }
Joe Malin32736f02011-01-19 16:14:20 -080010442
Romain Guy0fd89bf2011-01-26 15:41:30 -080010443 /**
10444 * Used to indicate that the parent of this view should be invalidated. This functionality
10445 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10446 * which is necessary when various parent-managed properties of the view change, such as
10447 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10448 * an invalidation event to the parent.
10449 *
10450 * @hide
10451 */
10452 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010453 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010454 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010455 }
10456 }
10457
10458 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010459 * Indicates whether this View is opaque. An opaque View guarantees that it will
10460 * draw all the pixels overlapping its bounds using a fully opaque color.
10461 *
10462 * Subclasses of View should override this method whenever possible to indicate
10463 * whether an instance is opaque. Opaque Views are treated in a special way by
10464 * the View hierarchy, possibly allowing it to perform optimizations during
10465 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010466 *
Romain Guy24443ea2009-05-11 11:56:30 -070010467 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010468 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010469 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010470 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010471 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010472 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010473 }
10474
Adam Powell20232d02010-12-08 21:08:53 -080010475 /**
10476 * @hide
10477 */
10478 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010479 // Opaque if:
10480 // - Has a background
10481 // - Background is opaque
10482 // - Doesn't have scrollbars or scrollbars are inside overlay
10483
Philip Milne6c8ea062012-04-03 17:38:43 -070010484 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010485 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010486 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010487 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010488 }
10489
10490 final int flags = mViewFlags;
10491 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10492 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010493 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010494 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010495 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010496 }
10497 }
10498
10499 /**
10500 * @hide
10501 */
10502 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010503 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010504 }
10505
10506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 * @return A handler associated with the thread running the View. This
10508 * handler can be used to pump events in the UI events queue.
10509 */
10510 public Handler getHandler() {
10511 if (mAttachInfo != null) {
10512 return mAttachInfo.mHandler;
10513 }
10514 return null;
10515 }
10516
10517 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010518 * Gets the view root associated with the View.
10519 * @return The view root, or null if none.
10520 * @hide
10521 */
10522 public ViewRootImpl getViewRootImpl() {
10523 if (mAttachInfo != null) {
10524 return mAttachInfo.mViewRootImpl;
10525 }
10526 return null;
10527 }
10528
10529 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010530 * <p>Causes the Runnable to be added to the message queue.
10531 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010532 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 * @param action The Runnable that will be executed.
10534 *
10535 * @return Returns true if the Runnable was successfully placed in to the
10536 * message queue. Returns false on failure, usually because the
10537 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010538 *
10539 * @see #postDelayed
10540 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 */
10542 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010543 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010544 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010545 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010547 // Assume that post will succeed later
10548 ViewRootImpl.getRunQueue().post(action);
10549 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 }
10551
10552 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010553 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010555 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010556 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010557 * @param action The Runnable that will be executed.
10558 * @param delayMillis The delay (in milliseconds) until the Runnable
10559 * will be executed.
10560 *
10561 * @return true if the Runnable was successfully placed in to the
10562 * message queue. Returns false on failure, usually because the
10563 * looper processing the message queue is exiting. Note that a
10564 * result of true does not mean the Runnable will be processed --
10565 * if the looper is quit before the delivery time of the message
10566 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010567 *
10568 * @see #post
10569 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 */
10571 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010572 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010573 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010574 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010575 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010576 // Assume that post will succeed later
10577 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10578 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 }
10580
10581 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010582 * <p>Causes the Runnable to execute on the next animation time step.
10583 * The runnable will be run on the user interface thread.</p>
10584 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010585 * @param action The Runnable that will be executed.
10586 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010587 * @see #postOnAnimationDelayed
10588 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010589 */
10590 public void postOnAnimation(Runnable action) {
10591 final AttachInfo attachInfo = mAttachInfo;
10592 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010593 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10594 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010595 } else {
10596 // Assume that post will succeed later
10597 ViewRootImpl.getRunQueue().post(action);
10598 }
10599 }
10600
10601 /**
10602 * <p>Causes the Runnable to execute on the next animation time step,
10603 * after the specified amount of time elapses.
10604 * The runnable will be run on the user interface thread.</p>
10605 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010606 * @param action The Runnable that will be executed.
10607 * @param delayMillis The delay (in milliseconds) until the Runnable
10608 * will be executed.
10609 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010610 * @see #postOnAnimation
10611 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010612 */
10613 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10614 final AttachInfo attachInfo = mAttachInfo;
10615 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010616 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10617 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010618 } else {
10619 // Assume that post will succeed later
10620 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10621 }
10622 }
10623
10624 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010625 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010626 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 * @param action The Runnable to remove from the message handling queue
10628 *
10629 * @return true if this view could ask the Handler to remove the Runnable,
10630 * false otherwise. When the returned value is true, the Runnable
10631 * may or may not have been actually removed from the message queue
10632 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010633 *
10634 * @see #post
10635 * @see #postDelayed
10636 * @see #postOnAnimation
10637 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638 */
10639 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010640 if (action != null) {
10641 final AttachInfo attachInfo = mAttachInfo;
10642 if (attachInfo != null) {
10643 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010644 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10645 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010646 } else {
10647 // Assume that post will succeed later
10648 ViewRootImpl.getRunQueue().removeCallbacks(action);
10649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 return true;
10652 }
10653
10654 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010655 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10656 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 *
Romain Guye63a4f32011-08-11 11:33:31 -070010658 * <p>This method can be invoked from outside of the UI thread
10659 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010662 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 */
10664 public void postInvalidate() {
10665 postInvalidateDelayed(0);
10666 }
10667
10668 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010669 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10670 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010671 *
Romain Guye63a4f32011-08-11 11:33:31 -070010672 * <p>This method can be invoked from outside of the UI thread
10673 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 *
10675 * @param left The left coordinate of the rectangle to invalidate.
10676 * @param top The top coordinate of the rectangle to invalidate.
10677 * @param right The right coordinate of the rectangle to invalidate.
10678 * @param bottom The bottom coordinate of the rectangle to invalidate.
10679 *
10680 * @see #invalidate(int, int, int, int)
10681 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010682 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 */
10684 public void postInvalidate(int left, int top, int right, int bottom) {
10685 postInvalidateDelayed(0, left, top, right, bottom);
10686 }
10687
10688 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010689 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10690 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010691 *
Romain Guye63a4f32011-08-11 11:33:31 -070010692 * <p>This method can be invoked from outside of the UI thread
10693 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 *
10695 * @param delayMilliseconds the duration in milliseconds to delay the
10696 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010697 *
10698 * @see #invalidate()
10699 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 */
10701 public void postInvalidateDelayed(long delayMilliseconds) {
10702 // We try only with the AttachInfo because there's no point in invalidating
10703 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010704 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010705 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010706 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 }
10708 }
10709
10710 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010711 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10712 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010713 *
Romain Guye63a4f32011-08-11 11:33:31 -070010714 * <p>This method can be invoked from outside of the UI thread
10715 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 *
10717 * @param delayMilliseconds the duration in milliseconds to delay the
10718 * invalidation by
10719 * @param left The left coordinate of the rectangle to invalidate.
10720 * @param top The top coordinate of the rectangle to invalidate.
10721 * @param right The right coordinate of the rectangle to invalidate.
10722 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010723 *
10724 * @see #invalidate(int, int, int, int)
10725 * @see #invalidate(Rect)
10726 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010727 */
10728 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10729 int right, int bottom) {
10730
10731 // We try only with the AttachInfo because there's no point in invalidating
10732 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010733 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010734 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10736 info.target = this;
10737 info.left = left;
10738 info.top = top;
10739 info.right = right;
10740 info.bottom = bottom;
10741
Jeff Browna175a5b2012-02-15 19:18:31 -080010742 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 }
10744 }
10745
10746 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010747 * <p>Cause an invalidate to happen on the next animation time step, typically the
10748 * next display frame.</p>
10749 *
10750 * <p>This method can be invoked from outside of the UI thread
10751 * only when this View is attached to a window.</p>
10752 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010753 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010754 */
10755 public void postInvalidateOnAnimation() {
10756 // We try only with the AttachInfo because there's no point in invalidating
10757 // if we are not attached to our window
10758 final AttachInfo attachInfo = mAttachInfo;
10759 if (attachInfo != null) {
10760 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10761 }
10762 }
10763
10764 /**
10765 * <p>Cause an invalidate of the specified area to happen on the next animation
10766 * time step, typically the next display frame.</p>
10767 *
10768 * <p>This method can be invoked from outside of the UI thread
10769 * only when this View is attached to a window.</p>
10770 *
10771 * @param left The left coordinate of the rectangle to invalidate.
10772 * @param top The top coordinate of the rectangle to invalidate.
10773 * @param right The right coordinate of the rectangle to invalidate.
10774 * @param bottom The bottom coordinate of the rectangle to invalidate.
10775 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010776 * @see #invalidate(int, int, int, int)
10777 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010778 */
10779 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10780 // We try only with the AttachInfo because there's no point in invalidating
10781 // if we are not attached to our window
10782 final AttachInfo attachInfo = mAttachInfo;
10783 if (attachInfo != null) {
10784 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10785 info.target = this;
10786 info.left = left;
10787 info.top = top;
10788 info.right = right;
10789 info.bottom = bottom;
10790
10791 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10792 }
10793 }
10794
10795 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010796 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10797 * This event is sent at most once every
10798 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10799 */
10800 private void postSendViewScrolledAccessibilityEventCallback() {
10801 if (mSendViewScrolledAccessibilityEvent == null) {
10802 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10803 }
10804 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10805 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10806 postDelayed(mSendViewScrolledAccessibilityEvent,
10807 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10808 }
10809 }
10810
10811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812 * Called by a parent to request that a child update its values for mScrollX
10813 * and mScrollY if necessary. This will typically be done if the child is
10814 * animating a scroll using a {@link android.widget.Scroller Scroller}
10815 * object.
10816 */
10817 public void computeScroll() {
10818 }
10819
10820 /**
10821 * <p>Indicate whether the horizontal edges are faded when the view is
10822 * scrolled horizontally.</p>
10823 *
10824 * @return true if the horizontal edges should are faded on scroll, false
10825 * otherwise
10826 *
10827 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010828 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010829 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830 */
10831 public boolean isHorizontalFadingEdgeEnabled() {
10832 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10833 }
10834
10835 /**
10836 * <p>Define whether the horizontal edges should be faded when this view
10837 * is scrolled horizontally.</p>
10838 *
10839 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10840 * be faded when the view is scrolled
10841 * horizontally
10842 *
10843 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010844 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010845 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846 */
10847 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10848 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10849 if (horizontalFadingEdgeEnabled) {
10850 initScrollCache();
10851 }
10852
10853 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10854 }
10855 }
10856
10857 /**
10858 * <p>Indicate whether the vertical edges are faded when the view is
10859 * scrolled horizontally.</p>
10860 *
10861 * @return true if the vertical edges should are faded on scroll, false
10862 * otherwise
10863 *
10864 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010865 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010866 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010867 */
10868 public boolean isVerticalFadingEdgeEnabled() {
10869 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10870 }
10871
10872 /**
10873 * <p>Define whether the vertical edges should be faded when this view
10874 * is scrolled vertically.</p>
10875 *
10876 * @param verticalFadingEdgeEnabled true if the vertical edges should
10877 * be faded when the view is scrolled
10878 * vertically
10879 *
10880 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010881 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010882 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 */
10884 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10885 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10886 if (verticalFadingEdgeEnabled) {
10887 initScrollCache();
10888 }
10889
10890 mViewFlags ^= FADING_EDGE_VERTICAL;
10891 }
10892 }
10893
10894 /**
10895 * Returns the strength, or intensity, of the top faded edge. The strength is
10896 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10897 * returns 0.0 or 1.0 but no value in between.
10898 *
10899 * Subclasses should override this method to provide a smoother fade transition
10900 * when scrolling occurs.
10901 *
10902 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10903 */
10904 protected float getTopFadingEdgeStrength() {
10905 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10906 }
10907
10908 /**
10909 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10910 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10911 * returns 0.0 or 1.0 but no value in between.
10912 *
10913 * Subclasses should override this method to provide a smoother fade transition
10914 * when scrolling occurs.
10915 *
10916 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10917 */
10918 protected float getBottomFadingEdgeStrength() {
10919 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10920 computeVerticalScrollRange() ? 1.0f : 0.0f;
10921 }
10922
10923 /**
10924 * Returns the strength, or intensity, of the left faded edge. The strength is
10925 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10926 * returns 0.0 or 1.0 but no value in between.
10927 *
10928 * Subclasses should override this method to provide a smoother fade transition
10929 * when scrolling occurs.
10930 *
10931 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10932 */
10933 protected float getLeftFadingEdgeStrength() {
10934 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10935 }
10936
10937 /**
10938 * Returns the strength, or intensity, of the right faded edge. The strength is
10939 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10940 * returns 0.0 or 1.0 but no value in between.
10941 *
10942 * Subclasses should override this method to provide a smoother fade transition
10943 * when scrolling occurs.
10944 *
10945 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10946 */
10947 protected float getRightFadingEdgeStrength() {
10948 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10949 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10950 }
10951
10952 /**
10953 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10954 * scrollbar is not drawn by default.</p>
10955 *
10956 * @return true if the horizontal scrollbar should be painted, false
10957 * otherwise
10958 *
10959 * @see #setHorizontalScrollBarEnabled(boolean)
10960 */
10961 public boolean isHorizontalScrollBarEnabled() {
10962 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10963 }
10964
10965 /**
10966 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10967 * scrollbar is not drawn by default.</p>
10968 *
10969 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10970 * be painted
10971 *
10972 * @see #isHorizontalScrollBarEnabled()
10973 */
10974 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10975 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10976 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010977 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010978 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979 }
10980 }
10981
10982 /**
10983 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10984 * scrollbar is not drawn by default.</p>
10985 *
10986 * @return true if the vertical scrollbar should be painted, false
10987 * otherwise
10988 *
10989 * @see #setVerticalScrollBarEnabled(boolean)
10990 */
10991 public boolean isVerticalScrollBarEnabled() {
10992 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10993 }
10994
10995 /**
10996 * <p>Define whether the vertical scrollbar should be drawn or not. The
10997 * scrollbar is not drawn by default.</p>
10998 *
10999 * @param verticalScrollBarEnabled true if the vertical scrollbar should
11000 * be painted
11001 *
11002 * @see #isVerticalScrollBarEnabled()
11003 */
11004 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
11005 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
11006 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070011007 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011008 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 }
11010 }
11011
Adam Powell20232d02010-12-08 21:08:53 -080011012 /**
11013 * @hide
11014 */
11015 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011016 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 }
Joe Malin32736f02011-01-19 16:14:20 -080011018
Mike Cleronfe81d382009-09-28 14:22:16 -070011019 /**
11020 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011021 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011022 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011023 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011024 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011025 */
11026 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11027 initScrollCache();
11028 final ScrollabilityCache scrollabilityCache = mScrollCache;
11029 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011030 if (fadeScrollbars) {
11031 scrollabilityCache.state = ScrollabilityCache.OFF;
11032 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011033 scrollabilityCache.state = ScrollabilityCache.ON;
11034 }
11035 }
Joe Malin32736f02011-01-19 16:14:20 -080011036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 /**
Joe Malin32736f02011-01-19 16:14:20 -080011038 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011039 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011040 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011041 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011042 *
11043 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011044 */
11045 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011046 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011047 }
Joe Malin32736f02011-01-19 16:14:20 -080011048
Mike Cleron52f0a642009-09-28 18:21:37 -070011049 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011050 *
11051 * Returns the delay before scrollbars fade.
11052 *
11053 * @return the delay before scrollbars fade
11054 *
11055 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11056 */
11057 public int getScrollBarDefaultDelayBeforeFade() {
11058 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11059 mScrollCache.scrollBarDefaultDelayBeforeFade;
11060 }
11061
11062 /**
11063 * Define the delay before scrollbars fade.
11064 *
11065 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11066 *
11067 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11068 */
11069 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11070 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11071 }
11072
11073 /**
11074 *
11075 * Returns the scrollbar fade duration.
11076 *
11077 * @return the scrollbar fade duration
11078 *
11079 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11080 */
11081 public int getScrollBarFadeDuration() {
11082 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11083 mScrollCache.scrollBarFadeDuration;
11084 }
11085
11086 /**
11087 * Define the scrollbar fade duration.
11088 *
11089 * @param scrollBarFadeDuration - the scrollbar fade duration
11090 *
11091 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11092 */
11093 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11094 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11095 }
11096
11097 /**
11098 *
11099 * Returns the scrollbar size.
11100 *
11101 * @return the scrollbar size
11102 *
11103 * @attr ref android.R.styleable#View_scrollbarSize
11104 */
11105 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011106 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011107 mScrollCache.scrollBarSize;
11108 }
11109
11110 /**
11111 * Define the scrollbar size.
11112 *
11113 * @param scrollBarSize - the scrollbar size
11114 *
11115 * @attr ref android.R.styleable#View_scrollbarSize
11116 */
11117 public void setScrollBarSize(int scrollBarSize) {
11118 getScrollCache().scrollBarSize = scrollBarSize;
11119 }
11120
11121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11123 * inset. When inset, they add to the padding of the view. And the scrollbars
11124 * can be drawn inside the padding area or on the edge of the view. For example,
11125 * if a view has a background drawable and you want to draw the scrollbars
11126 * inside the padding specified by the drawable, you can use
11127 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11128 * appear at the edge of the view, ignoring the padding, then you can use
11129 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11130 * @param style the style of the scrollbars. Should be one of
11131 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11132 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11133 * @see #SCROLLBARS_INSIDE_OVERLAY
11134 * @see #SCROLLBARS_INSIDE_INSET
11135 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11136 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011137 *
11138 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 */
11140 public void setScrollBarStyle(int style) {
11141 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11142 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011143 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011144 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011145 }
11146 }
11147
11148 /**
11149 * <p>Returns the current scrollbar style.</p>
11150 * @return the current scrollbar style
11151 * @see #SCROLLBARS_INSIDE_OVERLAY
11152 * @see #SCROLLBARS_INSIDE_INSET
11153 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11154 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011155 *
11156 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011158 @ViewDebug.ExportedProperty(mapping = {
11159 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11160 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11161 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11162 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11163 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011164 public int getScrollBarStyle() {
11165 return mViewFlags & SCROLLBARS_STYLE_MASK;
11166 }
11167
11168 /**
11169 * <p>Compute the horizontal range that the horizontal scrollbar
11170 * represents.</p>
11171 *
11172 * <p>The range is expressed in arbitrary units that must be the same as the
11173 * units used by {@link #computeHorizontalScrollExtent()} and
11174 * {@link #computeHorizontalScrollOffset()}.</p>
11175 *
11176 * <p>The default range is the drawing width of this view.</p>
11177 *
11178 * @return the total horizontal range represented by the horizontal
11179 * scrollbar
11180 *
11181 * @see #computeHorizontalScrollExtent()
11182 * @see #computeHorizontalScrollOffset()
11183 * @see android.widget.ScrollBarDrawable
11184 */
11185 protected int computeHorizontalScrollRange() {
11186 return getWidth();
11187 }
11188
11189 /**
11190 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11191 * within the horizontal range. This value is used to compute the position
11192 * of the thumb within the scrollbar's track.</p>
11193 *
11194 * <p>The range is expressed in arbitrary units that must be the same as the
11195 * units used by {@link #computeHorizontalScrollRange()} and
11196 * {@link #computeHorizontalScrollExtent()}.</p>
11197 *
11198 * <p>The default offset is the scroll offset of this view.</p>
11199 *
11200 * @return the horizontal offset of the scrollbar's thumb
11201 *
11202 * @see #computeHorizontalScrollRange()
11203 * @see #computeHorizontalScrollExtent()
11204 * @see android.widget.ScrollBarDrawable
11205 */
11206 protected int computeHorizontalScrollOffset() {
11207 return mScrollX;
11208 }
11209
11210 /**
11211 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11212 * within the horizontal range. This value is used to compute the length
11213 * of the thumb within the scrollbar's track.</p>
11214 *
11215 * <p>The range is expressed in arbitrary units that must be the same as the
11216 * units used by {@link #computeHorizontalScrollRange()} and
11217 * {@link #computeHorizontalScrollOffset()}.</p>
11218 *
11219 * <p>The default extent is the drawing width of this view.</p>
11220 *
11221 * @return the horizontal extent of the scrollbar's thumb
11222 *
11223 * @see #computeHorizontalScrollRange()
11224 * @see #computeHorizontalScrollOffset()
11225 * @see android.widget.ScrollBarDrawable
11226 */
11227 protected int computeHorizontalScrollExtent() {
11228 return getWidth();
11229 }
11230
11231 /**
11232 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11233 *
11234 * <p>The range is expressed in arbitrary units that must be the same as the
11235 * units used by {@link #computeVerticalScrollExtent()} and
11236 * {@link #computeVerticalScrollOffset()}.</p>
11237 *
11238 * @return the total vertical range represented by the vertical scrollbar
11239 *
11240 * <p>The default range is the drawing height of this view.</p>
11241 *
11242 * @see #computeVerticalScrollExtent()
11243 * @see #computeVerticalScrollOffset()
11244 * @see android.widget.ScrollBarDrawable
11245 */
11246 protected int computeVerticalScrollRange() {
11247 return getHeight();
11248 }
11249
11250 /**
11251 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11252 * within the horizontal range. This value is used to compute the position
11253 * of the thumb within the scrollbar's track.</p>
11254 *
11255 * <p>The range is expressed in arbitrary units that must be the same as the
11256 * units used by {@link #computeVerticalScrollRange()} and
11257 * {@link #computeVerticalScrollExtent()}.</p>
11258 *
11259 * <p>The default offset is the scroll offset of this view.</p>
11260 *
11261 * @return the vertical offset of the scrollbar's thumb
11262 *
11263 * @see #computeVerticalScrollRange()
11264 * @see #computeVerticalScrollExtent()
11265 * @see android.widget.ScrollBarDrawable
11266 */
11267 protected int computeVerticalScrollOffset() {
11268 return mScrollY;
11269 }
11270
11271 /**
11272 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11273 * within the vertical range. This value is used to compute the length
11274 * of the thumb within the scrollbar's track.</p>
11275 *
11276 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011277 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 * {@link #computeVerticalScrollOffset()}.</p>
11279 *
11280 * <p>The default extent is the drawing height of this view.</p>
11281 *
11282 * @return the vertical extent of the scrollbar's thumb
11283 *
11284 * @see #computeVerticalScrollRange()
11285 * @see #computeVerticalScrollOffset()
11286 * @see android.widget.ScrollBarDrawable
11287 */
11288 protected int computeVerticalScrollExtent() {
11289 return getHeight();
11290 }
11291
11292 /**
Adam Powell69159442011-06-13 17:53:06 -070011293 * Check if this view can be scrolled horizontally in a certain direction.
11294 *
11295 * @param direction Negative to check scrolling left, positive to check scrolling right.
11296 * @return true if this view can be scrolled in the specified direction, false otherwise.
11297 */
11298 public boolean canScrollHorizontally(int direction) {
11299 final int offset = computeHorizontalScrollOffset();
11300 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11301 if (range == 0) return false;
11302 if (direction < 0) {
11303 return offset > 0;
11304 } else {
11305 return offset < range - 1;
11306 }
11307 }
11308
11309 /**
11310 * Check if this view can be scrolled vertically in a certain direction.
11311 *
11312 * @param direction Negative to check scrolling up, positive to check scrolling down.
11313 * @return true if this view can be scrolled in the specified direction, false otherwise.
11314 */
11315 public boolean canScrollVertically(int direction) {
11316 final int offset = computeVerticalScrollOffset();
11317 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11318 if (range == 0) return false;
11319 if (direction < 0) {
11320 return offset > 0;
11321 } else {
11322 return offset < range - 1;
11323 }
11324 }
11325
11326 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011327 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11328 * scrollbars are painted only if they have been awakened first.</p>
11329 *
11330 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011331 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011332 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011334 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011335 // scrollbars are drawn only when the animation is running
11336 final ScrollabilityCache cache = mScrollCache;
11337 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011338
Mike Cleronf116bf82009-09-27 19:14:12 -070011339 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011340
Mike Cleronf116bf82009-09-27 19:14:12 -070011341 if (state == ScrollabilityCache.OFF) {
11342 return;
11343 }
Joe Malin32736f02011-01-19 16:14:20 -080011344
Mike Cleronf116bf82009-09-27 19:14:12 -070011345 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011346
Mike Cleronf116bf82009-09-27 19:14:12 -070011347 if (state == ScrollabilityCache.FADING) {
11348 // We're fading -- get our fade interpolation
11349 if (cache.interpolatorValues == null) {
11350 cache.interpolatorValues = new float[1];
11351 }
Joe Malin32736f02011-01-19 16:14:20 -080011352
Mike Cleronf116bf82009-09-27 19:14:12 -070011353 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011354
Mike Cleronf116bf82009-09-27 19:14:12 -070011355 // Stops the animation if we're done
11356 if (cache.scrollBarInterpolator.timeToValues(values) ==
11357 Interpolator.Result.FREEZE_END) {
11358 cache.state = ScrollabilityCache.OFF;
11359 } else {
11360 cache.scrollBar.setAlpha(Math.round(values[0]));
11361 }
Joe Malin32736f02011-01-19 16:14:20 -080011362
11363 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011364 // drawing. We only want this when we're fading so that
11365 // we prevent excessive redraws
11366 invalidate = true;
11367 } else {
11368 // We're just on -- but we may have been fading before so
11369 // reset alpha
11370 cache.scrollBar.setAlpha(255);
11371 }
11372
Joe Malin32736f02011-01-19 16:14:20 -080011373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011374 final int viewFlags = mViewFlags;
11375
11376 final boolean drawHorizontalScrollBar =
11377 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11378 final boolean drawVerticalScrollBar =
11379 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11380 && !isVerticalScrollBarHidden();
11381
11382 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11383 final int width = mRight - mLeft;
11384 final int height = mBottom - mTop;
11385
11386 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387
Mike Reede8853fc2009-09-04 14:01:48 -040011388 final int scrollX = mScrollX;
11389 final int scrollY = mScrollY;
11390 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11391
Mike Cleronf116bf82009-09-27 19:14:12 -070011392 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011395 int size = scrollBar.getSize(false);
11396 if (size <= 0) {
11397 size = cache.scrollBarSize;
11398 }
11399
Mike Cleronf116bf82009-09-27 19:14:12 -070011400 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011401 computeHorizontalScrollOffset(),
11402 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011403 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011404 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011405 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011406 left = scrollX + (mPaddingLeft & inside);
11407 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11408 bottom = top + size;
11409 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11410 if (invalidate) {
11411 invalidate(left, top, right, bottom);
11412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011413 }
11414
11415 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011416 int size = scrollBar.getSize(true);
11417 if (size <= 0) {
11418 size = cache.scrollBarSize;
11419 }
11420
Mike Reede8853fc2009-09-04 14:01:48 -040011421 scrollBar.setParameters(computeVerticalScrollRange(),
11422 computeVerticalScrollOffset(),
11423 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011424 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11425 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11426 verticalScrollbarPosition = isLayoutRtl() ?
11427 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11428 }
11429 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011430 default:
Adam Powell20232d02010-12-08 21:08:53 -080011431 case SCROLLBAR_POSITION_RIGHT:
11432 left = scrollX + width - size - (mUserPaddingRight & inside);
11433 break;
11434 case SCROLLBAR_POSITION_LEFT:
11435 left = scrollX + (mUserPaddingLeft & inside);
11436 break;
11437 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011438 top = scrollY + (mPaddingTop & inside);
11439 right = left + size;
11440 bottom = scrollY + height - (mUserPaddingBottom & inside);
11441 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11442 if (invalidate) {
11443 invalidate(left, top, right, bottom);
11444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011445 }
11446 }
11447 }
11448 }
Romain Guy8506ab42009-06-11 17:35:47 -070011449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011451 * 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 -080011452 * FastScroller is visible.
11453 * @return whether to temporarily hide the vertical scrollbar
11454 * @hide
11455 */
11456 protected boolean isVerticalScrollBarHidden() {
11457 return false;
11458 }
11459
11460 /**
11461 * <p>Draw the horizontal scrollbar if
11462 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11463 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 * @param canvas the canvas on which to draw the scrollbar
11465 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 *
11467 * @see #isHorizontalScrollBarEnabled()
11468 * @see #computeHorizontalScrollRange()
11469 * @see #computeHorizontalScrollExtent()
11470 * @see #computeHorizontalScrollOffset()
11471 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011472 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011473 */
Romain Guy8fb95422010-08-17 18:38:51 -070011474 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11475 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011476 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011477 scrollBar.draw(canvas);
11478 }
Mike Reede8853fc2009-09-04 14:01:48 -040011479
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011480 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011481 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11482 * returns true.</p>
11483 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 * @param canvas the canvas on which to draw the scrollbar
11485 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011486 *
11487 * @see #isVerticalScrollBarEnabled()
11488 * @see #computeVerticalScrollRange()
11489 * @see #computeVerticalScrollExtent()
11490 * @see #computeVerticalScrollOffset()
11491 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011492 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 */
Romain Guy8fb95422010-08-17 18:38:51 -070011494 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11495 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011496 scrollBar.setBounds(l, t, r, b);
11497 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 }
11499
11500 /**
11501 * Implement this to do your drawing.
11502 *
11503 * @param canvas the canvas on which the background will be drawn
11504 */
11505 protected void onDraw(Canvas canvas) {
11506 }
11507
11508 /*
11509 * Caller is responsible for calling requestLayout if necessary.
11510 * (This allows addViewInLayout to not request a new layout.)
11511 */
11512 void assignParent(ViewParent parent) {
11513 if (mParent == null) {
11514 mParent = parent;
11515 } else if (parent == null) {
11516 mParent = null;
11517 } else {
11518 throw new RuntimeException("view " + this + " being added, but"
11519 + " it already has a parent");
11520 }
11521 }
11522
11523 /**
11524 * This is called when the view is attached to a window. At this point it
11525 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011526 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11527 * however it may be called any time before the first onDraw -- including
11528 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 *
11530 * @see #onDetachedFromWindow()
11531 */
11532 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011533 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011534 mParent.requestTransparentRegion(this);
11535 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011536
Dianne Hackborn4702a852012-08-17 15:18:29 -070011537 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011538 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011539 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011540 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011541
Chet Haasea9b61ac2010-12-20 07:40:25 -080011542 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011543
Svetoslav Ganov42138042012-03-20 11:51:39 -070011544 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011545 if (isFocused()) {
11546 InputMethodManager imm = InputMethodManager.peekInstance();
11547 imm.focusIn(this);
11548 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011549
11550 if (mAttachInfo != null && mDisplayList != null) {
11551 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11552 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011553 }
Cibu Johny86666632010-02-22 13:01:02 -080011554
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011555 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011556 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011557 *
11558 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011559 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011560 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011561 if (!needRtlPropertiesResolution()) return;
11562
11563 // Order is important here: LayoutDirection MUST be resolved first
11564 if (!isLayoutDirectionResolved()) {
11565 resolveLayoutDirection();
11566 resolveLayoutParams();
11567 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011568 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011569 if (!isTextDirectionResolved()) {
11570 resolveTextDirection();
11571 }
11572 if (!isTextAlignmentResolved()) {
11573 resolveTextAlignment();
11574 }
11575 if (!isPaddingResolved()) {
11576 resolvePadding();
11577 }
11578 if (!isDrawablesResolved()) {
11579 resolveDrawables();
11580 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011581 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011582 }
11583
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011584 /**
11585 * Reset resolution of all RTL related properties.
11586 *
11587 * @hide
11588 */
11589 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011590 resetResolvedLayoutDirection();
11591 resetResolvedTextDirection();
11592 resetResolvedTextAlignment();
11593 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011594 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011595 }
11596
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011597 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011598 * @see #onScreenStateChanged(int)
11599 */
11600 void dispatchScreenStateChanged(int screenState) {
11601 onScreenStateChanged(screenState);
11602 }
11603
11604 /**
11605 * This method is called whenever the state of the screen this view is
11606 * attached to changes. A state change will usually occurs when the screen
11607 * turns on or off (whether it happens automatically or the user does it
11608 * manually.)
11609 *
11610 * @param screenState The new state of the screen. Can be either
11611 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11612 */
11613 public void onScreenStateChanged(int screenState) {
11614 }
11615
11616 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011617 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11618 */
11619 private boolean hasRtlSupport() {
11620 return mContext.getApplicationInfo().hasRtlSupport();
11621 }
11622
11623 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011624 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11625 * RTL not supported)
11626 */
11627 private boolean isRtlCompatibilityMode() {
11628 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11629 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11630 }
11631
11632 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011633 * @return true if RTL properties need resolution.
11634 */
11635 private boolean needRtlPropertiesResolution() {
11636 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11637 }
11638
11639 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011640 * Called when any RTL property (layout direction or text direction or text alignment) has
11641 * been changed.
11642 *
11643 * Subclasses need to override this method to take care of cached information that depends on the
11644 * resolved layout direction, or to inform child views that inherit their layout direction.
11645 *
11646 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011647 *
11648 * @param layoutDirection the direction of the layout
11649 *
11650 * @see #LAYOUT_DIRECTION_LTR
11651 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011652 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011653 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011654 }
11655
11656 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011657 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11658 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011659 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011660 * @return true if resolution has been done, false otherwise.
11661 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011662 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011663 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011664 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011665 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011666 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011667
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011668 if (hasRtlSupport()) {
11669 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011670 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11671 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011672 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011673 // We cannot resolve yet. LTR is by default and let the resolution happen again
11674 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011675 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011676
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011677 View parent = ((View) mParent);
11678 // Parent has not yet resolved, LTR is still the default
11679 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011680
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011681 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011682 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011683 }
11684 break;
11685 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011686 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011687 break;
11688 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011689 if((LAYOUT_DIRECTION_RTL ==
11690 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011691 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011692 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011693 break;
11694 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011695 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011696 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011697 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011698
11699 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011700 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011701 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011702 }
11703
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011704 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011705 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011706 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011707 * @return true if layout direction resolution can be done otherwise return false.
11708 *
11709 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011710 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011711 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011712 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011713 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011714 return (mParent != null) && (mParent instanceof ViewGroup) &&
11715 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011716 default:
11717 return true;
11718 }
11719 }
11720
11721 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011722 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11723 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011724 *
11725 * @hide
11726 */
11727 public void resetResolvedLayoutDirection() {
11728 // Reset the current resolved bits
11729 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011730 }
11731
11732 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011733 * @return true if the layout direction is inherited.
11734 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011735 * @hide
11736 */
11737 public boolean isLayoutDirectionInherited() {
11738 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11739 }
11740
11741 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011742 * @return true if layout direction has been resolved.
11743 */
11744 private boolean isLayoutDirectionResolved() {
11745 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11746 }
11747
11748 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011749 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011750 *
11751 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011752 */
11753 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011754 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011755 }
11756
11757 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011758 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011759 *
11760 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011761 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011762 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011763 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011764 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11765 // If start / end padding are defined, they will be resolved (hence overriding) to
11766 // left / right or right / left depending on the resolved layout direction.
11767 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011768 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011769 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011770 mUserPaddingLeft = mUserPaddingLeftInitial;
11771 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011772 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011773 switch (resolvedLayoutDirection) {
11774 case LAYOUT_DIRECTION_RTL:
11775 if (mUserPaddingStart != UNDEFINED_PADDING) {
11776 mUserPaddingRight = mUserPaddingStart;
11777 }
11778 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11779 mUserPaddingLeft = mUserPaddingEnd;
11780 }
11781 break;
11782 case LAYOUT_DIRECTION_LTR:
11783 default:
11784 if (mUserPaddingStart != UNDEFINED_PADDING) {
11785 mUserPaddingLeft = mUserPaddingStart;
11786 }
11787 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11788 mUserPaddingRight = mUserPaddingEnd;
11789 }
11790 }
11791
11792 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11793
11794 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11795 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011796 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011797 }
11798
Dianne Hackborn4702a852012-08-17 15:18:29 -070011799 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011800 }
11801
11802 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011803 * Reset the resolved layout direction.
11804 *
11805 * @hide
11806 */
11807 public void resetResolvedPadding() {
11808 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11809 }
11810
11811 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011812 * This is called when the view is detached from a window. At this point it
11813 * no longer has a surface for drawing.
11814 *
11815 * @see #onAttachedToWindow()
11816 */
11817 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011818 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011819
Romain Guya440b002010-02-24 15:57:54 -080011820 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011821 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011822 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011823 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011825 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011826
Romain Guya998dff2012-03-23 18:58:36 -070011827 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011828
11829 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011830 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011831 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011832 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011833 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011834 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011835 // Should never happen
11836 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011837 }
11838
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011839 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011840
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011841 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011842 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011843 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011844 }
11845
11846 /**
11847 * @return The number of times this view has been attached to a window
11848 */
11849 protected int getWindowAttachCount() {
11850 return mWindowAttachCount;
11851 }
11852
11853 /**
11854 * Retrieve a unique token identifying the window this view is attached to.
11855 * @return Return the window's token for use in
11856 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11857 */
11858 public IBinder getWindowToken() {
11859 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11860 }
11861
11862 /**
11863 * Retrieve a unique token identifying the top-level "real" window of
11864 * the window that this view is attached to. That is, this is like
11865 * {@link #getWindowToken}, except if the window this view in is a panel
11866 * window (attached to another containing window), then the token of
11867 * the containing window is returned instead.
11868 *
11869 * @return Returns the associated window token, either
11870 * {@link #getWindowToken()} or the containing window's token.
11871 */
11872 public IBinder getApplicationWindowToken() {
11873 AttachInfo ai = mAttachInfo;
11874 if (ai != null) {
11875 IBinder appWindowToken = ai.mPanelParentWindowToken;
11876 if (appWindowToken == null) {
11877 appWindowToken = ai.mWindowToken;
11878 }
11879 return appWindowToken;
11880 }
11881 return null;
11882 }
11883
11884 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011885 * Gets the logical display to which the view's window has been attached.
11886 *
11887 * @return The logical display, or null if the view is not currently attached to a window.
11888 */
11889 public Display getDisplay() {
11890 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11891 }
11892
11893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 * Retrieve private session object this view hierarchy is using to
11895 * communicate with the window manager.
11896 * @return the session object to communicate with the window manager
11897 */
11898 /*package*/ IWindowSession getWindowSession() {
11899 return mAttachInfo != null ? mAttachInfo.mSession : null;
11900 }
11901
11902 /**
11903 * @param info the {@link android.view.View.AttachInfo} to associated with
11904 * this view
11905 */
11906 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11907 //System.out.println("Attached! " + this);
11908 mAttachInfo = info;
11909 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011910 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011911 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011912 if (mFloatingTreeObserver != null) {
11913 info.mTreeObserver.merge(mFloatingTreeObserver);
11914 mFloatingTreeObserver = null;
11915 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011916 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011917 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011918 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011920 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011922
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011923 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011924 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011925 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011926 if (listeners != null && listeners.size() > 0) {
11927 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11928 // perform the dispatching. The iterator is a safe guard against listeners that
11929 // could mutate the list by calling the various add/remove methods. This prevents
11930 // the array from being modified while we iterate it.
11931 for (OnAttachStateChangeListener listener : listeners) {
11932 listener.onViewAttachedToWindow(this);
11933 }
11934 }
11935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011936 int vis = info.mWindowVisibility;
11937 if (vis != GONE) {
11938 onWindowVisibilityChanged(vis);
11939 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011940 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011941 // If nobody has evaluated the drawable state yet, then do it now.
11942 refreshDrawableState();
11943 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011944 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011945 }
11946
11947 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011948 AttachInfo info = mAttachInfo;
11949 if (info != null) {
11950 int vis = info.mWindowVisibility;
11951 if (vis != GONE) {
11952 onWindowVisibilityChanged(GONE);
11953 }
11954 }
11955
11956 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011957
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011958 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011959 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011960 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011961 if (listeners != null && listeners.size() > 0) {
11962 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11963 // perform the dispatching. The iterator is a safe guard against listeners that
11964 // could mutate the list by calling the various add/remove methods. This prevents
11965 // the array from being modified while we iterate it.
11966 for (OnAttachStateChangeListener listener : listeners) {
11967 listener.onViewDetachedFromWindow(this);
11968 }
11969 }
11970
Dianne Hackborn4702a852012-08-17 15:18:29 -070011971 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011972 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011973 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011974 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 mAttachInfo = null;
11977 }
11978
11979 /**
11980 * Store this view hierarchy's frozen state into the given container.
11981 *
11982 * @param container The SparseArray in which to save the view's state.
11983 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011984 * @see #restoreHierarchyState(android.util.SparseArray)
11985 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11986 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 */
11988 public void saveHierarchyState(SparseArray<Parcelable> container) {
11989 dispatchSaveInstanceState(container);
11990 }
11991
11992 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011993 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11994 * this view and its children. May be overridden to modify how freezing happens to a
11995 * 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 -080011996 *
11997 * @param container The SparseArray in which to save the view's state.
11998 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011999 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12000 * @see #saveHierarchyState(android.util.SparseArray)
12001 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002 */
12003 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
12004 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012005 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070012007 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012008 throw new IllegalStateException(
12009 "Derived class did not call super.onSaveInstanceState()");
12010 }
12011 if (state != null) {
12012 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12013 // + ": " + state);
12014 container.put(mID, state);
12015 }
12016 }
12017 }
12018
12019 /**
12020 * Hook allowing a view to generate a representation of its internal state
12021 * that can later be used to create a new instance with that same state.
12022 * This state should only contain information that is not persistent or can
12023 * not be reconstructed later. For example, you will never store your
12024 * current position on screen because that will be computed again when a
12025 * new instance of the view is placed in its view hierarchy.
12026 * <p>
12027 * Some examples of things you may store here: the current cursor position
12028 * in a text view (but usually not the text itself since that is stored in a
12029 * content provider or other persistent storage), the currently selected
12030 * item in a list view.
12031 *
12032 * @return Returns a Parcelable object containing the view's current dynamic
12033 * state, or null if there is nothing interesting to save. The
12034 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012035 * @see #onRestoreInstanceState(android.os.Parcelable)
12036 * @see #saveHierarchyState(android.util.SparseArray)
12037 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012038 * @see #setSaveEnabled(boolean)
12039 */
12040 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012041 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 return BaseSavedState.EMPTY_STATE;
12043 }
12044
12045 /**
12046 * Restore this view hierarchy's frozen state from the given container.
12047 *
12048 * @param container The SparseArray which holds previously frozen states.
12049 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012050 * @see #saveHierarchyState(android.util.SparseArray)
12051 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12052 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012053 */
12054 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12055 dispatchRestoreInstanceState(container);
12056 }
12057
12058 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012059 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12060 * state for this view and its children. May be overridden to modify how restoring
12061 * happens to a view's children; for example, some views may want to not store state
12062 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012063 *
12064 * @param container The SparseArray which holds previously saved state.
12065 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012066 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12067 * @see #restoreHierarchyState(android.util.SparseArray)
12068 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012069 */
12070 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12071 if (mID != NO_ID) {
12072 Parcelable state = container.get(mID);
12073 if (state != null) {
12074 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12075 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012076 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012077 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012078 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079 throw new IllegalStateException(
12080 "Derived class did not call super.onRestoreInstanceState()");
12081 }
12082 }
12083 }
12084 }
12085
12086 /**
12087 * Hook allowing a view to re-apply a representation of its internal state that had previously
12088 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12089 * null state.
12090 *
12091 * @param state The frozen state that had previously been returned by
12092 * {@link #onSaveInstanceState}.
12093 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012094 * @see #onSaveInstanceState()
12095 * @see #restoreHierarchyState(android.util.SparseArray)
12096 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012097 */
12098 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012099 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012100 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012101 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12102 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012103 + "when two views of different type have the same id in the same hierarchy. "
12104 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012105 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012106 }
12107 }
12108
12109 /**
12110 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12111 *
12112 * @return the drawing start time in milliseconds
12113 */
12114 public long getDrawingTime() {
12115 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12116 }
12117
12118 /**
12119 * <p>Enables or disables the duplication of the parent's state into this view. When
12120 * duplication is enabled, this view gets its drawable state from its parent rather
12121 * than from its own internal properties.</p>
12122 *
12123 * <p>Note: in the current implementation, setting this property to true after the
12124 * view was added to a ViewGroup might have no effect at all. This property should
12125 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12126 *
12127 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12128 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012129 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012130 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12131 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132 *
12133 * @param enabled True to enable duplication of the parent's drawable state, false
12134 * to disable it.
12135 *
12136 * @see #getDrawableState()
12137 * @see #isDuplicateParentStateEnabled()
12138 */
12139 public void setDuplicateParentStateEnabled(boolean enabled) {
12140 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12141 }
12142
12143 /**
12144 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12145 *
12146 * @return True if this view's drawable state is duplicated from the parent,
12147 * false otherwise
12148 *
12149 * @see #getDrawableState()
12150 * @see #setDuplicateParentStateEnabled(boolean)
12151 */
12152 public boolean isDuplicateParentStateEnabled() {
12153 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12154 }
12155
12156 /**
Romain Guy171c5922011-01-06 10:04:23 -080012157 * <p>Specifies the type of layer backing this view. The layer can be
12158 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12159 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012160 *
Romain Guy171c5922011-01-06 10:04:23 -080012161 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12162 * instance that controls how the layer is composed on screen. The following
12163 * properties of the paint are taken into account when composing the layer:</p>
12164 * <ul>
12165 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12166 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12167 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12168 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012169 *
Romain Guy171c5922011-01-06 10:04:23 -080012170 * <p>If this view has an alpha value set to < 1.0 by calling
12171 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12172 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12173 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012174 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012175 *
Romain Guy171c5922011-01-06 10:04:23 -080012176 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12177 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12178 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012179 *
Chet Haased15ebf22012-09-05 11:40:29 -070012180 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012181 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12182 * {@link #LAYER_TYPE_HARDWARE}
12183 * @param paint The paint used to compose the layer. This argument is optional
12184 * and can be null. It is ignored when the layer type is
12185 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012186 *
12187 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012188 * @see #LAYER_TYPE_NONE
12189 * @see #LAYER_TYPE_SOFTWARE
12190 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012191 * @see #setAlpha(float)
12192 *
Romain Guy171c5922011-01-06 10:04:23 -080012193 * @attr ref android.R.styleable#View_layerType
12194 */
12195 public void setLayerType(int layerType, Paint paint) {
12196 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012197 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012198 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12199 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012200
Romain Guyd6cd5722011-01-17 14:42:41 -080012201 if (layerType == mLayerType) {
12202 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12203 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012204 invalidateParentCaches();
12205 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012206 }
12207 return;
12208 }
Romain Guy171c5922011-01-06 10:04:23 -080012209
12210 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012211 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012212 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012213 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012214 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012215 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012216 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012217 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012218 default:
12219 break;
Romain Guy171c5922011-01-06 10:04:23 -080012220 }
12221
12222 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012223 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12224 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12225 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012226
Romain Guy0fd89bf2011-01-26 15:41:30 -080012227 invalidateParentCaches();
12228 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012229 }
12230
12231 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012232 * Updates the {@link Paint} object used with the current layer (used only if the current
12233 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12234 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12235 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12236 * ensure that the view gets redrawn immediately.
12237 *
12238 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12239 * instance that controls how the layer is composed on screen. The following
12240 * properties of the paint are taken into account when composing the layer:</p>
12241 * <ul>
12242 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12243 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12244 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12245 * </ul>
12246 *
12247 * <p>If this view has an alpha value set to < 1.0 by calling
12248 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12249 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12250 * equivalent to setting a hardware layer on this view and providing a paint with
12251 * the desired alpha value.</p>
12252 *
12253 * @param paint The paint used to compose the layer. This argument is optional
12254 * and can be null. It is ignored when the layer type is
12255 * {@link #LAYER_TYPE_NONE}
12256 *
12257 * @see #setLayerType(int, android.graphics.Paint)
12258 */
12259 public void setLayerPaint(Paint paint) {
12260 int layerType = getLayerType();
12261 if (layerType != LAYER_TYPE_NONE) {
12262 mLayerPaint = paint == null ? new Paint() : paint;
12263 if (layerType == LAYER_TYPE_HARDWARE) {
12264 HardwareLayer layer = getHardwareLayer();
12265 if (layer != null) {
12266 layer.setLayerPaint(paint);
12267 }
12268 invalidateViewProperty(false, false);
12269 } else {
12270 invalidate();
12271 }
12272 }
12273 }
12274
12275 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012276 * Indicates whether this view has a static layer. A view with layer type
12277 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12278 * dynamic.
12279 */
12280 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012281 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012282 }
12283
12284 /**
Romain Guy171c5922011-01-06 10:04:23 -080012285 * Indicates what type of layer is currently associated with this view. By default
12286 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12287 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12288 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012289 *
Romain Guy171c5922011-01-06 10:04:23 -080012290 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12291 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012292 *
12293 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012294 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012295 * @see #LAYER_TYPE_NONE
12296 * @see #LAYER_TYPE_SOFTWARE
12297 * @see #LAYER_TYPE_HARDWARE
12298 */
12299 public int getLayerType() {
12300 return mLayerType;
12301 }
Joe Malin32736f02011-01-19 16:14:20 -080012302
Romain Guy6c319ca2011-01-11 14:29:25 -080012303 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012304 * Forces this view's layer to be created and this view to be rendered
12305 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12306 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012307 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012308 * This method can for instance be used to render a view into its layer before
12309 * starting an animation. If this view is complex, rendering into the layer
12310 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012311 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012312 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012313 *
12314 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012315 */
12316 public void buildLayer() {
12317 if (mLayerType == LAYER_TYPE_NONE) return;
12318
12319 if (mAttachInfo == null) {
12320 throw new IllegalStateException("This view must be attached to a window first");
12321 }
12322
12323 switch (mLayerType) {
12324 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012325 if (mAttachInfo.mHardwareRenderer != null &&
12326 mAttachInfo.mHardwareRenderer.isEnabled() &&
12327 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012328 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012329 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012330 break;
12331 case LAYER_TYPE_SOFTWARE:
12332 buildDrawingCache(true);
12333 break;
12334 }
12335 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012336
Romain Guyf1ae1062011-03-02 18:16:04 -080012337 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012338 * <p>Returns a hardware layer that can be used to draw this view again
12339 * without executing its draw method.</p>
12340 *
12341 * @return A HardwareLayer ready to render, or null if an error occurred.
12342 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012343 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012344 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12345 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012346 return null;
12347 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012348
Romain Guy9c4b79a2011-11-10 19:23:58 -080012349 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012350
12351 final int width = mRight - mLeft;
12352 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012353
Romain Guy6c319ca2011-01-11 14:29:25 -080012354 if (width == 0 || height == 0) {
12355 return null;
12356 }
12357
Dianne Hackborn4702a852012-08-17 15:18:29 -070012358 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012359 if (mHardwareLayer == null) {
12360 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12361 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012362 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012363 } else {
12364 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012365 if (mHardwareLayer.resize(width, height)) {
12366 mLocalDirtyRect.set(0, 0, width, height);
12367 }
Romain Guy846a5332012-07-11 17:44:57 -070012368 }
12369
12370 // This should not be necessary but applications that change
12371 // the parameters of their background drawable without calling
12372 // this.setBackground(Drawable) can leave the view in a bad state
12373 // (for instance isOpaque() returns true, but the background is
12374 // not opaque.)
12375 computeOpaqueFlags();
12376
12377 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012378 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012379 mHardwareLayer.setOpaque(opaque);
12380 mLocalDirtyRect.set(0, 0, width, height);
12381 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012382 }
12383
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012384 // The layer is not valid if the underlying GPU resources cannot be allocated
12385 if (!mHardwareLayer.isValid()) {
12386 return null;
12387 }
12388
Romain Guy11cb6422012-09-21 00:39:43 -070012389 mHardwareLayer.setLayerPaint(mLayerPaint);
12390 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12391 ViewRootImpl viewRoot = getViewRootImpl();
12392 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12393
Michael Jurka7e52caf2012-03-06 15:57:06 -080012394 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012395 }
12396
12397 return mHardwareLayer;
12398 }
Romain Guy171c5922011-01-06 10:04:23 -080012399
Romain Guy589b0bb2011-10-10 13:57:47 -070012400 /**
12401 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012402 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012403 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012404 *
12405 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012406 * @see #LAYER_TYPE_HARDWARE
12407 */
Romain Guya998dff2012-03-23 18:58:36 -070012408 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012409 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012410 AttachInfo info = mAttachInfo;
12411 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012412 info.mHardwareRenderer.isEnabled() &&
12413 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012414 mHardwareLayer.destroy();
12415 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012416
Chet Haase6a2d17f2012-09-30 12:14:13 -070012417 if (mDisplayList != null) {
12418 mDisplayList.reset();
12419 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012420 invalidate(true);
12421 invalidateParentCaches();
12422 }
Romain Guy65b345f2011-07-27 18:51:50 -070012423 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012424 }
Romain Guy65b345f2011-07-27 18:51:50 -070012425 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012426 }
12427
Romain Guy171c5922011-01-06 10:04:23 -080012428 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012429 * Destroys all hardware rendering resources. This method is invoked
12430 * when the system needs to reclaim resources. Upon execution of this
12431 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012432 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012433 * Note: you <strong>must</strong> call
12434 * <code>super.destroyHardwareResources()</code> when overriding
12435 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012436 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012437 * @hide
12438 */
12439 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012440 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012441 }
12442
12443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012444 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12445 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12446 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12447 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12448 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12449 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012450 *
Romain Guy171c5922011-01-06 10:04:23 -080012451 * <p>Enabling the drawing cache is similar to
12452 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012453 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12454 * drawing cache has no effect on rendering because the system uses a different mechanism
12455 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12456 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12457 * for information on how to enable software and hardware layers.</p>
12458 *
12459 * <p>This API can be used to manually generate
12460 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12461 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 *
12463 * @param enabled true to enable the drawing cache, false otherwise
12464 *
12465 * @see #isDrawingCacheEnabled()
12466 * @see #getDrawingCache()
12467 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012468 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012469 */
12470 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012471 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012472 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12473 }
12474
12475 /**
12476 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12477 *
12478 * @return true if the drawing cache is enabled
12479 *
12480 * @see #setDrawingCacheEnabled(boolean)
12481 * @see #getDrawingCache()
12482 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012483 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 public boolean isDrawingCacheEnabled() {
12485 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12486 }
12487
12488 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012489 * Debugging utility which recursively outputs the dirty state of a view and its
12490 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012491 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012492 * @hide
12493 */
Romain Guy676b1732011-02-14 14:45:33 -080012494 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012495 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012496 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12497 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12498 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12499 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012500 if (clear) {
12501 mPrivateFlags &= clearMask;
12502 }
12503 if (this instanceof ViewGroup) {
12504 ViewGroup parent = (ViewGroup) this;
12505 final int count = parent.getChildCount();
12506 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012507 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012508 child.outputDirtyFlags(indent + " ", clear, clearMask);
12509 }
12510 }
12511 }
12512
12513 /**
12514 * This method is used by ViewGroup to cause its children to restore or recreate their
12515 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12516 * to recreate its own display list, which would happen if it went through the normal
12517 * draw/dispatchDraw mechanisms.
12518 *
12519 * @hide
12520 */
12521 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012522
12523 /**
12524 * A view that is not attached or hardware accelerated cannot create a display list.
12525 * This method checks these conditions and returns the appropriate result.
12526 *
12527 * @return true if view has the ability to create a display list, false otherwise.
12528 *
12529 * @hide
12530 */
12531 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012532 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012533 }
Joe Malin32736f02011-01-19 16:14:20 -080012534
Chet Haasedaf98e92011-01-10 14:10:36 -080012535 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012536 * @return The HardwareRenderer associated with that view or null if hardware rendering
12537 * is not supported or this this has not been attached to a window.
12538 *
12539 * @hide
12540 */
12541 public HardwareRenderer getHardwareRenderer() {
12542 if (mAttachInfo != null) {
12543 return mAttachInfo.mHardwareRenderer;
12544 }
12545 return null;
12546 }
12547
12548 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012549 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12550 * Otherwise, the same display list will be returned (after having been rendered into
12551 * along the way, depending on the invalidation state of the view).
12552 *
12553 * @param displayList The previous version of this displayList, could be null.
12554 * @param isLayer Whether the requester of the display list is a layer. If so,
12555 * the view will avoid creating a layer inside the resulting display list.
12556 * @return A new or reused DisplayList object.
12557 */
12558 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12559 if (!canHaveDisplayList()) {
12560 return null;
12561 }
12562
Dianne Hackborn4702a852012-08-17 15:18:29 -070012563 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012564 displayList == null || !displayList.isValid() ||
12565 (!isLayer && mRecreateDisplayList))) {
12566 // Don't need to recreate the display list, just need to tell our
12567 // children to restore/recreate theirs
12568 if (displayList != null && displayList.isValid() &&
12569 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012570 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12571 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012572 dispatchGetDisplayList();
12573
12574 return displayList;
12575 }
12576
12577 if (!isLayer) {
12578 // If we got here, we're recreating it. Mark it as such to ensure that
12579 // we copy in child display lists into ours in drawChild()
12580 mRecreateDisplayList = true;
12581 }
12582 if (displayList == null) {
12583 final String name = getClass().getSimpleName();
12584 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12585 // If we're creating a new display list, make sure our parent gets invalidated
12586 // since they will need to recreate their display list to account for this
12587 // new child display list.
12588 invalidateParentCaches();
12589 }
12590
12591 boolean caching = false;
12592 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012593 int width = mRight - mLeft;
12594 int height = mBottom - mTop;
12595
12596 try {
12597 canvas.setViewport(width, height);
12598 // The dirty rect should always be null for a display list
12599 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012600 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012601 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012602 if (layerType == LAYER_TYPE_HARDWARE) {
12603 final HardwareLayer layer = getHardwareLayer();
12604 if (layer != null && layer.isValid()) {
12605 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12606 } else {
12607 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12608 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12609 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12610 }
12611 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012612 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012613 buildDrawingCache(true);
12614 Bitmap cache = getDrawingCache(true);
12615 if (cache != null) {
12616 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12617 caching = true;
12618 }
Chet Haasea1cff502012-02-21 13:43:44 -080012619 }
Chet Haasea1cff502012-02-21 13:43:44 -080012620 } else {
12621
12622 computeScroll();
12623
Chet Haasea1cff502012-02-21 13:43:44 -080012624 canvas.translate(-mScrollX, -mScrollY);
12625 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012626 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12627 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012628 }
12629
12630 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012631 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012632 dispatchDraw(canvas);
12633 } else {
12634 draw(canvas);
12635 }
12636 }
12637 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012638 canvas.onPostDraw();
12639
12640 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012641 displayList.setCaching(caching);
12642 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012643 displayList.setLeftTopRightBottom(0, 0, width, height);
12644 } else {
12645 setDisplayListProperties(displayList);
12646 }
12647 }
12648 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012649 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12650 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012651 }
12652
12653 return displayList;
12654 }
12655
12656 /**
12657 * Get the DisplayList for the HardwareLayer
12658 *
12659 * @param layer The HardwareLayer whose DisplayList we want
12660 * @return A DisplayList fopr the specified HardwareLayer
12661 */
12662 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12663 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12664 layer.setDisplayList(displayList);
12665 return displayList;
12666 }
12667
12668
12669 /**
Romain Guyb051e892010-09-28 19:09:36 -070012670 * <p>Returns a display list that can be used to draw this view again
12671 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012672 *
Romain Guyb051e892010-09-28 19:09:36 -070012673 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012674 *
12675 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012676 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012677 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012678 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012679 return mDisplayList;
12680 }
12681
Romain Guy38c2ece2012-05-24 14:20:56 -070012682 private void clearDisplayList() {
12683 if (mDisplayList != null) {
12684 mDisplayList.invalidate();
12685 mDisplayList.clear();
12686 }
12687 }
12688
Romain Guyb051e892010-09-28 19:09:36 -070012689 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012690 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012691 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012692 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012693 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012694 * @see #getDrawingCache(boolean)
12695 */
12696 public Bitmap getDrawingCache() {
12697 return getDrawingCache(false);
12698 }
12699
12700 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012701 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12702 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12703 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12704 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12705 * request the drawing cache by calling this method and draw it on screen if the
12706 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012707 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012708 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12709 * this method will create a bitmap of the same size as this view. Because this bitmap
12710 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12711 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12712 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12713 * size than the view. This implies that your application must be able to handle this
12714 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012715 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012716 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12717 * the current density of the screen when the application is in compatibility
12718 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012719 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012720 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012721 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722 * @see #setDrawingCacheEnabled(boolean)
12723 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012724 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012725 * @see #destroyDrawingCache()
12726 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012727 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012728 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12729 return null;
12730 }
12731 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012732 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 }
Romain Guy02890fd2010-08-06 17:58:44 -070012734 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 }
12736
12737 /**
12738 * <p>Frees the resources used by the drawing cache. If you call
12739 * {@link #buildDrawingCache()} manually without calling
12740 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12741 * should cleanup the cache with this method afterwards.</p>
12742 *
12743 * @see #setDrawingCacheEnabled(boolean)
12744 * @see #buildDrawingCache()
12745 * @see #getDrawingCache()
12746 */
12747 public void destroyDrawingCache() {
12748 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012749 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012750 mDrawingCache = null;
12751 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012752 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012753 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012754 mUnscaledDrawingCache = null;
12755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012756 }
12757
12758 /**
12759 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012760 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012761 * view will always be drawn on top of a solid color.
12762 *
12763 * @param color The background color to use for the drawing cache's bitmap
12764 *
12765 * @see #setDrawingCacheEnabled(boolean)
12766 * @see #buildDrawingCache()
12767 * @see #getDrawingCache()
12768 */
12769 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012770 if (color != mDrawingCacheBackgroundColor) {
12771 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012772 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 }
12775
12776 /**
12777 * @see #setDrawingCacheBackgroundColor(int)
12778 *
12779 * @return The background color to used for the drawing cache's bitmap
12780 */
12781 public int getDrawingCacheBackgroundColor() {
12782 return mDrawingCacheBackgroundColor;
12783 }
12784
12785 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012786 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012787 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012788 * @see #buildDrawingCache(boolean)
12789 */
12790 public void buildDrawingCache() {
12791 buildDrawingCache(false);
12792 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012793
Romain Guyfbd8f692009-06-26 14:51:58 -070012794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012795 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12796 *
12797 * <p>If you call {@link #buildDrawingCache()} manually without calling
12798 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12799 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012800 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012801 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12802 * this method will create a bitmap of the same size as this view. Because this bitmap
12803 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12804 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12805 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12806 * size than the view. This implies that your application must be able to handle this
12807 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012808 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012809 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12810 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012811 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012812 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012813 *
12814 * @see #getDrawingCache()
12815 * @see #destroyDrawingCache()
12816 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012817 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012818 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012819 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012820 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821
Romain Guy8506ab42009-06-11 17:35:47 -070012822 int width = mRight - mLeft;
12823 int height = mBottom - mTop;
12824
12825 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012826 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012827
Romain Guye1123222009-06-29 14:24:56 -070012828 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012829 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12830 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012832
12833 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012834 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012835 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836
Romain Guyaf61cc42012-09-30 10:49:15 -070012837 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12838 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012839 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12840 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012841 if (width > 0 && height > 0) {
12842 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12843 + projectedBitmapSize + " bytes, only "
12844 + drawingCacheSize + " available");
12845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012846 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012847 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012848 return;
12849 }
12850
12851 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012852 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012853
12854 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012855 Bitmap.Config quality;
12856 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012857 // Never pick ARGB_4444 because it looks awful
12858 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012859 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12860 case DRAWING_CACHE_QUALITY_AUTO:
12861 quality = Bitmap.Config.ARGB_8888;
12862 break;
12863 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012864 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012865 break;
12866 case DRAWING_CACHE_QUALITY_HIGH:
12867 quality = Bitmap.Config.ARGB_8888;
12868 break;
12869 default:
12870 quality = Bitmap.Config.ARGB_8888;
12871 break;
12872 }
12873 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012874 // Optimization for translucent windows
12875 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012876 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012877 }
12878
12879 // Try to cleanup memory
12880 if (bitmap != null) bitmap.recycle();
12881
12882 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012883 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12884 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012885 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012886 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012887 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012888 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012889 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012890 }
Adam Powell26153a32010-11-08 15:22:27 -080012891 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012892 } catch (OutOfMemoryError e) {
12893 // If there is not enough memory to create the bitmap cache, just
12894 // ignore the issue as bitmap caches are not required to draw the
12895 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012896 if (autoScale) {
12897 mDrawingCache = null;
12898 } else {
12899 mUnscaledDrawingCache = null;
12900 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012901 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012902 return;
12903 }
12904
12905 clear = drawingCacheBackgroundColor != 0;
12906 }
12907
12908 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012909 if (attachInfo != null) {
12910 canvas = attachInfo.mCanvas;
12911 if (canvas == null) {
12912 canvas = new Canvas();
12913 }
12914 canvas.setBitmap(bitmap);
12915 // Temporarily clobber the cached Canvas in case one of our children
12916 // is also using a drawing cache. Without this, the children would
12917 // steal the canvas by attaching their own bitmap to it and bad, bad
12918 // thing would happen (invisible views, corrupted drawings, etc.)
12919 attachInfo.mCanvas = null;
12920 } else {
12921 // This case should hopefully never or seldom happen
12922 canvas = new Canvas(bitmap);
12923 }
12924
12925 if (clear) {
12926 bitmap.eraseColor(drawingCacheBackgroundColor);
12927 }
12928
12929 computeScroll();
12930 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012931
Romain Guye1123222009-06-29 14:24:56 -070012932 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012933 final float scale = attachInfo.mApplicationScale;
12934 canvas.scale(scale, scale);
12935 }
Joe Malin32736f02011-01-19 16:14:20 -080012936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 canvas.translate(-mScrollX, -mScrollY);
12938
Dianne Hackborn4702a852012-08-17 15:18:29 -070012939 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012940 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12941 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012942 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012944
12945 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012946 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12947 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012948 dispatchDraw(canvas);
12949 } else {
12950 draw(canvas);
12951 }
12952
12953 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012954 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012955
12956 if (attachInfo != null) {
12957 // Restore the cached Canvas for our siblings
12958 attachInfo.mCanvas = canvas;
12959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012960 }
12961 }
12962
12963 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012964 * Create a snapshot of the view into a bitmap. We should probably make
12965 * some form of this public, but should think about the API.
12966 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012967 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012968 int width = mRight - mLeft;
12969 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012970
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012971 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012972 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012973 width = (int) ((width * scale) + 0.5f);
12974 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012975
Dianne Hackborndde331c2012-08-03 14:01:57 -070012976 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12977 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012978 if (bitmap == null) {
12979 throw new OutOfMemoryError();
12980 }
12981
Romain Guyc529d8d2011-09-06 15:01:39 -070012982 Resources resources = getResources();
12983 if (resources != null) {
12984 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12985 }
Joe Malin32736f02011-01-19 16:14:20 -080012986
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012987 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012988 if (attachInfo != null) {
12989 canvas = attachInfo.mCanvas;
12990 if (canvas == null) {
12991 canvas = new Canvas();
12992 }
12993 canvas.setBitmap(bitmap);
12994 // Temporarily clobber the cached Canvas in case one of our children
12995 // is also using a drawing cache. Without this, the children would
12996 // steal the canvas by attaching their own bitmap to it and bad, bad
12997 // things would happen (invisible views, corrupted drawings, etc.)
12998 attachInfo.mCanvas = null;
12999 } else {
13000 // This case should hopefully never or seldom happen
13001 canvas = new Canvas(bitmap);
13002 }
13003
Romain Guy5bcdff42009-05-14 21:27:18 -070013004 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013005 bitmap.eraseColor(backgroundColor);
13006 }
13007
13008 computeScroll();
13009 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070013010 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013011 canvas.translate(-mScrollX, -mScrollY);
13012
Romain Guy5bcdff42009-05-14 21:27:18 -070013013 // Temporarily remove the dirty mask
13014 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013015 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013016
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013017 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013018 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013019 dispatchDraw(canvas);
13020 } else {
13021 draw(canvas);
13022 }
13023
Romain Guy5bcdff42009-05-14 21:27:18 -070013024 mPrivateFlags = flags;
13025
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013026 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013027 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013028
13029 if (attachInfo != null) {
13030 // Restore the cached Canvas for our siblings
13031 attachInfo.mCanvas = canvas;
13032 }
Romain Guy8506ab42009-06-11 17:35:47 -070013033
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013034 return bitmap;
13035 }
13036
13037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013038 * Indicates whether this View is currently in edit mode. A View is usually
13039 * in edit mode when displayed within a developer tool. For instance, if
13040 * this View is being drawn by a visual user interface builder, this method
13041 * should return true.
13042 *
13043 * Subclasses should check the return value of this method to provide
13044 * different behaviors if their normal behavior might interfere with the
13045 * host environment. For instance: the class spawns a thread in its
13046 * constructor, the drawing code relies on device-specific features, etc.
13047 *
13048 * This method is usually checked in the drawing code of custom widgets.
13049 *
13050 * @return True if this View is in edit mode, false otherwise.
13051 */
13052 public boolean isInEditMode() {
13053 return false;
13054 }
13055
13056 /**
13057 * If the View draws content inside its padding and enables fading edges,
13058 * it needs to support padding offsets. Padding offsets are added to the
13059 * fading edges to extend the length of the fade so that it covers pixels
13060 * drawn inside the padding.
13061 *
13062 * Subclasses of this class should override this method if they need
13063 * to draw content inside the padding.
13064 *
13065 * @return True if padding offset must be applied, false otherwise.
13066 *
13067 * @see #getLeftPaddingOffset()
13068 * @see #getRightPaddingOffset()
13069 * @see #getTopPaddingOffset()
13070 * @see #getBottomPaddingOffset()
13071 *
13072 * @since CURRENT
13073 */
13074 protected boolean isPaddingOffsetRequired() {
13075 return false;
13076 }
13077
13078 /**
13079 * Amount by which to extend the left fading region. Called only when
13080 * {@link #isPaddingOffsetRequired()} returns true.
13081 *
13082 * @return The left padding offset in pixels.
13083 *
13084 * @see #isPaddingOffsetRequired()
13085 *
13086 * @since CURRENT
13087 */
13088 protected int getLeftPaddingOffset() {
13089 return 0;
13090 }
13091
13092 /**
13093 * Amount by which to extend the right fading region. Called only when
13094 * {@link #isPaddingOffsetRequired()} returns true.
13095 *
13096 * @return The right padding offset in pixels.
13097 *
13098 * @see #isPaddingOffsetRequired()
13099 *
13100 * @since CURRENT
13101 */
13102 protected int getRightPaddingOffset() {
13103 return 0;
13104 }
13105
13106 /**
13107 * Amount by which to extend the top fading region. Called only when
13108 * {@link #isPaddingOffsetRequired()} returns true.
13109 *
13110 * @return The top padding offset in pixels.
13111 *
13112 * @see #isPaddingOffsetRequired()
13113 *
13114 * @since CURRENT
13115 */
13116 protected int getTopPaddingOffset() {
13117 return 0;
13118 }
13119
13120 /**
13121 * Amount by which to extend the bottom fading region. Called only when
13122 * {@link #isPaddingOffsetRequired()} returns true.
13123 *
13124 * @return The bottom padding offset in pixels.
13125 *
13126 * @see #isPaddingOffsetRequired()
13127 *
13128 * @since CURRENT
13129 */
13130 protected int getBottomPaddingOffset() {
13131 return 0;
13132 }
13133
13134 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013135 * @hide
13136 * @param offsetRequired
13137 */
13138 protected int getFadeTop(boolean offsetRequired) {
13139 int top = mPaddingTop;
13140 if (offsetRequired) top += getTopPaddingOffset();
13141 return top;
13142 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013143
Romain Guyf2fc4602011-07-19 15:20:03 -070013144 /**
13145 * @hide
13146 * @param offsetRequired
13147 */
13148 protected int getFadeHeight(boolean offsetRequired) {
13149 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013150 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013151 return mBottom - mTop - mPaddingBottom - padding;
13152 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013153
Romain Guyf2fc4602011-07-19 15:20:03 -070013154 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013155 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013156 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013157 *
Romain Guy2bffd262010-09-12 17:40:02 -070013158 * <p>Even if this method returns true, it does not mean that every call
13159 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13160 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013161 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013162 * window is hardware accelerated,
13163 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13164 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013165 *
Romain Guy2bffd262010-09-12 17:40:02 -070013166 * @return True if the view is attached to a window and the window is
13167 * hardware accelerated; false in any other case.
13168 */
13169 public boolean isHardwareAccelerated() {
13170 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13171 }
Joe Malin32736f02011-01-19 16:14:20 -080013172
Romain Guy2bffd262010-09-12 17:40:02 -070013173 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013174 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13175 * case of an active Animation being run on the view.
13176 */
13177 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13178 Animation a, boolean scalingRequired) {
13179 Transformation invalidationTransform;
13180 final int flags = parent.mGroupFlags;
13181 final boolean initialized = a.isInitialized();
13182 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013183 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013184 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013185 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013186 onAnimationStart();
13187 }
13188
13189 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13190 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13191 if (parent.mInvalidationTransformation == null) {
13192 parent.mInvalidationTransformation = new Transformation();
13193 }
13194 invalidationTransform = parent.mInvalidationTransformation;
13195 a.getTransformation(drawingTime, invalidationTransform, 1f);
13196 } else {
13197 invalidationTransform = parent.mChildTransformation;
13198 }
Romain Guy393a52c2012-05-22 20:21:08 -070013199
Chet Haasebcca79a2012-02-14 08:45:14 -080013200 if (more) {
13201 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013202 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13203 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13204 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13205 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013206 // The child need to draw an animation, potentially offscreen, so
13207 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013208 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013209 parent.invalidate(mLeft, mTop, mRight, mBottom);
13210 }
13211 } else {
13212 if (parent.mInvalidateRegion == null) {
13213 parent.mInvalidateRegion = new RectF();
13214 }
13215 final RectF region = parent.mInvalidateRegion;
13216 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13217 invalidationTransform);
13218
13219 // The child need to draw an animation, potentially offscreen, so
13220 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013221 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013222
13223 final int left = mLeft + (int) region.left;
13224 final int top = mTop + (int) region.top;
13225 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13226 top + (int) (region.height() + .5f));
13227 }
13228 }
13229 return more;
13230 }
13231
Chet Haasea1cff502012-02-21 13:43:44 -080013232 /**
13233 * This method is called by getDisplayList() when a display list is created or re-rendered.
13234 * It sets or resets the current value of all properties on that display list (resetting is
13235 * necessary when a display list is being re-created, because we need to make sure that
13236 * previously-set transform values
13237 */
13238 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013239 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013240 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013241 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013242 if (mParent instanceof ViewGroup) {
13243 displayList.setClipChildren(
13244 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13245 }
Chet Haase9420abd2012-03-29 16:28:32 -070013246 float alpha = 1;
13247 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13248 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13249 ViewGroup parentVG = (ViewGroup) mParent;
13250 final boolean hasTransform =
13251 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13252 if (hasTransform) {
13253 Transformation transform = parentVG.mChildTransformation;
13254 final int transformType = parentVG.mChildTransformation.getTransformationType();
13255 if (transformType != Transformation.TYPE_IDENTITY) {
13256 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13257 alpha = transform.getAlpha();
13258 }
13259 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13260 displayList.setStaticMatrix(transform.getMatrix());
13261 }
13262 }
13263 }
Chet Haasea1cff502012-02-21 13:43:44 -080013264 }
13265 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013266 alpha *= mTransformationInfo.mAlpha;
13267 if (alpha < 1) {
13268 final int multipliedAlpha = (int) (255 * alpha);
13269 if (onSetAlpha(multipliedAlpha)) {
13270 alpha = 1;
13271 }
13272 }
13273 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013274 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13275 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13276 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13277 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013278 if (mTransformationInfo.mCamera == null) {
13279 mTransformationInfo.mCamera = new Camera();
13280 mTransformationInfo.matrix3D = new Matrix();
13281 }
13282 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013283 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013284 displayList.setPivotX(getPivotX());
13285 displayList.setPivotY(getPivotY());
13286 }
Chet Haase9420abd2012-03-29 16:28:32 -070013287 } else if (alpha < 1) {
13288 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013289 }
13290 }
13291 }
13292
Chet Haasebcca79a2012-02-14 08:45:14 -080013293 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013294 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13295 * This draw() method is an implementation detail and is not intended to be overridden or
13296 * to be called from anywhere else other than ViewGroup.drawChild().
13297 */
13298 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013299 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013300 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013301 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013302 final int flags = parent.mGroupFlags;
13303
Chet Haasea1cff502012-02-21 13:43:44 -080013304 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013305 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013306 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013307 }
13308
13309 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013310 boolean concatMatrix = false;
13311
13312 boolean scalingRequired = false;
13313 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013314 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013315
13316 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013317 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13318 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013319 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013320 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013321 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13322 } else {
13323 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13324 }
13325
Chet Haasebcca79a2012-02-14 08:45:14 -080013326 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013327 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013328 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013329 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013330 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013331 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013332 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013333 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013334 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013335 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013336 mDisplayList != null) {
13337 // No longer animating: clear out old animation matrix
13338 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013339 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013340 }
13341 if (!useDisplayListProperties &&
13342 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13343 final boolean hasTransform =
13344 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13345 if (hasTransform) {
13346 final int transformType = parent.mChildTransformation.getTransformationType();
13347 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13348 parent.mChildTransformation : null;
13349 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13350 }
Chet Haase64a48c12012-02-13 16:33:29 -080013351 }
13352 }
13353
13354 concatMatrix |= !childHasIdentityMatrix;
13355
13356 // Sets the flag as early as possible to allow draw() implementations
13357 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013358 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013359
Romain Guyfbb43212012-08-30 15:19:27 -070013360 if (!concatMatrix &&
13361 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13362 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013363 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013364 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13365 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013366 return more;
13367 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013368 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013369
13370 if (hardwareAccelerated) {
13371 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13372 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013373 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13374 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013375 }
13376
Chet Haase64a48c12012-02-13 16:33:29 -080013377 DisplayList displayList = null;
13378 Bitmap cache = null;
13379 boolean hasDisplayList = false;
13380 if (caching) {
13381 if (!hardwareAccelerated) {
13382 if (layerType != LAYER_TYPE_NONE) {
13383 layerType = LAYER_TYPE_SOFTWARE;
13384 buildDrawingCache(true);
13385 }
13386 cache = getDrawingCache(true);
13387 } else {
13388 switch (layerType) {
13389 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013390 if (useDisplayListProperties) {
13391 hasDisplayList = canHaveDisplayList();
13392 } else {
13393 buildDrawingCache(true);
13394 cache = getDrawingCache(true);
13395 }
Chet Haase64a48c12012-02-13 16:33:29 -080013396 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013397 case LAYER_TYPE_HARDWARE:
13398 if (useDisplayListProperties) {
13399 hasDisplayList = canHaveDisplayList();
13400 }
13401 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013402 case LAYER_TYPE_NONE:
13403 // Delay getting the display list until animation-driven alpha values are
13404 // set up and possibly passed on to the view
13405 hasDisplayList = canHaveDisplayList();
13406 break;
13407 }
13408 }
13409 }
Chet Haasea1cff502012-02-21 13:43:44 -080013410 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013411 if (useDisplayListProperties) {
13412 displayList = getDisplayList();
13413 if (!displayList.isValid()) {
13414 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13415 // to getDisplayList(), the display list will be marked invalid and we should not
13416 // try to use it again.
13417 displayList = null;
13418 hasDisplayList = false;
13419 useDisplayListProperties = false;
13420 }
13421 }
Chet Haase64a48c12012-02-13 16:33:29 -080013422
Chet Haase526057b2012-07-12 17:50:41 -070013423 int sx = 0;
13424 int sy = 0;
13425 if (!hasDisplayList) {
13426 computeScroll();
13427 sx = mScrollX;
13428 sy = mScrollY;
13429 }
13430
Chet Haase64a48c12012-02-13 16:33:29 -080013431 final boolean hasNoCache = cache == null || hasDisplayList;
13432 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13433 layerType != LAYER_TYPE_HARDWARE;
13434
Chet Haasea1cff502012-02-21 13:43:44 -080013435 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013436 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013437 restoreTo = canvas.save();
13438 }
Chet Haase64a48c12012-02-13 16:33:29 -080013439 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013440 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013441 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013442 if (!useDisplayListProperties) {
13443 canvas.translate(mLeft, mTop);
13444 }
Chet Haase64a48c12012-02-13 16:33:29 -080013445 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013446 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013447 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013448 restoreTo = canvas.save();
13449 }
Chet Haase64a48c12012-02-13 16:33:29 -080013450 // mAttachInfo cannot be null, otherwise scalingRequired == false
13451 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13452 canvas.scale(scale, scale);
13453 }
13454 }
13455
Chet Haasea1cff502012-02-21 13:43:44 -080013456 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013457 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013458 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013459 if (transformToApply != null || !childHasIdentityMatrix) {
13460 int transX = 0;
13461 int transY = 0;
13462
13463 if (offsetForScroll) {
13464 transX = -sx;
13465 transY = -sy;
13466 }
13467
13468 if (transformToApply != null) {
13469 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013470 if (useDisplayListProperties) {
13471 displayList.setAnimationMatrix(transformToApply.getMatrix());
13472 } else {
13473 // Undo the scroll translation, apply the transformation matrix,
13474 // then redo the scroll translate to get the correct result.
13475 canvas.translate(-transX, -transY);
13476 canvas.concat(transformToApply.getMatrix());
13477 canvas.translate(transX, transY);
13478 }
Chet Haasea1cff502012-02-21 13:43:44 -080013479 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013480 }
13481
13482 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013483 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013484 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013485 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013486 }
13487 }
13488
Chet Haasea1cff502012-02-21 13:43:44 -080013489 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013490 canvas.translate(-transX, -transY);
13491 canvas.concat(getMatrix());
13492 canvas.translate(transX, transY);
13493 }
13494 }
13495
Chet Haase21433372012-06-05 07:54:09 -070013496 // Deal with alpha if it is or used to be <1
13497 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013498 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013499 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013500 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013501 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013502 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013503 }
Chet Haasea1cff502012-02-21 13:43:44 -080013504 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013505 if (hasNoCache) {
13506 final int multipliedAlpha = (int) (255 * alpha);
13507 if (!onSetAlpha(multipliedAlpha)) {
13508 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013509 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013510 layerType != LAYER_TYPE_NONE) {
13511 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13512 }
Chet Haase9420abd2012-03-29 16:28:32 -070013513 if (useDisplayListProperties) {
13514 displayList.setAlpha(alpha * getAlpha());
13515 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013516 final int scrollX = hasDisplayList ? 0 : sx;
13517 final int scrollY = hasDisplayList ? 0 : sy;
13518 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13519 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013520 }
13521 } else {
13522 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013523 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013524 }
13525 }
13526 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013527 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013528 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013529 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013530 }
13531
Chet Haasea1cff502012-02-21 13:43:44 -080013532 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13533 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013534 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013535 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013536 } else {
13537 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013538 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013539 } else {
13540 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13541 }
13542 }
13543 }
13544
Chet Haase9420abd2012-03-29 16:28:32 -070013545 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013546 displayList = getDisplayList();
13547 if (!displayList.isValid()) {
13548 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13549 // to getDisplayList(), the display list will be marked invalid and we should not
13550 // try to use it again.
13551 displayList = null;
13552 hasDisplayList = false;
13553 }
13554 }
13555
13556 if (hasNoCache) {
13557 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013558 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013559 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013560 if (layer != null && layer.isValid()) {
13561 mLayerPaint.setAlpha((int) (alpha * 255));
13562 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13563 layerRendered = true;
13564 } else {
13565 final int scrollX = hasDisplayList ? 0 : sx;
13566 final int scrollY = hasDisplayList ? 0 : sy;
13567 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013568 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013569 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13570 }
13571 }
13572
13573 if (!layerRendered) {
13574 if (!hasDisplayList) {
13575 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013576 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13577 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013578 dispatchDraw(canvas);
13579 } else {
13580 draw(canvas);
13581 }
13582 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013583 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013584 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013585 }
13586 }
13587 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013588 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013589 Paint cachePaint;
13590
13591 if (layerType == LAYER_TYPE_NONE) {
13592 cachePaint = parent.mCachePaint;
13593 if (cachePaint == null) {
13594 cachePaint = new Paint();
13595 cachePaint.setDither(false);
13596 parent.mCachePaint = cachePaint;
13597 }
Chet Haase9420abd2012-03-29 16:28:32 -070013598 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013599 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013600 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13601 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013602 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013603 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013604 }
13605 } else {
13606 cachePaint = mLayerPaint;
13607 cachePaint.setAlpha((int) (alpha * 255));
13608 }
13609 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13610 }
13611
Chet Haasea1cff502012-02-21 13:43:44 -080013612 if (restoreTo >= 0) {
13613 canvas.restoreToCount(restoreTo);
13614 }
Chet Haase64a48c12012-02-13 16:33:29 -080013615
13616 if (a != null && !more) {
13617 if (!hardwareAccelerated && !a.getFillAfter()) {
13618 onSetAlpha(255);
13619 }
13620 parent.finishAnimatingView(this, a);
13621 }
13622
13623 if (more && hardwareAccelerated) {
13624 // invalidation is the trigger to recreate display lists, so if we're using
13625 // display lists to render, force an invalidate to allow the animation to
13626 // continue drawing another frame
13627 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013628 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013629 // alpha animations should cause the child to recreate its display list
13630 invalidate(true);
13631 }
13632 }
13633
13634 mRecreateDisplayList = false;
13635
13636 return more;
13637 }
13638
13639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 * Manually render this view (and all of its children) to the given Canvas.
13641 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013642 * called. When implementing a view, implement
13643 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13644 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 *
13646 * @param canvas The Canvas to which the View is rendered.
13647 */
13648 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013649 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013650 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013651 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013652 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013654 /*
13655 * Draw traversal performs several drawing steps which must be executed
13656 * in the appropriate order:
13657 *
13658 * 1. Draw the background
13659 * 2. If necessary, save the canvas' layers to prepare for fading
13660 * 3. Draw view's content
13661 * 4. Draw children
13662 * 5. If necessary, draw the fading edges and restore layers
13663 * 6. Draw decorations (scrollbars for instance)
13664 */
13665
13666 // Step 1, draw the background, if needed
13667 int saveCount;
13668
Romain Guy24443ea2009-05-11 11:56:30 -070013669 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013670 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013671 if (background != null) {
13672 final int scrollX = mScrollX;
13673 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013674
Romain Guy24443ea2009-05-11 11:56:30 -070013675 if (mBackgroundSizeChanged) {
13676 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13677 mBackgroundSizeChanged = false;
13678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013679
Romain Guy24443ea2009-05-11 11:56:30 -070013680 if ((scrollX | scrollY) == 0) {
13681 background.draw(canvas);
13682 } else {
13683 canvas.translate(scrollX, scrollY);
13684 background.draw(canvas);
13685 canvas.translate(-scrollX, -scrollY);
13686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013687 }
13688 }
13689
13690 // skip step 2 & 5 if possible (common case)
13691 final int viewFlags = mViewFlags;
13692 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13693 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13694 if (!verticalEdges && !horizontalEdges) {
13695 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013696 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013697
13698 // Step 4, draw the children
13699 dispatchDraw(canvas);
13700
13701 // Step 6, draw decorations (scrollbars)
13702 onDrawScrollBars(canvas);
13703
13704 // we're done...
13705 return;
13706 }
13707
13708 /*
13709 * Here we do the full fledged routine...
13710 * (this is an uncommon case where speed matters less,
13711 * this is why we repeat some of the tests that have been
13712 * done above)
13713 */
13714
13715 boolean drawTop = false;
13716 boolean drawBottom = false;
13717 boolean drawLeft = false;
13718 boolean drawRight = false;
13719
13720 float topFadeStrength = 0.0f;
13721 float bottomFadeStrength = 0.0f;
13722 float leftFadeStrength = 0.0f;
13723 float rightFadeStrength = 0.0f;
13724
13725 // Step 2, save the canvas' layers
13726 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013727
13728 final boolean offsetRequired = isPaddingOffsetRequired();
13729 if (offsetRequired) {
13730 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 }
13732
13733 int left = mScrollX + paddingLeft;
13734 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013735 int top = mScrollY + getFadeTop(offsetRequired);
13736 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737
13738 if (offsetRequired) {
13739 right += getRightPaddingOffset();
13740 bottom += getBottomPaddingOffset();
13741 }
13742
13743 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013744 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013745 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013746
13747 // clip the fade length if top and bottom fades overlap
13748 // overlapping fades produce odd-looking artifacts
13749 if (verticalEdges && (top + length > bottom - length)) {
13750 length = (bottom - top) / 2;
13751 }
13752
13753 // also clip horizontal fades if necessary
13754 if (horizontalEdges && (left + length > right - length)) {
13755 length = (right - left) / 2;
13756 }
13757
13758 if (verticalEdges) {
13759 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013760 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013761 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013762 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763 }
13764
13765 if (horizontalEdges) {
13766 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013767 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013768 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013769 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 }
13771
13772 saveCount = canvas.getSaveCount();
13773
13774 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013775 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13777
13778 if (drawTop) {
13779 canvas.saveLayer(left, top, right, top + length, null, flags);
13780 }
13781
13782 if (drawBottom) {
13783 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13784 }
13785
13786 if (drawLeft) {
13787 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13788 }
13789
13790 if (drawRight) {
13791 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13792 }
13793 } else {
13794 scrollabilityCache.setFadeColor(solidColor);
13795 }
13796
13797 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013798 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013799
13800 // Step 4, draw the children
13801 dispatchDraw(canvas);
13802
13803 // Step 5, draw the fade effect and restore layers
13804 final Paint p = scrollabilityCache.paint;
13805 final Matrix matrix = scrollabilityCache.matrix;
13806 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013807
13808 if (drawTop) {
13809 matrix.setScale(1, fadeHeight * topFadeStrength);
13810 matrix.postTranslate(left, top);
13811 fade.setLocalMatrix(matrix);
13812 canvas.drawRect(left, top, right, top + length, p);
13813 }
13814
13815 if (drawBottom) {
13816 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13817 matrix.postRotate(180);
13818 matrix.postTranslate(left, bottom);
13819 fade.setLocalMatrix(matrix);
13820 canvas.drawRect(left, bottom - length, right, bottom, p);
13821 }
13822
13823 if (drawLeft) {
13824 matrix.setScale(1, fadeHeight * leftFadeStrength);
13825 matrix.postRotate(-90);
13826 matrix.postTranslate(left, top);
13827 fade.setLocalMatrix(matrix);
13828 canvas.drawRect(left, top, left + length, bottom, p);
13829 }
13830
13831 if (drawRight) {
13832 matrix.setScale(1, fadeHeight * rightFadeStrength);
13833 matrix.postRotate(90);
13834 matrix.postTranslate(right, top);
13835 fade.setLocalMatrix(matrix);
13836 canvas.drawRect(right - length, top, right, bottom, p);
13837 }
13838
13839 canvas.restoreToCount(saveCount);
13840
13841 // Step 6, draw decorations (scrollbars)
13842 onDrawScrollBars(canvas);
13843 }
13844
13845 /**
13846 * Override this if your view is known to always be drawn on top of a solid color background,
13847 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13848 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13849 * should be set to 0xFF.
13850 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013851 * @see #setVerticalFadingEdgeEnabled(boolean)
13852 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013853 *
13854 * @return The known solid color background for this view, or 0 if the color may vary
13855 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013856 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 public int getSolidColor() {
13858 return 0;
13859 }
13860
13861 /**
13862 * Build a human readable string representation of the specified view flags.
13863 *
13864 * @param flags the view flags to convert to a string
13865 * @return a String representing the supplied flags
13866 */
13867 private static String printFlags(int flags) {
13868 String output = "";
13869 int numFlags = 0;
13870 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13871 output += "TAKES_FOCUS";
13872 numFlags++;
13873 }
13874
13875 switch (flags & VISIBILITY_MASK) {
13876 case INVISIBLE:
13877 if (numFlags > 0) {
13878 output += " ";
13879 }
13880 output += "INVISIBLE";
13881 // USELESS HERE numFlags++;
13882 break;
13883 case GONE:
13884 if (numFlags > 0) {
13885 output += " ";
13886 }
13887 output += "GONE";
13888 // USELESS HERE numFlags++;
13889 break;
13890 default:
13891 break;
13892 }
13893 return output;
13894 }
13895
13896 /**
13897 * Build a human readable string representation of the specified private
13898 * view flags.
13899 *
13900 * @param privateFlags the private view flags to convert to a string
13901 * @return a String representing the supplied flags
13902 */
13903 private static String printPrivateFlags(int privateFlags) {
13904 String output = "";
13905 int numFlags = 0;
13906
Dianne Hackborn4702a852012-08-17 15:18:29 -070013907 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013908 output += "WANTS_FOCUS";
13909 numFlags++;
13910 }
13911
Dianne Hackborn4702a852012-08-17 15:18:29 -070013912 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013913 if (numFlags > 0) {
13914 output += " ";
13915 }
13916 output += "FOCUSED";
13917 numFlags++;
13918 }
13919
Dianne Hackborn4702a852012-08-17 15:18:29 -070013920 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 if (numFlags > 0) {
13922 output += " ";
13923 }
13924 output += "SELECTED";
13925 numFlags++;
13926 }
13927
Dianne Hackborn4702a852012-08-17 15:18:29 -070013928 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929 if (numFlags > 0) {
13930 output += " ";
13931 }
13932 output += "IS_ROOT_NAMESPACE";
13933 numFlags++;
13934 }
13935
Dianne Hackborn4702a852012-08-17 15:18:29 -070013936 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 if (numFlags > 0) {
13938 output += " ";
13939 }
13940 output += "HAS_BOUNDS";
13941 numFlags++;
13942 }
13943
Dianne Hackborn4702a852012-08-17 15:18:29 -070013944 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 if (numFlags > 0) {
13946 output += " ";
13947 }
13948 output += "DRAWN";
13949 // USELESS HERE numFlags++;
13950 }
13951 return output;
13952 }
13953
13954 /**
13955 * <p>Indicates whether or not this view's layout will be requested during
13956 * the next hierarchy layout pass.</p>
13957 *
13958 * @return true if the layout will be forced during next layout pass
13959 */
13960 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013961 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013962 }
13963
13964 /**
13965 * Assign a size and position to a view and all of its
13966 * descendants
13967 *
13968 * <p>This is the second phase of the layout mechanism.
13969 * (The first is measuring). In this phase, each parent calls
13970 * layout on all of its children to position them.
13971 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013972 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013973 *
Chet Haase9c087442011-01-12 16:20:16 -080013974 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013975 * Derived classes with children should override
13976 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013977 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013978 *
13979 * @param l Left position, relative to parent
13980 * @param t Top position, relative to parent
13981 * @param r Right position, relative to parent
13982 * @param b Bottom position, relative to parent
13983 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013984 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013985 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013986 int oldL = mLeft;
13987 int oldT = mTop;
13988 int oldB = mBottom;
13989 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013990 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013991 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013992 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013993 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013994
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013995 ListenerInfo li = mListenerInfo;
13996 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013997 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013998 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013999 int numListeners = listenersCopy.size();
14000 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070014001 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070014002 }
14003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014005 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014006 }
14007
14008 /**
14009 * Called from layout when this view should
14010 * assign a size and position to each of its children.
14011 *
14012 * Derived classes with children should override
14013 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014014 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014015 * @param changed This is a new size or position for this view
14016 * @param left Left position, relative to parent
14017 * @param top Top position, relative to parent
14018 * @param right Right position, relative to parent
14019 * @param bottom Bottom position, relative to parent
14020 */
14021 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14022 }
14023
14024 /**
14025 * Assign a size and position to this view.
14026 *
14027 * This is called from layout.
14028 *
14029 * @param left Left position, relative to parent
14030 * @param top Top position, relative to parent
14031 * @param right Right position, relative to parent
14032 * @param bottom Bottom position, relative to parent
14033 * @return true if the new size and position are different than the
14034 * previous ones
14035 * {@hide}
14036 */
14037 protected boolean setFrame(int left, int top, int right, int bottom) {
14038 boolean changed = false;
14039
14040 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014041 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014042 + right + "," + bottom + ")");
14043 }
14044
14045 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14046 changed = true;
14047
14048 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014049 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014051 int oldWidth = mRight - mLeft;
14052 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014053 int newWidth = right - left;
14054 int newHeight = bottom - top;
14055 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14056
14057 // Invalidate our old position
14058 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014059
14060 mLeft = left;
14061 mTop = top;
14062 mRight = right;
14063 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014064 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014065 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067
Dianne Hackborn4702a852012-08-17 15:18:29 -070014068 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070
Chet Haase75755e22011-07-18 17:48:25 -070014071 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014072 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014073 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014074 if (mTransformationInfo != null) {
14075 mTransformationInfo.mMatrixDirty = true;
14076 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014078 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14079 }
14080
14081 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14082 // If we are visible, force the DRAWN bit to on so that
14083 // this invalidate will go through (at least to our parent).
14084 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014085 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014086 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014087 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014088 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014089 // parent display list may need to be recreated based on a change in the bounds
14090 // of any child
14091 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014092 }
14093
14094 // Reset drawn bit to original value (invalidate turns it off)
14095 mPrivateFlags |= drawn;
14096
14097 mBackgroundSizeChanged = true;
14098 }
14099 return changed;
14100 }
14101
14102 /**
14103 * Finalize inflating a view from XML. This is called as the last phase
14104 * of inflation, after all child views have been added.
14105 *
14106 * <p>Even if the subclass overrides onFinishInflate, they should always be
14107 * sure to call the super method, so that we get called.
14108 */
14109 protected void onFinishInflate() {
14110 }
14111
14112 /**
14113 * Returns the resources associated with this view.
14114 *
14115 * @return Resources object.
14116 */
14117 public Resources getResources() {
14118 return mResources;
14119 }
14120
14121 /**
14122 * Invalidates the specified Drawable.
14123 *
14124 * @param drawable the drawable to invalidate
14125 */
14126 public void invalidateDrawable(Drawable drawable) {
14127 if (verifyDrawable(drawable)) {
14128 final Rect dirty = drawable.getBounds();
14129 final int scrollX = mScrollX;
14130 final int scrollY = mScrollY;
14131
14132 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14133 dirty.right + scrollX, dirty.bottom + scrollY);
14134 }
14135 }
14136
14137 /**
14138 * Schedules an action on a drawable to occur at a specified time.
14139 *
14140 * @param who the recipient of the action
14141 * @param what the action to run on the drawable
14142 * @param when the time at which the action must occur. Uses the
14143 * {@link SystemClock#uptimeMillis} timebase.
14144 */
14145 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014146 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014147 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014148 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014149 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14150 Choreographer.CALLBACK_ANIMATION, what, who,
14151 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014152 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014153 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014155 }
14156 }
14157
14158 /**
14159 * Cancels a scheduled action on a drawable.
14160 *
14161 * @param who the recipient of the action
14162 * @param what the action to cancel
14163 */
14164 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014165 if (verifyDrawable(who) && what != null) {
14166 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014167 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14168 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014169 } else {
14170 ViewRootImpl.getRunQueue().removeCallbacks(what);
14171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 }
14173 }
14174
14175 /**
14176 * Unschedule any events associated with the given Drawable. This can be
14177 * used when selecting a new Drawable into a view, so that the previous
14178 * one is completely unscheduled.
14179 *
14180 * @param who The Drawable to unschedule.
14181 *
14182 * @see #drawableStateChanged
14183 */
14184 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014185 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014186 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14187 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014188 }
14189 }
14190
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014191 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014192 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14193 * that the View directionality can and will be resolved before its Drawables.
14194 *
14195 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014196 *
14197 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014198 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014199 protected void resolveDrawables() {
Fabrice Di Meglio1957d282012-10-25 17:42:39 -070014200 if (canResolveLayoutDirection()) {
14201 if (mBackground != null) {
14202 mBackground.setLayoutDirection(getLayoutDirection());
14203 }
14204 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
14205 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014206 }
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014207 }
14208
14209 /**
14210 * Called when layout direction has been resolved.
14211 *
14212 * The default implementation does nothing.
14213 *
14214 * @param layoutDirection The resolved layout direction.
14215 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014216 * @see #LAYOUT_DIRECTION_LTR
14217 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014218 *
14219 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014220 */
14221 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014222 }
14223
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014224 /**
14225 * @hide
14226 */
14227 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014228 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14229 }
14230
14231 private boolean isDrawablesResolved() {
14232 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14233 }
14234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 /**
14236 * If your view subclass is displaying its own Drawable objects, it should
14237 * override this function and return true for any Drawable it is
14238 * displaying. This allows animations for those drawables to be
14239 * scheduled.
14240 *
14241 * <p>Be sure to call through to the super class when overriding this
14242 * function.
14243 *
14244 * @param who The Drawable to verify. Return true if it is one you are
14245 * displaying, else return the result of calling through to the
14246 * super class.
14247 *
14248 * @return boolean If true than the Drawable is being displayed in the
14249 * view; else false and it is not allowed to animate.
14250 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014251 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14252 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014253 */
14254 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014255 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 }
14257
14258 /**
14259 * This function is called whenever the state of the view changes in such
14260 * a way that it impacts the state of drawables being shown.
14261 *
14262 * <p>Be sure to call through to the superclass when overriding this
14263 * function.
14264 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014265 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 */
14267 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014268 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014269 if (d != null && d.isStateful()) {
14270 d.setState(getDrawableState());
14271 }
14272 }
14273
14274 /**
14275 * Call this to force a view to update its drawable state. This will cause
14276 * drawableStateChanged to be called on this view. Views that are interested
14277 * in the new state should call getDrawableState.
14278 *
14279 * @see #drawableStateChanged
14280 * @see #getDrawableState
14281 */
14282 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014283 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014284 drawableStateChanged();
14285
14286 ViewParent parent = mParent;
14287 if (parent != null) {
14288 parent.childDrawableStateChanged(this);
14289 }
14290 }
14291
14292 /**
14293 * Return an array of resource IDs of the drawable states representing the
14294 * current state of the view.
14295 *
14296 * @return The current drawable state
14297 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014298 * @see Drawable#setState(int[])
14299 * @see #drawableStateChanged()
14300 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014301 */
14302 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014303 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014304 return mDrawableState;
14305 } else {
14306 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014307 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014308 return mDrawableState;
14309 }
14310 }
14311
14312 /**
14313 * Generate the new {@link android.graphics.drawable.Drawable} state for
14314 * this view. This is called by the view
14315 * system when the cached Drawable state is determined to be invalid. To
14316 * retrieve the current state, you should use {@link #getDrawableState}.
14317 *
14318 * @param extraSpace if non-zero, this is the number of extra entries you
14319 * would like in the returned array in which you can place your own
14320 * states.
14321 *
14322 * @return Returns an array holding the current {@link Drawable} state of
14323 * the view.
14324 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014325 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014326 */
14327 protected int[] onCreateDrawableState(int extraSpace) {
14328 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14329 mParent instanceof View) {
14330 return ((View) mParent).onCreateDrawableState(extraSpace);
14331 }
14332
14333 int[] drawableState;
14334
14335 int privateFlags = mPrivateFlags;
14336
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014337 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014338 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014339 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14340 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014341 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014342 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014343 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014344 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14345 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014346 // This is set if HW acceleration is requested, even if the current
14347 // process doesn't allow it. This is just to allow app preview
14348 // windows to better match their app.
14349 viewStateIndex |= VIEW_STATE_ACCELERATED;
14350 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014351 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014352
Christopher Tate3d4bf172011-03-28 16:16:46 -070014353 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014354 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14355 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014357 drawableState = VIEW_STATE_SETS[viewStateIndex];
14358
14359 //noinspection ConstantIfStatement
14360 if (false) {
14361 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14362 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014363 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014364 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14365 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014366 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014367 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014368 + ": " + Arrays.toString(drawableState));
14369 }
14370
14371 if (extraSpace == 0) {
14372 return drawableState;
14373 }
14374
14375 final int[] fullState;
14376 if (drawableState != null) {
14377 fullState = new int[drawableState.length + extraSpace];
14378 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14379 } else {
14380 fullState = new int[extraSpace];
14381 }
14382
14383 return fullState;
14384 }
14385
14386 /**
14387 * Merge your own state values in <var>additionalState</var> into the base
14388 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014389 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014390 *
14391 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014392 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014393 * own additional state values.
14394 *
14395 * @param additionalState The additional state values you would like
14396 * added to <var>baseState</var>; this array is not modified.
14397 *
14398 * @return As a convenience, the <var>baseState</var> array you originally
14399 * passed into the function is returned.
14400 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014401 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014402 */
14403 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14404 final int N = baseState.length;
14405 int i = N - 1;
14406 while (i >= 0 && baseState[i] == 0) {
14407 i--;
14408 }
14409 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14410 return baseState;
14411 }
14412
14413 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014414 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14415 * on all Drawable objects associated with this view.
14416 */
14417 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014418 if (mBackground != null) {
14419 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014420 }
14421 }
14422
14423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014424 * Sets the background color for this view.
14425 * @param color the color of the background
14426 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014427 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014428 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014429 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014430 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014431 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014432 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014433 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014435 }
14436
14437 /**
14438 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014439 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014440 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014441 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014442 * @attr ref android.R.styleable#View_background
14443 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014444 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014445 public void setBackgroundResource(int resid) {
14446 if (resid != 0 && resid == mBackgroundResource) {
14447 return;
14448 }
14449
14450 Drawable d= null;
14451 if (resid != 0) {
14452 d = mResources.getDrawable(resid);
14453 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014454 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014455
14456 mBackgroundResource = resid;
14457 }
14458
14459 /**
14460 * Set the background to a given Drawable, or remove the background. If the
14461 * background has padding, this View's padding is set to the background's
14462 * padding. However, when a background is removed, this View's padding isn't
14463 * touched. If setting the padding is desired, please use
14464 * {@link #setPadding(int, int, int, int)}.
14465 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014466 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014467 * background
14468 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014469 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014470 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014471 setBackgroundDrawable(background);
14472 }
14473
14474 /**
14475 * @deprecated use {@link #setBackground(Drawable)} instead
14476 */
14477 @Deprecated
14478 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014479 computeOpaqueFlags();
14480
Philip Milne6c8ea062012-04-03 17:38:43 -070014481 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014482 return;
14483 }
14484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014485 boolean requestLayout = false;
14486
14487 mBackgroundResource = 0;
14488
14489 /*
14490 * Regardless of whether we're setting a new background or not, we want
14491 * to clear the previous drawable.
14492 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014493 if (mBackground != null) {
14494 mBackground.setCallback(null);
14495 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014496 }
14497
Philip Milne6c8ea062012-04-03 17:38:43 -070014498 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014499 Rect padding = sThreadLocal.get();
14500 if (padding == null) {
14501 padding = new Rect();
14502 sThreadLocal.set(padding);
14503 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014504 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014505 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014506 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014507 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014508 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014509 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014510 mUserPaddingLeftInitial = padding.right;
14511 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014512 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014513 break;
14514 case LAYOUT_DIRECTION_LTR:
14515 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014516 mUserPaddingLeftInitial = padding.left;
14517 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014518 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014520 }
14521
14522 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14523 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014524 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14525 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 requestLayout = true;
14527 }
14528
Philip Milne6c8ea062012-04-03 17:38:43 -070014529 background.setCallback(this);
14530 if (background.isStateful()) {
14531 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014532 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014533 background.setVisible(getVisibility() == VISIBLE, false);
14534 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014535
Dianne Hackborn4702a852012-08-17 15:18:29 -070014536 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14537 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14538 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014539 requestLayout = true;
14540 }
14541 } else {
14542 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014543 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014544
Dianne Hackborn4702a852012-08-17 15:18:29 -070014545 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014546 /*
14547 * This view ONLY drew the background before and we're removing
14548 * the background, so now it won't draw anything
14549 * (hence we SKIP_DRAW)
14550 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014551 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14552 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014553 }
14554
14555 /*
14556 * When the background is set, we try to apply its padding to this
14557 * View. When the background is removed, we don't touch this View's
14558 * padding. This is noted in the Javadocs. Hence, we don't need to
14559 * requestLayout(), the invalidate() below is sufficient.
14560 */
14561
14562 // The old background's minimum size could have affected this
14563 // View's layout, so let's requestLayout
14564 requestLayout = true;
14565 }
14566
Romain Guy8f1344f52009-05-15 16:03:59 -070014567 computeOpaqueFlags();
14568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014569 if (requestLayout) {
14570 requestLayout();
14571 }
14572
14573 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014574 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 }
14576
14577 /**
14578 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014579 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014580 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014581 *
14582 * @see #setBackground(Drawable)
14583 *
14584 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014585 */
14586 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014587 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014588 }
14589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014590 /**
14591 * Sets the padding. The view may add on the space required to display
14592 * the scrollbars, depending on the style and visibility of the scrollbars.
14593 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14594 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14595 * from the values set in this call.
14596 *
14597 * @attr ref android.R.styleable#View_padding
14598 * @attr ref android.R.styleable#View_paddingBottom
14599 * @attr ref android.R.styleable#View_paddingLeft
14600 * @attr ref android.R.styleable#View_paddingRight
14601 * @attr ref android.R.styleable#View_paddingTop
14602 * @param left the left padding in pixels
14603 * @param top the top padding in pixels
14604 * @param right the right padding in pixels
14605 * @param bottom the bottom padding in pixels
14606 */
14607 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014608 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014609
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014610 mUserPaddingStart = UNDEFINED_PADDING;
14611 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014612
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014613 mUserPaddingLeftInitial = left;
14614 mUserPaddingRightInitial = right;
14615
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014616 internalSetPadding(left, top, right, bottom);
14617 }
14618
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014619 /**
14620 * @hide
14621 */
14622 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014623 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014624 mUserPaddingRight = right;
14625 mUserPaddingBottom = bottom;
14626
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014627 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014628 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014629
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014630 // Common case is there are no scroll bars.
14631 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014632 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014633 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014634 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014635 switch (mVerticalScrollbarPosition) {
14636 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014637 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014638 left += offset;
14639 } else {
14640 right += offset;
14641 }
14642 break;
Adam Powell20232d02010-12-08 21:08:53 -080014643 case SCROLLBAR_POSITION_RIGHT:
14644 right += offset;
14645 break;
14646 case SCROLLBAR_POSITION_LEFT:
14647 left += offset;
14648 break;
14649 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014650 }
Adam Powell20232d02010-12-08 21:08:53 -080014651 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014652 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14653 ? 0 : getHorizontalScrollbarHeight();
14654 }
14655 }
Romain Guy8506ab42009-06-11 17:35:47 -070014656
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014657 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014658 changed = true;
14659 mPaddingLeft = left;
14660 }
14661 if (mPaddingTop != top) {
14662 changed = true;
14663 mPaddingTop = top;
14664 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014665 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014666 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014667 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014668 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014669 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014670 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014671 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014672 }
14673
14674 if (changed) {
14675 requestLayout();
14676 }
14677 }
14678
14679 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014680 * Sets the relative padding. The view may add on the space required to display
14681 * the scrollbars, depending on the style and visibility of the scrollbars.
14682 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14683 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14684 * from the values set in this call.
14685 *
14686 * @attr ref android.R.styleable#View_padding
14687 * @attr ref android.R.styleable#View_paddingBottom
14688 * @attr ref android.R.styleable#View_paddingStart
14689 * @attr ref android.R.styleable#View_paddingEnd
14690 * @attr ref android.R.styleable#View_paddingTop
14691 * @param start the start padding in pixels
14692 * @param top the top padding in pixels
14693 * @param end the end padding in pixels
14694 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014695 */
14696 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014697 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014698
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014699 mUserPaddingStart = start;
14700 mUserPaddingEnd = end;
14701
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014702 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014703 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014704 mUserPaddingLeftInitial = end;
14705 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014706 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014707 break;
14708 case LAYOUT_DIRECTION_LTR:
14709 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014710 mUserPaddingLeftInitial = start;
14711 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014712 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014713 }
14714 }
14715
14716 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014717 * Returns the top padding of this view.
14718 *
14719 * @return the top padding in pixels
14720 */
14721 public int getPaddingTop() {
14722 return mPaddingTop;
14723 }
14724
14725 /**
14726 * Returns the bottom padding of this view. If there are inset and enabled
14727 * scrollbars, this value may include the space required to display the
14728 * scrollbars as well.
14729 *
14730 * @return the bottom padding in pixels
14731 */
14732 public int getPaddingBottom() {
14733 return mPaddingBottom;
14734 }
14735
14736 /**
14737 * Returns the left padding of this view. If there are inset and enabled
14738 * scrollbars, this value may include the space required to display the
14739 * scrollbars as well.
14740 *
14741 * @return the left padding in pixels
14742 */
14743 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014744 if (!isPaddingResolved()) {
14745 resolvePadding();
14746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014747 return mPaddingLeft;
14748 }
14749
14750 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014751 * Returns the start padding of this view depending on its resolved layout direction.
14752 * If there are inset and enabled scrollbars, this value may include the space
14753 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014754 *
14755 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014756 */
14757 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014758 if (!isPaddingResolved()) {
14759 resolvePadding();
14760 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014761 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014762 mPaddingRight : mPaddingLeft;
14763 }
14764
14765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014766 * Returns the right padding of this view. If there are inset and enabled
14767 * scrollbars, this value may include the space required to display the
14768 * scrollbars as well.
14769 *
14770 * @return the right padding in pixels
14771 */
14772 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014773 if (!isPaddingResolved()) {
14774 resolvePadding();
14775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014776 return mPaddingRight;
14777 }
14778
14779 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014780 * Returns the end padding of this view depending on its resolved layout direction.
14781 * If there are inset and enabled scrollbars, this value may include the space
14782 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014783 *
14784 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014785 */
14786 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014787 if (!isPaddingResolved()) {
14788 resolvePadding();
14789 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014790 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014791 mPaddingLeft : mPaddingRight;
14792 }
14793
14794 /**
14795 * Return if the padding as been set thru relative values
14796 * {@link #setPaddingRelative(int, int, int, int)} or thru
14797 * @attr ref android.R.styleable#View_paddingStart or
14798 * @attr ref android.R.styleable#View_paddingEnd
14799 *
14800 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014801 */
14802 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014803 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014804 }
14805
14806 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014807 * @hide
14808 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014809 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014810 if (isRtlCompatibilityMode()) {
14811 mPaddingLeft = mUserPaddingLeftInitial;
14812 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014813 return;
14814 }
14815 if (isLayoutRtl()) {
14816 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14817 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014818 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014819 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14820 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014821 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014822 }
14823
14824 /**
14825 * @hide
14826 */
Philip Milne7a23b492012-04-24 22:12:36 -070014827 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014828 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014829 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014830 }
14831 return mLayoutInsets;
14832 }
14833
14834 /**
14835 * @hide
14836 */
14837 public void setLayoutInsets(Insets layoutInsets) {
14838 mLayoutInsets = layoutInsets;
14839 }
14840
14841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014842 * Changes the selection state of this view. A view can be selected or not.
14843 * Note that selection is not the same as focus. Views are typically
14844 * selected in the context of an AdapterView like ListView or GridView;
14845 * the selected view is the view that is highlighted.
14846 *
14847 * @param selected true if the view must be selected, false otherwise
14848 */
14849 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014850 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14851 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014852 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014853 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014854 refreshDrawableState();
14855 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014856 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14857 notifyAccessibilityStateChanged();
14858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014859 }
14860 }
14861
14862 /**
14863 * Dispatch setSelected to all of this View's children.
14864 *
14865 * @see #setSelected(boolean)
14866 *
14867 * @param selected The new selected state
14868 */
14869 protected void dispatchSetSelected(boolean selected) {
14870 }
14871
14872 /**
14873 * Indicates the selection state of this view.
14874 *
14875 * @return true if the view is selected, false otherwise
14876 */
14877 @ViewDebug.ExportedProperty
14878 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014879 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014880 }
14881
14882 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014883 * Changes the activated state of this view. A view can be activated or not.
14884 * Note that activation is not the same as selection. Selection is
14885 * a transient property, representing the view (hierarchy) the user is
14886 * currently interacting with. Activation is a longer-term state that the
14887 * user can move views in and out of. For example, in a list view with
14888 * single or multiple selection enabled, the views in the current selection
14889 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14890 * here.) The activated state is propagated down to children of the view it
14891 * is set on.
14892 *
14893 * @param activated true if the view must be activated, false otherwise
14894 */
14895 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014896 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14897 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014898 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014899 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014900 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014901 }
14902 }
14903
14904 /**
14905 * Dispatch setActivated to all of this View's children.
14906 *
14907 * @see #setActivated(boolean)
14908 *
14909 * @param activated The new activated state
14910 */
14911 protected void dispatchSetActivated(boolean activated) {
14912 }
14913
14914 /**
14915 * Indicates the activation state of this view.
14916 *
14917 * @return true if the view is activated, false otherwise
14918 */
14919 @ViewDebug.ExportedProperty
14920 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014921 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014922 }
14923
14924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014925 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14926 * observer can be used to get notifications when global events, like
14927 * layout, happen.
14928 *
14929 * The returned ViewTreeObserver observer is not guaranteed to remain
14930 * valid for the lifetime of this View. If the caller of this method keeps
14931 * a long-lived reference to ViewTreeObserver, it should always check for
14932 * the return value of {@link ViewTreeObserver#isAlive()}.
14933 *
14934 * @return The ViewTreeObserver for this view's hierarchy.
14935 */
14936 public ViewTreeObserver getViewTreeObserver() {
14937 if (mAttachInfo != null) {
14938 return mAttachInfo.mTreeObserver;
14939 }
14940 if (mFloatingTreeObserver == null) {
14941 mFloatingTreeObserver = new ViewTreeObserver();
14942 }
14943 return mFloatingTreeObserver;
14944 }
14945
14946 /**
14947 * <p>Finds the topmost view in the current view hierarchy.</p>
14948 *
14949 * @return the topmost view containing this view
14950 */
14951 public View getRootView() {
14952 if (mAttachInfo != null) {
14953 final View v = mAttachInfo.mRootView;
14954 if (v != null) {
14955 return v;
14956 }
14957 }
Romain Guy8506ab42009-06-11 17:35:47 -070014958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014959 View parent = this;
14960
14961 while (parent.mParent != null && parent.mParent instanceof View) {
14962 parent = (View) parent.mParent;
14963 }
14964
14965 return parent;
14966 }
14967
14968 /**
14969 * <p>Computes the coordinates of this view on the screen. The argument
14970 * must be an array of two integers. After the method returns, the array
14971 * contains the x and y location in that order.</p>
14972 *
14973 * @param location an array of two integers in which to hold the coordinates
14974 */
14975 public void getLocationOnScreen(int[] location) {
14976 getLocationInWindow(location);
14977
14978 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014979 if (info != null) {
14980 location[0] += info.mWindowLeft;
14981 location[1] += info.mWindowTop;
14982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014983 }
14984
14985 /**
14986 * <p>Computes the coordinates of this view in its window. The argument
14987 * must be an array of two integers. After the method returns, the array
14988 * contains the x and y location in that order.</p>
14989 *
14990 * @param location an array of two integers in which to hold the coordinates
14991 */
14992 public void getLocationInWindow(int[] location) {
14993 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014994 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014995 }
14996
Gilles Debunne6583ce52011-12-06 18:09:02 -080014997 if (mAttachInfo == null) {
14998 // When the view is not attached to a window, this method does not make sense
14999 location[0] = location[1] = 0;
15000 return;
15001 }
15002
Gilles Debunnecea45132011-11-24 02:19:27 +010015003 float[] position = mAttachInfo.mTmpTransformLocation;
15004 position[0] = position[1] = 0.0f;
15005
15006 if (!hasIdentityMatrix()) {
15007 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015009
Gilles Debunnecea45132011-11-24 02:19:27 +010015010 position[0] += mLeft;
15011 position[1] += mTop;
15012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015013 ViewParent viewParent = mParent;
15014 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015015 final View view = (View) viewParent;
15016
15017 position[0] -= view.mScrollX;
15018 position[1] -= view.mScrollY;
15019
15020 if (!view.hasIdentityMatrix()) {
15021 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015022 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015023
15024 position[0] += view.mLeft;
15025 position[1] += view.mTop;
15026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015027 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015028 }
Romain Guy8506ab42009-06-11 17:35:47 -070015029
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015030 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015031 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015032 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15033 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015034 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015035
15036 location[0] = (int) (position[0] + 0.5f);
15037 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015038 }
15039
15040 /**
15041 * {@hide}
15042 * @param id the id of the view to be found
15043 * @return the view of the specified id, null if cannot be found
15044 */
15045 protected View findViewTraversal(int id) {
15046 if (id == mID) {
15047 return this;
15048 }
15049 return null;
15050 }
15051
15052 /**
15053 * {@hide}
15054 * @param tag the tag of the view to be found
15055 * @return the view of specified tag, null if cannot be found
15056 */
15057 protected View findViewWithTagTraversal(Object tag) {
15058 if (tag != null && tag.equals(mTag)) {
15059 return this;
15060 }
15061 return null;
15062 }
15063
15064 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015065 * {@hide}
15066 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015067 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015068 * @return The first view that matches the predicate or null.
15069 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015070 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015071 if (predicate.apply(this)) {
15072 return this;
15073 }
15074 return null;
15075 }
15076
15077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015078 * Look for a child view with the given id. If this view has the given
15079 * id, return this view.
15080 *
15081 * @param id The id to search for.
15082 * @return The view that has the given id in the hierarchy or null
15083 */
15084 public final View findViewById(int id) {
15085 if (id < 0) {
15086 return null;
15087 }
15088 return findViewTraversal(id);
15089 }
15090
15091 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015092 * Finds a view by its unuque and stable accessibility id.
15093 *
15094 * @param accessibilityId The searched accessibility id.
15095 * @return The found view.
15096 */
15097 final View findViewByAccessibilityId(int accessibilityId) {
15098 if (accessibilityId < 0) {
15099 return null;
15100 }
15101 return findViewByAccessibilityIdTraversal(accessibilityId);
15102 }
15103
15104 /**
15105 * Performs the traversal to find a view by its unuque and stable accessibility id.
15106 *
15107 * <strong>Note:</strong>This method does not stop at the root namespace
15108 * boundary since the user can touch the screen at an arbitrary location
15109 * potentially crossing the root namespace bounday which will send an
15110 * accessibility event to accessibility services and they should be able
15111 * to obtain the event source. Also accessibility ids are guaranteed to be
15112 * unique in the window.
15113 *
15114 * @param accessibilityId The accessibility id.
15115 * @return The found view.
15116 */
15117 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15118 if (getAccessibilityViewId() == accessibilityId) {
15119 return this;
15120 }
15121 return null;
15122 }
15123
15124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015125 * Look for a child view with the given tag. If this view has the given
15126 * tag, return this view.
15127 *
15128 * @param tag The tag to search for, using "tag.equals(getTag())".
15129 * @return The View that has the given tag in the hierarchy or null
15130 */
15131 public final View findViewWithTag(Object tag) {
15132 if (tag == null) {
15133 return null;
15134 }
15135 return findViewWithTagTraversal(tag);
15136 }
15137
15138 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015139 * {@hide}
15140 * Look for a child view that matches the specified predicate.
15141 * If this view matches the predicate, return this view.
15142 *
15143 * @param predicate The predicate to evaluate.
15144 * @return The first view that matches the predicate or null.
15145 */
15146 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015147 return findViewByPredicateTraversal(predicate, null);
15148 }
15149
15150 /**
15151 * {@hide}
15152 * Look for a child view that matches the specified predicate,
15153 * starting with the specified view and its descendents and then
15154 * recusively searching the ancestors and siblings of that view
15155 * until this view is reached.
15156 *
15157 * This method is useful in cases where the predicate does not match
15158 * a single unique view (perhaps multiple views use the same id)
15159 * and we are trying to find the view that is "closest" in scope to the
15160 * starting view.
15161 *
15162 * @param start The view to start from.
15163 * @param predicate The predicate to evaluate.
15164 * @return The first view that matches the predicate or null.
15165 */
15166 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15167 View childToSkip = null;
15168 for (;;) {
15169 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15170 if (view != null || start == this) {
15171 return view;
15172 }
15173
15174 ViewParent parent = start.getParent();
15175 if (parent == null || !(parent instanceof View)) {
15176 return null;
15177 }
15178
15179 childToSkip = start;
15180 start = (View) parent;
15181 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015182 }
15183
15184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015185 * Sets the identifier for this view. The identifier does not have to be
15186 * unique in this view's hierarchy. The identifier should be a positive
15187 * number.
15188 *
15189 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015190 * @see #getId()
15191 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015192 *
15193 * @param id a number used to identify the view
15194 *
15195 * @attr ref android.R.styleable#View_id
15196 */
15197 public void setId(int id) {
15198 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015199 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15200 mID = generateViewId();
15201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015202 }
15203
15204 /**
15205 * {@hide}
15206 *
15207 * @param isRoot true if the view belongs to the root namespace, false
15208 * otherwise
15209 */
15210 public void setIsRootNamespace(boolean isRoot) {
15211 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015212 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015213 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015214 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015215 }
15216 }
15217
15218 /**
15219 * {@hide}
15220 *
15221 * @return true if the view belongs to the root namespace, false otherwise
15222 */
15223 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015224 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015225 }
15226
15227 /**
15228 * Returns this view's identifier.
15229 *
15230 * @return a positive integer used to identify the view or {@link #NO_ID}
15231 * if the view has no ID
15232 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015233 * @see #setId(int)
15234 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015235 * @attr ref android.R.styleable#View_id
15236 */
15237 @ViewDebug.CapturedViewProperty
15238 public int getId() {
15239 return mID;
15240 }
15241
15242 /**
15243 * Returns this view's tag.
15244 *
15245 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015246 *
15247 * @see #setTag(Object)
15248 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015249 */
15250 @ViewDebug.ExportedProperty
15251 public Object getTag() {
15252 return mTag;
15253 }
15254
15255 /**
15256 * Sets the tag associated with this view. A tag can be used to mark
15257 * a view in its hierarchy and does not have to be unique within the
15258 * hierarchy. Tags can also be used to store data within a view without
15259 * resorting to another data structure.
15260 *
15261 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015262 *
15263 * @see #getTag()
15264 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015265 */
15266 public void setTag(final Object tag) {
15267 mTag = tag;
15268 }
15269
15270 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015271 * Returns the tag associated with this view and the specified key.
15272 *
15273 * @param key The key identifying the tag
15274 *
15275 * @return the Object stored in this view as a tag
15276 *
15277 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015278 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015279 */
15280 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015281 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015282 return null;
15283 }
15284
15285 /**
15286 * Sets a tag associated with this view and a key. A tag can be used
15287 * to mark a view in its hierarchy and does not have to be unique within
15288 * the hierarchy. Tags can also be used to store data within a view
15289 * without resorting to another data structure.
15290 *
15291 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015292 * application to ensure it is unique (see the <a
15293 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15294 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015295 * the Android framework or not associated with any package will cause
15296 * an {@link IllegalArgumentException} to be thrown.
15297 *
15298 * @param key The key identifying the tag
15299 * @param tag An Object to tag the view with
15300 *
15301 * @throws IllegalArgumentException If they specified key is not valid
15302 *
15303 * @see #setTag(Object)
15304 * @see #getTag(int)
15305 */
15306 public void setTag(int key, final Object tag) {
15307 // If the package id is 0x00 or 0x01, it's either an undefined package
15308 // or a framework id
15309 if ((key >>> 24) < 2) {
15310 throw new IllegalArgumentException("The key must be an application-specific "
15311 + "resource id.");
15312 }
15313
Adam Powell2b2f6d62011-09-23 11:15:39 -070015314 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015315 }
15316
15317 /**
15318 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15319 * framework id.
15320 *
15321 * @hide
15322 */
15323 public void setTagInternal(int key, Object tag) {
15324 if ((key >>> 24) != 0x1) {
15325 throw new IllegalArgumentException("The key must be a framework-specific "
15326 + "resource id.");
15327 }
15328
Adam Powell2b2f6d62011-09-23 11:15:39 -070015329 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015330 }
15331
Adam Powell2b2f6d62011-09-23 11:15:39 -070015332 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015333 if (mKeyedTags == null) {
15334 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015335 }
15336
Adam Powell7db82ac2011-09-22 19:44:04 -070015337 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015338 }
15339
15340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015341 * Prints information about this view in the log output, with the tag
15342 * {@link #VIEW_LOG_TAG}.
15343 *
15344 * @hide
15345 */
15346 public void debug() {
15347 debug(0);
15348 }
15349
15350 /**
15351 * Prints information about this view in the log output, with the tag
15352 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15353 * indentation defined by the <code>depth</code>.
15354 *
15355 * @param depth the indentation level
15356 *
15357 * @hide
15358 */
15359 protected void debug(int depth) {
15360 String output = debugIndent(depth - 1);
15361
15362 output += "+ " + this;
15363 int id = getId();
15364 if (id != -1) {
15365 output += " (id=" + id + ")";
15366 }
15367 Object tag = getTag();
15368 if (tag != null) {
15369 output += " (tag=" + tag + ")";
15370 }
15371 Log.d(VIEW_LOG_TAG, output);
15372
Dianne Hackborn4702a852012-08-17 15:18:29 -070015373 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015374 output = debugIndent(depth) + " FOCUSED";
15375 Log.d(VIEW_LOG_TAG, output);
15376 }
15377
15378 output = debugIndent(depth);
15379 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15380 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15381 + "} ";
15382 Log.d(VIEW_LOG_TAG, output);
15383
15384 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15385 || mPaddingBottom != 0) {
15386 output = debugIndent(depth);
15387 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15388 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15389 Log.d(VIEW_LOG_TAG, output);
15390 }
15391
15392 output = debugIndent(depth);
15393 output += "mMeasureWidth=" + mMeasuredWidth +
15394 " mMeasureHeight=" + mMeasuredHeight;
15395 Log.d(VIEW_LOG_TAG, output);
15396
15397 output = debugIndent(depth);
15398 if (mLayoutParams == null) {
15399 output += "BAD! no layout params";
15400 } else {
15401 output = mLayoutParams.debug(output);
15402 }
15403 Log.d(VIEW_LOG_TAG, output);
15404
15405 output = debugIndent(depth);
15406 output += "flags={";
15407 output += View.printFlags(mViewFlags);
15408 output += "}";
15409 Log.d(VIEW_LOG_TAG, output);
15410
15411 output = debugIndent(depth);
15412 output += "privateFlags={";
15413 output += View.printPrivateFlags(mPrivateFlags);
15414 output += "}";
15415 Log.d(VIEW_LOG_TAG, output);
15416 }
15417
15418 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015419 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015420 *
15421 * @param depth the indentation level
15422 * @return a String containing (depth * 2 + 3) * 2 white spaces
15423 *
15424 * @hide
15425 */
15426 protected static String debugIndent(int depth) {
15427 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15428 for (int i = 0; i < (depth * 2) + 3; i++) {
15429 spaces.append(' ').append(' ');
15430 }
15431 return spaces.toString();
15432 }
15433
15434 /**
15435 * <p>Return the offset of the widget's text baseline from the widget's top
15436 * boundary. If this widget does not support baseline alignment, this
15437 * method returns -1. </p>
15438 *
15439 * @return the offset of the baseline within the widget's bounds or -1
15440 * if baseline alignment is not supported
15441 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015442 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015443 public int getBaseline() {
15444 return -1;
15445 }
15446
15447 /**
15448 * Call this when something has changed which has invalidated the
15449 * layout of this view. This will schedule a layout pass of the view
15450 * tree.
15451 */
15452 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015453 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15454 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015455
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015456 if (mParent != null && !mParent.isLayoutRequested()) {
15457 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015458 }
15459 }
15460
15461 /**
15462 * Forces this view to be laid out during the next layout pass.
15463 * This method does not call requestLayout() or forceLayout()
15464 * on the parent.
15465 */
15466 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015467 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15468 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015469 }
15470
15471 /**
15472 * <p>
15473 * This is called to find out how big a view should be. The parent
15474 * supplies constraint information in the width and height parameters.
15475 * </p>
15476 *
15477 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015478 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015479 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015480 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015481 * </p>
15482 *
15483 *
15484 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15485 * parent
15486 * @param heightMeasureSpec Vertical space requirements as imposed by the
15487 * parent
15488 *
15489 * @see #onMeasure(int, int)
15490 */
15491 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015492 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015493 widthMeasureSpec != mOldWidthMeasureSpec ||
15494 heightMeasureSpec != mOldHeightMeasureSpec) {
15495
15496 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015497 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015498
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015499 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015501 // measure ourselves, this should set the measured dimension flag back
15502 onMeasure(widthMeasureSpec, heightMeasureSpec);
15503
15504 // flag not set, setMeasuredDimension() was not invoked, we raise
15505 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015506 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015507 throw new IllegalStateException("onMeasure() did not set the"
15508 + " measured dimension by calling"
15509 + " setMeasuredDimension()");
15510 }
15511
Dianne Hackborn4702a852012-08-17 15:18:29 -070015512 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015513 }
15514
15515 mOldWidthMeasureSpec = widthMeasureSpec;
15516 mOldHeightMeasureSpec = heightMeasureSpec;
15517 }
15518
15519 /**
15520 * <p>
15521 * Measure the view and its content to determine the measured width and the
15522 * measured height. This method is invoked by {@link #measure(int, int)} and
15523 * should be overriden by subclasses to provide accurate and efficient
15524 * measurement of their contents.
15525 * </p>
15526 *
15527 * <p>
15528 * <strong>CONTRACT:</strong> When overriding this method, you
15529 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15530 * measured width and height of this view. Failure to do so will trigger an
15531 * <code>IllegalStateException</code>, thrown by
15532 * {@link #measure(int, int)}. Calling the superclass'
15533 * {@link #onMeasure(int, int)} is a valid use.
15534 * </p>
15535 *
15536 * <p>
15537 * The base class implementation of measure defaults to the background size,
15538 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15539 * override {@link #onMeasure(int, int)} to provide better measurements of
15540 * their content.
15541 * </p>
15542 *
15543 * <p>
15544 * If this method is overridden, it is the subclass's responsibility to make
15545 * sure the measured height and width are at least the view's minimum height
15546 * and width ({@link #getSuggestedMinimumHeight()} and
15547 * {@link #getSuggestedMinimumWidth()}).
15548 * </p>
15549 *
15550 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15551 * The requirements are encoded with
15552 * {@link android.view.View.MeasureSpec}.
15553 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15554 * The requirements are encoded with
15555 * {@link android.view.View.MeasureSpec}.
15556 *
15557 * @see #getMeasuredWidth()
15558 * @see #getMeasuredHeight()
15559 * @see #setMeasuredDimension(int, int)
15560 * @see #getSuggestedMinimumHeight()
15561 * @see #getSuggestedMinimumWidth()
15562 * @see android.view.View.MeasureSpec#getMode(int)
15563 * @see android.view.View.MeasureSpec#getSize(int)
15564 */
15565 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15566 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15567 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15568 }
15569
15570 /**
15571 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15572 * measured width and measured height. Failing to do so will trigger an
15573 * exception at measurement time.</p>
15574 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015575 * @param measuredWidth The measured width of this view. May be a complex
15576 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15577 * {@link #MEASURED_STATE_TOO_SMALL}.
15578 * @param measuredHeight The measured height of this view. May be a complex
15579 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15580 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015581 */
15582 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15583 mMeasuredWidth = measuredWidth;
15584 mMeasuredHeight = measuredHeight;
15585
Dianne Hackborn4702a852012-08-17 15:18:29 -070015586 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015587 }
15588
15589 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015590 * Merge two states as returned by {@link #getMeasuredState()}.
15591 * @param curState The current state as returned from a view or the result
15592 * of combining multiple views.
15593 * @param newState The new view state to combine.
15594 * @return Returns a new integer reflecting the combination of the two
15595 * states.
15596 */
15597 public static int combineMeasuredStates(int curState, int newState) {
15598 return curState | newState;
15599 }
15600
15601 /**
15602 * Version of {@link #resolveSizeAndState(int, int, int)}
15603 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15604 */
15605 public static int resolveSize(int size, int measureSpec) {
15606 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15607 }
15608
15609 /**
15610 * Utility to reconcile a desired size and state, with constraints imposed
15611 * by a MeasureSpec. Will take the desired size, unless a different size
15612 * is imposed by the constraints. The returned value is a compound integer,
15613 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15614 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15615 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015616 *
15617 * @param size How big the view wants to be
15618 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015619 * @return Size information bit mask as defined by
15620 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015621 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015622 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015623 int result = size;
15624 int specMode = MeasureSpec.getMode(measureSpec);
15625 int specSize = MeasureSpec.getSize(measureSpec);
15626 switch (specMode) {
15627 case MeasureSpec.UNSPECIFIED:
15628 result = size;
15629 break;
15630 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015631 if (specSize < size) {
15632 result = specSize | MEASURED_STATE_TOO_SMALL;
15633 } else {
15634 result = size;
15635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015636 break;
15637 case MeasureSpec.EXACTLY:
15638 result = specSize;
15639 break;
15640 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015641 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015642 }
15643
15644 /**
15645 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015646 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015647 * by the MeasureSpec.
15648 *
15649 * @param size Default size for this view
15650 * @param measureSpec Constraints imposed by the parent
15651 * @return The size this view should be.
15652 */
15653 public static int getDefaultSize(int size, int measureSpec) {
15654 int result = size;
15655 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015656 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015657
15658 switch (specMode) {
15659 case MeasureSpec.UNSPECIFIED:
15660 result = size;
15661 break;
15662 case MeasureSpec.AT_MOST:
15663 case MeasureSpec.EXACTLY:
15664 result = specSize;
15665 break;
15666 }
15667 return result;
15668 }
15669
15670 /**
15671 * Returns the suggested minimum height that the view should use. This
15672 * returns the maximum of the view's minimum height
15673 * and the background's minimum height
15674 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15675 * <p>
15676 * When being used in {@link #onMeasure(int, int)}, the caller should still
15677 * ensure the returned height is within the requirements of the parent.
15678 *
15679 * @return The suggested minimum height of the view.
15680 */
15681 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015682 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015684 }
15685
15686 /**
15687 * Returns the suggested minimum width that the view should use. This
15688 * returns the maximum of the view's minimum width)
15689 * and the background's minimum width
15690 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15691 * <p>
15692 * When being used in {@link #onMeasure(int, int)}, the caller should still
15693 * ensure the returned width is within the requirements of the parent.
15694 *
15695 * @return The suggested minimum width of the view.
15696 */
15697 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015698 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015700
Philip Milne6c8ea062012-04-03 17:38:43 -070015701 /**
15702 * Returns the minimum height of the view.
15703 *
15704 * @return the minimum height the view will try to be.
15705 *
15706 * @see #setMinimumHeight(int)
15707 *
15708 * @attr ref android.R.styleable#View_minHeight
15709 */
15710 public int getMinimumHeight() {
15711 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015712 }
15713
15714 /**
15715 * Sets the minimum height of the view. It is not guaranteed the view will
15716 * be able to achieve this minimum height (for example, if its parent layout
15717 * constrains it with less available height).
15718 *
15719 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015720 *
15721 * @see #getMinimumHeight()
15722 *
15723 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015724 */
15725 public void setMinimumHeight(int minHeight) {
15726 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015727 requestLayout();
15728 }
15729
15730 /**
15731 * Returns the minimum width of the view.
15732 *
15733 * @return the minimum width the view will try to be.
15734 *
15735 * @see #setMinimumWidth(int)
15736 *
15737 * @attr ref android.R.styleable#View_minWidth
15738 */
15739 public int getMinimumWidth() {
15740 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015741 }
15742
15743 /**
15744 * Sets the minimum width of the view. It is not guaranteed the view will
15745 * be able to achieve this minimum width (for example, if its parent layout
15746 * constrains it with less available width).
15747 *
15748 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015749 *
15750 * @see #getMinimumWidth()
15751 *
15752 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015753 */
15754 public void setMinimumWidth(int minWidth) {
15755 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015756 requestLayout();
15757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015758 }
15759
15760 /**
15761 * Get the animation currently associated with this view.
15762 *
15763 * @return The animation that is currently playing or
15764 * scheduled to play for this view.
15765 */
15766 public Animation getAnimation() {
15767 return mCurrentAnimation;
15768 }
15769
15770 /**
15771 * Start the specified animation now.
15772 *
15773 * @param animation the animation to start now
15774 */
15775 public void startAnimation(Animation animation) {
15776 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15777 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015778 invalidateParentCaches();
15779 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015780 }
15781
15782 /**
15783 * Cancels any animations for this view.
15784 */
15785 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015786 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015787 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015789 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015790 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015791 }
15792
15793 /**
15794 * Sets the next animation to play for this view.
15795 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015796 * {@link #startAnimation(android.view.animation.Animation)} instead.
15797 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015798 * control over the start time and invalidation, but you
15799 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015800 * 2) the view's parent (which controls animations on its children)
15801 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015802 * start.
15803 *
15804 * @param animation The next animation, or null.
15805 */
15806 public void setAnimation(Animation animation) {
15807 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015809 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015810 // If the screen is off assume the animation start time is now instead of
15811 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15812 // would cause the animation to start when the screen turns back on
15813 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15814 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15815 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015817 animation.reset();
15818 }
15819 }
15820
15821 /**
15822 * Invoked by a parent ViewGroup to notify the start of the animation
15823 * currently associated with this view. If you override this method,
15824 * always call super.onAnimationStart();
15825 *
15826 * @see #setAnimation(android.view.animation.Animation)
15827 * @see #getAnimation()
15828 */
15829 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015830 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015831 }
15832
15833 /**
15834 * Invoked by a parent ViewGroup to notify the end of the animation
15835 * currently associated with this view. If you override this method,
15836 * always call super.onAnimationEnd();
15837 *
15838 * @see #setAnimation(android.view.animation.Animation)
15839 * @see #getAnimation()
15840 */
15841 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015842 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015843 }
15844
15845 /**
15846 * Invoked if there is a Transform that involves alpha. Subclass that can
15847 * draw themselves with the specified alpha should return true, and then
15848 * respect that alpha when their onDraw() is called. If this returns false
15849 * then the view may be redirected to draw into an offscreen buffer to
15850 * fulfill the request, which will look fine, but may be slower than if the
15851 * subclass handles it internally. The default implementation returns false.
15852 *
15853 * @param alpha The alpha (0..255) to apply to the view's drawing
15854 * @return true if the view can draw with the specified alpha.
15855 */
15856 protected boolean onSetAlpha(int alpha) {
15857 return false;
15858 }
15859
15860 /**
15861 * This is used by the RootView to perform an optimization when
15862 * the view hierarchy contains one or several SurfaceView.
15863 * SurfaceView is always considered transparent, but its children are not,
15864 * therefore all View objects remove themselves from the global transparent
15865 * region (passed as a parameter to this function).
15866 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015867 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015868 *
15869 * @return Returns true if the effective visibility of the view at this
15870 * point is opaque, regardless of the transparent region; returns false
15871 * if it is possible for underlying windows to be seen behind the view.
15872 *
15873 * {@hide}
15874 */
15875 public boolean gatherTransparentRegion(Region region) {
15876 final AttachInfo attachInfo = mAttachInfo;
15877 if (region != null && attachInfo != null) {
15878 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015879 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015880 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15881 // remove it from the transparent region.
15882 final int[] location = attachInfo.mTransparentLocation;
15883 getLocationInWindow(location);
15884 region.op(location[0], location[1], location[0] + mRight - mLeft,
15885 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015886 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015887 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15888 // exists, so we remove the background drawable's non-transparent
15889 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015890 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015891 }
15892 }
15893 return true;
15894 }
15895
15896 /**
15897 * Play a sound effect for this view.
15898 *
15899 * <p>The framework will play sound effects for some built in actions, such as
15900 * clicking, but you may wish to play these effects in your widget,
15901 * for instance, for internal navigation.
15902 *
15903 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15904 * {@link #isSoundEffectsEnabled()} is true.
15905 *
15906 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15907 */
15908 public void playSoundEffect(int soundConstant) {
15909 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15910 return;
15911 }
15912 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15913 }
15914
15915 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015916 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015917 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015918 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015919 *
15920 * <p>The framework will provide haptic feedback for some built in actions,
15921 * such as long presses, but you may wish to provide feedback for your
15922 * own widget.
15923 *
15924 * <p>The feedback will only be performed if
15925 * {@link #isHapticFeedbackEnabled()} is true.
15926 *
15927 * @param feedbackConstant One of the constants defined in
15928 * {@link HapticFeedbackConstants}
15929 */
15930 public boolean performHapticFeedback(int feedbackConstant) {
15931 return performHapticFeedback(feedbackConstant, 0);
15932 }
15933
15934 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015935 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015936 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015937 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015938 *
15939 * @param feedbackConstant One of the constants defined in
15940 * {@link HapticFeedbackConstants}
15941 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15942 */
15943 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15944 if (mAttachInfo == null) {
15945 return false;
15946 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015947 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015948 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015949 && !isHapticFeedbackEnabled()) {
15950 return false;
15951 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015952 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15953 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015954 }
15955
15956 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015957 * Request that the visibility of the status bar or other screen/window
15958 * decorations be changed.
15959 *
15960 * <p>This method is used to put the over device UI into temporary modes
15961 * where the user's attention is focused more on the application content,
15962 * by dimming or hiding surrounding system affordances. This is typically
15963 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15964 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15965 * to be placed behind the action bar (and with these flags other system
15966 * affordances) so that smooth transitions between hiding and showing them
15967 * can be done.
15968 *
15969 * <p>Two representative examples of the use of system UI visibility is
15970 * implementing a content browsing application (like a magazine reader)
15971 * and a video playing application.
15972 *
15973 * <p>The first code shows a typical implementation of a View in a content
15974 * browsing application. In this implementation, the application goes
15975 * into a content-oriented mode by hiding the status bar and action bar,
15976 * and putting the navigation elements into lights out mode. The user can
15977 * then interact with content while in this mode. Such an application should
15978 * provide an easy way for the user to toggle out of the mode (such as to
15979 * check information in the status bar or access notifications). In the
15980 * implementation here, this is done simply by tapping on the content.
15981 *
15982 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15983 * content}
15984 *
15985 * <p>This second code sample shows a typical implementation of a View
15986 * in a video playing application. In this situation, while the video is
15987 * playing the application would like to go into a complete full-screen mode,
15988 * to use as much of the display as possible for the video. When in this state
15989 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015990 * touching on the screen to pop the UI out of full screen mode. See
15991 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015992 *
15993 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15994 * content}
15995 *
15996 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15997 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15998 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15999 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016000 */
16001 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040016002 if (visibility != mSystemUiVisibility) {
16003 mSystemUiVisibility = visibility;
16004 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16005 mParent.recomputeViewAttributes(this);
16006 }
Joe Onorato664644d2011-01-23 17:53:23 -080016007 }
16008 }
16009
16010 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070016011 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
16012 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16013 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16014 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16015 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016016 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016017 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016018 return mSystemUiVisibility;
16019 }
16020
Scott Mainec6331b2011-05-24 16:55:56 -070016021 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016022 * Returns the current system UI visibility that is currently set for
16023 * the entire window. This is the combination of the
16024 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16025 * views in the window.
16026 */
16027 public int getWindowSystemUiVisibility() {
16028 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16029 }
16030
16031 /**
16032 * Override to find out when the window's requested system UI visibility
16033 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16034 * This is different from the callbacks recieved through
16035 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16036 * in that this is only telling you about the local request of the window,
16037 * not the actual values applied by the system.
16038 */
16039 public void onWindowSystemUiVisibilityChanged(int visible) {
16040 }
16041
16042 /**
16043 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16044 * the view hierarchy.
16045 */
16046 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16047 onWindowSystemUiVisibilityChanged(visible);
16048 }
16049
16050 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016051 * Set a listener to receive callbacks when the visibility of the system bar changes.
16052 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16053 */
Joe Onorato664644d2011-01-23 17:53:23 -080016054 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016055 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016056 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16057 mParent.recomputeViewAttributes(this);
16058 }
16059 }
16060
16061 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016062 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16063 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016064 */
16065 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016066 ListenerInfo li = mListenerInfo;
16067 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16068 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016069 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016070 }
16071 }
16072
Dianne Hackborncf675782012-05-10 15:07:24 -070016073 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016074 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16075 if (val != mSystemUiVisibility) {
16076 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016077 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016078 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016079 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016080 }
16081
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016082 /** @hide */
16083 public void setDisabledSystemUiVisibility(int flags) {
16084 if (mAttachInfo != null) {
16085 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16086 mAttachInfo.mDisabledSystemUiVisibility = flags;
16087 if (mParent != null) {
16088 mParent.recomputeViewAttributes(this);
16089 }
16090 }
16091 }
16092 }
16093
Joe Onorato664644d2011-01-23 17:53:23 -080016094 /**
Joe Malin32736f02011-01-19 16:14:20 -080016095 * Creates an image that the system displays during the drag and drop
16096 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16097 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16098 * appearance as the given View. The default also positions the center of the drag shadow
16099 * directly under the touch point. If no View is provided (the constructor with no parameters
16100 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16101 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16102 * default is an invisible drag shadow.
16103 * <p>
16104 * You are not required to use the View you provide to the constructor as the basis of the
16105 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16106 * anything you want as the drag shadow.
16107 * </p>
16108 * <p>
16109 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16110 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16111 * size and position of the drag shadow. It uses this data to construct a
16112 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16113 * so that your application can draw the shadow image in the Canvas.
16114 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016115 *
16116 * <div class="special reference">
16117 * <h3>Developer Guides</h3>
16118 * <p>For a guide to implementing drag and drop features, read the
16119 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16120 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016121 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016122 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016123 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016124
16125 /**
Joe Malin32736f02011-01-19 16:14:20 -080016126 * Constructs a shadow image builder based on a View. By default, the resulting drag
16127 * shadow will have the same appearance and dimensions as the View, with the touch point
16128 * over the center of the View.
16129 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016130 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016131 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016132 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016133 }
16134
Christopher Tate17ed60c2011-01-18 12:50:26 -080016135 /**
16136 * Construct a shadow builder object with no associated View. This
16137 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16138 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16139 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016140 * reference to any View object. If they are not overridden, then the result is an
16141 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016142 */
16143 public DragShadowBuilder() {
16144 mView = new WeakReference<View>(null);
16145 }
16146
16147 /**
16148 * Returns the View object that had been passed to the
16149 * {@link #View.DragShadowBuilder(View)}
16150 * constructor. If that View parameter was {@code null} or if the
16151 * {@link #View.DragShadowBuilder()}
16152 * constructor was used to instantiate the builder object, this method will return
16153 * null.
16154 *
16155 * @return The View object associate with this builder object.
16156 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016157 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016158 final public View getView() {
16159 return mView.get();
16160 }
16161
Christopher Tate2c095f32010-10-04 14:13:40 -070016162 /**
Joe Malin32736f02011-01-19 16:14:20 -080016163 * Provides the metrics for the shadow image. These include the dimensions of
16164 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016165 * be centered under the touch location while dragging.
16166 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016167 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016168 * same as the dimensions of the View itself and centers the shadow under
16169 * the touch point.
16170 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016171 *
Joe Malin32736f02011-01-19 16:14:20 -080016172 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16173 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16174 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16175 * image.
16176 *
16177 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16178 * shadow image that should be underneath the touch point during the drag and drop
16179 * operation. Your application must set {@link android.graphics.Point#x} to the
16180 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016181 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016182 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016183 final View view = mView.get();
16184 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016185 shadowSize.set(view.getWidth(), view.getHeight());
16186 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016187 } else {
16188 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16189 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016190 }
16191
16192 /**
Joe Malin32736f02011-01-19 16:14:20 -080016193 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16194 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016195 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016196 *
Joe Malin32736f02011-01-19 16:14:20 -080016197 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016198 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016199 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016200 final View view = mView.get();
16201 if (view != null) {
16202 view.draw(canvas);
16203 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016204 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016205 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016206 }
16207 }
16208
16209 /**
Joe Malin32736f02011-01-19 16:14:20 -080016210 * Starts a drag and drop operation. When your application calls this method, it passes a
16211 * {@link android.view.View.DragShadowBuilder} object to the system. The
16212 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16213 * to get metrics for the drag shadow, and then calls the object's
16214 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16215 * <p>
16216 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16217 * drag events to all the View objects in your application that are currently visible. It does
16218 * this either by calling the View object's drag listener (an implementation of
16219 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16220 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16221 * Both are passed a {@link android.view.DragEvent} object that has a
16222 * {@link android.view.DragEvent#getAction()} value of
16223 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16224 * </p>
16225 * <p>
16226 * Your application can invoke startDrag() on any attached View object. The View object does not
16227 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16228 * be related to the View the user selected for dragging.
16229 * </p>
16230 * @param data A {@link android.content.ClipData} object pointing to the data to be
16231 * transferred by the drag and drop operation.
16232 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16233 * drag shadow.
16234 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16235 * drop operation. This Object is put into every DragEvent object sent by the system during the
16236 * current drag.
16237 * <p>
16238 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16239 * to the target Views. For example, it can contain flags that differentiate between a
16240 * a copy operation and a move operation.
16241 * </p>
16242 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16243 * so the parameter should be set to 0.
16244 * @return {@code true} if the method completes successfully, or
16245 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16246 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016247 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016248 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016249 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016250 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016251 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016252 }
16253 boolean okay = false;
16254
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016255 Point shadowSize = new Point();
16256 Point shadowTouchPoint = new Point();
16257 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016258
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016259 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16260 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16261 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016262 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016263
Chris Tatea32dcf72010-10-14 12:13:50 -070016264 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016265 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16266 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016267 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016268 Surface surface = new Surface();
16269 try {
16270 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016271 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016272 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016273 + " surface=" + surface);
16274 if (token != null) {
16275 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016276 try {
Chris Tate6b391282010-10-14 15:48:59 -070016277 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016278 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016279 } finally {
16280 surface.unlockCanvasAndPost(canvas);
16281 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016282
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016283 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016284
16285 // Cache the local state object for delivery with DragEvents
16286 root.setLocalDragState(myLocalState);
16287
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016288 // repurpose 'shadowSize' for the last touch point
16289 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016290
Christopher Tatea53146c2010-09-07 11:57:52 -070016291 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016292 shadowSize.x, shadowSize.y,
16293 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016294 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016295
16296 // Off and running! Release our local surface instance; the drag
16297 // shadow surface is now managed by the system process.
16298 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016299 }
16300 } catch (Exception e) {
16301 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16302 surface.destroy();
16303 }
16304
16305 return okay;
16306 }
16307
Christopher Tatea53146c2010-09-07 11:57:52 -070016308 /**
Joe Malin32736f02011-01-19 16:14:20 -080016309 * Handles drag events sent by the system following a call to
16310 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16311 *<p>
16312 * When the system calls this method, it passes a
16313 * {@link android.view.DragEvent} object. A call to
16314 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16315 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16316 * operation.
16317 * @param event The {@link android.view.DragEvent} sent by the system.
16318 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16319 * in DragEvent, indicating the type of drag event represented by this object.
16320 * @return {@code true} if the method was successful, otherwise {@code false}.
16321 * <p>
16322 * The method should return {@code true} in response to an action type of
16323 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16324 * operation.
16325 * </p>
16326 * <p>
16327 * The method should also return {@code true} in response to an action type of
16328 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16329 * {@code false} if it didn't.
16330 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016331 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016332 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016333 return false;
16334 }
16335
16336 /**
Joe Malin32736f02011-01-19 16:14:20 -080016337 * Detects if this View is enabled and has a drag event listener.
16338 * If both are true, then it calls the drag event listener with the
16339 * {@link android.view.DragEvent} it received. If the drag event listener returns
16340 * {@code true}, then dispatchDragEvent() returns {@code true}.
16341 * <p>
16342 * For all other cases, the method calls the
16343 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16344 * method and returns its result.
16345 * </p>
16346 * <p>
16347 * This ensures that a drag event is always consumed, even if the View does not have a drag
16348 * event listener. However, if the View has a listener and the listener returns true, then
16349 * onDragEvent() is not called.
16350 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016351 */
16352 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016353 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016354 ListenerInfo li = mListenerInfo;
16355 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16356 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016357 return true;
16358 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016359 return onDragEvent(event);
16360 }
16361
Christopher Tate3d4bf172011-03-28 16:16:46 -070016362 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016363 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016364 }
16365
Christopher Tatea53146c2010-09-07 11:57:52 -070016366 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016367 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16368 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016369 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016370 */
16371 public void onCloseSystemDialogs(String reason) {
16372 }
Joe Malin32736f02011-01-19 16:14:20 -080016373
Dianne Hackbornffa42482009-09-23 22:20:11 -070016374 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016375 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016376 * update a Region being computed for
16377 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016378 * that any non-transparent parts of the Drawable are removed from the
16379 * given transparent region.
16380 *
16381 * @param dr The Drawable whose transparency is to be applied to the region.
16382 * @param region A Region holding the current transparency information,
16383 * where any parts of the region that are set are considered to be
16384 * transparent. On return, this region will be modified to have the
16385 * transparency information reduced by the corresponding parts of the
16386 * Drawable that are not transparent.
16387 * {@hide}
16388 */
16389 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16390 if (DBG) {
16391 Log.i("View", "Getting transparent region for: " + this);
16392 }
16393 final Region r = dr.getTransparentRegion();
16394 final Rect db = dr.getBounds();
16395 final AttachInfo attachInfo = mAttachInfo;
16396 if (r != null && attachInfo != null) {
16397 final int w = getRight()-getLeft();
16398 final int h = getBottom()-getTop();
16399 if (db.left > 0) {
16400 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16401 r.op(0, 0, db.left, h, Region.Op.UNION);
16402 }
16403 if (db.right < w) {
16404 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16405 r.op(db.right, 0, w, h, Region.Op.UNION);
16406 }
16407 if (db.top > 0) {
16408 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16409 r.op(0, 0, w, db.top, Region.Op.UNION);
16410 }
16411 if (db.bottom < h) {
16412 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16413 r.op(0, db.bottom, w, h, Region.Op.UNION);
16414 }
16415 final int[] location = attachInfo.mTransparentLocation;
16416 getLocationInWindow(location);
16417 r.translate(location[0], location[1]);
16418 region.op(r, Region.Op.INTERSECT);
16419 } else {
16420 region.op(db, Region.Op.DIFFERENCE);
16421 }
16422 }
16423
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016424 private void checkForLongClick(int delayOffset) {
16425 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16426 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016427
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016428 if (mPendingCheckForLongPress == null) {
16429 mPendingCheckForLongPress = new CheckForLongPress();
16430 }
16431 mPendingCheckForLongPress.rememberWindowAttachCount();
16432 postDelayed(mPendingCheckForLongPress,
16433 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016435 }
16436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016437 /**
16438 * Inflate a view from an XML resource. This convenience method wraps the {@link
16439 * LayoutInflater} class, which provides a full range of options for view inflation.
16440 *
16441 * @param context The Context object for your activity or application.
16442 * @param resource The resource ID to inflate
16443 * @param root A view group that will be the parent. Used to properly inflate the
16444 * layout_* parameters.
16445 * @see LayoutInflater
16446 */
16447 public static View inflate(Context context, int resource, ViewGroup root) {
16448 LayoutInflater factory = LayoutInflater.from(context);
16449 return factory.inflate(resource, root);
16450 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016452 /**
Adam Powell637d3372010-08-25 14:37:03 -070016453 * Scroll the view with standard behavior for scrolling beyond the normal
16454 * content boundaries. Views that call this method should override
16455 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16456 * results of an over-scroll operation.
16457 *
16458 * Views can use this method to handle any touch or fling-based scrolling.
16459 *
16460 * @param deltaX Change in X in pixels
16461 * @param deltaY Change in Y in pixels
16462 * @param scrollX Current X scroll value in pixels before applying deltaX
16463 * @param scrollY Current Y scroll value in pixels before applying deltaY
16464 * @param scrollRangeX Maximum content scroll range along the X axis
16465 * @param scrollRangeY Maximum content scroll range along the Y axis
16466 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16467 * along the X axis.
16468 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16469 * along the Y axis.
16470 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16471 * @return true if scrolling was clamped to an over-scroll boundary along either
16472 * axis, false otherwise.
16473 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016474 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016475 protected boolean overScrollBy(int deltaX, int deltaY,
16476 int scrollX, int scrollY,
16477 int scrollRangeX, int scrollRangeY,
16478 int maxOverScrollX, int maxOverScrollY,
16479 boolean isTouchEvent) {
16480 final int overScrollMode = mOverScrollMode;
16481 final boolean canScrollHorizontal =
16482 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16483 final boolean canScrollVertical =
16484 computeVerticalScrollRange() > computeVerticalScrollExtent();
16485 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16486 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16487 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16488 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16489
16490 int newScrollX = scrollX + deltaX;
16491 if (!overScrollHorizontal) {
16492 maxOverScrollX = 0;
16493 }
16494
16495 int newScrollY = scrollY + deltaY;
16496 if (!overScrollVertical) {
16497 maxOverScrollY = 0;
16498 }
16499
16500 // Clamp values if at the limits and record
16501 final int left = -maxOverScrollX;
16502 final int right = maxOverScrollX + scrollRangeX;
16503 final int top = -maxOverScrollY;
16504 final int bottom = maxOverScrollY + scrollRangeY;
16505
16506 boolean clampedX = false;
16507 if (newScrollX > right) {
16508 newScrollX = right;
16509 clampedX = true;
16510 } else if (newScrollX < left) {
16511 newScrollX = left;
16512 clampedX = true;
16513 }
16514
16515 boolean clampedY = false;
16516 if (newScrollY > bottom) {
16517 newScrollY = bottom;
16518 clampedY = true;
16519 } else if (newScrollY < top) {
16520 newScrollY = top;
16521 clampedY = true;
16522 }
16523
16524 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16525
16526 return clampedX || clampedY;
16527 }
16528
16529 /**
16530 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16531 * respond to the results of an over-scroll operation.
16532 *
16533 * @param scrollX New X scroll value in pixels
16534 * @param scrollY New Y scroll value in pixels
16535 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16536 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16537 */
16538 protected void onOverScrolled(int scrollX, int scrollY,
16539 boolean clampedX, boolean clampedY) {
16540 // Intentionally empty.
16541 }
16542
16543 /**
16544 * Returns the over-scroll mode for this view. The result will be
16545 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16546 * (allow over-scrolling only if the view content is larger than the container),
16547 * or {@link #OVER_SCROLL_NEVER}.
16548 *
16549 * @return This view's over-scroll mode.
16550 */
16551 public int getOverScrollMode() {
16552 return mOverScrollMode;
16553 }
16554
16555 /**
16556 * Set the over-scroll mode for this view. Valid over-scroll modes are
16557 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16558 * (allow over-scrolling only if the view content is larger than the container),
16559 * or {@link #OVER_SCROLL_NEVER}.
16560 *
16561 * Setting the over-scroll mode of a view will have an effect only if the
16562 * view is capable of scrolling.
16563 *
16564 * @param overScrollMode The new over-scroll mode for this view.
16565 */
16566 public void setOverScrollMode(int overScrollMode) {
16567 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16568 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16569 overScrollMode != OVER_SCROLL_NEVER) {
16570 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16571 }
16572 mOverScrollMode = overScrollMode;
16573 }
16574
16575 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016576 * Gets a scale factor that determines the distance the view should scroll
16577 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16578 * @return The vertical scroll scale factor.
16579 * @hide
16580 */
16581 protected float getVerticalScrollFactor() {
16582 if (mVerticalScrollFactor == 0) {
16583 TypedValue outValue = new TypedValue();
16584 if (!mContext.getTheme().resolveAttribute(
16585 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16586 throw new IllegalStateException(
16587 "Expected theme to define listPreferredItemHeight.");
16588 }
16589 mVerticalScrollFactor = outValue.getDimension(
16590 mContext.getResources().getDisplayMetrics());
16591 }
16592 return mVerticalScrollFactor;
16593 }
16594
16595 /**
16596 * Gets a scale factor that determines the distance the view should scroll
16597 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16598 * @return The horizontal scroll scale factor.
16599 * @hide
16600 */
16601 protected float getHorizontalScrollFactor() {
16602 // TODO: Should use something else.
16603 return getVerticalScrollFactor();
16604 }
16605
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016606 /**
16607 * Return the value specifying the text direction or policy that was set with
16608 * {@link #setTextDirection(int)}.
16609 *
16610 * @return the defined text direction. It can be one of:
16611 *
16612 * {@link #TEXT_DIRECTION_INHERIT},
16613 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16614 * {@link #TEXT_DIRECTION_ANY_RTL},
16615 * {@link #TEXT_DIRECTION_LTR},
16616 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016617 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016618 *
16619 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016620 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016621 @ViewDebug.ExportedProperty(category = "text", mapping = {
16622 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16623 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16624 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16625 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16626 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16627 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16628 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016629 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016630 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016631 }
16632
16633 /**
16634 * Set the text direction.
16635 *
16636 * @param textDirection the direction to set. Should be one of:
16637 *
16638 * {@link #TEXT_DIRECTION_INHERIT},
16639 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16640 * {@link #TEXT_DIRECTION_ANY_RTL},
16641 * {@link #TEXT_DIRECTION_LTR},
16642 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016643 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016644 *
16645 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16646 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16647 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016648 */
16649 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016650 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016651 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016652 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016653 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016654 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016655 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016656 // Do resolution
16657 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016658 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016659 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016660 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016661 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016662 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016663 }
16664 }
16665
16666 /**
16667 * Return the resolved text direction.
16668 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016669 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016670 *
Doug Feltcb3791202011-07-07 11:57:48 -070016671 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16672 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016673 * {@link #TEXT_DIRECTION_LTR},
16674 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016675 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016676 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016677 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016678 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016679 }
16680
16681 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016682 * Resolve the text direction.
16683 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016684 * @return true if resolution has been done, false otherwise.
16685 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016686 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016687 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016688 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016689 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016690 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016691
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016692 if (hasRtlSupport()) {
16693 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016694 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016695 switch(textDirection) {
16696 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016697 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016698 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016699 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016700 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016701 return false;
16702 }
16703
16704 View parent = ((View) mParent);
16705 // Parent has not yet resolved, so we still return the default
16706 if (!parent.isTextDirectionResolved()) {
16707 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16708 // Resolution will need to happen again later
16709 return false;
16710 }
16711
16712 // Set current resolved direction to the same value as the parent's one
16713 final int parentResolvedDirection = parent.getTextDirection();
16714 switch (parentResolvedDirection) {
16715 case TEXT_DIRECTION_FIRST_STRONG:
16716 case TEXT_DIRECTION_ANY_RTL:
16717 case TEXT_DIRECTION_LTR:
16718 case TEXT_DIRECTION_RTL:
16719 case TEXT_DIRECTION_LOCALE:
16720 mPrivateFlags2 |=
16721 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16722 break;
16723 default:
16724 // Default resolved direction is "first strong" heuristic
16725 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016726 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016727 break;
16728 case TEXT_DIRECTION_FIRST_STRONG:
16729 case TEXT_DIRECTION_ANY_RTL:
16730 case TEXT_DIRECTION_LTR:
16731 case TEXT_DIRECTION_RTL:
16732 case TEXT_DIRECTION_LOCALE:
16733 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016734 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016735 break;
16736 default:
16737 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016738 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016739 }
16740 } else {
16741 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016742 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016743 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016744
16745 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016746 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016747 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016748 }
16749
16750 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016751 * Check if text direction resolution can be done.
16752 *
16753 * @return true if text direction resolution can be done otherwise return false.
16754 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016755 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016756 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016757 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016758 return (mParent != null) && (mParent instanceof View) &&
16759 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016760 default:
16761 return true;
16762 }
16763 }
16764
16765 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016766 * Reset resolved text direction. Text direction will be resolved during a call to
16767 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016768 *
16769 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016770 */
16771 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016772 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016773 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016774 // Set to default value
16775 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016776 }
16777
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016778 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016779 * @return true if text direction is inherited.
16780 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016781 * @hide
16782 */
16783 public boolean isTextDirectionInherited() {
16784 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16785 }
16786
16787 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016788 * @return true if text direction is resolved.
16789 */
16790 private boolean isTextDirectionResolved() {
16791 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16792 }
16793
16794 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016795 * Return the value specifying the text alignment or policy that was set with
16796 * {@link #setTextAlignment(int)}.
16797 *
16798 * @return the defined text alignment. It can be one of:
16799 *
16800 * {@link #TEXT_ALIGNMENT_INHERIT},
16801 * {@link #TEXT_ALIGNMENT_GRAVITY},
16802 * {@link #TEXT_ALIGNMENT_CENTER},
16803 * {@link #TEXT_ALIGNMENT_TEXT_START},
16804 * {@link #TEXT_ALIGNMENT_TEXT_END},
16805 * {@link #TEXT_ALIGNMENT_VIEW_START},
16806 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016807 *
16808 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016809 */
16810 @ViewDebug.ExportedProperty(category = "text", mapping = {
16811 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16812 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16813 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16814 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16815 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16816 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16817 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16818 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016819 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016820 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016821 }
16822
16823 /**
16824 * Set the text alignment.
16825 *
16826 * @param textAlignment The text alignment to set. Should be one of
16827 *
16828 * {@link #TEXT_ALIGNMENT_INHERIT},
16829 * {@link #TEXT_ALIGNMENT_GRAVITY},
16830 * {@link #TEXT_ALIGNMENT_CENTER},
16831 * {@link #TEXT_ALIGNMENT_TEXT_START},
16832 * {@link #TEXT_ALIGNMENT_TEXT_END},
16833 * {@link #TEXT_ALIGNMENT_VIEW_START},
16834 * {@link #TEXT_ALIGNMENT_VIEW_END}
16835 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016836 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16837 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16838 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16839 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016840 * @attr ref android.R.styleable#View_textAlignment
16841 */
16842 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016843 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016844 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016845 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016846 resetResolvedTextAlignment();
16847 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016848 mPrivateFlags2 |=
16849 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16850 // Do resolution
16851 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016852 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016853 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016854 // Refresh
16855 requestLayout();
16856 invalidate(true);
16857 }
16858 }
16859
16860 /**
16861 * Return the resolved text alignment.
16862 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016863 * @return the resolved text alignment. Returns one of:
16864 *
16865 * {@link #TEXT_ALIGNMENT_GRAVITY},
16866 * {@link #TEXT_ALIGNMENT_CENTER},
16867 * {@link #TEXT_ALIGNMENT_TEXT_START},
16868 * {@link #TEXT_ALIGNMENT_TEXT_END},
16869 * {@link #TEXT_ALIGNMENT_VIEW_START},
16870 * {@link #TEXT_ALIGNMENT_VIEW_END}
16871 */
16872 @ViewDebug.ExportedProperty(category = "text", mapping = {
16873 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16874 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16875 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16876 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16877 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16878 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16879 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16880 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016881 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016882 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16883 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016884 }
16885
16886 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016887 * Resolve the text alignment.
16888 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016889 * @return true if resolution has been done, false otherwise.
16890 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016891 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016892 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016893 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016894 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016895 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016896
16897 if (hasRtlSupport()) {
16898 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016899 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016900 switch (textAlignment) {
16901 case TEXT_ALIGNMENT_INHERIT:
16902 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016903 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016904 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016905 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016906 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016907 return false;
16908 }
16909 View parent = (View) mParent;
16910
16911 // Parent has not yet resolved, so we still return the default
16912 if (!parent.isTextAlignmentResolved()) {
16913 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16914 // Resolution will need to happen again later
16915 return false;
16916 }
16917
16918 final int parentResolvedTextAlignment = parent.getTextAlignment();
16919 switch (parentResolvedTextAlignment) {
16920 case TEXT_ALIGNMENT_GRAVITY:
16921 case TEXT_ALIGNMENT_TEXT_START:
16922 case TEXT_ALIGNMENT_TEXT_END:
16923 case TEXT_ALIGNMENT_CENTER:
16924 case TEXT_ALIGNMENT_VIEW_START:
16925 case TEXT_ALIGNMENT_VIEW_END:
16926 // Resolved text alignment is the same as the parent resolved
16927 // text alignment
16928 mPrivateFlags2 |=
16929 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16930 break;
16931 default:
16932 // Use default resolved text alignment
16933 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016934 }
16935 break;
16936 case TEXT_ALIGNMENT_GRAVITY:
16937 case TEXT_ALIGNMENT_TEXT_START:
16938 case TEXT_ALIGNMENT_TEXT_END:
16939 case TEXT_ALIGNMENT_CENTER:
16940 case TEXT_ALIGNMENT_VIEW_START:
16941 case TEXT_ALIGNMENT_VIEW_END:
16942 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016943 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016944 break;
16945 default:
16946 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016947 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016948 }
16949 } else {
16950 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016951 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016952 }
16953
16954 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016955 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016956 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016957 }
16958
16959 /**
16960 * Check if text alignment resolution can be done.
16961 *
16962 * @return true if text alignment resolution can be done otherwise return false.
16963 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016964 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016965 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016966 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016967 return (mParent != null) && (mParent instanceof View) &&
16968 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016969 default:
16970 return true;
16971 }
16972 }
16973
16974 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016975 * Reset resolved text alignment. Text alignment will be resolved during a call to
16976 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016977 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016978 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016979 */
16980 public void resetResolvedTextAlignment() {
16981 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016982 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016983 // Set to default
16984 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016985 }
16986
Adam Powella9108a22012-07-18 11:18:09 -070016987 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016988 * @return true if text alignment is inherited.
16989 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016990 * @hide
16991 */
16992 public boolean isTextAlignmentInherited() {
16993 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16994 }
16995
16996 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016997 * @return true if text alignment is resolved.
16998 */
16999 private boolean isTextAlignmentResolved() {
17000 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
17001 }
17002
17003 /**
Adam Powella9108a22012-07-18 11:18:09 -070017004 * Generate a value suitable for use in {@link #setId(int)}.
17005 * This value will not collide with ID values generated at build time by aapt for R.id.
17006 *
17007 * @return a generated ID value
17008 */
17009 public static int generateViewId() {
17010 for (;;) {
17011 final int result = sNextGeneratedId.get();
17012 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17013 int newValue = result + 1;
17014 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17015 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17016 return result;
17017 }
17018 }
17019 }
17020
Chet Haaseb39f0512011-05-24 14:36:40 -070017021 //
17022 // Properties
17023 //
17024 /**
17025 * A Property wrapper around the <code>alpha</code> functionality handled by the
17026 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17027 */
Chet Haased47f1532011-12-16 11:18:52 -080017028 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017029 @Override
17030 public void setValue(View object, float value) {
17031 object.setAlpha(value);
17032 }
17033
17034 @Override
17035 public Float get(View object) {
17036 return object.getAlpha();
17037 }
17038 };
17039
17040 /**
17041 * A Property wrapper around the <code>translationX</code> functionality handled by the
17042 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17043 */
Chet Haased47f1532011-12-16 11:18:52 -080017044 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017045 @Override
17046 public void setValue(View object, float value) {
17047 object.setTranslationX(value);
17048 }
17049
17050 @Override
17051 public Float get(View object) {
17052 return object.getTranslationX();
17053 }
17054 };
17055
17056 /**
17057 * A Property wrapper around the <code>translationY</code> functionality handled by the
17058 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17059 */
Chet Haased47f1532011-12-16 11:18:52 -080017060 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017061 @Override
17062 public void setValue(View object, float value) {
17063 object.setTranslationY(value);
17064 }
17065
17066 @Override
17067 public Float get(View object) {
17068 return object.getTranslationY();
17069 }
17070 };
17071
17072 /**
17073 * A Property wrapper around the <code>x</code> functionality handled by the
17074 * {@link View#setX(float)} and {@link View#getX()} methods.
17075 */
Chet Haased47f1532011-12-16 11:18:52 -080017076 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017077 @Override
17078 public void setValue(View object, float value) {
17079 object.setX(value);
17080 }
17081
17082 @Override
17083 public Float get(View object) {
17084 return object.getX();
17085 }
17086 };
17087
17088 /**
17089 * A Property wrapper around the <code>y</code> functionality handled by the
17090 * {@link View#setY(float)} and {@link View#getY()} methods.
17091 */
Chet Haased47f1532011-12-16 11:18:52 -080017092 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017093 @Override
17094 public void setValue(View object, float value) {
17095 object.setY(value);
17096 }
17097
17098 @Override
17099 public Float get(View object) {
17100 return object.getY();
17101 }
17102 };
17103
17104 /**
17105 * A Property wrapper around the <code>rotation</code> functionality handled by the
17106 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17107 */
Chet Haased47f1532011-12-16 11:18:52 -080017108 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017109 @Override
17110 public void setValue(View object, float value) {
17111 object.setRotation(value);
17112 }
17113
17114 @Override
17115 public Float get(View object) {
17116 return object.getRotation();
17117 }
17118 };
17119
17120 /**
17121 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17122 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17123 */
Chet Haased47f1532011-12-16 11:18:52 -080017124 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017125 @Override
17126 public void setValue(View object, float value) {
17127 object.setRotationX(value);
17128 }
17129
17130 @Override
17131 public Float get(View object) {
17132 return object.getRotationX();
17133 }
17134 };
17135
17136 /**
17137 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17138 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17139 */
Chet Haased47f1532011-12-16 11:18:52 -080017140 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017141 @Override
17142 public void setValue(View object, float value) {
17143 object.setRotationY(value);
17144 }
17145
17146 @Override
17147 public Float get(View object) {
17148 return object.getRotationY();
17149 }
17150 };
17151
17152 /**
17153 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17154 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17155 */
Chet Haased47f1532011-12-16 11:18:52 -080017156 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017157 @Override
17158 public void setValue(View object, float value) {
17159 object.setScaleX(value);
17160 }
17161
17162 @Override
17163 public Float get(View object) {
17164 return object.getScaleX();
17165 }
17166 };
17167
17168 /**
17169 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17170 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17171 */
Chet Haased47f1532011-12-16 11:18:52 -080017172 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017173 @Override
17174 public void setValue(View object, float value) {
17175 object.setScaleY(value);
17176 }
17177
17178 @Override
17179 public Float get(View object) {
17180 return object.getScaleY();
17181 }
17182 };
17183
Jeff Brown33bbfd22011-02-24 20:55:35 -080017184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017185 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17186 * Each MeasureSpec represents a requirement for either the width or the height.
17187 * A MeasureSpec is comprised of a size and a mode. There are three possible
17188 * modes:
17189 * <dl>
17190 * <dt>UNSPECIFIED</dt>
17191 * <dd>
17192 * The parent has not imposed any constraint on the child. It can be whatever size
17193 * it wants.
17194 * </dd>
17195 *
17196 * <dt>EXACTLY</dt>
17197 * <dd>
17198 * The parent has determined an exact size for the child. The child is going to be
17199 * given those bounds regardless of how big it wants to be.
17200 * </dd>
17201 *
17202 * <dt>AT_MOST</dt>
17203 * <dd>
17204 * The child can be as large as it wants up to the specified size.
17205 * </dd>
17206 * </dl>
17207 *
17208 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17209 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17210 */
17211 public static class MeasureSpec {
17212 private static final int MODE_SHIFT = 30;
17213 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17214
17215 /**
17216 * Measure specification mode: The parent has not imposed any constraint
17217 * on the child. It can be whatever size it wants.
17218 */
17219 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17220
17221 /**
17222 * Measure specification mode: The parent has determined an exact size
17223 * for the child. The child is going to be given those bounds regardless
17224 * of how big it wants to be.
17225 */
17226 public static final int EXACTLY = 1 << MODE_SHIFT;
17227
17228 /**
17229 * Measure specification mode: The child can be as large as it wants up
17230 * to the specified size.
17231 */
17232 public static final int AT_MOST = 2 << MODE_SHIFT;
17233
17234 /**
17235 * Creates a measure specification based on the supplied size and mode.
17236 *
17237 * The mode must always be one of the following:
17238 * <ul>
17239 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17240 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17241 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17242 * </ul>
17243 *
17244 * @param size the size of the measure specification
17245 * @param mode the mode of the measure specification
17246 * @return the measure specification based on size and mode
17247 */
17248 public static int makeMeasureSpec(int size, int mode) {
Adam Powell946d05b2012-10-01 15:06:20 -070017249 return size + mode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017250 }
17251
17252 /**
17253 * Extracts the mode from the supplied measure specification.
17254 *
17255 * @param measureSpec the measure specification to extract the mode from
17256 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17257 * {@link android.view.View.MeasureSpec#AT_MOST} or
17258 * {@link android.view.View.MeasureSpec#EXACTLY}
17259 */
17260 public static int getMode(int measureSpec) {
17261 return (measureSpec & MODE_MASK);
17262 }
17263
17264 /**
17265 * Extracts the size from the supplied measure specification.
17266 *
17267 * @param measureSpec the measure specification to extract the size from
17268 * @return the size in pixels defined in the supplied measure specification
17269 */
17270 public static int getSize(int measureSpec) {
17271 return (measureSpec & ~MODE_MASK);
17272 }
17273
17274 /**
17275 * Returns a String representation of the specified measure
17276 * specification.
17277 *
17278 * @param measureSpec the measure specification to convert to a String
17279 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17280 */
17281 public static String toString(int measureSpec) {
17282 int mode = getMode(measureSpec);
17283 int size = getSize(measureSpec);
17284
17285 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17286
17287 if (mode == UNSPECIFIED)
17288 sb.append("UNSPECIFIED ");
17289 else if (mode == EXACTLY)
17290 sb.append("EXACTLY ");
17291 else if (mode == AT_MOST)
17292 sb.append("AT_MOST ");
17293 else
17294 sb.append(mode).append(" ");
17295
17296 sb.append(size);
17297 return sb.toString();
17298 }
17299 }
17300
17301 class CheckForLongPress implements Runnable {
17302
17303 private int mOriginalWindowAttachCount;
17304
17305 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017306 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017307 && mOriginalWindowAttachCount == mWindowAttachCount) {
17308 if (performLongClick()) {
17309 mHasPerformedLongPress = true;
17310 }
17311 }
17312 }
17313
17314 public void rememberWindowAttachCount() {
17315 mOriginalWindowAttachCount = mWindowAttachCount;
17316 }
17317 }
Joe Malin32736f02011-01-19 16:14:20 -080017318
Adam Powelle14579b2009-12-16 18:39:52 -080017319 private final class CheckForTap implements Runnable {
17320 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017321 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017322 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017323 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017324 }
17325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017326
Adam Powella35d7682010-03-12 14:48:13 -080017327 private final class PerformClick implements Runnable {
17328 public void run() {
17329 performClick();
17330 }
17331 }
17332
Dianne Hackborn63042d62011-01-26 18:56:29 -080017333 /** @hide */
17334 public void hackTurnOffWindowResizeAnim(boolean off) {
17335 mAttachInfo.mTurnOffWindowResizeAnim = off;
17336 }
Joe Malin32736f02011-01-19 16:14:20 -080017337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017338 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017339 * This method returns a ViewPropertyAnimator object, which can be used to animate
17340 * specific properties on this View.
17341 *
17342 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17343 */
17344 public ViewPropertyAnimator animate() {
17345 if (mAnimator == null) {
17346 mAnimator = new ViewPropertyAnimator(this);
17347 }
17348 return mAnimator;
17349 }
17350
17351 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017352 * Interface definition for a callback to be invoked when a hardware key event is
17353 * dispatched to this view. The callback will be invoked before the key event is
17354 * given to the view. This is only useful for hardware keyboards; a software input
17355 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017356 */
17357 public interface OnKeyListener {
17358 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017359 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017360 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017361 * <p>Key presses in software keyboards will generally NOT trigger this method,
17362 * although some may elect to do so in some situations. Do not assume a
17363 * software input method has to be key-based; even if it is, it may use key presses
17364 * in a different way than you expect, so there is no way to reliably catch soft
17365 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017366 *
17367 * @param v The view the key has been dispatched to.
17368 * @param keyCode The code for the physical key that was pressed
17369 * @param event The KeyEvent object containing full information about
17370 * the event.
17371 * @return True if the listener has consumed the event, false otherwise.
17372 */
17373 boolean onKey(View v, int keyCode, KeyEvent event);
17374 }
17375
17376 /**
17377 * Interface definition for a callback to be invoked when a touch event is
17378 * dispatched to this view. The callback will be invoked before the touch
17379 * event is given to the view.
17380 */
17381 public interface OnTouchListener {
17382 /**
17383 * Called when a touch event is dispatched to a view. This allows listeners to
17384 * get a chance to respond before the target view.
17385 *
17386 * @param v The view the touch event has been dispatched to.
17387 * @param event The MotionEvent object containing full information about
17388 * the event.
17389 * @return True if the listener has consumed the event, false otherwise.
17390 */
17391 boolean onTouch(View v, MotionEvent event);
17392 }
17393
17394 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017395 * Interface definition for a callback to be invoked when a hover event is
17396 * dispatched to this view. The callback will be invoked before the hover
17397 * event is given to the view.
17398 */
17399 public interface OnHoverListener {
17400 /**
17401 * Called when a hover event is dispatched to a view. This allows listeners to
17402 * get a chance to respond before the target view.
17403 *
17404 * @param v The view the hover event has been dispatched to.
17405 * @param event The MotionEvent object containing full information about
17406 * the event.
17407 * @return True if the listener has consumed the event, false otherwise.
17408 */
17409 boolean onHover(View v, MotionEvent event);
17410 }
17411
17412 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017413 * Interface definition for a callback to be invoked when a generic motion event is
17414 * dispatched to this view. The callback will be invoked before the generic motion
17415 * event is given to the view.
17416 */
17417 public interface OnGenericMotionListener {
17418 /**
17419 * Called when a generic motion event is dispatched to a view. This allows listeners to
17420 * get a chance to respond before the target view.
17421 *
17422 * @param v The view the generic motion event has been dispatched to.
17423 * @param event The MotionEvent object containing full information about
17424 * the event.
17425 * @return True if the listener has consumed the event, false otherwise.
17426 */
17427 boolean onGenericMotion(View v, MotionEvent event);
17428 }
17429
17430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017431 * Interface definition for a callback to be invoked when a view has been clicked and held.
17432 */
17433 public interface OnLongClickListener {
17434 /**
17435 * Called when a view has been clicked and held.
17436 *
17437 * @param v The view that was clicked and held.
17438 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017439 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017440 */
17441 boolean onLongClick(View v);
17442 }
17443
17444 /**
Chris Tate32affef2010-10-18 15:29:21 -070017445 * Interface definition for a callback to be invoked when a drag is being dispatched
17446 * to this view. The callback will be invoked before the hosting view's own
17447 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17448 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017449 *
17450 * <div class="special reference">
17451 * <h3>Developer Guides</h3>
17452 * <p>For a guide to implementing drag and drop features, read the
17453 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17454 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017455 */
17456 public interface OnDragListener {
17457 /**
17458 * Called when a drag event is dispatched to a view. This allows listeners
17459 * to get a chance to override base View behavior.
17460 *
Joe Malin32736f02011-01-19 16:14:20 -080017461 * @param v The View that received the drag event.
17462 * @param event The {@link android.view.DragEvent} object for the drag event.
17463 * @return {@code true} if the drag event was handled successfully, or {@code false}
17464 * if the drag event was not handled. Note that {@code false} will trigger the View
17465 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017466 */
17467 boolean onDrag(View v, DragEvent event);
17468 }
17469
17470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017471 * Interface definition for a callback to be invoked when the focus state of
17472 * a view changed.
17473 */
17474 public interface OnFocusChangeListener {
17475 /**
17476 * Called when the focus state of a view has changed.
17477 *
17478 * @param v The view whose state has changed.
17479 * @param hasFocus The new focus state of v.
17480 */
17481 void onFocusChange(View v, boolean hasFocus);
17482 }
17483
17484 /**
17485 * Interface definition for a callback to be invoked when a view is clicked.
17486 */
17487 public interface OnClickListener {
17488 /**
17489 * Called when a view has been clicked.
17490 *
17491 * @param v The view that was clicked.
17492 */
17493 void onClick(View v);
17494 }
17495
17496 /**
17497 * Interface definition for a callback to be invoked when the context menu
17498 * for this view is being built.
17499 */
17500 public interface OnCreateContextMenuListener {
17501 /**
17502 * Called when the context menu for this view is being built. It is not
17503 * safe to hold onto the menu after this method returns.
17504 *
17505 * @param menu The context menu that is being built
17506 * @param v The view for which the context menu is being built
17507 * @param menuInfo Extra information about the item for which the
17508 * context menu should be shown. This information will vary
17509 * depending on the class of v.
17510 */
17511 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17512 }
17513
Joe Onorato664644d2011-01-23 17:53:23 -080017514 /**
17515 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017516 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017517 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017518 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017519 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017520 */
17521 public interface OnSystemUiVisibilityChangeListener {
17522 /**
17523 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017524 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017525 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017526 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17527 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17528 * This tells you the <strong>global</strong> state of these UI visibility
17529 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017530 */
17531 public void onSystemUiVisibilityChange(int visibility);
17532 }
17533
Adam Powell4afd62b2011-02-18 15:02:18 -080017534 /**
17535 * Interface definition for a callback to be invoked when this view is attached
17536 * or detached from its window.
17537 */
17538 public interface OnAttachStateChangeListener {
17539 /**
17540 * Called when the view is attached to a window.
17541 * @param v The view that was attached
17542 */
17543 public void onViewAttachedToWindow(View v);
17544 /**
17545 * Called when the view is detached from a window.
17546 * @param v The view that was detached
17547 */
17548 public void onViewDetachedFromWindow(View v);
17549 }
17550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017551 private final class UnsetPressedState implements Runnable {
17552 public void run() {
17553 setPressed(false);
17554 }
17555 }
17556
17557 /**
17558 * Base class for derived classes that want to save and restore their own
17559 * state in {@link android.view.View#onSaveInstanceState()}.
17560 */
17561 public static class BaseSavedState extends AbsSavedState {
17562 /**
17563 * Constructor used when reading from a parcel. Reads the state of the superclass.
17564 *
17565 * @param source
17566 */
17567 public BaseSavedState(Parcel source) {
17568 super(source);
17569 }
17570
17571 /**
17572 * Constructor called by derived classes when creating their SavedState objects
17573 *
17574 * @param superState The state of the superclass of this view
17575 */
17576 public BaseSavedState(Parcelable superState) {
17577 super(superState);
17578 }
17579
17580 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17581 new Parcelable.Creator<BaseSavedState>() {
17582 public BaseSavedState createFromParcel(Parcel in) {
17583 return new BaseSavedState(in);
17584 }
17585
17586 public BaseSavedState[] newArray(int size) {
17587 return new BaseSavedState[size];
17588 }
17589 };
17590 }
17591
17592 /**
17593 * A set of information given to a view when it is attached to its parent
17594 * window.
17595 */
17596 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017597 interface Callbacks {
17598 void playSoundEffect(int effectId);
17599 boolean performHapticFeedback(int effectId, boolean always);
17600 }
17601
17602 /**
17603 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17604 * to a Handler. This class contains the target (View) to invalidate and
17605 * the coordinates of the dirty rectangle.
17606 *
17607 * For performance purposes, this class also implements a pool of up to
17608 * POOL_LIMIT objects that get reused. This reduces memory allocations
17609 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017610 */
Romain Guyd928d682009-03-31 17:52:16 -070017611 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017612 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017613 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17614 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017615 public InvalidateInfo newInstance() {
17616 return new InvalidateInfo();
17617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017618
Romain Guyd928d682009-03-31 17:52:16 -070017619 public void onAcquired(InvalidateInfo element) {
17620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017621
Romain Guyd928d682009-03-31 17:52:16 -070017622 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017623 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017624 }
17625 }, POOL_LIMIT)
17626 );
17627
17628 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017629 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017630
17631 View target;
17632
17633 int left;
17634 int top;
17635 int right;
17636 int bottom;
17637
Romain Guyd928d682009-03-31 17:52:16 -070017638 public void setNextPoolable(InvalidateInfo element) {
17639 mNext = element;
17640 }
17641
17642 public InvalidateInfo getNextPoolable() {
17643 return mNext;
17644 }
17645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017646 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017647 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017648 }
17649
17650 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017651 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017652 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017653
17654 public boolean isPooled() {
17655 return mIsPooled;
17656 }
17657
17658 public void setPooled(boolean isPooled) {
17659 mIsPooled = isPooled;
17660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017661 }
17662
17663 final IWindowSession mSession;
17664
17665 final IWindow mWindow;
17666
17667 final IBinder mWindowToken;
17668
Jeff Brown98365d72012-08-19 20:30:52 -070017669 final Display mDisplay;
17670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017671 final Callbacks mRootCallbacks;
17672
Romain Guy59a12ca2011-06-09 17:48:21 -070017673 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017675 /**
17676 * The top view of the hierarchy.
17677 */
17678 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017680 IBinder mPanelParentWindowToken;
17681 Surface mSurface;
17682
Romain Guyb051e892010-09-28 19:09:36 -070017683 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017684 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017685 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017686
Romain Guy7e4e5612012-03-05 14:37:29 -080017687 boolean mScreenOn;
17688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017689 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017690 * Scale factor used by the compatibility mode
17691 */
17692 float mApplicationScale;
17693
17694 /**
17695 * Indicates whether the application is in compatibility mode
17696 */
17697 boolean mScalingRequired;
17698
17699 /**
Romain Guy3d1728c2012-10-31 20:31:58 -070017700 * If set, ViewRootImpl doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017701 */
17702 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017703
Dianne Hackborn63042d62011-01-26 18:56:29 -080017704 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017705 * Left position of this view's window
17706 */
17707 int mWindowLeft;
17708
17709 /**
17710 * Top position of this view's window
17711 */
17712 int mWindowTop;
17713
17714 /**
Adam Powell26153a32010-11-08 15:22:27 -080017715 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017716 */
Adam Powell26153a32010-11-08 15:22:27 -080017717 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017718
17719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017720 * For windows that are full-screen but using insets to layout inside
17721 * of the screen decorations, these are the current insets for the
17722 * content of the window.
17723 */
17724 final Rect mContentInsets = new Rect();
17725
17726 /**
17727 * For windows that are full-screen but using insets to layout inside
17728 * of the screen decorations, these are the current insets for the
17729 * actual visible parts of the window.
17730 */
17731 final Rect mVisibleInsets = new Rect();
17732
17733 /**
17734 * The internal insets given by this window. This value is
17735 * supplied by the client (through
17736 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17737 * be given to the window manager when changed to be used in laying
17738 * out windows behind it.
17739 */
17740 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17741 = new ViewTreeObserver.InternalInsetsInfo();
17742
17743 /**
17744 * All views in the window's hierarchy that serve as scroll containers,
17745 * used to determine if the window can be resized or must be panned
17746 * to adjust for a soft input area.
17747 */
17748 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17749
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017750 final KeyEvent.DispatcherState mKeyDispatchState
17751 = new KeyEvent.DispatcherState();
17752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017753 /**
17754 * Indicates whether the view's window currently has the focus.
17755 */
17756 boolean mHasWindowFocus;
17757
17758 /**
17759 * The current visibility of the window.
17760 */
17761 int mWindowVisibility;
17762
17763 /**
17764 * Indicates the time at which drawing started to occur.
17765 */
17766 long mDrawingTime;
17767
17768 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017769 * Indicates whether or not ignoring the DIRTY_MASK flags.
17770 */
17771 boolean mIgnoreDirtyState;
17772
17773 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017774 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17775 * to avoid clearing that flag prematurely.
17776 */
17777 boolean mSetIgnoreDirtyState = false;
17778
17779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017780 * Indicates whether the view's window is currently in touch mode.
17781 */
17782 boolean mInTouchMode;
17783
17784 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017785 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017786 * the next time it performs a traversal
17787 */
17788 boolean mRecomputeGlobalAttributes;
17789
17790 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017791 * Always report new attributes at next traversal.
17792 */
17793 boolean mForceReportNewAttributes;
17794
17795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017796 * Set during a traveral if any views want to keep the screen on.
17797 */
17798 boolean mKeepScreenOn;
17799
17800 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017801 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17802 */
17803 int mSystemUiVisibility;
17804
17805 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017806 * Hack to force certain system UI visibility flags to be cleared.
17807 */
17808 int mDisabledSystemUiVisibility;
17809
17810 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017811 * Last global system UI visibility reported by the window manager.
17812 */
17813 int mGlobalSystemUiVisibility;
17814
17815 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017816 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17817 * attached.
17818 */
17819 boolean mHasSystemUiListeners;
17820
17821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017822 * Set if the visibility of any views has changed.
17823 */
17824 boolean mViewVisibilityChanged;
17825
17826 /**
17827 * Set to true if a view has been scrolled.
17828 */
17829 boolean mViewScrollChanged;
17830
17831 /**
17832 * Global to the view hierarchy used as a temporary for dealing with
17833 * x/y points in the transparent region computations.
17834 */
17835 final int[] mTransparentLocation = new int[2];
17836
17837 /**
17838 * Global to the view hierarchy used as a temporary for dealing with
17839 * x/y points in the ViewGroup.invalidateChild implementation.
17840 */
17841 final int[] mInvalidateChildLocation = new int[2];
17842
Chet Haasec3aa3612010-06-17 08:50:37 -070017843
17844 /**
17845 * Global to the view hierarchy used as a temporary for dealing with
17846 * x/y location when view is transformed.
17847 */
17848 final float[] mTmpTransformLocation = new float[2];
17849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017850 /**
17851 * The view tree observer used to dispatch global events like
17852 * layout, pre-draw, touch mode change, etc.
17853 */
17854 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17855
17856 /**
17857 * A Canvas used by the view hierarchy to perform bitmap caching.
17858 */
17859 Canvas mCanvas;
17860
17861 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017862 * The view root impl.
17863 */
17864 final ViewRootImpl mViewRootImpl;
17865
17866 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017867 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017868 * handler can be used to pump events in the UI events queue.
17869 */
17870 final Handler mHandler;
17871
17872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017873 * Temporary for use in computing invalidate rectangles while
17874 * calling up the hierarchy.
17875 */
17876 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017877
17878 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017879 * Temporary for use in computing hit areas with transformed views
17880 */
17881 final RectF mTmpTransformRect = new RectF();
17882
17883 /**
Chet Haase599913d2012-07-23 16:22:05 -070017884 * Temporary for use in transforming invalidation rect
17885 */
17886 final Matrix mTmpMatrix = new Matrix();
17887
17888 /**
17889 * Temporary for use in transforming invalidation rect
17890 */
17891 final Transformation mTmpTransformation = new Transformation();
17892
17893 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017894 * Temporary list for use in collecting focusable descendents of a view.
17895 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017896 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017898 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017899 * The id of the window for accessibility purposes.
17900 */
17901 int mAccessibilityWindowId = View.NO_ID;
17902
17903 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017904 * Whether to ingore not exposed for accessibility Views when
17905 * reporting the view tree to accessibility services.
17906 */
17907 boolean mIncludeNotImportantViews;
17908
17909 /**
17910 * The drawable for highlighting accessibility focus.
17911 */
17912 Drawable mAccessibilityFocusDrawable;
17913
17914 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017915 * Show where the margins, bounds and layout bounds are for each view.
17916 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017917 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017918
17919 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017920 * Point used to compute visible regions.
17921 */
17922 final Point mPoint = new Point();
17923
17924 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017925 * Creates a new set of attachment information with the specified
17926 * events handler and thread.
17927 *
17928 * @param handler the events handler the view must use
17929 */
Jeff Brown98365d72012-08-19 20:30:52 -070017930 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017931 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017932 mSession = session;
17933 mWindow = window;
17934 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017935 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017936 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017937 mHandler = handler;
17938 mRootCallbacks = effectPlayer;
17939 }
17940 }
17941
17942 /**
17943 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17944 * is supported. This avoids keeping too many unused fields in most
17945 * instances of View.</p>
17946 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017947 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017948
Mike Cleronf116bf82009-09-27 19:14:12 -070017949 /**
17950 * Scrollbars are not visible
17951 */
17952 public static final int OFF = 0;
17953
17954 /**
17955 * Scrollbars are visible
17956 */
17957 public static final int ON = 1;
17958
17959 /**
17960 * Scrollbars are fading away
17961 */
17962 public static final int FADING = 2;
17963
17964 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017966 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017967 public int scrollBarDefaultDelayBeforeFade;
17968 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017969
17970 public int scrollBarSize;
17971 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017972 public float[] interpolatorValues;
17973 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017974
17975 public final Paint paint;
17976 public final Matrix matrix;
17977 public Shader shader;
17978
Mike Cleronf116bf82009-09-27 19:14:12 -070017979 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17980
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017981 private static final float[] OPAQUE = { 255 };
17982 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017983
Mike Cleronf116bf82009-09-27 19:14:12 -070017984 /**
17985 * When fading should start. This time moves into the future every time
17986 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17987 */
17988 public long fadeStartTime;
17989
17990
17991 /**
17992 * The current state of the scrollbars: ON, OFF, or FADING
17993 */
17994 public int state = OFF;
17995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017996 private int mLastColor;
17997
Mike Cleronf116bf82009-09-27 19:14:12 -070017998 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017999 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
18000 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070018001 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
18002 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018003
18004 paint = new Paint();
18005 matrix = new Matrix();
18006 // use use a height of 1, and then wack the matrix each time we
18007 // actually use it.
18008 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018009 paint.setShader(shader);
18010 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070018011
Mike Cleronf116bf82009-09-27 19:14:12 -070018012 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018013 }
Romain Guy8506ab42009-06-11 17:35:47 -070018014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018015 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018016 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018017 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018018
Romain Guyd679b572012-08-29 21:49:00 -070018019 if (color != 0) {
18020 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18021 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18022 paint.setShader(shader);
18023 // Restore the default transfer mode (src_over)
18024 paint.setXfermode(null);
18025 } else {
18026 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18027 paint.setShader(shader);
18028 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018030 }
18031 }
Joe Malin32736f02011-01-19 16:14:20 -080018032
Mike Cleronf116bf82009-09-27 19:14:12 -070018033 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018034 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018035 if (now >= fadeStartTime) {
18036
18037 // the animation fades the scrollbars out by changing
18038 // the opacity (alpha) from fully opaque to fully
18039 // transparent
18040 int nextFrame = (int) now;
18041 int framesCount = 0;
18042
18043 Interpolator interpolator = scrollBarInterpolator;
18044
18045 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018046 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018047
18048 // End transparent
18049 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018050 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018051
18052 state = FADING;
18053
18054 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018055 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018056 }
18057 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018058 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018059
Svetoslav Ganova0156172011-06-26 17:55:44 -070018060 /**
18061 * Resuable callback for sending
18062 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18063 */
18064 private class SendViewScrolledAccessibilityEvent implements Runnable {
18065 public volatile boolean mIsPending;
18066
18067 public void run() {
18068 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18069 mIsPending = false;
18070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018071 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018072
18073 /**
18074 * <p>
18075 * This class represents a delegate that can be registered in a {@link View}
18076 * to enhance accessibility support via composition rather via inheritance.
18077 * It is specifically targeted to widget developers that extend basic View
18078 * classes i.e. classes in package android.view, that would like their
18079 * applications to be backwards compatible.
18080 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018081 * <div class="special reference">
18082 * <h3>Developer Guides</h3>
18083 * <p>For more information about making applications accessible, read the
18084 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18085 * developer guide.</p>
18086 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018087 * <p>
18088 * A scenario in which a developer would like to use an accessibility delegate
18089 * is overriding a method introduced in a later API version then the minimal API
18090 * version supported by the application. For example, the method
18091 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18092 * in API version 4 when the accessibility APIs were first introduced. If a
18093 * developer would like his application to run on API version 4 devices (assuming
18094 * all other APIs used by the application are version 4 or lower) and take advantage
18095 * of this method, instead of overriding the method which would break the application's
18096 * backwards compatibility, he can override the corresponding method in this
18097 * delegate and register the delegate in the target View if the API version of
18098 * the system is high enough i.e. the API version is same or higher to the API
18099 * version that introduced
18100 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18101 * </p>
18102 * <p>
18103 * Here is an example implementation:
18104 * </p>
18105 * <code><pre><p>
18106 * if (Build.VERSION.SDK_INT >= 14) {
18107 * // If the API version is equal of higher than the version in
18108 * // which onInitializeAccessibilityNodeInfo was introduced we
18109 * // register a delegate with a customized implementation.
18110 * View view = findViewById(R.id.view_id);
18111 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18112 * public void onInitializeAccessibilityNodeInfo(View host,
18113 * AccessibilityNodeInfo info) {
18114 * // Let the default implementation populate the info.
18115 * super.onInitializeAccessibilityNodeInfo(host, info);
18116 * // Set some other information.
18117 * info.setEnabled(host.isEnabled());
18118 * }
18119 * });
18120 * }
18121 * </code></pre></p>
18122 * <p>
18123 * This delegate contains methods that correspond to the accessibility methods
18124 * in View. If a delegate has been specified the implementation in View hands
18125 * off handling to the corresponding method in this delegate. The default
18126 * implementation the delegate methods behaves exactly as the corresponding
18127 * method in View for the case of no accessibility delegate been set. Hence,
18128 * to customize the behavior of a View method, clients can override only the
18129 * corresponding delegate method without altering the behavior of the rest
18130 * accessibility related methods of the host view.
18131 * </p>
18132 */
18133 public static class AccessibilityDelegate {
18134
18135 /**
18136 * Sends an accessibility event of the given type. If accessibility is not
18137 * enabled this method has no effect.
18138 * <p>
18139 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18140 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18141 * been set.
18142 * </p>
18143 *
18144 * @param host The View hosting the delegate.
18145 * @param eventType The type of the event to send.
18146 *
18147 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18148 */
18149 public void sendAccessibilityEvent(View host, int eventType) {
18150 host.sendAccessibilityEventInternal(eventType);
18151 }
18152
18153 /**
alanv8eeefef2012-05-07 16:57:53 -070018154 * Performs the specified accessibility action on the view. For
18155 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18156 * <p>
18157 * The default implementation behaves as
18158 * {@link View#performAccessibilityAction(int, Bundle)
18159 * View#performAccessibilityAction(int, Bundle)} for the case of
18160 * no accessibility delegate been set.
18161 * </p>
18162 *
18163 * @param action The action to perform.
18164 * @return Whether the action was performed.
18165 *
18166 * @see View#performAccessibilityAction(int, Bundle)
18167 * View#performAccessibilityAction(int, Bundle)
18168 */
18169 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18170 return host.performAccessibilityActionInternal(action, args);
18171 }
18172
18173 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018174 * Sends an accessibility event. This method behaves exactly as
18175 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18176 * empty {@link AccessibilityEvent} and does not perform a check whether
18177 * accessibility is enabled.
18178 * <p>
18179 * The default implementation behaves as
18180 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18181 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18182 * the case of no accessibility delegate been set.
18183 * </p>
18184 *
18185 * @param host The View hosting the delegate.
18186 * @param event The event to send.
18187 *
18188 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18189 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18190 */
18191 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18192 host.sendAccessibilityEventUncheckedInternal(event);
18193 }
18194
18195 /**
18196 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18197 * to its children for adding their text content to the event.
18198 * <p>
18199 * The default implementation behaves as
18200 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18201 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18202 * the case of no accessibility delegate been set.
18203 * </p>
18204 *
18205 * @param host The View hosting the delegate.
18206 * @param event The event.
18207 * @return True if the event population was completed.
18208 *
18209 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18210 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18211 */
18212 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18213 return host.dispatchPopulateAccessibilityEventInternal(event);
18214 }
18215
18216 /**
18217 * Gives a chance to the host View to populate the accessibility event with its
18218 * text content.
18219 * <p>
18220 * The default implementation behaves as
18221 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18222 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18223 * the case of no accessibility delegate been set.
18224 * </p>
18225 *
18226 * @param host The View hosting the delegate.
18227 * @param event The accessibility event which to populate.
18228 *
18229 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18230 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18231 */
18232 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18233 host.onPopulateAccessibilityEventInternal(event);
18234 }
18235
18236 /**
18237 * Initializes an {@link AccessibilityEvent} with information about the
18238 * the host View which is the event source.
18239 * <p>
18240 * The default implementation behaves as
18241 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18242 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18243 * the case of no accessibility delegate been set.
18244 * </p>
18245 *
18246 * @param host The View hosting the delegate.
18247 * @param event The event to initialize.
18248 *
18249 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18250 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18251 */
18252 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18253 host.onInitializeAccessibilityEventInternal(event);
18254 }
18255
18256 /**
18257 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18258 * <p>
18259 * The default implementation behaves as
18260 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18261 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18262 * the case of no accessibility delegate been set.
18263 * </p>
18264 *
18265 * @param host The View hosting the delegate.
18266 * @param info The instance to initialize.
18267 *
18268 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18269 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18270 */
18271 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18272 host.onInitializeAccessibilityNodeInfoInternal(info);
18273 }
18274
18275 /**
18276 * Called when a child of the host View has requested sending an
18277 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18278 * to augment the event.
18279 * <p>
18280 * The default implementation behaves as
18281 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18282 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18283 * the case of no accessibility delegate been set.
18284 * </p>
18285 *
18286 * @param host The View hosting the delegate.
18287 * @param child The child which requests sending the event.
18288 * @param event The event to be sent.
18289 * @return True if the event should be sent
18290 *
18291 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18292 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18293 */
18294 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18295 AccessibilityEvent event) {
18296 return host.onRequestSendAccessibilityEventInternal(child, event);
18297 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018298
18299 /**
18300 * Gets the provider for managing a virtual view hierarchy rooted at this View
18301 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18302 * that explore the window content.
18303 * <p>
18304 * The default implementation behaves as
18305 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18306 * the case of no accessibility delegate been set.
18307 * </p>
18308 *
18309 * @return The provider.
18310 *
18311 * @see AccessibilityNodeProvider
18312 */
18313 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18314 return null;
18315 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018316 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018317
18318 private class MatchIdPredicate implements Predicate<View> {
18319 public int mId;
18320
18321 @Override
18322 public boolean apply(View view) {
18323 return (view.mID == mId);
18324 }
18325 }
18326
18327 private class MatchLabelForPredicate implements Predicate<View> {
18328 private int mLabeledId;
18329
18330 @Override
18331 public boolean apply(View view) {
18332 return (view.mLabelForId == mLabeledId);
18333 }
18334 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018335
18336 /**
18337 * Dump all private flags in readable format, useful for documentation and
18338 * sanity checking.
18339 */
18340 private static void dumpFlags() {
18341 final HashMap<String, String> found = Maps.newHashMap();
18342 try {
18343 for (Field field : View.class.getDeclaredFields()) {
18344 final int modifiers = field.getModifiers();
18345 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18346 if (field.getType().equals(int.class)) {
18347 final int value = field.getInt(null);
18348 dumpFlag(found, field.getName(), value);
18349 } else if (field.getType().equals(int[].class)) {
18350 final int[] values = (int[]) field.get(null);
18351 for (int i = 0; i < values.length; i++) {
18352 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18353 }
18354 }
18355 }
18356 }
18357 } catch (IllegalAccessException e) {
18358 throw new RuntimeException(e);
18359 }
18360
18361 final ArrayList<String> keys = Lists.newArrayList();
18362 keys.addAll(found.keySet());
18363 Collections.sort(keys);
18364 for (String key : keys) {
18365 Log.d(VIEW_LOG_TAG, found.get(key));
18366 }
18367 }
18368
18369 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18370 // Sort flags by prefix, then by bits, always keeping unique keys
18371 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18372 final int prefix = name.indexOf('_');
18373 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18374 final String output = bits + " " + name;
18375 found.put(key, output);
18376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018377}