blob: b36db7ff6f3466c35fc8a05ae6307c42e7223ce3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Philip Milne1557fd72012-04-04 23:41:34 -070027import android.graphics.Insets;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
Jeff Brownbd6e1502012-08-28 03:27:37 -070042import android.hardware.display.DisplayManagerGlobal;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Handler;
45import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
48import android.os.RemoteException;
49import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070050import android.os.SystemProperties;
Fabrice Di Megliod3d9f3f2012-09-18 12:55:32 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070053import android.util.FloatProperty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070059import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080061import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.view.ContextMenu.ContextMenuInfo;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -070063import android.view.AccessibilityIterators.TextSegmentIterator;
64import android.view.AccessibilityIterators.CharacterTextSegmentIterator;
65import android.view.AccessibilityIterators.WordTextSegmentIterator;
66import android.view.AccessibilityIterators.ParagraphTextSegmentIterator;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.accessibility.AccessibilityEvent;
68import android.view.accessibility.AccessibilityEventSource;
69import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070070import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070071import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070073import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080074import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.inputmethod.InputConnection;
77import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.widget.ScrollBarDrawable;
79
Romain Guy1ef3fdb2011-09-09 15:30:30 -070080import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070081import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070082
Doug Feltcb3791202011-07-07 11:57:48 -070083import com.android.internal.R;
84import com.android.internal.util.Predicate;
85import com.android.internal.view.menu.MenuBuilder;
Jeff Sharkey36901b62012-09-19 19:06:22 -070086import com.google.android.collect.Lists;
87import com.google.android.collect.Maps;
Doug Feltcb3791202011-07-07 11:57:48 -070088
Christopher Tatea0374192010-10-05 13:06:41 -070089import java.lang.ref.WeakReference;
Jeff Sharkey36901b62012-09-19 19:06:22 -070090import java.lang.reflect.Field;
svetoslavganov75986cf2009-05-14 22:28:01 -070091import java.lang.reflect.InvocationTargetException;
92import java.lang.reflect.Method;
Jeff Sharkey36901b62012-09-19 19:06:22 -070093import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.util.ArrayList;
95import java.util.Arrays;
Jeff Sharkey36901b62012-09-19 19:06:22 -070096import java.util.Collections;
97import java.util.HashMap;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070098import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080099import java.util.concurrent.CopyOnWriteArrayList;
Adam Powella9108a22012-07-18 11:18:09 -0700100import java.util.concurrent.atomic.AtomicInteger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102/**
103 * <p>
104 * This class represents the basic building block for user interface components. A View
105 * occupies a rectangular area on the screen and is responsible for drawing and
106 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -0700107 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
109 * are invisible containers that hold other Views (or other ViewGroups) and define
110 * their layout properties.
111 * </p>
112 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700113 * <div class="special reference">
114 * <h3>Developer Guides</h3>
115 * <p>For information about using this class to develop your application's user interface,
116 * read the <a href="{@docRoot}guide/topics/ui/index.html">User Interface</a> developer guide.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * <a name="Using"></a>
120 * <h3>Using Views</h3>
121 * <p>
122 * All of the views in a window are arranged in a single tree. You can add views
123 * either from code or by specifying a tree of views in one or more XML layout
124 * files. There are many specialized subclasses of views that act as controls or
125 * are capable of displaying text, images, or other content.
126 * </p>
127 * <p>
128 * Once you have created a tree of views, there are typically a few types of
129 * common operations you may wish to perform:
130 * <ul>
131 * <li><strong>Set properties:</strong> for example setting the text of a
132 * {@link android.widget.TextView}. The available properties and the methods
133 * that set them will vary among the different subclasses of views. Note that
134 * properties that are known at build time can be set in the XML layout
135 * files.</li>
136 * <li><strong>Set focus:</strong> The framework will handled moving focus in
137 * response to user input. To force focus to a specific view, call
138 * {@link #requestFocus}.</li>
139 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
140 * that will be notified when something interesting happens to the view. For
141 * example, all views will let you set a listener to be notified when the view
142 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700143 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700144 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700145 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700147 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * </ul>
149 * </p>
150 * <p><em>
151 * Note: The Android framework is responsible for measuring, laying out and
152 * drawing views. You should not call methods that perform these actions on
153 * views yourself unless you are actually implementing a
154 * {@link android.view.ViewGroup}.
155 * </em></p>
156 *
157 * <a name="Lifecycle"></a>
158 * <h3>Implementing a Custom View</h3>
159 *
160 * <p>
161 * To implement a custom view, you will usually begin by providing overrides for
162 * some of the standard methods that the framework calls on all views. You do
163 * not need to override all of these methods. In fact, you can start by just
164 * overriding {@link #onDraw(android.graphics.Canvas)}.
165 * <table border="2" width="85%" align="center" cellpadding="5">
166 * <thead>
167 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
168 * </thead>
169 *
170 * <tbody>
171 * <tr>
172 * <td rowspan="2">Creation</td>
173 * <td>Constructors</td>
174 * <td>There is a form of the constructor that are called when the view
175 * is created from code and a form that is called when the view is
176 * inflated from a layout file. The second form should parse and apply
177 * any attributes defined in the layout file.
178 * </td>
179 * </tr>
180 * <tr>
181 * <td><code>{@link #onFinishInflate()}</code></td>
182 * <td>Called after a view and all of its children has been inflated
183 * from XML.</td>
184 * </tr>
185 *
186 * <tr>
187 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700188 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 * <td>Called to determine the size requirements for this view and all
190 * of its children.
191 * </td>
192 * </tr>
193 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700194 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 * <td>Called when this view should assign a size and position to all
196 * of its children.
197 * </td>
198 * </tr>
199 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700200 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * <td>Called when the size of this view has changed.
202 * </td>
203 * </tr>
204 *
205 * <tr>
206 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700207 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 * <td>Called when the view should render its content.
209 * </td>
210 * </tr>
211 *
212 * <tr>
213 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700214 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900215 * <td>Called when a new hardware key event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * </td>
217 * </tr>
218 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700219 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
Jean Chalard405bc512012-05-29 19:12:34 +0900220 * <td>Called when a hardware key up event occurs.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * </td>
222 * </tr>
223 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700224 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * <td>Called when a trackball motion event occurs.
226 * </td>
227 * </tr>
228 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700229 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 * <td>Called when a touch screen motion event occurs.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700236 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 * <td>Called when the view gains or loses focus.
238 * </td>
239 * </tr>
240 *
241 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700242 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 * <td>Called when the window containing the view gains or loses focus.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td rowspan="3">Attaching</td>
249 * <td><code>{@link #onAttachedToWindow()}</code></td>
250 * <td>Called when the view is attached to a window.
251 * </td>
252 * </tr>
253 *
254 * <tr>
255 * <td><code>{@link #onDetachedFromWindow}</code></td>
256 * <td>Called when the view is detached from its window.
257 * </td>
258 * </tr>
259 *
260 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700261 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 * <td>Called when the visibility of the window containing the view
263 * has changed.
264 * </td>
265 * </tr>
266 * </tbody>
267 *
268 * </table>
269 * </p>
270 *
271 * <a name="IDs"></a>
272 * <h3>IDs</h3>
273 * Views may have an integer id associated with them. These ids are typically
274 * assigned in the layout XML files, and are used to find specific views within
275 * the view tree. A common pattern is to:
276 * <ul>
277 * <li>Define a Button in the layout file and assign it a unique ID.
278 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700279 * &lt;Button
280 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 * android:layout_width="wrap_content"
282 * android:layout_height="wrap_content"
283 * android:text="@string/my_button_text"/&gt;
284 * </pre></li>
285 * <li>From the onCreate method of an Activity, find the Button
286 * <pre class="prettyprint">
287 * Button myButton = (Button) findViewById(R.id.my_button);
288 * </pre></li>
289 * </ul>
290 * <p>
291 * View IDs need not be unique throughout the tree, but it is good practice to
292 * ensure that they are at least unique within the part of the tree you are
293 * searching.
294 * </p>
295 *
296 * <a name="Position"></a>
297 * <h3>Position</h3>
298 * <p>
299 * The geometry of a view is that of a rectangle. A view has a location,
300 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
301 * two dimensions, expressed as a width and a height. The unit for location
302 * and dimensions is the pixel.
303 * </p>
304 *
305 * <p>
306 * It is possible to retrieve the location of a view by invoking the methods
307 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
308 * coordinate of the rectangle representing the view. The latter returns the
309 * top, or Y, coordinate of the rectangle representing the view. These methods
310 * both return the location of the view relative to its parent. For instance,
311 * when getLeft() returns 20, that means the view is located 20 pixels to the
312 * right of the left edge of its direct parent.
313 * </p>
314 *
315 * <p>
316 * In addition, several convenience methods are offered to avoid unnecessary
317 * computations, namely {@link #getRight()} and {@link #getBottom()}.
318 * These methods return the coordinates of the right and bottom edges of the
319 * rectangle representing the view. For instance, calling {@link #getRight()}
320 * is similar to the following computation: <code>getLeft() + getWidth()</code>
321 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
322 * </p>
323 *
324 * <a name="SizePaddingMargins"></a>
325 * <h3>Size, padding and margins</h3>
326 * <p>
327 * The size of a view is expressed with a width and a height. A view actually
328 * possess two pairs of width and height values.
329 * </p>
330 *
331 * <p>
332 * The first pair is known as <em>measured width</em> and
333 * <em>measured height</em>. These dimensions define how big a view wants to be
334 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
335 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
336 * and {@link #getMeasuredHeight()}.
337 * </p>
338 *
339 * <p>
340 * The second pair is simply known as <em>width</em> and <em>height</em>, or
341 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
342 * dimensions define the actual size of the view on screen, at drawing time and
343 * after layout. These values may, but do not have to, be different from the
344 * measured width and height. The width and height can be obtained by calling
345 * {@link #getWidth()} and {@link #getHeight()}.
346 * </p>
347 *
348 * <p>
349 * To measure its dimensions, a view takes into account its padding. The padding
350 * is expressed in pixels for the left, top, right and bottom parts of the view.
351 * Padding can be used to offset the content of the view by a specific amount of
352 * pixels. For instance, a left padding of 2 will push the view's content by
353 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -0700354 * {@link #setPadding(int, int, int, int)} or {@link #setPaddingRelative(int, int, int, int)}
355 * method and queried by calling {@link #getPaddingLeft()}, {@link #getPaddingTop()},
356 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}, {@link #getPaddingStart()},
357 * {@link #getPaddingEnd()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 * </p>
359 *
360 * <p>
361 * Even though a view can define a padding, it does not provide any support for
362 * margins. However, view groups provide such a support. Refer to
363 * {@link android.view.ViewGroup} and
364 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
365 * </p>
366 *
367 * <a name="Layout"></a>
368 * <h3>Layout</h3>
369 * <p>
370 * Layout is a two pass process: a measure pass and a layout pass. The measuring
371 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
372 * of the view tree. Each view pushes dimension specifications down the tree
373 * during the recursion. At the end of the measure pass, every view has stored
374 * its measurements. The second pass happens in
375 * {@link #layout(int,int,int,int)} and is also top-down. During
376 * this pass each parent is responsible for positioning all of its children
377 * using the sizes computed in the measure pass.
378 * </p>
379 *
380 * <p>
381 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
382 * {@link #getMeasuredHeight()} values must be set, along with those for all of
383 * that view's descendants. A view's measured width and measured height values
384 * must respect the constraints imposed by the view's parents. This guarantees
385 * that at the end of the measure pass, all parents accept all of their
386 * children's measurements. A parent view may call measure() more than once on
387 * its children. For example, the parent may measure each child once with
388 * unspecified dimensions to find out how big they want to be, then call
389 * measure() on them again with actual numbers if the sum of all the children's
390 * unconstrained sizes is too big or too small.
391 * </p>
392 *
393 * <p>
394 * The measure pass uses two classes to communicate dimensions. The
395 * {@link MeasureSpec} class is used by views to tell their parents how they
396 * want to be measured and positioned. The base LayoutParams class just
397 * describes how big the view wants to be for both width and height. For each
398 * dimension, it can specify one of:
399 * <ul>
400 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800401 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 * (minus padding)
403 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
404 * enclose its content (plus padding).
405 * </ul>
406 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
407 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
408 * an X and Y value.
409 * </p>
410 *
411 * <p>
412 * MeasureSpecs are used to push requirements down the tree from parent to
413 * child. A MeasureSpec can be in one of three modes:
414 * <ul>
415 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
416 * of a child view. For example, a LinearLayout may call measure() on its child
417 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
418 * tall the child view wants to be given a width of 240 pixels.
419 * <li>EXACTLY: This is used by the parent to impose an exact size on the
420 * child. The child must use this size, and guarantee that all of its
421 * descendants will fit within this size.
422 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
423 * child. The child must gurantee that it and all of its descendants will fit
424 * within this size.
425 * </ul>
426 * </p>
427 *
428 * <p>
429 * To intiate a layout, call {@link #requestLayout}. This method is typically
430 * called by a view on itself when it believes that is can no longer fit within
431 * its current bounds.
432 * </p>
433 *
434 * <a name="Drawing"></a>
435 * <h3>Drawing</h3>
436 * <p>
437 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700438 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 * this means that parents will draw before (i.e., behind) their children, with
440 * siblings drawn in the order they appear in the tree.
441 * If you set a background drawable for a View, then the View will draw it for you
442 * before calling back to its <code>onDraw()</code> method.
443 * </p>
444 *
445 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700446 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 * </p>
448 *
449 * <p>
450 * To force a view to draw, call {@link #invalidate()}.
451 * </p>
452 *
453 * <a name="EventHandlingThreading"></a>
454 * <h3>Event Handling and Threading</h3>
455 * <p>
456 * The basic cycle of a view is as follows:
457 * <ol>
458 * <li>An event comes in and is dispatched to the appropriate view. The view
459 * handles the event and notifies any listeners.</li>
460 * <li>If in the course of processing the event, the view's bounds may need
461 * to be changed, the view will call {@link #requestLayout()}.</li>
462 * <li>Similarly, if in the course of processing the event the view's appearance
463 * may need to be changed, the view will call {@link #invalidate()}.</li>
464 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
465 * the framework will take care of measuring, laying out, and drawing the tree
466 * as appropriate.</li>
467 * </ol>
468 * </p>
469 *
470 * <p><em>Note: The entire view tree is single threaded. You must always be on
471 * the UI thread when calling any method on any view.</em>
472 * If you are doing work on other threads and want to update the state of a view
473 * from that thread, you should use a {@link Handler}.
474 * </p>
475 *
476 * <a name="FocusHandling"></a>
477 * <h3>Focus Handling</h3>
478 * <p>
479 * The framework will handle routine focus movement in response to user input.
480 * This includes changing the focus as views are removed or hidden, or as new
481 * views become available. Views indicate their willingness to take focus
482 * through the {@link #isFocusable} method. To change whether a view can take
483 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
484 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
485 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
486 * </p>
487 * <p>
488 * Focus movement is based on an algorithm which finds the nearest neighbor in a
489 * given direction. In rare cases, the default algorithm may not match the
490 * intended behavior of the developer. In these situations, you can provide
491 * explicit overrides by using these XML attributes in the layout file:
492 * <pre>
493 * nextFocusDown
494 * nextFocusLeft
495 * nextFocusRight
496 * nextFocusUp
497 * </pre>
498 * </p>
499 *
500 *
501 * <p>
502 * To get a particular view to take focus, call {@link #requestFocus()}.
503 * </p>
504 *
505 * <a name="TouchMode"></a>
506 * <h3>Touch Mode</h3>
507 * <p>
508 * When a user is navigating a user interface via directional keys such as a D-pad, it is
509 * necessary to give focus to actionable items such as buttons so the user can see
510 * what will take input. If the device has touch capabilities, however, and the user
511 * begins interacting with the interface by touching it, it is no longer necessary to
512 * always highlight, or give focus to, a particular view. This motivates a mode
513 * for interaction named 'touch mode'.
514 * </p>
515 * <p>
516 * For a touch capable device, once the user touches the screen, the device
517 * will enter touch mode. From this point onward, only views for which
518 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
519 * Other views that are touchable, like buttons, will not take focus when touched; they will
520 * only fire the on click listeners.
521 * </p>
522 * <p>
523 * Any time a user hits a directional key, such as a D-pad direction, the view device will
524 * exit touch mode, and find a view to take focus, so that the user may resume interacting
525 * with the user interface without touching the screen again.
526 * </p>
527 * <p>
528 * The touch mode state is maintained across {@link android.app.Activity}s. Call
529 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
530 * </p>
531 *
532 * <a name="Scrolling"></a>
533 * <h3>Scrolling</h3>
534 * <p>
535 * The framework provides basic support for views that wish to internally
536 * scroll their content. This includes keeping track of the X and Y scroll
537 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800538 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700539 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 * </p>
541 *
542 * <a name="Tags"></a>
543 * <h3>Tags</h3>
544 * <p>
545 * Unlike IDs, tags are not used to identify views. Tags are essentially an
546 * extra piece of information that can be associated with a view. They are most
547 * often used as a convenience to store data related to views in the views
548 * themselves rather than by putting them in a separate structure.
549 * </p>
550 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700551 * <a name="Properties"></a>
552 * <h3>Properties</h3>
553 * <p>
554 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
555 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
556 * available both in the {@link Property} form as well as in similarly-named setter/getter
557 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
558 * be used to set persistent state associated with these rendering-related properties on the view.
559 * The properties and methods can also be used in conjunction with
560 * {@link android.animation.Animator Animator}-based animations, described more in the
561 * <a href="#Animation">Animation</a> section.
562 * </p>
563 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * <a name="Animation"></a>
565 * <h3>Animation</h3>
566 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700567 * Starting with Android 3.0, the preferred way of animating views is to use the
568 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
569 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
570 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
571 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
572 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
573 * makes animating these View properties particularly easy and efficient.
574 * </p>
575 * <p>
576 * Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 * You can attach an {@link Animation} object to a view using
578 * {@link #setAnimation(Animation)} or
579 * {@link #startAnimation(Animation)}. The animation can alter the scale,
580 * rotation, translation and alpha of a view over time. If the animation is
581 * attached to a view that has children, the animation will affect the entire
582 * subtree rooted by that node. When an animation is started, the framework will
583 * take care of redrawing the appropriate views until the animation completes.
584 * </p>
585 *
Jeff Brown85a31762010-09-01 17:01:00 -0700586 * <a name="Security"></a>
587 * <h3>Security</h3>
588 * <p>
589 * Sometimes it is essential that an application be able to verify that an action
590 * is being performed with the full knowledge and consent of the user, such as
591 * granting a permission request, making a purchase or clicking on an advertisement.
592 * Unfortunately, a malicious application could try to spoof the user into
593 * performing these actions, unaware, by concealing the intended purpose of the view.
594 * As a remedy, the framework offers a touch filtering mechanism that can be used to
595 * improve the security of views that provide access to sensitive functionality.
596 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700597 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800598 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700599 * will discard touches that are received whenever the view's window is obscured by
600 * another visible window. As a result, the view will not receive touches whenever a
601 * toast, dialog or other window appears above the view's window.
602 * </p><p>
603 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700604 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
605 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700606 * </p>
607 *
Romain Guy171c5922011-01-06 10:04:23 -0800608 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700609 * @attr ref android.R.styleable#View_background
610 * @attr ref android.R.styleable#View_clickable
611 * @attr ref android.R.styleable#View_contentDescription
612 * @attr ref android.R.styleable#View_drawingCacheQuality
613 * @attr ref android.R.styleable#View_duplicateParentState
614 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700615 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700616 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700617 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700618 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_isScrollContainer
621 * @attr ref android.R.styleable#View_focusable
622 * @attr ref android.R.styleable#View_focusableInTouchMode
623 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
624 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800625 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700626 * @attr ref android.R.styleable#View_longClickable
627 * @attr ref android.R.styleable#View_minHeight
628 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 * @attr ref android.R.styleable#View_nextFocusDown
630 * @attr ref android.R.styleable#View_nextFocusLeft
631 * @attr ref android.R.styleable#View_nextFocusRight
632 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700633 * @attr ref android.R.styleable#View_onClick
634 * @attr ref android.R.styleable#View_padding
635 * @attr ref android.R.styleable#View_paddingBottom
636 * @attr ref android.R.styleable#View_paddingLeft
637 * @attr ref android.R.styleable#View_paddingRight
638 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Meglio101d5aa2012-02-16 18:36:06 -0800639 * @attr ref android.R.styleable#View_paddingStart
640 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700641 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800642 * @attr ref android.R.styleable#View_rotation
643 * @attr ref android.R.styleable#View_rotationX
644 * @attr ref android.R.styleable#View_rotationY
645 * @attr ref android.R.styleable#View_scaleX
646 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 * @attr ref android.R.styleable#View_scrollX
648 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700649 * @attr ref android.R.styleable#View_scrollbarSize
650 * @attr ref android.R.styleable#View_scrollbarStyle
651 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700652 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
653 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
655 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 * @attr ref android.R.styleable#View_scrollbarThumbVertical
657 * @attr ref android.R.styleable#View_scrollbarTrackVertical
658 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
659 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700660 * @attr ref android.R.styleable#View_soundEffectsEnabled
661 * @attr ref android.R.styleable#View_tag
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -0700662 * @attr ref android.R.styleable#View_textAlignment
Chet Haase73066682010-11-29 15:55:32 -0800663 * @attr ref android.R.styleable#View_transformPivotX
664 * @attr ref android.R.styleable#View_transformPivotY
665 * @attr ref android.R.styleable#View_translationX
666 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700667 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 *
669 * @see android.view.ViewGroup
670 */
Fabrice Di Megliob03b4342012-06-04 12:55:30 -0700671public class View implements Drawable.Callback, KeyEvent.Callback,
Adam Powell8fc54f92011-09-07 16:40:45 -0700672 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 private static final boolean DBG = false;
674
675 /**
676 * The logging tag used by this class with android.util.Log.
677 */
678 protected static final String VIEW_LOG_TAG = "View";
679
680 /**
Guang Zhu0d607fb2012-05-11 19:34:56 -0700681 * When set to true, apps will draw debugging information about their layouts.
Romain Guy4b8c4f82012-04-27 15:48:35 -0700682 *
683 * @hide
684 */
685 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
686
687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 * Used to mark a View that has no ID.
689 */
690 public static final int NO_ID = -1;
691
692 /**
693 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
694 * calling setFlags.
695 */
696 private static final int NOT_FOCUSABLE = 0x00000000;
697
698 /**
699 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
700 * setFlags.
701 */
702 private static final int FOCUSABLE = 0x00000001;
703
704 /**
705 * Mask for use with setFlags indicating bits used for focus.
706 */
707 private static final int FOCUSABLE_MASK = 0x00000001;
708
709 /**
710 * This view will adjust its padding to fit sytem windows (e.g. status bar)
711 */
712 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
713
714 /**
Scott Main812634c22011-07-27 13:22:35 -0700715 * This view is visible.
716 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
717 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 */
719 public static final int VISIBLE = 0x00000000;
720
721 /**
722 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700723 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
724 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 */
726 public static final int INVISIBLE = 0x00000004;
727
728 /**
729 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700730 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
731 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 */
733 public static final int GONE = 0x00000008;
734
735 /**
736 * Mask for use with setFlags indicating bits used for visibility.
737 * {@hide}
738 */
739 static final int VISIBILITY_MASK = 0x0000000C;
740
741 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
742
743 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700744 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 * Use with ENABLED_MASK when calling setFlags.
746 * {@hide}
747 */
748 static final int ENABLED = 0x00000000;
749
750 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700751 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 * Use with ENABLED_MASK when calling setFlags.
753 * {@hide}
754 */
755 static final int DISABLED = 0x00000020;
756
757 /**
758 * Mask for use with setFlags indicating bits used for indicating whether
759 * this view is enabled
760 * {@hide}
761 */
762 static final int ENABLED_MASK = 0x00000020;
763
764 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700765 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
766 * called and further optimizations will be performed. It is okay to have
767 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 * {@hide}
769 */
770 static final int WILL_NOT_DRAW = 0x00000080;
771
772 /**
773 * Mask for use with setFlags indicating bits used for indicating whether
774 * this view is will draw
775 * {@hide}
776 */
777 static final int DRAW_MASK = 0x00000080;
778
779 /**
780 * <p>This view doesn't show scrollbars.</p>
781 * {@hide}
782 */
783 static final int SCROLLBARS_NONE = 0x00000000;
784
785 /**
786 * <p>This view shows horizontal scrollbars.</p>
787 * {@hide}
788 */
789 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
790
791 /**
792 * <p>This view shows vertical scrollbars.</p>
793 * {@hide}
794 */
795 static final int SCROLLBARS_VERTICAL = 0x00000200;
796
797 /**
798 * <p>Mask for use with setFlags indicating bits used for indicating which
799 * scrollbars are enabled.</p>
800 * {@hide}
801 */
802 static final int SCROLLBARS_MASK = 0x00000300;
803
Jeff Brown85a31762010-09-01 17:01:00 -0700804 /**
805 * Indicates that the view should filter touches when its window is obscured.
806 * Refer to the class comments for more information about this security feature.
807 * {@hide}
808 */
809 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
810
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700811 /**
812 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
813 * that they are optional and should be skipped if the window has
814 * requested system UI flags that ignore those insets for layout.
815 */
816 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 /**
819 * <p>This view doesn't show fading edges.</p>
820 * {@hide}
821 */
822 static final int FADING_EDGE_NONE = 0x00000000;
823
824 /**
825 * <p>This view shows horizontal fading edges.</p>
826 * {@hide}
827 */
828 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
829
830 /**
831 * <p>This view shows vertical fading edges.</p>
832 * {@hide}
833 */
834 static final int FADING_EDGE_VERTICAL = 0x00002000;
835
836 /**
837 * <p>Mask for use with setFlags indicating bits used for indicating which
838 * fading edges are enabled.</p>
839 * {@hide}
840 */
841 static final int FADING_EDGE_MASK = 0x00003000;
842
843 /**
844 * <p>Indicates this view can be clicked. When clickable, a View reacts
845 * to clicks by notifying the OnClickListener.<p>
846 * {@hide}
847 */
848 static final int CLICKABLE = 0x00004000;
849
850 /**
851 * <p>Indicates this view is caching its drawing into a bitmap.</p>
852 * {@hide}
853 */
854 static final int DRAWING_CACHE_ENABLED = 0x00008000;
855
856 /**
857 * <p>Indicates that no icicle should be saved for this view.<p>
858 * {@hide}
859 */
860 static final int SAVE_DISABLED = 0x000010000;
861
862 /**
863 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
864 * property.</p>
865 * {@hide}
866 */
867 static final int SAVE_DISABLED_MASK = 0x000010000;
868
869 /**
870 * <p>Indicates that no drawing cache should ever be created for this view.<p>
871 * {@hide}
872 */
873 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
874
875 /**
876 * <p>Indicates this view can take / keep focus when int touch mode.</p>
877 * {@hide}
878 */
879 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
880
881 /**
882 * <p>Enables low quality mode for the drawing cache.</p>
883 */
884 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
885
886 /**
887 * <p>Enables high quality mode for the drawing cache.</p>
888 */
889 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
890
891 /**
892 * <p>Enables automatic quality mode for the drawing cache.</p>
893 */
894 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
895
896 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
897 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
898 };
899
900 /**
901 * <p>Mask for use with setFlags indicating bits used for the cache
902 * quality property.</p>
903 * {@hide}
904 */
905 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
906
907 /**
908 * <p>
909 * Indicates this view can be long clicked. When long clickable, a View
910 * reacts to long clicks by notifying the OnLongClickListener or showing a
911 * context menu.
912 * </p>
913 * {@hide}
914 */
915 static final int LONG_CLICKABLE = 0x00200000;
916
917 /**
918 * <p>Indicates that this view gets its drawable states from its direct parent
919 * and ignores its original internal states.</p>
920 *
921 * @hide
922 */
923 static final int DUPLICATE_PARENT_STATE = 0x00400000;
924
925 /**
926 * The scrollbar style to display the scrollbars inside the content area,
927 * without increasing the padding. The scrollbars will be overlaid with
928 * translucency on the view's content.
929 */
930 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
931
932 /**
933 * The scrollbar style to display the scrollbars inside the padded area,
934 * increasing the padding of the view. The scrollbars will not overlap the
935 * content area of the view.
936 */
937 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
938
939 /**
940 * The scrollbar style to display the scrollbars at the edge of the view,
941 * without increasing the padding. The scrollbars will be overlaid with
942 * translucency.
943 */
944 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
945
946 /**
947 * The scrollbar style to display the scrollbars at the edge of the view,
948 * increasing the padding of the view. The scrollbars will only overlap the
949 * background, if any.
950 */
951 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
952
953 /**
954 * Mask to check if the scrollbar style is overlay or inset.
955 * {@hide}
956 */
957 static final int SCROLLBARS_INSET_MASK = 0x01000000;
958
959 /**
960 * Mask to check if the scrollbar style is inside or outside.
961 * {@hide}
962 */
963 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
964
965 /**
966 * Mask for scrollbar style.
967 * {@hide}
968 */
969 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
970
971 /**
972 * View flag indicating that the screen should remain on while the
973 * window containing this view is visible to the user. This effectively
974 * takes care of automatically setting the WindowManager's
975 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
976 */
977 public static final int KEEP_SCREEN_ON = 0x04000000;
978
979 /**
980 * View flag indicating whether this view should have sound effects enabled
981 * for events such as clicking and touching.
982 */
983 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
984
985 /**
986 * View flag indicating whether this view should have haptic feedback
987 * enabled for events such as long presses.
988 */
989 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
990
991 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700992 * <p>Indicates that the view hierarchy should stop saving state when
993 * it reaches this view. If state saving is initiated immediately at
994 * the view, it will be allowed.
995 * {@hide}
996 */
997 static final int PARENT_SAVE_DISABLED = 0x20000000;
998
999 /**
1000 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
1001 * {@hide}
1002 */
1003 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
1004
1005 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001006 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1007 * should add all focusable Views regardless if they are focusable in touch mode.
1008 */
1009 public static final int FOCUSABLES_ALL = 0x00000000;
1010
1011 /**
1012 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1013 * should add only Views focusable in touch mode.
1014 */
1015 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1016
1017 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001018 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 * item.
1020 */
1021 public static final int FOCUS_BACKWARD = 0x00000001;
1022
1023 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001024 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * item.
1026 */
1027 public static final int FOCUS_FORWARD = 0x00000002;
1028
1029 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001030 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 */
1032 public static final int FOCUS_LEFT = 0x00000011;
1033
1034 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001035 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 */
1037 public static final int FOCUS_UP = 0x00000021;
1038
1039 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001040 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 */
1042 public static final int FOCUS_RIGHT = 0x00000042;
1043
1044 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001045 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 */
1047 public static final int FOCUS_DOWN = 0x00000082;
1048
Svetoslav Ganov42138042012-03-20 11:51:39 -07001049 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001050 * Bits of {@link #getMeasuredWidthAndState()} and
1051 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1052 */
1053 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1054
1055 /**
1056 * Bits of {@link #getMeasuredWidthAndState()} and
1057 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1058 */
1059 public static final int MEASURED_STATE_MASK = 0xff000000;
1060
1061 /**
1062 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1063 * for functions that combine both width and height into a single int,
1064 * such as {@link #getMeasuredState()} and the childState argument of
1065 * {@link #resolveSizeAndState(int, int, int)}.
1066 */
1067 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1068
1069 /**
1070 * Bit of {@link #getMeasuredWidthAndState()} and
1071 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1072 * is smaller that the space the view would like to have.
1073 */
1074 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1075
1076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 * Base View state sets
1078 */
1079 // Singles
1080 /**
1081 * Indicates the view has no states set. States are used with
1082 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1083 * view depending on its state.
1084 *
1085 * @see android.graphics.drawable.Drawable
1086 * @see #getDrawableState()
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled. States are used with
1091 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1092 * view depending on its state.
1093 *
1094 * @see android.graphics.drawable.Drawable
1095 * @see #getDrawableState()
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is focused. States are used with
1100 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1101 * view depending on its state.
1102 *
1103 * @see android.graphics.drawable.Drawable
1104 * @see #getDrawableState()
1105 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001106 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 /**
1108 * Indicates the view is selected. States are used with
1109 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1110 * view depending on its state.
1111 *
1112 * @see android.graphics.drawable.Drawable
1113 * @see #getDrawableState()
1114 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001115 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 /**
1117 * Indicates the view is pressed. States are used with
1118 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1119 * view depending on its state.
1120 *
1121 * @see android.graphics.drawable.Drawable
1122 * @see #getDrawableState()
1123 * @hide
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 /**
1127 * Indicates the view's window has focus. States are used with
1128 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1129 * view depending on its state.
1130 *
1131 * @see android.graphics.drawable.Drawable
1132 * @see #getDrawableState()
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 // Doubles
1136 /**
1137 * Indicates the view is enabled and has the focus.
1138 *
1139 * @see #ENABLED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled and selected.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is enabled and that its window has focus.
1152 *
1153 * @see #ENABLED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is focused and selected.
1159 *
1160 * @see #FOCUSED_STATE_SET
1161 * @see #SELECTED_STATE_SET
1162 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001163 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 /**
1165 * Indicates the view has the focus and that its window has the focus.
1166 *
1167 * @see #FOCUSED_STATE_SET
1168 * @see #WINDOW_FOCUSED_STATE_SET
1169 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001170 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 /**
1172 * Indicates the view is selected and that its window has the focus.
1173 *
1174 * @see #SELECTED_STATE_SET
1175 * @see #WINDOW_FOCUSED_STATE_SET
1176 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001177 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // Triples
1179 /**
1180 * Indicates the view is enabled, focused and selected.
1181 *
1182 * @see #ENABLED_STATE_SET
1183 * @see #FOCUSED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001186 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 /**
1188 * Indicates the view is enabled, focused and its window has the focus.
1189 *
1190 * @see #ENABLED_STATE_SET
1191 * @see #FOCUSED_STATE_SET
1192 * @see #WINDOW_FOCUSED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view is enabled, selected and its window has the focus.
1197 *
1198 * @see #ENABLED_STATE_SET
1199 * @see #SELECTED_STATE_SET
1200 * @see #WINDOW_FOCUSED_STATE_SET
1201 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001202 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 /**
1204 * Indicates the view is focused, selected and its window has the focus.
1205 *
1206 * @see #FOCUSED_STATE_SET
1207 * @see #SELECTED_STATE_SET
1208 * @see #WINDOW_FOCUSED_STATE_SET
1209 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001210 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 /**
1212 * Indicates the view is enabled, focused, selected and its window
1213 * has the focus.
1214 *
1215 * @see #ENABLED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and its window has the focus.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed and selected.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #SELECTED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed, selected and its window has the focus.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #SELECTED_STATE_SET
1240 * @see #WINDOW_FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed and focused.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001249 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 /**
1251 * Indicates the view is pressed, focused and its window has the focus.
1252 *
1253 * @see #PRESSED_STATE_SET
1254 * @see #FOCUSED_STATE_SET
1255 * @see #WINDOW_FOCUSED_STATE_SET
1256 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001257 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 /**
1259 * Indicates the view is pressed, focused and selected.
1260 *
1261 * @see #PRESSED_STATE_SET
1262 * @see #SELECTED_STATE_SET
1263 * @see #FOCUSED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, focused, selected and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #SELECTED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed and enabled.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled and its window has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #WINDOW_FOCUSED_STATE_SET
1288 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001289 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 /**
1291 * Indicates the view is pressed, enabled and selected.
1292 *
1293 * @see #PRESSED_STATE_SET
1294 * @see #ENABLED_STATE_SET
1295 * @see #SELECTED_STATE_SET
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 /**
1299 * Indicates the view is pressed, enabled, selected and its window has the
1300 * focus.
1301 *
1302 * @see #PRESSED_STATE_SET
1303 * @see #ENABLED_STATE_SET
1304 * @see #SELECTED_STATE_SET
1305 * @see #WINDOW_FOCUSED_STATE_SET
1306 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 /**
1309 * Indicates the view is pressed, enabled and focused.
1310 *
1311 * @see #PRESSED_STATE_SET
1312 * @see #ENABLED_STATE_SET
1313 * @see #FOCUSED_STATE_SET
1314 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001315 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 /**
1317 * Indicates the view is pressed, enabled, focused and its window has the
1318 * focus.
1319 *
1320 * @see #PRESSED_STATE_SET
1321 * @see #ENABLED_STATE_SET
1322 * @see #FOCUSED_STATE_SET
1323 * @see #WINDOW_FOCUSED_STATE_SET
1324 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 /**
1327 * Indicates the view is pressed, enabled, focused and selected.
1328 *
1329 * @see #PRESSED_STATE_SET
1330 * @see #ENABLED_STATE_SET
1331 * @see #SELECTED_STATE_SET
1332 * @see #FOCUSED_STATE_SET
1333 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * Indicates the view is pressed, enabled, focused, selected and its window
1337 * has the focus.
1338 *
1339 * @see #PRESSED_STATE_SET
1340 * @see #ENABLED_STATE_SET
1341 * @see #SELECTED_STATE_SET
1342 * @see #FOCUSED_STATE_SET
1343 * @see #WINDOW_FOCUSED_STATE_SET
1344 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001345 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346
1347 /**
1348 * The order here is very important to {@link #getDrawableState()}
1349 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001350 private static final int[][] VIEW_STATE_SETS;
1351
Romain Guyb051e892010-09-28 19:09:36 -07001352 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1353 static final int VIEW_STATE_SELECTED = 1 << 1;
1354 static final int VIEW_STATE_FOCUSED = 1 << 2;
1355 static final int VIEW_STATE_ENABLED = 1 << 3;
1356 static final int VIEW_STATE_PRESSED = 1 << 4;
1357 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001358 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001359 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001360 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1361 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001362
1363 static final int[] VIEW_STATE_IDS = new int[] {
1364 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1365 R.attr.state_selected, VIEW_STATE_SELECTED,
1366 R.attr.state_focused, VIEW_STATE_FOCUSED,
1367 R.attr.state_enabled, VIEW_STATE_ENABLED,
1368 R.attr.state_pressed, VIEW_STATE_PRESSED,
1369 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001370 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001371 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001372 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001373 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 };
1375
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001377 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1378 throw new IllegalStateException(
1379 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1380 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001383 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001384 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001386 orderedIds[i * 2] = viewState;
1387 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 }
1389 }
1390 }
Romain Guyb051e892010-09-28 19:09:36 -07001391 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1392 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1393 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001394 int numBits = Integer.bitCount(i);
1395 int[] set = new int[numBits];
1396 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001397 for (int j = 0; j < orderedIds.length; j += 2) {
1398 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001399 set[pos++] = orderedIds[j];
1400 }
1401 }
1402 VIEW_STATE_SETS[i] = set;
1403 }
1404
1405 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1406 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1407 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1408 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1410 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1411 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1413 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1415 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1417 | VIEW_STATE_FOCUSED];
1418 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1419 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1421 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1422 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1423 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1425 | VIEW_STATE_ENABLED];
1426 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1428 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1430 | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1436 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1437
1438 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1439 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1441 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1442 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1443 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1445 | VIEW_STATE_PRESSED];
1446 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1448 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1449 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1450 | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1456 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1457 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1461 | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1467 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1470 | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1473 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1479 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1480 | VIEW_STATE_PRESSED];
1481 }
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001484 * Accessibility event types that are dispatched for text population.
1485 */
1486 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1487 AccessibilityEvent.TYPE_VIEW_CLICKED
1488 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1489 | AccessibilityEvent.TYPE_VIEW_SELECTED
1490 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1491 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1492 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001493 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001494 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001495 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001496 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
1497 | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001498
1499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 * Temporary Rect currently for use in setBackground(). This will probably
1501 * be extended in the future to hold our own class with more than just
1502 * a Rect. :)
1503 */
1504 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001505
1506 /**
1507 * Map used to store views' tags.
1508 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001509 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07001512 * The next available accessibility id.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001513 */
1514 private static int sNextAccessibilityViewId;
1515
1516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * The animation currently associated with this view.
1518 * @hide
1519 */
1520 protected Animation mCurrentAnimation = null;
1521
1522 /**
1523 * Width as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
1530 * Height as measured during measure pass.
1531 * {@hide}
1532 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001533 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001534 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535
1536 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001537 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1538 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1539 * its display list. This flag, used only when hw accelerated, allows us to clear the
1540 * flag while retaining this information until it's needed (at getDisplayList() time and
1541 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1542 *
1543 * {@hide}
1544 */
1545 boolean mRecreateDisplayList = false;
1546
1547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 * The view's identifier.
1549 * {@hide}
1550 *
1551 * @see #setId(int)
1552 * @see #getId()
1553 */
1554 @ViewDebug.ExportedProperty(resolveId = true)
1555 int mID = NO_ID;
1556
1557 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001558 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001559 */
1560 int mAccessibilityViewId = NO_ID;
1561
1562 /**
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001563 * @hide
1564 */
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07001565 private int mAccessibilityCursorPosition = ACCESSIBILITY_CURSOR_POSITION_UNDEFINED;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07001566
1567 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 * The view's tag.
1569 * {@hide}
1570 *
1571 * @see #setTag(Object)
1572 * @see #getTag()
1573 */
1574 protected Object mTag;
1575
1576 // for mPrivateFlags:
1577 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001578 static final int PFLAG_WANTS_FOCUS = 0x00000001;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001580 static final int PFLAG_FOCUSED = 0x00000002;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001582 static final int PFLAG_SELECTED = 0x00000004;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001584 static final int PFLAG_IS_ROOT_NAMESPACE = 0x00000008;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001586 static final int PFLAG_HAS_BOUNDS = 0x00000010;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001588 static final int PFLAG_DRAWN = 0x00000020;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 /**
1590 * When this flag is set, this view is running an animation on behalf of its
1591 * children and should therefore not cancel invalidate requests, even if they
1592 * lie outside of this view's bounds.
1593 *
1594 * {@hide}
1595 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001596 static final int PFLAG_DRAW_ANIMATION = 0x00000040;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001598 static final int PFLAG_SKIP_DRAW = 0x00000080;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001600 static final int PFLAG_ONLY_DRAWS_BACKGROUND = 0x00000100;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001602 static final int PFLAG_REQUEST_TRANSPARENT_REGIONS = 0x00000200;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001604 static final int PFLAG_DRAWABLE_STATE_DIRTY = 0x00000400;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001606 static final int PFLAG_MEASURED_DIMENSION_SET = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001608 static final int PFLAG_FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001609 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001610 static final int PFLAG_LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611
Dianne Hackborn4702a852012-08-17 15:18:29 -07001612 private static final int PFLAG_PRESSED = 0x00004000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
1614 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001615 static final int PFLAG_DRAWING_CACHE_VALID = 0x00008000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 /**
1617 * Flag used to indicate that this view should be drawn once more (and only once
1618 * more) after its animation has completed.
1619 * {@hide}
1620 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001621 static final int PFLAG_ANIMATION_STARTED = 0x00010000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622
Dianne Hackborn4702a852012-08-17 15:18:29 -07001623 private static final int PFLAG_SAVE_STATE_CALLED = 0x00020000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
1625 /**
1626 * Indicates that the View returned true when onSetAlpha() was called and that
1627 * the alpha must be restored.
1628 * {@hide}
1629 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001630 static final int PFLAG_ALPHA_SET = 0x00040000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631
1632 /**
1633 * Set by {@link #setScrollContainer(boolean)}.
1634 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001635 static final int PFLAG_SCROLL_CONTAINER = 0x00080000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 /**
1638 * Set by {@link #setScrollContainer(boolean)}.
1639 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001640 static final int PFLAG_SCROLL_CONTAINER_ADDED = 0x00100000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
1642 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001643 * View flag indicating whether this view was invalidated (fully or partially.)
1644 *
1645 * @hide
1646 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001647 static final int PFLAG_DIRTY = 0x00200000;
Romain Guy809a7f62009-05-14 15:44:42 -07001648
1649 /**
1650 * View flag indicating whether this view was invalidated by an opaque
1651 * invalidate request.
1652 *
1653 * @hide
1654 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001655 static final int PFLAG_DIRTY_OPAQUE = 0x00400000;
Romain Guy809a7f62009-05-14 15:44:42 -07001656
1657 /**
Dianne Hackborn4702a852012-08-17 15:18:29 -07001658 * Mask for {@link #PFLAG_DIRTY} and {@link #PFLAG_DIRTY_OPAQUE}.
Romain Guy809a7f62009-05-14 15:44:42 -07001659 *
1660 * @hide
1661 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001662 static final int PFLAG_DIRTY_MASK = 0x00600000;
Romain Guy809a7f62009-05-14 15:44:42 -07001663
1664 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001665 * Indicates whether the background is opaque.
1666 *
1667 * @hide
1668 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001669 static final int PFLAG_OPAQUE_BACKGROUND = 0x00800000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001670
1671 /**
1672 * Indicates whether the scrollbars are opaque.
1673 *
1674 * @hide
1675 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001676 static final int PFLAG_OPAQUE_SCROLLBARS = 0x01000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001677
1678 /**
1679 * Indicates whether the view is opaque.
1680 *
1681 * @hide
1682 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001683 static final int PFLAG_OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001684
Adam Powelle14579b2009-12-16 18:39:52 -08001685 /**
1686 * Indicates a prepressed state;
1687 * the short time between ACTION_DOWN and recognizing
1688 * a 'real' press. Prepressed is used to recognize quick taps
1689 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001690 *
Adam Powelle14579b2009-12-16 18:39:52 -08001691 * @hide
1692 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001693 private static final int PFLAG_PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001694
Adam Powellc9fbaab2010-02-16 17:16:19 -08001695 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001696 * Indicates whether the view is temporarily detached.
1697 *
1698 * @hide
1699 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001700 static final int PFLAG_CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001701
Adam Powell8568c3a2010-04-19 14:26:11 -07001702 /**
1703 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001704 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001705 * @hide
1706 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001707 private static final int PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001708
1709 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001710 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1711 * @hide
1712 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001713 private static final int PFLAG_HOVERED = 0x10000000;
Jeff Browna032cc02011-03-07 16:56:21 -08001714
1715 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001716 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1717 * for transform operations
1718 *
1719 * @hide
1720 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001721 private static final int PFLAG_PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001722
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001723 /** {@hide} */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001724 static final int PFLAG_ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001725
Chet Haasefd2b0022010-08-06 13:08:56 -07001726 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001727 * Indicates that this view was specifically invalidated, not just dirtied because some
1728 * child view was invalidated. The flag is used to determine when we need to recreate
1729 * a view's display list (as opposed to just returning a reference to its existing
1730 * display list).
1731 *
1732 * @hide
1733 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001734 static final int PFLAG_INVALIDATED = 0x80000000;
Chet Haasedaf98e92011-01-10 14:10:36 -08001735
Jeff Sharkey36901b62012-09-19 19:06:22 -07001736 /**
1737 * Masks for mPrivateFlags2, as generated by dumpFlags():
1738 *
1739 * -------|-------|-------|-------|
1740 * PFLAG2_TEXT_ALIGNMENT_FLAGS[0]
1741 * PFLAG2_TEXT_DIRECTION_FLAGS[0]
1742 * 1 PFLAG2_DRAG_CAN_ACCEPT
1743 * 1 PFLAG2_DRAG_HOVERED
1744 * 1 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT
1745 * 11 PFLAG2_TEXT_DIRECTION_MASK_SHIFT
1746 * 1 1 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT
1747 * 11 PFLAG2_LAYOUT_DIRECTION_MASK
1748 * 11 1 PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
1749 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL
1750 * 1 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT
1751 * 1 1 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT
1752 * 1 PFLAG2_LAYOUT_DIRECTION_RESOLVED
1753 * 11 PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK
1754 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[1]
1755 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[2]
1756 * 11 PFLAG2_TEXT_DIRECTION_FLAGS[3]
1757 * 1 PFLAG2_TEXT_DIRECTION_FLAGS[4]
1758 * 1 1 PFLAG2_TEXT_DIRECTION_FLAGS[5]
1759 * 111 PFLAG2_TEXT_DIRECTION_MASK
1760 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED
1761 * 1 PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT
1762 * 111 PFLAG2_TEXT_DIRECTION_RESOLVED_MASK
1763 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[1]
1764 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[2]
1765 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[3]
1766 * 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[4]
1767 * 1 1 PFLAG2_TEXT_ALIGNMENT_FLAGS[5]
1768 * 11 PFLAG2_TEXT_ALIGNMENT_FLAGS[6]
1769 * 111 PFLAG2_TEXT_ALIGNMENT_MASK
1770 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED
1771 * 1 PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT
1772 * 111 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK
1773 * 11 PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK
1774 * 1 PFLAG2_HAS_TRANSIENT_STATE
1775 * 1 PFLAG2_ACCESSIBILITY_FOCUSED
1776 * 1 PFLAG2_ACCESSIBILITY_STATE_CHANGED
1777 * 1 PFLAG2_VIEW_QUICK_REJECTED
1778 * 1 PFLAG2_PADDING_RESOLVED
1779 * -------|-------|-------|-------|
1780 */
Christopher Tate3d4bf172011-03-28 16:16:46 -07001781
1782 /**
1783 * Indicates that this view has reported that it can accept the current drag's content.
1784 * Cleared when the drag operation concludes.
1785 * @hide
1786 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001787 static final int PFLAG2_DRAG_CAN_ACCEPT = 0x00000001;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001788
1789 /**
1790 * Indicates that this view is currently directly under the drag location in a
1791 * drag-and-drop operation involving content that it can accept. Cleared when
1792 * the drag exits the view, or when the drag operation concludes.
1793 * @hide
1794 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001795 static final int PFLAG2_DRAG_HOVERED = 0x00000002;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001796
Cibu Johny86666632010-02-22 13:01:02 -08001797 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798 * Horizontal layout direction of this view is from Left to Right.
1799 * Use with {@link #setLayoutDirection}.
Cibu Johny86666632010-02-22 13:01:02 -08001800 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001801 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001802
1803 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001804 * Horizontal layout direction of this view is from Right to Left.
1805 * Use with {@link #setLayoutDirection}.
1806 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001807 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001808
1809 /**
1810 * Horizontal layout direction of this view is inherited from its parent.
1811 * Use with {@link #setLayoutDirection}.
1812 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001813 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001814
1815 /**
1816 * Horizontal layout direction of this view is from deduced from the default language
1817 * script for the locale. Use with {@link #setLayoutDirection}.
1818 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001819 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001820
1821 /**
1822 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001823 * @hide
1824 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001825 static final int PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001826
1827 /**
1828 * Mask for use with private flags indicating bits used for horizontal layout direction.
1829 * @hide
1830 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001831 static final int PFLAG2_LAYOUT_DIRECTION_MASK = 0x00000003 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001832
1833 /**
1834 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1835 * right-to-left direction.
1836 * @hide
1837 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001838 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL = 4 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001839
1840 /**
1841 * Indicates whether the view horizontal layout direction has been resolved.
1842 * @hide
1843 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001844 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED = 8 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001845
1846 /**
1847 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1848 * @hide
1849 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001850 static final int PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C
1851 << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001852
1853 /*
1854 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1855 * flag value.
1856 * @hide
1857 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001858 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1859 LAYOUT_DIRECTION_LTR,
1860 LAYOUT_DIRECTION_RTL,
1861 LAYOUT_DIRECTION_INHERIT,
1862 LAYOUT_DIRECTION_LOCALE
1863 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001864
1865 /**
1866 * Default horizontal layout direction.
1867 * @hide
1868 */
1869 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001870
Adam Powell539ee872012-02-03 19:00:49 -08001871 /**
1872 * Indicates that the view is tracking some sort of transient state
1873 * that the app should not need to be aware of, but that the framework
1874 * should take special care to preserve.
1875 *
1876 * @hide
1877 */
Jeff Sharkey36901b62012-09-19 19:06:22 -07001878 static final int PFLAG2_HAS_TRANSIENT_STATE = 0x1 << 22;
Adam Powell539ee872012-02-03 19:00:49 -08001879
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001880 /**
1881 * Text direction is inherited thru {@link ViewGroup}
1882 */
1883 public static final int TEXT_DIRECTION_INHERIT = 0;
1884
1885 /**
1886 * Text direction is using "first strong algorithm". The first strong directional character
1887 * determines the paragraph direction. If there is no strong directional character, the
1888 * paragraph direction is the view's resolved layout direction.
1889 */
1890 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1891
1892 /**
1893 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1894 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1895 * If there are neither, the paragraph direction is the view's resolved layout direction.
1896 */
1897 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1898
1899 /**
1900 * Text direction is forced to LTR.
1901 */
1902 public static final int TEXT_DIRECTION_LTR = 3;
1903
1904 /**
1905 * Text direction is forced to RTL.
1906 */
1907 public static final int TEXT_DIRECTION_RTL = 4;
1908
1909 /**
1910 * Text direction is coming from the system Locale.
1911 */
1912 public static final int TEXT_DIRECTION_LOCALE = 5;
1913
1914 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001915 * Default text direction is inherited
1916 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001917 public static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001918
1919 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001920 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1921 * @hide
1922 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001923 static final int PFLAG2_TEXT_DIRECTION_MASK_SHIFT = 6;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001924
1925 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001926 * Mask for use with private flags indicating bits used for text direction.
1927 * @hide
1928 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001929 static final int PFLAG2_TEXT_DIRECTION_MASK = 0x00000007
1930 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001931
1932 /**
1933 * Array of text direction flags for mapping attribute "textDirection" to correct
1934 * flag value.
1935 * @hide
1936 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001937 private static final int[] PFLAG2_TEXT_DIRECTION_FLAGS = {
1938 TEXT_DIRECTION_INHERIT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1939 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1940 TEXT_DIRECTION_ANY_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1941 TEXT_DIRECTION_LTR << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1942 TEXT_DIRECTION_RTL << PFLAG2_TEXT_DIRECTION_MASK_SHIFT,
1943 TEXT_DIRECTION_LOCALE << PFLAG2_TEXT_DIRECTION_MASK_SHIFT
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001944 };
1945
1946 /**
1947 * Indicates whether the view text direction has been resolved.
1948 * @hide
1949 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001950 static final int PFLAG2_TEXT_DIRECTION_RESOLVED = 0x00000008
1951 << PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001952
1953 /**
1954 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1955 * @hide
1956 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001957 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001958
1959 /**
1960 * Mask for use with private flags indicating bits used for resolved text direction.
1961 * @hide
1962 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001963 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_MASK = 0x00000007
1964 << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001965
1966 /**
1967 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1968 * @hide
1969 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07001970 static final int PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT =
1971 TEXT_DIRECTION_FIRST_STRONG << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001972
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001973 /*
1974 * Default text alignment. The text alignment of this View is inherited from its parent.
1975 * Use with {@link #setTextAlignment(int)}
1976 */
1977 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1978
1979 /**
1980 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1981 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1982 *
1983 * Use with {@link #setTextAlignment(int)}
1984 */
1985 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1986
1987 /**
1988 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1989 *
1990 * Use with {@link #setTextAlignment(int)}
1991 */
1992 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1993
1994 /**
1995 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1996 *
1997 * Use with {@link #setTextAlignment(int)}
1998 */
1999 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
2000
2001 /**
2002 * Center the paragraph, e.g. ALIGN_CENTER.
2003 *
2004 * Use with {@link #setTextAlignment(int)}
2005 */
2006 public static final int TEXT_ALIGNMENT_CENTER = 4;
2007
2008 /**
2009 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2010 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2011 *
2012 * Use with {@link #setTextAlignment(int)}
2013 */
2014 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2015
2016 /**
2017 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2018 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2019 *
2020 * Use with {@link #setTextAlignment(int)}
2021 */
2022 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2023
2024 /**
2025 * Default text alignment is inherited
2026 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002027 public static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002028
2029 /**
2030 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2031 * @hide
2032 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002033 static final int PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT = 13;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002034
2035 /**
2036 * Mask for use with private flags indicating bits used for text alignment.
2037 * @hide
2038 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002039 static final int PFLAG2_TEXT_ALIGNMENT_MASK = 0x00000007 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002040
2041 /**
2042 * Array of text direction flags for mapping attribute "textAlignment" to correct
2043 * flag value.
2044 * @hide
2045 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002046 private static final int[] PFLAG2_TEXT_ALIGNMENT_FLAGS = {
2047 TEXT_ALIGNMENT_INHERIT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_TEXT_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_TEXT_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2051 TEXT_ALIGNMENT_CENTER << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2052 TEXT_ALIGNMENT_VIEW_START << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT,
2053 TEXT_ALIGNMENT_VIEW_END << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002054 };
2055
2056 /**
2057 * Indicates whether the view text alignment has been resolved.
2058 * @hide
2059 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002060 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED = 0x00000008 << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002061
2062 /**
2063 * Bit shift to get the resolved text alignment.
2064 * @hide
2065 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002066 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002067
2068 /**
2069 * Mask for use with private flags indicating bits used for text alignment.
2070 * @hide
2071 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002072 static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007
2073 << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002074
2075 /**
2076 * Indicates whether if the view text alignment has been resolved to gravity
2077 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002078 private static final int PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2079 TEXT_ALIGNMENT_GRAVITY << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002080
Svetoslav Ganov42138042012-03-20 11:51:39 -07002081 // Accessiblity constants for mPrivateFlags2
2082
2083 /**
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002084 * Shift for the bits in {@link #mPrivateFlags2} related to the
2085 * "importantForAccessibility" attribute.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002086 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002087 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002088
2089 /**
2090 * Automatically determine whether a view is important for accessibility.
2091 */
2092 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2093
2094 /**
2095 * The view is important for accessibility.
2096 */
2097 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2098
2099 /**
2100 * The view is not important for accessibility.
2101 */
2102 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2103
2104 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07002105 * The default whether the view is important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07002106 */
2107 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2108
2109 /**
2110 * Mask for obtainig the bits which specify how to determine
2111 * whether a view is important for accessibility.
2112 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002113 static final int PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
Svetoslav Ganov42138042012-03-20 11:51:39 -07002114 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
Dianne Hackborn4702a852012-08-17 15:18:29 -07002115 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002116
2117 /**
2118 * Flag indicating whether a view has accessibility focus.
2119 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002120 static final int PFLAG2_ACCESSIBILITY_FOCUSED = 0x00000040 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07002121
2122 /**
2123 * Flag indicating whether a view state for accessibility has changed.
2124 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002125 static final int PFLAG2_ACCESSIBILITY_STATE_CHANGED = 0x00000080
2126 << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002127
Chet Haaseafd5c3e2012-05-10 13:21:10 -07002128 /**
Chet Haase1a3ab172012-05-11 08:41:20 -07002129 * Flag indicating whether a view failed the quickReject() check in draw(). This condition
2130 * is used to check whether later changes to the view's transform should invalidate the
2131 * view to force the quickReject test to run again.
2132 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002133 static final int PFLAG2_VIEW_QUICK_REJECTED = 0x10000000;
Chet Haase1a3ab172012-05-11 08:41:20 -07002134
Adam Powell0090f202012-08-07 17:15:30 -07002135 /**
2136 * Flag indicating that start/end padding has been resolved into left/right padding
2137 * for use in measurement, layout, drawing, etc. This is set by {@link #resolvePadding()}
2138 * and checked by {@link #measure(int, int)} to determine if padding needs to be resolved
2139 * during measurement. In some special cases this is required such as when an adapter-based
2140 * view measures prospective children without attaching them to a window.
2141 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002142 static final int PFLAG2_PADDING_RESOLVED = 0x20000000;
Adam Powell0090f202012-08-07 17:15:30 -07002143
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002144 /**
2145 * Flag indicating that the start/end drawables has been resolved into left/right ones.
2146 */
2147 static final int PFLAG2_DRAWABLE_RESOLVED = 0x40000000;
2148
2149 /**
2150 * Group of bits indicating that RTL properties resolution is done.
2151 */
2152 static final int ALL_RTL_PROPERTIES_RESOLVED = PFLAG2_LAYOUT_DIRECTION_RESOLVED |
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07002153 PFLAG2_TEXT_DIRECTION_RESOLVED |
2154 PFLAG2_TEXT_ALIGNMENT_RESOLVED |
2155 PFLAG2_PADDING_RESOLVED |
2156 PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07002157
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07002158 // There are a couple of flags left in mPrivateFlags2
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07002159
Christopher Tate3d4bf172011-03-28 16:16:46 -07002160 /* End of masks for mPrivateFlags2 */
2161
Chet Haase21433372012-06-05 07:54:09 -07002162 /* Masks for mPrivateFlags3 */
2163
2164 /**
2165 * Flag indicating that view has a transform animation set on it. This is used to track whether
2166 * an animation is cleared between successive frames, in order to tell the associated
2167 * DisplayList to clear its animation matrix.
2168 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002169 static final int PFLAG3_VIEW_IS_ANIMATING_TRANSFORM = 0x1;
Chet Haase21433372012-06-05 07:54:09 -07002170
2171 /**
2172 * Flag indicating that view has an alpha animation set on it. This is used to track whether an
2173 * animation is cleared between successive frames, in order to tell the associated
2174 * DisplayList to restore its alpha value.
2175 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07002176 static final int PFLAG3_VIEW_IS_ANIMATING_ALPHA = 0x2;
Chet Haase21433372012-06-05 07:54:09 -07002177
2178
2179 /* End of masks for mPrivateFlags3 */
2180
Dianne Hackborn4702a852012-08-17 15:18:29 -07002181 static final int DRAG_MASK = PFLAG2_DRAG_CAN_ACCEPT | PFLAG2_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002182
Chet Haasedaf98e92011-01-10 14:10:36 -08002183 /**
Adam Powell637d3372010-08-25 14:37:03 -07002184 * Always allow a user to over-scroll this view, provided it is a
2185 * view that can scroll.
2186 *
2187 * @see #getOverScrollMode()
2188 * @see #setOverScrollMode(int)
2189 */
2190 public static final int OVER_SCROLL_ALWAYS = 0;
2191
2192 /**
2193 * Allow a user to over-scroll this view only if the content is large
2194 * enough to meaningfully scroll, provided it is a view that can scroll.
2195 *
2196 * @see #getOverScrollMode()
2197 * @see #setOverScrollMode(int)
2198 */
2199 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2200
2201 /**
2202 * Never allow a user to over-scroll this view.
2203 *
2204 * @see #getOverScrollMode()
2205 * @see #setOverScrollMode(int)
2206 */
2207 public static final int OVER_SCROLL_NEVER = 2;
2208
2209 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002210 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2211 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002212 *
Joe Malin32736f02011-01-19 16:14:20 -08002213 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002214 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002215 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002216
2217 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002218 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2219 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002220 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002221 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002222 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002223 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002224 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002225 *
Joe Malin32736f02011-01-19 16:14:20 -08002226 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002227 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002228 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2229
2230 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002231 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2232 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002233 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002234 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002235 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2236 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2237 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002238 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002239 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2240 * window flags) for displaying content using every last pixel on the display.
2241 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002242 * <p>There is a limitation: because navigation controls are so important, the least user
2243 * interaction will cause them to reappear immediately. When this happens, both
2244 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2245 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002246 *
2247 * @see #setSystemUiVisibility(int)
2248 */
2249 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2250
2251 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002252 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2253 * into the normal fullscreen mode so that its content can take over the screen
2254 * while still allowing the user to interact with the application.
2255 *
2256 * <p>This has the same visual effect as
2257 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2258 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2259 * meaning that non-critical screen decorations (such as the status bar) will be
2260 * hidden while the user is in the View's window, focusing the experience on
2261 * that content. Unlike the window flag, if you are using ActionBar in
2262 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2263 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2264 * hide the action bar.
2265 *
2266 * <p>This approach to going fullscreen is best used over the window flag when
2267 * it is a transient state -- that is, the application does this at certain
2268 * points in its user interaction where it wants to allow the user to focus
2269 * on content, but not as a continuous state. For situations where the application
2270 * would like to simply stay full screen the entire time (such as a game that
2271 * wants to take over the screen), the
2272 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2273 * is usually a better approach. The state set here will be removed by the system
2274 * in various situations (such as the user moving to another application) like
2275 * the other system UI states.
2276 *
2277 * <p>When using this flag, the application should provide some easy facility
2278 * for the user to go out of it. A common example would be in an e-book
2279 * reader, where tapping on the screen brings back whatever screen and UI
2280 * decorations that had been hidden while the user was immersed in reading
2281 * the book.
2282 *
2283 * @see #setSystemUiVisibility(int)
2284 */
2285 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2286
2287 /**
2288 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2289 * flags, we would like a stable view of the content insets given to
2290 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2291 * will always represent the worst case that the application can expect
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002292 * as a continuous state. In the stock Android UI this is the space for
2293 * the system bar, nav bar, and status bar, but not more transient elements
2294 * such as an input method.
2295 *
2296 * The stable layout your UI sees is based on the system UI modes you can
2297 * switch to. That is, if you specify {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
2298 * then you will get a stable layout for changes of the
2299 * {@link #SYSTEM_UI_FLAG_FULLSCREEN} mode; if you specify
2300 * {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
2301 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION}, then you can transition
2302 * to {@link #SYSTEM_UI_FLAG_FULLSCREEN} and {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}
2303 * with a stable layout. (Note that you should avoid using
2304 * {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} by itself.)
2305 *
Jeff Smitha45746e2012-07-19 14:19:24 -05002306 * If you have set the window flag {@link WindowManager.LayoutParams#FLAG_FULLSCREEN}
Dianne Hackborn5b5cc4d2012-05-16 13:15:00 -07002307 * to hide the status bar (instead of using {@link #SYSTEM_UI_FLAG_FULLSCREEN}),
2308 * then a hidden status bar will be considered a "stable" state for purposes
2309 * here. This allows your UI to continually hide the status bar, while still
2310 * using the system UI flags to hide the action bar while still retaining
2311 * a stable layout. Note that changing the window fullscreen flag will never
2312 * provide a stable layout for a clean transition.
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002313 *
2314 * <p>If you are using ActionBar in
2315 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2316 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2317 * insets it adds to those given to the application.
2318 */
2319 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2320
2321 /**
2322 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2323 * to be layed out as if it has requested
2324 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2325 * allows it to avoid artifacts when switching in and out of that mode, at
2326 * the expense that some of its user interface may be covered by screen
2327 * decorations when they are shown. You can perform layout of your inner
2328 * UI elements to account for the navagation system UI through the
2329 * {@link #fitSystemWindows(Rect)} method.
2330 */
2331 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2332
2333 /**
2334 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2335 * to be layed out as if it has requested
2336 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2337 * allows it to avoid artifacts when switching in and out of that mode, at
2338 * the expense that some of its user interface may be covered by screen
2339 * decorations when they are shown. You can perform layout of your inner
2340 * UI elements to account for non-fullscreen system UI through the
2341 * {@link #fitSystemWindows(Rect)} method.
2342 */
2343 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2344
2345 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002346 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2347 */
2348 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2349
2350 /**
2351 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2352 */
2353 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002354
2355 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002356 * @hide
2357 *
2358 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2359 * out of the public fields to keep the undefined bits out of the developer's way.
2360 *
2361 * Flag to make the status bar not expandable. Unless you also
2362 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2363 */
2364 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2365
2366 /**
2367 * @hide
2368 *
2369 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2370 * out of the public fields to keep the undefined bits out of the developer's way.
2371 *
2372 * Flag to hide notification icons and scrolling ticker text.
2373 */
2374 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2375
2376 /**
2377 * @hide
2378 *
2379 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2380 * out of the public fields to keep the undefined bits out of the developer's way.
2381 *
2382 * Flag to disable incoming notification alerts. This will not block
2383 * icons, but it will block sound, vibrating and other visual or aural notifications.
2384 */
2385 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2386
2387 /**
2388 * @hide
2389 *
2390 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2391 * out of the public fields to keep the undefined bits out of the developer's way.
2392 *
2393 * Flag to hide only the scrolling ticker. Note that
2394 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2395 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2396 */
2397 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2398
2399 /**
2400 * @hide
2401 *
2402 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2403 * out of the public fields to keep the undefined bits out of the developer's way.
2404 *
2405 * Flag to hide the center system info area.
2406 */
2407 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2408
2409 /**
2410 * @hide
2411 *
2412 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2413 * out of the public fields to keep the undefined bits out of the developer's way.
2414 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002415 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002416 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2417 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002418 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002419
2420 /**
2421 * @hide
2422 *
2423 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2424 * out of the public fields to keep the undefined bits out of the developer's way.
2425 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002426 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002427 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2428 */
2429 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2430
2431 /**
2432 * @hide
2433 *
2434 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2435 * out of the public fields to keep the undefined bits out of the developer's way.
2436 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002437 * Flag to hide only the clock. You might use this if your activity has
2438 * its own clock making the status bar's clock redundant.
2439 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002440 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2441
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002442 /**
2443 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002444 *
2445 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2446 * out of the public fields to keep the undefined bits out of the developer's way.
2447 *
2448 * Flag to hide only the recent apps button. Don't use this
2449 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2450 */
2451 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2452
2453 /**
2454 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002455 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002456 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002457
2458 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002459 * These are the system UI flags that can be cleared by events outside
2460 * of an application. Currently this is just the ability to tap on the
2461 * screen while hiding the navigation bar to have it return.
2462 * @hide
2463 */
2464 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002465 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2466 | SYSTEM_UI_FLAG_FULLSCREEN;
2467
2468 /**
2469 * Flags that can impact the layout in relation to system UI.
2470 */
2471 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2472 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2473 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002474
2475 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002476 * Find views that render the specified text.
2477 *
2478 * @see #findViewsWithText(ArrayList, CharSequence, int)
2479 */
2480 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2481
2482 /**
2483 * Find find views that contain the specified content description.
2484 *
2485 * @see #findViewsWithText(ArrayList, CharSequence, int)
2486 */
2487 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2488
2489 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002490 * Find views that contain {@link AccessibilityNodeProvider}. Such
2491 * a View is a root of virtual view hierarchy and may contain the searched
2492 * text. If this flag is set Views with providers are automatically
2493 * added and it is a responsibility of the client to call the APIs of
2494 * the provider to determine whether the virtual tree rooted at this View
2495 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2496 * represeting the virtual views with this text.
2497 *
2498 * @see #findViewsWithText(ArrayList, CharSequence, int)
2499 *
2500 * @hide
2501 */
2502 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2503
2504 /**
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07002505 * The undefined cursor position.
2506 */
2507 private static final int ACCESSIBILITY_CURSOR_POSITION_UNDEFINED = -1;
2508
2509 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002510 * Indicates that the screen has changed state and is now off.
2511 *
2512 * @see #onScreenStateChanged(int)
2513 */
2514 public static final int SCREEN_STATE_OFF = 0x0;
2515
2516 /**
2517 * Indicates that the screen has changed state and is now on.
2518 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002519 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002520 */
2521 public static final int SCREEN_STATE_ON = 0x1;
2522
2523 /**
Adam Powell637d3372010-08-25 14:37:03 -07002524 * Controls the over-scroll mode for this view.
2525 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2526 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2527 * and {@link #OVER_SCROLL_NEVER}.
2528 */
2529 private int mOverScrollMode;
2530
2531 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 * The parent this view is attached to.
2533 * {@hide}
2534 *
2535 * @see #getParent()
2536 */
2537 protected ViewParent mParent;
2538
2539 /**
2540 * {@hide}
2541 */
2542 AttachInfo mAttachInfo;
2543
2544 /**
2545 * {@hide}
2546 */
Romain Guy809a7f62009-05-14 15:44:42 -07002547 @ViewDebug.ExportedProperty(flagMapping = {
Dianne Hackborn4702a852012-08-17 15:18:29 -07002548 @ViewDebug.FlagToString(mask = PFLAG_FORCE_LAYOUT, equals = PFLAG_FORCE_LAYOUT,
Romain Guy809a7f62009-05-14 15:44:42 -07002549 name = "FORCE_LAYOUT"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002550 @ViewDebug.FlagToString(mask = PFLAG_LAYOUT_REQUIRED, equals = PFLAG_LAYOUT_REQUIRED,
Romain Guy809a7f62009-05-14 15:44:42 -07002551 name = "LAYOUT_REQUIRED"),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002552 @ViewDebug.FlagToString(mask = PFLAG_DRAWING_CACHE_VALID, equals = PFLAG_DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002553 name = "DRAWING_CACHE_INVALID", outputIf = false),
Dianne Hackborn4702a852012-08-17 15:18:29 -07002554 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "DRAWN", outputIf = true),
2555 @ViewDebug.FlagToString(mask = PFLAG_DRAWN, equals = PFLAG_DRAWN, name = "NOT_DRAWN", outputIf = false),
2556 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2557 @ViewDebug.FlagToString(mask = PFLAG_DIRTY_MASK, equals = PFLAG_DIRTY, name = "DIRTY")
Romain Guy809a7f62009-05-14 15:44:42 -07002558 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002560 int mPrivateFlags2;
Chet Haase21433372012-06-05 07:54:09 -07002561 int mPrivateFlags3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562
2563 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002564 * This view's request for the visibility of the status bar.
2565 * @hide
2566 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002567 @ViewDebug.ExportedProperty(flagMapping = {
2568 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2569 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2570 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2571 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2572 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2573 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2574 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2575 equals = SYSTEM_UI_FLAG_VISIBLE,
2576 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2577 })
Joe Onorato664644d2011-01-23 17:53:23 -08002578 int mSystemUiVisibility;
2579
2580 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002581 * Reference count for transient state.
2582 * @see #setHasTransientState(boolean)
2583 */
2584 int mTransientStateCount = 0;
2585
2586 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 * Count of how many windows this view has been attached to.
2588 */
2589 int mWindowAttachCount;
2590
2591 /**
2592 * The layout parameters associated with this view and used by the parent
2593 * {@link android.view.ViewGroup} to determine how this view should be
2594 * laid out.
2595 * {@hide}
2596 */
2597 protected ViewGroup.LayoutParams mLayoutParams;
2598
2599 /**
2600 * The view flags hold various views states.
2601 * {@hide}
2602 */
2603 @ViewDebug.ExportedProperty
2604 int mViewFlags;
2605
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002606 static class TransformationInfo {
2607 /**
2608 * The transform matrix for the View. This transform is calculated internally
2609 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2610 * is used by default. Do *not* use this variable directly; instead call
2611 * getMatrix(), which will automatically recalculate the matrix if necessary
2612 * to get the correct matrix based on the latest rotation and scale properties.
2613 */
2614 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002615
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002616 /**
2617 * The transform matrix for the View. This transform is calculated internally
2618 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2619 * is used by default. Do *not* use this variable directly; instead call
2620 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2621 * to get the correct matrix based on the latest rotation and scale properties.
2622 */
2623 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002624
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002625 /**
2626 * An internal variable that tracks whether we need to recalculate the
2627 * transform matrix, based on whether the rotation or scaleX/Y properties
2628 * have changed since the matrix was last calculated.
2629 */
2630 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002631
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002632 /**
2633 * An internal variable that tracks whether we need to recalculate the
2634 * transform matrix, based on whether the rotation or scaleX/Y properties
2635 * have changed since the matrix was last calculated.
2636 */
2637 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002638
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002639 /**
2640 * A variable that tracks whether we need to recalculate the
2641 * transform matrix, based on whether the rotation or scaleX/Y properties
2642 * have changed since the matrix was last calculated. This variable
2643 * is only valid after a call to updateMatrix() or to a function that
2644 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2645 */
2646 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002647
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002648 /**
2649 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2650 */
2651 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002652
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002653 /**
2654 * This matrix is used when computing the matrix for 3D rotations.
2655 */
2656 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002657
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002658 /**
2659 * These prev values are used to recalculate a centered pivot point when necessary. The
2660 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2661 * set), so thes values are only used then as well.
2662 */
2663 private int mPrevWidth = -1;
2664 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002665
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002666 /**
2667 * The degrees rotation around the vertical axis through the pivot point.
2668 */
2669 @ViewDebug.ExportedProperty
2670 float mRotationY = 0f;
2671
2672 /**
2673 * The degrees rotation around the horizontal axis through the pivot point.
2674 */
2675 @ViewDebug.ExportedProperty
2676 float mRotationX = 0f;
2677
2678 /**
2679 * The degrees rotation around the pivot point.
2680 */
2681 @ViewDebug.ExportedProperty
2682 float mRotation = 0f;
2683
2684 /**
2685 * The amount of translation of the object away from its left property (post-layout).
2686 */
2687 @ViewDebug.ExportedProperty
2688 float mTranslationX = 0f;
2689
2690 /**
2691 * The amount of translation of the object away from its top property (post-layout).
2692 */
2693 @ViewDebug.ExportedProperty
2694 float mTranslationY = 0f;
2695
2696 /**
2697 * The amount of scale in the x direction around the pivot point. A
2698 * value of 1 means no scaling is applied.
2699 */
2700 @ViewDebug.ExportedProperty
2701 float mScaleX = 1f;
2702
2703 /**
2704 * The amount of scale in the y direction around the pivot point. A
2705 * value of 1 means no scaling is applied.
2706 */
2707 @ViewDebug.ExportedProperty
2708 float mScaleY = 1f;
2709
2710 /**
Chet Haasea33de552012-02-03 16:28:24 -08002711 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002712 */
2713 @ViewDebug.ExportedProperty
2714 float mPivotX = 0f;
2715
2716 /**
Chet Haasea33de552012-02-03 16:28:24 -08002717 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002718 */
2719 @ViewDebug.ExportedProperty
2720 float mPivotY = 0f;
2721
2722 /**
2723 * The opacity of the View. This is a value from 0 to 1, where 0 means
2724 * completely transparent and 1 means completely opaque.
2725 */
2726 @ViewDebug.ExportedProperty
2727 float mAlpha = 1f;
2728 }
2729
2730 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002731
Joe Malin32736f02011-01-19 16:14:20 -08002732 private boolean mLastIsOpaque;
2733
Chet Haasefd2b0022010-08-06 13:08:56 -07002734 /**
2735 * Convenience value to check for float values that are close enough to zero to be considered
2736 * zero.
2737 */
Romain Guy2542d192010-08-18 11:47:12 -07002738 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002739
2740 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 * The distance in pixels from the left edge of this view's parent
2742 * to the left edge of this view.
2743 * {@hide}
2744 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002745 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 protected int mLeft;
2747 /**
2748 * The distance in pixels from the left edge of this view's parent
2749 * to the right edge of this view.
2750 * {@hide}
2751 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002752 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 protected int mRight;
2754 /**
2755 * The distance in pixels from the top edge of this view's parent
2756 * to the top edge of this view.
2757 * {@hide}
2758 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002759 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 protected int mTop;
2761 /**
2762 * The distance in pixels from the top edge of this view's parent
2763 * to the bottom edge of this view.
2764 * {@hide}
2765 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002766 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 protected int mBottom;
2768
2769 /**
2770 * The offset, in pixels, by which the content of this view is scrolled
2771 * horizontally.
2772 * {@hide}
2773 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002774 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 protected int mScrollX;
2776 /**
2777 * The offset, in pixels, by which the content of this view is scrolled
2778 * vertically.
2779 * {@hide}
2780 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002781 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 protected int mScrollY;
2783
2784 /**
2785 * The left padding in pixels, that is the distance in pixels between the
2786 * left edge of this view and the left edge of its content.
2787 * {@hide}
2788 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002789 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002790 protected int mPaddingLeft = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 /**
2792 * The right padding in pixels, that is the distance in pixels between the
2793 * right edge of this view and the right edge of its content.
2794 * {@hide}
2795 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002796 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07002797 protected int mPaddingRight = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * The top padding in pixels, that is the distance in pixels between the
2800 * top edge of this view and the top edge of its content.
2801 * {@hide}
2802 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002803 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 protected int mPaddingTop;
2805 /**
2806 * The bottom padding in pixels, that is the distance in pixels between the
2807 * bottom edge of this view and the bottom edge of its content.
2808 * {@hide}
2809 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002810 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 protected int mPaddingBottom;
2812
2813 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002814 * The layout insets in pixels, that is the distance in pixels between the
2815 * visible edges of this view its bounds.
2816 */
2817 private Insets mLayoutInsets;
2818
2819 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002820 * Briefly describes the view and is primarily used for accessibility support.
2821 */
2822 private CharSequence mContentDescription;
2823
2824 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07002825 * Specifies the id of a view for which this view serves as a label for
2826 * accessibility purposes.
2827 */
2828 private int mLabelForId = View.NO_ID;
2829
2830 /**
2831 * Predicate for matching labeled view id with its label for
2832 * accessibility purposes.
2833 */
2834 private MatchLabelForPredicate mMatchLabelForPredicate;
2835
2836 /**
2837 * Predicate for matching a view by its id.
2838 */
2839 private MatchIdPredicate mMatchIdPredicate;
2840
2841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002843 *
2844 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002846 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002847 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848
2849 /**
2850 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002851 *
2852 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002853 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002854 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002855 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002857 /**
Adam Powell20232d02010-12-08 21:08:53 -08002858 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002859 *
2860 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002861 */
2862 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002863 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002864
2865 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002866 * Cache the paddingStart set by the user to append to the scrollbar's size.
2867 *
2868 */
2869 @ViewDebug.ExportedProperty(category = "padding")
2870 int mUserPaddingStart;
2871
2872 /**
2873 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2874 *
2875 */
2876 @ViewDebug.ExportedProperty(category = "padding")
2877 int mUserPaddingEnd;
2878
2879 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002880 * Cache initial left padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002881 *
2882 * @hide
2883 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002884 int mUserPaddingLeftInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002885
2886 /**
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07002887 * Cache initial right padding.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002888 *
2889 * @hide
2890 */
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07002891 int mUserPaddingRightInitial = 0;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07002892
2893 /**
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07002894 * Default undefined padding
2895 */
2896 private static final int UNDEFINED_PADDING = Integer.MIN_VALUE;
2897
2898 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002899 * @hide
2900 */
2901 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2902 /**
2903 * @hide
2904 */
2905 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906
Romain Guydfab3632012-10-03 14:53:25 -07002907 @ViewDebug.ExportedProperty(deepExport = true, prefix = "bg_")
Philip Milne6c8ea062012-04-03 17:38:43 -07002908 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 private int mBackgroundResource;
2911 private boolean mBackgroundSizeChanged;
2912
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002913 static class ListenerInfo {
2914 /**
2915 * Listener used to dispatch focus change events.
2916 * This field should be made private, so it is hidden from the SDK.
2917 * {@hide}
2918 */
2919 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002921 /**
2922 * Listeners for layout change events.
2923 */
2924 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002925
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002926 /**
2927 * Listeners for attach events.
2928 */
2929 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002930
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002931 /**
2932 * Listener used to dispatch click events.
2933 * This field should be made private, so it is hidden from the SDK.
2934 * {@hide}
2935 */
2936 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002938 /**
2939 * Listener used to dispatch long click events.
2940 * This field should be made private, so it is hidden from the SDK.
2941 * {@hide}
2942 */
2943 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002945 /**
2946 * Listener used to build the context menu.
2947 * This field should be made private, so it is hidden from the SDK.
2948 * {@hide}
2949 */
2950 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002952 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002954 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002956 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002957
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002958 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002959
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002960 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002961
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002962 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2963 }
2964
2965 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 /**
2968 * The application environment this view lives in.
2969 * This field should be made private, so it is hidden from the SDK.
2970 * {@hide}
2971 */
2972 protected Context mContext;
2973
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002974 private final Resources mResources;
2975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 private ScrollabilityCache mScrollCache;
2977
2978 private int[] mDrawableState = null;
2979
Romain Guy0211a0a2011-02-14 16:34:59 -08002980 /**
2981 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002982 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002983 * @hide
2984 */
2985 public boolean mCachingFailed;
2986
Romain Guy02890fd2010-08-06 17:58:44 -07002987 private Bitmap mDrawingCache;
2988 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002989 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002990 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991
2992 /**
2993 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2994 * the user may specify which view to go to next.
2995 */
2996 private int mNextFocusLeftId = View.NO_ID;
2997
2998 /**
2999 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
3000 * the user may specify which view to go to next.
3001 */
3002 private int mNextFocusRightId = View.NO_ID;
3003
3004 /**
3005 * When this view has focus and the next focus is {@link #FOCUS_UP},
3006 * the user may specify which view to go to next.
3007 */
3008 private int mNextFocusUpId = View.NO_ID;
3009
3010 /**
3011 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
3012 * the user may specify which view to go to next.
3013 */
3014 private int mNextFocusDownId = View.NO_ID;
3015
Jeff Brown4e6319b2010-12-13 10:36:51 -08003016 /**
3017 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
3018 * the user may specify which view to go to next.
3019 */
3020 int mNextFocusForwardId = View.NO_ID;
3021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08003023 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08003024 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07003025 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08003026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 private UnsetPressedState mUnsetPressedState;
3028
3029 /**
3030 * Whether the long press's action has been invoked. The tap's action is invoked on the
3031 * up event while a long press is invoked as soon as the long press duration is reached, so
3032 * a long press could be performed before the tap is checked, in which case the tap's action
3033 * should not be invoked.
3034 */
3035 private boolean mHasPerformedLongPress;
3036
3037 /**
3038 * The minimum height of the view. We'll try our best to have the height
3039 * of this view to at least this amount.
3040 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003041 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 private int mMinHeight;
3043
3044 /**
3045 * The minimum width of the view. We'll try our best to have the width
3046 * of this view to at least this amount.
3047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003048 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 private int mMinWidth;
3050
3051 /**
3052 * The delegate to handle touch events that are physically in this view
3053 * but should be handled by another view.
3054 */
3055 private TouchDelegate mTouchDelegate = null;
3056
3057 /**
3058 * Solid color to use as a background when creating the drawing cache. Enables
3059 * the cache to use 16 bit bitmaps instead of 32 bit.
3060 */
3061 private int mDrawingCacheBackgroundColor = 0;
3062
3063 /**
3064 * Special tree observer used when mAttachInfo is null.
3065 */
3066 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08003067
Adam Powelle14579b2009-12-16 18:39:52 -08003068 /**
3069 * Cache the touch slop from the context that created the view.
3070 */
3071 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 /**
Chet Haasea00f3862011-02-22 06:34:40 -08003074 * Object that handles automatic animation of view properties.
3075 */
3076 private ViewPropertyAnimator mAnimator = null;
3077
3078 /**
Christopher Tate251602f2011-01-28 17:54:12 -08003079 * Flag indicating that a drag can cross window boundaries. When
3080 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
3081 * with this flag set, all visible applications will be able to participate
3082 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08003083 *
3084 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08003085 */
3086 public static final int DRAG_FLAG_GLOBAL = 1;
3087
3088 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003089 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
3090 */
3091 private float mVerticalScrollFactor;
3092
3093 /**
Adam Powell20232d02010-12-08 21:08:53 -08003094 * Position of the vertical scroll bar.
3095 */
3096 private int mVerticalScrollbarPosition;
3097
3098 /**
3099 * Position the scroll bar at the default position as determined by the system.
3100 */
3101 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
3102
3103 /**
3104 * Position the scroll bar along the left edge.
3105 */
3106 public static final int SCROLLBAR_POSITION_LEFT = 1;
3107
3108 /**
3109 * Position the scroll bar along the right edge.
3110 */
3111 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3112
3113 /**
Romain Guy171c5922011-01-06 10:04:23 -08003114 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003115 *
3116 * @see #getLayerType()
3117 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003118 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003119 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003120 */
3121 public static final int LAYER_TYPE_NONE = 0;
3122
3123 /**
3124 * <p>Indicates that the view has a software layer. A software layer is backed
3125 * by a bitmap and causes the view to be rendered using Android's software
3126 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003127 *
Romain Guy171c5922011-01-06 10:04:23 -08003128 * <p>Software layers have various usages:</p>
3129 * <p>When the application is not using hardware acceleration, a software layer
3130 * is useful to apply a specific color filter and/or blending mode and/or
3131 * translucency to a view and all its children.</p>
3132 * <p>When the application is using hardware acceleration, a software layer
3133 * is useful to render drawing primitives not supported by the hardware
3134 * accelerated pipeline. It can also be used to cache a complex view tree
3135 * into a texture and reduce the complexity of drawing operations. For instance,
3136 * when animating a complex view tree with a translation, a software layer can
3137 * be used to render the view tree only once.</p>
3138 * <p>Software layers should be avoided when the affected view tree updates
3139 * often. Every update will require to re-render the software layer, which can
3140 * potentially be slow (particularly when hardware acceleration is turned on
3141 * since the layer will have to be uploaded into a hardware texture after every
3142 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003143 *
3144 * @see #getLayerType()
3145 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003146 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003147 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003148 */
3149 public static final int LAYER_TYPE_SOFTWARE = 1;
3150
3151 /**
3152 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3153 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3154 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3155 * rendering pipeline, but only if hardware acceleration is turned on for the
3156 * view hierarchy. When hardware acceleration is turned off, hardware layers
3157 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003158 *
Romain Guy171c5922011-01-06 10:04:23 -08003159 * <p>A hardware layer is useful to apply a specific color filter and/or
3160 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003161 * <p>A hardware layer can be used to cache a complex view tree into a
3162 * texture and reduce the complexity of drawing operations. For instance,
3163 * when animating a complex view tree with a translation, a hardware layer can
3164 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003165 * <p>A hardware layer can also be used to increase the rendering quality when
3166 * rotation transformations are applied on a view. It can also be used to
3167 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003168 *
3169 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003170 * @see #setLayerType(int, android.graphics.Paint)
3171 * @see #LAYER_TYPE_NONE
3172 * @see #LAYER_TYPE_SOFTWARE
3173 */
3174 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003175
Romain Guy3aaff3a2011-01-12 14:18:47 -08003176 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3177 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3178 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3179 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3180 })
Romain Guy171c5922011-01-06 10:04:23 -08003181 int mLayerType = LAYER_TYPE_NONE;
3182 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003183 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003184
3185 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003186 * Set to true when the view is sending hover accessibility events because it
3187 * is the innermost hovered view.
3188 */
3189 private boolean mSendingHoverAccessibilityEvents;
3190
Dianne Hackborn4702a852012-08-17 15:18:29 -07003191 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07003192 * Delegate for injecting accessibility functionality.
Dianne Hackborn4702a852012-08-17 15:18:29 -07003193 */
3194 AccessibilityDelegate mAccessibilityDelegate;
3195
3196 /**
3197 * Consistency verifier for debugging purposes.
3198 * @hide
3199 */
3200 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3201 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3202 new InputEventConsistencyVerifier(this, 0) : null;
3203
Adam Powella9108a22012-07-18 11:18:09 -07003204 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
3205
Jeff Brown87b7f802011-06-21 18:35:45 -07003206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 * Simple constructor to use when creating a view from code.
3208 *
3209 * @param context The Context the view is running in, through which it can
3210 * access the current theme, resources, etc.
3211 */
3212 public View(Context context) {
3213 mContext = context;
3214 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003215 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003216 // Set layout and text direction defaults
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003217 mPrivateFlags2 =
3218 (LAYOUT_DIRECTION_DEFAULT << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003219 (TEXT_DIRECTION_DEFAULT << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003220 (PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003221 (TEXT_ALIGNMENT_DEFAULT << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) |
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003222 (PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT) |
Dianne Hackborn4702a852012-08-17 15:18:29 -07003223 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003224 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003225 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003226 mUserPaddingStart = UNDEFINED_PADDING;
3227 mUserPaddingEnd = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 }
3229
3230 /**
3231 * Constructor that is called when inflating a view from XML. This is called
3232 * when a view is being constructed from an XML file, supplying attributes
3233 * that were specified in the XML file. This version uses a default style of
3234 * 0, so the only attribute values applied are those in the Context's Theme
3235 * and the given AttributeSet.
3236 *
3237 * <p>
3238 * The method onFinishInflate() will be called after all children have been
3239 * added.
3240 *
3241 * @param context The Context the view is running in, through which it can
3242 * access the current theme, resources, etc.
3243 * @param attrs The attributes of the XML tag that is inflating the view.
3244 * @see #View(Context, AttributeSet, int)
3245 */
3246 public View(Context context, AttributeSet attrs) {
3247 this(context, attrs, 0);
3248 }
3249
3250 /**
3251 * Perform inflation from XML and apply a class-specific base style. This
3252 * constructor of View allows subclasses to use their own base style when
3253 * they are inflating. For example, a Button class's constructor would call
3254 * this version of the super class constructor and supply
3255 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3256 * the theme's button style to modify all of the base view attributes (in
3257 * particular its background) as well as the Button class's attributes.
3258 *
3259 * @param context The Context the view is running in, through which it can
3260 * access the current theme, resources, etc.
3261 * @param attrs The attributes of the XML tag that is inflating the view.
3262 * @param defStyle The default style to apply to this view. If 0, no style
3263 * will be applied (beyond what is included in the theme). This may
3264 * either be an attribute resource, whose value will be retrieved
3265 * from the current theme, or an explicit style resource.
3266 * @see #View(Context, AttributeSet)
3267 */
3268 public View(Context context, AttributeSet attrs, int defStyle) {
3269 this(context);
3270
3271 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3272 defStyle, 0);
3273
3274 Drawable background = null;
3275
3276 int leftPadding = -1;
3277 int topPadding = -1;
3278 int rightPadding = -1;
3279 int bottomPadding = -1;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003280 int startPadding = UNDEFINED_PADDING;
3281 int endPadding = UNDEFINED_PADDING;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282
3283 int padding = -1;
3284
3285 int viewFlagValues = 0;
3286 int viewFlagMasks = 0;
3287
3288 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 int x = 0;
3291 int y = 0;
3292
Chet Haase73066682010-11-29 15:55:32 -08003293 float tx = 0;
3294 float ty = 0;
3295 float rotation = 0;
3296 float rotationX = 0;
3297 float rotationY = 0;
3298 float sx = 1f;
3299 float sy = 1f;
3300 boolean transformSet = false;
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
Adam Powell637d3372010-08-25 14:37:03 -07003303 int overScrollMode = mOverScrollMode;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003304 boolean initializeScrollbars = false;
3305
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003306 boolean leftPaddingDefined = false;
3307 boolean rightPaddingDefined = false;
3308 boolean startPaddingDefined = false;
3309 boolean endPaddingDefined = false;
3310
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003311 final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
3312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 final int N = a.getIndexCount();
3314 for (int i = 0; i < N; i++) {
3315 int attr = a.getIndex(i);
3316 switch (attr) {
3317 case com.android.internal.R.styleable.View_background:
3318 background = a.getDrawable(attr);
3319 break;
3320 case com.android.internal.R.styleable.View_padding:
3321 padding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003322 mUserPaddingLeftInitial = padding;
3323 mUserPaddingRightInitial = padding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003324 leftPaddingDefined = true;
3325 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 break;
3327 case com.android.internal.R.styleable.View_paddingLeft:
3328 leftPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003329 mUserPaddingLeftInitial = leftPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003330 leftPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 break;
3332 case com.android.internal.R.styleable.View_paddingTop:
3333 topPadding = a.getDimensionPixelSize(attr, -1);
3334 break;
3335 case com.android.internal.R.styleable.View_paddingRight:
3336 rightPadding = a.getDimensionPixelSize(attr, -1);
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003337 mUserPaddingRightInitial = rightPadding;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003338 rightPaddingDefined = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 break;
3340 case com.android.internal.R.styleable.View_paddingBottom:
3341 bottomPadding = a.getDimensionPixelSize(attr, -1);
3342 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003343 case com.android.internal.R.styleable.View_paddingStart:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003344 startPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003345 startPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003346 break;
3347 case com.android.internal.R.styleable.View_paddingEnd:
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003348 endPadding = a.getDimensionPixelSize(attr, UNDEFINED_PADDING);
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003349 endPaddingDefined = true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003350 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 case com.android.internal.R.styleable.View_scrollX:
3352 x = a.getDimensionPixelOffset(attr, 0);
3353 break;
3354 case com.android.internal.R.styleable.View_scrollY:
3355 y = a.getDimensionPixelOffset(attr, 0);
3356 break;
Chet Haase73066682010-11-29 15:55:32 -08003357 case com.android.internal.R.styleable.View_alpha:
3358 setAlpha(a.getFloat(attr, 1f));
3359 break;
3360 case com.android.internal.R.styleable.View_transformPivotX:
3361 setPivotX(a.getDimensionPixelOffset(attr, 0));
3362 break;
3363 case com.android.internal.R.styleable.View_transformPivotY:
3364 setPivotY(a.getDimensionPixelOffset(attr, 0));
3365 break;
3366 case com.android.internal.R.styleable.View_translationX:
3367 tx = a.getDimensionPixelOffset(attr, 0);
3368 transformSet = true;
3369 break;
3370 case com.android.internal.R.styleable.View_translationY:
3371 ty = a.getDimensionPixelOffset(attr, 0);
3372 transformSet = true;
3373 break;
3374 case com.android.internal.R.styleable.View_rotation:
3375 rotation = a.getFloat(attr, 0);
3376 transformSet = true;
3377 break;
3378 case com.android.internal.R.styleable.View_rotationX:
3379 rotationX = a.getFloat(attr, 0);
3380 transformSet = true;
3381 break;
3382 case com.android.internal.R.styleable.View_rotationY:
3383 rotationY = a.getFloat(attr, 0);
3384 transformSet = true;
3385 break;
3386 case com.android.internal.R.styleable.View_scaleX:
3387 sx = a.getFloat(attr, 1f);
3388 transformSet = true;
3389 break;
3390 case com.android.internal.R.styleable.View_scaleY:
3391 sy = a.getFloat(attr, 1f);
3392 transformSet = true;
3393 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 case com.android.internal.R.styleable.View_id:
3395 mID = a.getResourceId(attr, NO_ID);
3396 break;
3397 case com.android.internal.R.styleable.View_tag:
3398 mTag = a.getText(attr);
3399 break;
3400 case com.android.internal.R.styleable.View_fitsSystemWindows:
3401 if (a.getBoolean(attr, false)) {
3402 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3403 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3404 }
3405 break;
3406 case com.android.internal.R.styleable.View_focusable:
3407 if (a.getBoolean(attr, false)) {
3408 viewFlagValues |= FOCUSABLE;
3409 viewFlagMasks |= FOCUSABLE_MASK;
3410 }
3411 break;
3412 case com.android.internal.R.styleable.View_focusableInTouchMode:
3413 if (a.getBoolean(attr, false)) {
3414 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3415 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3416 }
3417 break;
3418 case com.android.internal.R.styleable.View_clickable:
3419 if (a.getBoolean(attr, false)) {
3420 viewFlagValues |= CLICKABLE;
3421 viewFlagMasks |= CLICKABLE;
3422 }
3423 break;
3424 case com.android.internal.R.styleable.View_longClickable:
3425 if (a.getBoolean(attr, false)) {
3426 viewFlagValues |= LONG_CLICKABLE;
3427 viewFlagMasks |= LONG_CLICKABLE;
3428 }
3429 break;
3430 case com.android.internal.R.styleable.View_saveEnabled:
3431 if (!a.getBoolean(attr, true)) {
3432 viewFlagValues |= SAVE_DISABLED;
3433 viewFlagMasks |= SAVE_DISABLED_MASK;
3434 }
3435 break;
3436 case com.android.internal.R.styleable.View_duplicateParentState:
3437 if (a.getBoolean(attr, false)) {
3438 viewFlagValues |= DUPLICATE_PARENT_STATE;
3439 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3440 }
3441 break;
3442 case com.android.internal.R.styleable.View_visibility:
3443 final int visibility = a.getInt(attr, 0);
3444 if (visibility != 0) {
3445 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3446 viewFlagMasks |= VISIBILITY_MASK;
3447 }
3448 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003449 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003450 // Clear any layout direction flags (included resolved bits) already set
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07003451 mPrivateFlags2 &=
3452 ~(PFLAG2_LAYOUT_DIRECTION_MASK | PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003453 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003454 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003455 final int value = (layoutDirection != -1) ?
3456 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
Dianne Hackborn4702a852012-08-17 15:18:29 -07003457 mPrivateFlags2 |= (value << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003458 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 case com.android.internal.R.styleable.View_drawingCacheQuality:
3460 final int cacheQuality = a.getInt(attr, 0);
3461 if (cacheQuality != 0) {
3462 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3463 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3464 }
3465 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003466 case com.android.internal.R.styleable.View_contentDescription:
Svetoslav Ganove47957a2012-06-05 14:46:50 -07003467 setContentDescription(a.getString(attr));
svetoslavganov75986cf2009-05-14 22:28:01 -07003468 break;
Svetoslav Ganov33aef982012-09-13 12:49:03 -07003469 case com.android.internal.R.styleable.View_labelFor:
3470 setLabelFor(a.getResourceId(attr, NO_ID));
3471 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3473 if (!a.getBoolean(attr, true)) {
3474 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3475 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3476 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003477 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3479 if (!a.getBoolean(attr, true)) {
3480 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3481 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3482 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003483 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 case R.styleable.View_scrollbars:
3485 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3486 if (scrollbars != SCROLLBARS_NONE) {
3487 viewFlagValues |= scrollbars;
3488 viewFlagMasks |= SCROLLBARS_MASK;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003489 initializeScrollbars = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 }
3491 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003492 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 case R.styleable.View_fadingEdge:
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003494 if (targetSdkVersion >= ICE_CREAM_SANDWICH) {
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003495 // Ignore the attribute starting with ICS
3496 break;
3497 }
3498 // With builds < ICS, fall through and apply fading edges
3499 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3501 if (fadingEdge != FADING_EDGE_NONE) {
3502 viewFlagValues |= fadingEdge;
3503 viewFlagMasks |= FADING_EDGE_MASK;
3504 initializeFadingEdge(a);
3505 }
3506 break;
3507 case R.styleable.View_scrollbarStyle:
3508 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3509 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3510 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3511 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3512 }
3513 break;
3514 case R.styleable.View_isScrollContainer:
3515 setScrollContainer = true;
3516 if (a.getBoolean(attr, false)) {
3517 setScrollContainer(true);
3518 }
3519 break;
3520 case com.android.internal.R.styleable.View_keepScreenOn:
3521 if (a.getBoolean(attr, false)) {
3522 viewFlagValues |= KEEP_SCREEN_ON;
3523 viewFlagMasks |= KEEP_SCREEN_ON;
3524 }
3525 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003526 case R.styleable.View_filterTouchesWhenObscured:
3527 if (a.getBoolean(attr, false)) {
3528 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3529 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3530 }
3531 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 case R.styleable.View_nextFocusLeft:
3533 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3534 break;
3535 case R.styleable.View_nextFocusRight:
3536 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3537 break;
3538 case R.styleable.View_nextFocusUp:
3539 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3540 break;
3541 case R.styleable.View_nextFocusDown:
3542 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3543 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003544 case R.styleable.View_nextFocusForward:
3545 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3546 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 case R.styleable.View_minWidth:
3548 mMinWidth = a.getDimensionPixelSize(attr, 0);
3549 break;
3550 case R.styleable.View_minHeight:
3551 mMinHeight = a.getDimensionPixelSize(attr, 0);
3552 break;
Romain Guy9a817362009-05-01 10:57:14 -07003553 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003554 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003555 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003556 + "be used within a restricted context");
3557 }
3558
Romain Guy9a817362009-05-01 10:57:14 -07003559 final String handlerName = a.getString(attr);
3560 if (handlerName != null) {
3561 setOnClickListener(new OnClickListener() {
3562 private Method mHandler;
3563
3564 public void onClick(View v) {
3565 if (mHandler == null) {
3566 try {
3567 mHandler = getContext().getClass().getMethod(handlerName,
3568 View.class);
3569 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003570 int id = getId();
3571 String idText = id == NO_ID ? "" : " with id '"
3572 + getContext().getResources().getResourceEntryName(
3573 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003574 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003575 handlerName + "(View) in the activity "
3576 + getContext().getClass() + " for onClick handler"
3577 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003578 }
3579 }
3580
3581 try {
3582 mHandler.invoke(getContext(), View.this);
3583 } catch (IllegalAccessException e) {
3584 throw new IllegalStateException("Could not execute non "
3585 + "public method of the activity", e);
3586 } catch (InvocationTargetException e) {
3587 throw new IllegalStateException("Could not execute "
3588 + "method of the activity", e);
3589 }
3590 }
3591 });
3592 }
3593 break;
Adam Powell637d3372010-08-25 14:37:03 -07003594 case R.styleable.View_overScrollMode:
3595 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3596 break;
Adam Powell20232d02010-12-08 21:08:53 -08003597 case R.styleable.View_verticalScrollbarPosition:
3598 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3599 break;
Romain Guy171c5922011-01-06 10:04:23 -08003600 case R.styleable.View_layerType:
3601 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3602 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003603 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003604 // Clear any text direction flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003605 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003606 // Set the text direction flags depending on the value of the attribute
3607 final int textDirection = a.getInt(attr, -1);
3608 if (textDirection != -1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07003609 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_FLAGS[textDirection];
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003610 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003611 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003612 case R.styleable.View_textAlignment:
3613 // Clear any text alignment flag already set
Dianne Hackborn4702a852012-08-17 15:18:29 -07003614 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003615 // Set the text alignment flag depending on the value of the attribute
3616 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
Dianne Hackborn4702a852012-08-17 15:18:29 -07003617 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_FLAGS[textAlignment];
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003618 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003619 case R.styleable.View_importantForAccessibility:
3620 setImportantForAccessibility(a.getInt(attr,
3621 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
Svetoslav Ganov86783472012-06-06 21:12:20 -07003622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
3624 }
3625
Adam Powell637d3372010-08-25 14:37:03 -07003626 setOverScrollMode(overScrollMode);
3627
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003628 // Cache start/end user padding as we cannot fully resolve padding here (we dont have yet
3629 // the resolved layout direction). Those cached values will be used later during padding
3630 // resolution.
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003631 mUserPaddingStart = startPadding;
3632 mUserPaddingEnd = endPadding;
3633
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07003634 if (background != null) {
3635 setBackground(background);
3636 }
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 if (padding >= 0) {
3639 leftPadding = padding;
3640 topPadding = padding;
3641 rightPadding = padding;
3642 bottomPadding = padding;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -07003643 mUserPaddingLeftInitial = padding;
3644 mUserPaddingRightInitial = padding;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 }
3646
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003647 if (isRtlCompatibilityMode()) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003648 // RTL compatibility mode: pre Jelly Bean MR1 case OR no RTL support case.
3649 // left / right padding are used if defined (meaning here nothing to do). If they are not
3650 // defined and start / end padding are defined (e.g. in Frameworks resources), then we use
3651 // start / end and resolve them as left / right (layout direction is not taken into account).
3652 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3653 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3654 // defined.
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003655 if (!leftPaddingDefined && startPaddingDefined) {
3656 leftPadding = startPadding;
3657 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003658 mUserPaddingLeftInitial = (leftPadding >= 0) ? leftPadding : mUserPaddingLeftInitial;
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003659 if (!rightPaddingDefined && endPaddingDefined) {
3660 rightPadding = endPadding;
3661 }
Fabrice Di Meglio6e6d7812012-10-01 15:32:01 -07003662 mUserPaddingRightInitial = (rightPadding >= 0) ? rightPadding : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003663 } else {
3664 // Jelly Bean MR1 and after case: if start/end defined, they will override any left/right
3665 // values defined. Otherwise, left /right values are used.
3666 // Padding from the background drawable is stored at this point in mUserPaddingLeftInitial
3667 // and mUserPaddingRightInitial) so drawable padding will be used as ultimate default if
3668 // defined.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003669 if (leftPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003670 mUserPaddingLeftInitial = leftPadding;
3671 }
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -07003672 if (rightPaddingDefined) {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -07003673 mUserPaddingRightInitial = rightPadding;
3674 }
Fabrice Di Megliobbd10292012-09-27 14:08:49 -07003675 }
3676
Fabrice Di Meglio3e27c342012-09-20 17:56:58 -07003677 internalSetPadding(
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003678 mUserPaddingLeftInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 topPadding >= 0 ? topPadding : mPaddingTop,
Fabrice Di Meglio75aefb82012-09-27 19:55:18 -07003680 mUserPaddingRightInitial,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3682
3683 if (viewFlagMasks != 0) {
3684 setFlags(viewFlagValues, viewFlagMasks);
3685 }
3686
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07003687 if (initializeScrollbars) {
3688 initializeScrollbars(a);
3689 }
3690
3691 a.recycle();
3692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 // Needs to be called after mViewFlags is set
3694 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3695 recomputePadding();
3696 }
3697
3698 if (x != 0 || y != 0) {
3699 scrollTo(x, y);
3700 }
3701
Chet Haase73066682010-11-29 15:55:32 -08003702 if (transformSet) {
3703 setTranslationX(tx);
3704 setTranslationY(ty);
3705 setRotation(rotation);
3706 setRotationX(rotationX);
3707 setRotationY(rotationY);
3708 setScaleX(sx);
3709 setScaleY(sy);
3710 }
3711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3713 setScrollContainer(true);
3714 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003715
3716 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 }
3718
3719 /**
3720 * Non-public constructor for use in testing
3721 */
3722 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003723 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724 }
3725
Dianne Hackborn4702a852012-08-17 15:18:29 -07003726 public String toString() {
3727 StringBuilder out = new StringBuilder(128);
3728 out.append(getClass().getName());
3729 out.append('{');
3730 out.append(Integer.toHexString(System.identityHashCode(this)));
3731 out.append(' ');
3732 switch (mViewFlags&VISIBILITY_MASK) {
3733 case VISIBLE: out.append('V'); break;
3734 case INVISIBLE: out.append('I'); break;
3735 case GONE: out.append('G'); break;
3736 default: out.append('.'); break;
3737 }
3738 out.append((mViewFlags&FOCUSABLE_MASK) == FOCUSABLE ? 'F' : '.');
3739 out.append((mViewFlags&ENABLED_MASK) == ENABLED ? 'E' : '.');
3740 out.append((mViewFlags&DRAW_MASK) == WILL_NOT_DRAW ? '.' : 'D');
3741 out.append((mViewFlags&SCROLLBARS_HORIZONTAL) != 0 ? 'H' : '.');
3742 out.append((mViewFlags&SCROLLBARS_VERTICAL) != 0 ? 'V' : '.');
3743 out.append((mViewFlags&CLICKABLE) != 0 ? 'C' : '.');
3744 out.append((mViewFlags&LONG_CLICKABLE) != 0 ? 'L' : '.');
3745 out.append(' ');
3746 out.append((mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0 ? 'R' : '.');
3747 out.append((mPrivateFlags&PFLAG_FOCUSED) != 0 ? 'F' : '.');
3748 out.append((mPrivateFlags&PFLAG_SELECTED) != 0 ? 'S' : '.');
3749 if ((mPrivateFlags&PFLAG_PREPRESSED) != 0) {
3750 out.append('p');
3751 } else {
3752 out.append((mPrivateFlags&PFLAG_PRESSED) != 0 ? 'P' : '.');
3753 }
3754 out.append((mPrivateFlags&PFLAG_HOVERED) != 0 ? 'H' : '.');
3755 out.append((mPrivateFlags&PFLAG_ACTIVATED) != 0 ? 'A' : '.');
3756 out.append((mPrivateFlags&PFLAG_INVALIDATED) != 0 ? 'I' : '.');
3757 out.append((mPrivateFlags&PFLAG_DIRTY_MASK) != 0 ? 'D' : '.');
3758 out.append(' ');
3759 out.append(mLeft);
3760 out.append(',');
3761 out.append(mTop);
3762 out.append('-');
3763 out.append(mRight);
3764 out.append(',');
3765 out.append(mBottom);
3766 final int id = getId();
3767 if (id != NO_ID) {
3768 out.append(" #");
3769 out.append(Integer.toHexString(id));
3770 final Resources r = mResources;
3771 if (id != 0 && r != null) {
3772 try {
3773 String pkgname;
3774 switch (id&0xff000000) {
3775 case 0x7f000000:
3776 pkgname="app";
3777 break;
3778 case 0x01000000:
3779 pkgname="android";
3780 break;
3781 default:
3782 pkgname = r.getResourcePackageName(id);
3783 break;
3784 }
3785 String typename = r.getResourceTypeName(id);
3786 String entryname = r.getResourceEntryName(id);
3787 out.append(" ");
3788 out.append(pkgname);
3789 out.append(":");
3790 out.append(typename);
3791 out.append("/");
3792 out.append(entryname);
3793 } catch (Resources.NotFoundException e) {
3794 }
3795 }
3796 }
3797 out.append("}");
3798 return out.toString();
3799 }
3800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 /**
3802 * <p>
3803 * Initializes the fading edges from a given set of styled attributes. This
3804 * method should be called by subclasses that need fading edges and when an
3805 * instance of these subclasses is created programmatically rather than
3806 * being inflated from XML. This method is automatically called when the XML
3807 * is inflated.
3808 * </p>
3809 *
3810 * @param a the styled attributes set to initialize the fading edges from
3811 */
3812 protected void initializeFadingEdge(TypedArray a) {
3813 initScrollCache();
3814
3815 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3816 R.styleable.View_fadingEdgeLength,
3817 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3818 }
3819
3820 /**
3821 * Returns the size of the vertical faded edges used to indicate that more
3822 * content in this view is visible.
3823 *
3824 * @return The size in pixels of the vertical faded edge or 0 if vertical
3825 * faded edges are not enabled for this view.
3826 * @attr ref android.R.styleable#View_fadingEdgeLength
3827 */
3828 public int getVerticalFadingEdgeLength() {
3829 if (isVerticalFadingEdgeEnabled()) {
3830 ScrollabilityCache cache = mScrollCache;
3831 if (cache != null) {
3832 return cache.fadingEdgeLength;
3833 }
3834 }
3835 return 0;
3836 }
3837
3838 /**
3839 * Set the size of the faded edge used to indicate that more content in this
3840 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003841 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3842 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3843 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 *
3845 * @param length The size in pixels of the faded edge used to indicate that more
3846 * content in this view is visible.
3847 */
3848 public void setFadingEdgeLength(int length) {
3849 initScrollCache();
3850 mScrollCache.fadingEdgeLength = length;
3851 }
3852
3853 /**
3854 * Returns the size of the horizontal faded edges used to indicate that more
3855 * content in this view is visible.
3856 *
3857 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3858 * faded edges are not enabled for this view.
3859 * @attr ref android.R.styleable#View_fadingEdgeLength
3860 */
3861 public int getHorizontalFadingEdgeLength() {
3862 if (isHorizontalFadingEdgeEnabled()) {
3863 ScrollabilityCache cache = mScrollCache;
3864 if (cache != null) {
3865 return cache.fadingEdgeLength;
3866 }
3867 }
3868 return 0;
3869 }
3870
3871 /**
3872 * Returns the width of the vertical scrollbar.
3873 *
3874 * @return The width in pixels of the vertical scrollbar or 0 if there
3875 * is no vertical scrollbar.
3876 */
3877 public int getVerticalScrollbarWidth() {
3878 ScrollabilityCache cache = mScrollCache;
3879 if (cache != null) {
3880 ScrollBarDrawable scrollBar = cache.scrollBar;
3881 if (scrollBar != null) {
3882 int size = scrollBar.getSize(true);
3883 if (size <= 0) {
3884 size = cache.scrollBarSize;
3885 }
3886 return size;
3887 }
3888 return 0;
3889 }
3890 return 0;
3891 }
3892
3893 /**
3894 * Returns the height of the horizontal scrollbar.
3895 *
3896 * @return The height in pixels of the horizontal scrollbar or 0 if
3897 * there is no horizontal scrollbar.
3898 */
3899 protected int getHorizontalScrollbarHeight() {
3900 ScrollabilityCache cache = mScrollCache;
3901 if (cache != null) {
3902 ScrollBarDrawable scrollBar = cache.scrollBar;
3903 if (scrollBar != null) {
3904 int size = scrollBar.getSize(false);
3905 if (size <= 0) {
3906 size = cache.scrollBarSize;
3907 }
3908 return size;
3909 }
3910 return 0;
3911 }
3912 return 0;
3913 }
3914
3915 /**
3916 * <p>
3917 * Initializes the scrollbars from a given set of styled attributes. This
3918 * method should be called by subclasses that need scrollbars and when an
3919 * instance of these subclasses is created programmatically rather than
3920 * being inflated from XML. This method is automatically called when the XML
3921 * is inflated.
3922 * </p>
3923 *
3924 * @param a the styled attributes set to initialize the scrollbars from
3925 */
3926 protected void initializeScrollbars(TypedArray a) {
3927 initScrollCache();
3928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003930
Mike Cleronf116bf82009-09-27 19:14:12 -07003931 if (scrollabilityCache.scrollBar == null) {
3932 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3933 }
Joe Malin32736f02011-01-19 16:14:20 -08003934
Romain Guy8bda2482010-03-02 11:42:11 -08003935 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936
Mike Cleronf116bf82009-09-27 19:14:12 -07003937 if (!fadeScrollbars) {
3938 scrollabilityCache.state = ScrollabilityCache.ON;
3939 }
3940 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003941
3942
Mike Cleronf116bf82009-09-27 19:14:12 -07003943 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3944 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3945 .getScrollBarFadeDuration());
3946 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3947 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3948 ViewConfiguration.getScrollDefaultDelay());
3949
Joe Malin32736f02011-01-19 16:14:20 -08003950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3952 com.android.internal.R.styleable.View_scrollbarSize,
3953 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3954
3955 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3956 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3957
3958 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3959 if (thumb != null) {
3960 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3961 }
3962
3963 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3964 false);
3965 if (alwaysDraw) {
3966 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3967 }
3968
3969 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3970 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3971
3972 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3973 if (thumb != null) {
3974 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3975 }
3976
3977 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3978 false);
3979 if (alwaysDraw) {
3980 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3981 }
3982
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003983 // Apply layout direction to the new Drawables if needed
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07003984 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -07003985 if (track != null) {
3986 track.setLayoutDirection(layoutDirection);
3987 }
3988 if (thumb != null) {
3989 thumb.setLayoutDirection(layoutDirection);
3990 }
3991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003993 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 }
3995
3996 /**
3997 * <p>
3998 * Initalizes the scrollability cache if necessary.
3999 * </p>
4000 */
4001 private void initScrollCache() {
4002 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004003 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 }
4005 }
4006
Philip Milne6c8ea062012-04-03 17:38:43 -07004007 private ScrollabilityCache getScrollCache() {
4008 initScrollCache();
4009 return mScrollCache;
4010 }
4011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012 /**
Adam Powell20232d02010-12-08 21:08:53 -08004013 * Set the position of the vertical scroll bar. Should be one of
4014 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
4015 * {@link #SCROLLBAR_POSITION_RIGHT}.
4016 *
4017 * @param position Where the vertical scroll bar should be positioned.
4018 */
4019 public void setVerticalScrollbarPosition(int position) {
4020 if (mVerticalScrollbarPosition != position) {
4021 mVerticalScrollbarPosition = position;
4022 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004023 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08004024 }
4025 }
4026
4027 /**
4028 * @return The position where the vertical scroll bar will show, if applicable.
4029 * @see #setVerticalScrollbarPosition(int)
4030 */
4031 public int getVerticalScrollbarPosition() {
4032 return mVerticalScrollbarPosition;
4033 }
4034
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004035 ListenerInfo getListenerInfo() {
4036 if (mListenerInfo != null) {
4037 return mListenerInfo;
4038 }
4039 mListenerInfo = new ListenerInfo();
4040 return mListenerInfo;
4041 }
4042
Adam Powell20232d02010-12-08 21:08:53 -08004043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 * Register a callback to be invoked when focus of this view changed.
4045 *
4046 * @param l The callback that will run.
4047 */
4048 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004049 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 }
4051
4052 /**
Chet Haase21cd1382010-09-01 17:42:29 -07004053 * Add a listener that will be called when the bounds of the view change due to
4054 * layout processing.
4055 *
4056 * @param listener The listener that will be called when layout bounds change.
4057 */
4058 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004059 ListenerInfo li = getListenerInfo();
4060 if (li.mOnLayoutChangeListeners == null) {
4061 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07004062 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004063 if (!li.mOnLayoutChangeListeners.contains(listener)) {
4064 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07004065 }
Chet Haase21cd1382010-09-01 17:42:29 -07004066 }
4067
4068 /**
4069 * Remove a listener for layout changes.
4070 *
4071 * @param listener The listener for layout bounds change.
4072 */
4073 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004074 ListenerInfo li = mListenerInfo;
4075 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07004076 return;
4077 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004078 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07004079 }
4080
4081 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08004082 * Add a listener for attach state changes.
4083 *
4084 * This listener will be called whenever this view is attached or detached
4085 * from a window. Remove the listener using
4086 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
4087 *
4088 * @param listener Listener to attach
4089 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
4090 */
4091 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004092 ListenerInfo li = getListenerInfo();
4093 if (li.mOnAttachStateChangeListeners == null) {
4094 li.mOnAttachStateChangeListeners
4095 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08004096 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004097 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004098 }
4099
4100 /**
4101 * Remove a listener for attach state changes. The listener will receive no further
4102 * notification of window attach/detach events.
4103 *
4104 * @param listener Listener to remove
4105 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
4106 */
4107 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004108 ListenerInfo li = mListenerInfo;
4109 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08004110 return;
4111 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004112 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08004113 }
4114
4115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 * Returns the focus-change callback registered for this view.
4117 *
4118 * @return The callback, or null if one is not registered.
4119 */
4120 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004121 ListenerInfo li = mListenerInfo;
4122 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 }
4124
4125 /**
4126 * Register a callback to be invoked when this view is clicked. If this view is not
4127 * clickable, it becomes clickable.
4128 *
4129 * @param l The callback that will run
4130 *
4131 * @see #setClickable(boolean)
4132 */
4133 public void setOnClickListener(OnClickListener l) {
4134 if (!isClickable()) {
4135 setClickable(true);
4136 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004137 getListenerInfo().mOnClickListener = l;
4138 }
4139
4140 /**
4141 * Return whether this view has an attached OnClickListener. Returns
4142 * true if there is a listener, false if there is none.
4143 */
4144 public boolean hasOnClickListeners() {
4145 ListenerInfo li = mListenerInfo;
4146 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 }
4148
4149 /**
4150 * Register a callback to be invoked when this view is clicked and held. If this view is not
4151 * long clickable, it becomes long clickable.
4152 *
4153 * @param l The callback that will run
4154 *
4155 * @see #setLongClickable(boolean)
4156 */
4157 public void setOnLongClickListener(OnLongClickListener l) {
4158 if (!isLongClickable()) {
4159 setLongClickable(true);
4160 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004161 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 }
4163
4164 /**
4165 * Register a callback to be invoked when the context menu for this view is
4166 * being built. If this view is not long clickable, it becomes long clickable.
4167 *
4168 * @param l The callback that will run
4169 *
4170 */
4171 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
4172 if (!isLongClickable()) {
4173 setLongClickable(true);
4174 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004175 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004176 }
4177
4178 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004179 * Call this view's OnClickListener, if it is defined. Performs all normal
4180 * actions associated with clicking: reporting accessibility event, playing
4181 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 *
4183 * @return True there was an assigned OnClickListener that was called, false
4184 * otherwise is returned.
4185 */
4186 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004187 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
4188
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004189 ListenerInfo li = mListenerInfo;
4190 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004192 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 return true;
4194 }
4195
4196 return false;
4197 }
4198
4199 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004200 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
4201 * this only calls the listener, and does not do any associated clicking
4202 * actions like reporting an accessibility event.
4203 *
4204 * @return True there was an assigned OnClickListener that was called, false
4205 * otherwise is returned.
4206 */
4207 public boolean callOnClick() {
4208 ListenerInfo li = mListenerInfo;
4209 if (li != null && li.mOnClickListener != null) {
4210 li.mOnClickListener.onClick(this);
4211 return true;
4212 }
4213 return false;
4214 }
4215
4216 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004217 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
4218 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004220 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 */
4222 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07004223 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
4224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004226 ListenerInfo li = mListenerInfo;
4227 if (li != null && li.mOnLongClickListener != null) {
4228 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
4230 if (!handled) {
4231 handled = showContextMenu();
4232 }
4233 if (handled) {
4234 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
4235 }
4236 return handled;
4237 }
4238
4239 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004240 * Performs button-related actions during a touch down event.
4241 *
4242 * @param event The event.
4243 * @return True if the down was consumed.
4244 *
4245 * @hide
4246 */
4247 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
4248 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
4249 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4250 return true;
4251 }
4252 }
4253 return false;
4254 }
4255
4256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 * Bring up the context menu for this view.
4258 *
4259 * @return Whether a context menu was displayed.
4260 */
4261 public boolean showContextMenu() {
4262 return getParent().showContextMenuForChild(this);
4263 }
4264
4265 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004266 * Bring up the context menu for this view, referring to the item under the specified point.
4267 *
4268 * @param x The referenced x coordinate.
4269 * @param y The referenced y coordinate.
4270 * @param metaState The keyboard modifiers that were pressed.
4271 * @return Whether a context menu was displayed.
4272 *
4273 * @hide
4274 */
4275 public boolean showContextMenu(float x, float y, int metaState) {
4276 return showContextMenu();
4277 }
4278
4279 /**
Adam Powell6e346362010-07-23 10:18:23 -07004280 * Start an action mode.
4281 *
4282 * @param callback Callback that will control the lifecycle of the action mode
4283 * @return The new action mode if it is started, null otherwise
4284 *
4285 * @see ActionMode
4286 */
4287 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004288 ViewParent parent = getParent();
4289 if (parent == null) return null;
4290 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004291 }
4292
4293 /**
Jean Chalard405bc512012-05-29 19:12:34 +09004294 * Register a callback to be invoked when a hardware key is pressed in this view.
4295 * Key presses in software input methods will generally not trigger the methods of
4296 * this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 * @param l the key listener to attach to this view
4298 */
4299 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004300 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302
4303 /**
4304 * Register a callback to be invoked when a touch event is sent to this view.
4305 * @param l the touch listener to attach to this view
4306 */
4307 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004308 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
4310
4311 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004312 * Register a callback to be invoked when a generic motion event is sent to this view.
4313 * @param l the generic motion listener to attach to this view
4314 */
4315 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004316 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004317 }
4318
4319 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004320 * Register a callback to be invoked when a hover event is sent to this view.
4321 * @param l the hover listener to attach to this view
4322 */
4323 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004324 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004325 }
4326
4327 /**
Joe Malin32736f02011-01-19 16:14:20 -08004328 * Register a drag event listener callback object for this View. The parameter is
4329 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4330 * View, the system calls the
4331 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4332 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004333 */
4334 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004335 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004336 }
4337
4338 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004339 * Give this view focus. This will cause
4340 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 *
4342 * Note: this does not check whether this {@link View} should get focus, it just
4343 * gives it focus no matter what. It should only be called internally by framework
4344 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4345 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004346 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4347 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 * focus moved when requestFocus() is called. It may not always
4349 * apply, in which case use the default View.FOCUS_DOWN.
4350 * @param previouslyFocusedRect The rectangle of the view that had focus
4351 * prior in this View's coordinate system.
4352 */
4353 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4354 if (DBG) {
4355 System.out.println(this + " requestFocus()");
4356 }
4357
Dianne Hackborn4702a852012-08-17 15:18:29 -07004358 if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
4359 mPrivateFlags |= PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360
4361 if (mParent != null) {
4362 mParent.requestChildFocus(this, this);
4363 }
4364
4365 onFocusChanged(true, direction, previouslyFocusedRect);
4366 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004367
4368 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4369 notifyAccessibilityStateChanged();
4370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 }
4372 }
4373
4374 /**
4375 * Request that a rectangle of this view be visible on the screen,
4376 * scrolling if necessary just enough.
4377 *
4378 * <p>A View should call this if it maintains some notion of which part
4379 * of its content is interesting. For example, a text editing view
4380 * should call this when its cursor moves.
4381 *
4382 * @param rectangle The rectangle.
4383 * @return Whether any parent scrolled.
4384 */
4385 public boolean requestRectangleOnScreen(Rect rectangle) {
4386 return requestRectangleOnScreen(rectangle, false);
4387 }
4388
4389 /**
4390 * Request that a rectangle of this view be visible on the screen,
4391 * scrolling if necessary just enough.
4392 *
4393 * <p>A View should call this if it maintains some notion of which part
4394 * of its content is interesting. For example, a text editing view
4395 * should call this when its cursor moves.
4396 *
4397 * <p>When <code>immediate</code> is set to true, scrolling will not be
4398 * animated.
4399 *
4400 * @param rectangle The rectangle.
4401 * @param immediate True to forbid animated scrolling, false otherwise
4402 * @return Whether any parent scrolled.
4403 */
4404 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004405 if (mParent == null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004406 return false;
4407 }
4408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 View child = this;
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004410
Svetoslav Ganov989ae752012-09-25 15:16:48 -07004411 RectF position = (mAttachInfo != null) ? mAttachInfo.mTmpTransformRect : new RectF();
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004412 position.set(rectangle);
4413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 ViewParent parent = mParent;
4415 boolean scrolled = false;
4416 while (parent != null) {
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004417 rectangle.set((int) position.left, (int) position.top,
4418 (int) position.right, (int) position.bottom);
4419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004420 scrolled |= parent.requestChildRectangleOnScreen(child,
4421 rectangle, immediate);
4422
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004423 if (!child.hasIdentityMatrix()) {
4424 child.getMatrix().mapRect(position);
4425 }
4426
4427 position.offset(child.mLeft, child.mTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428
4429 if (!(parent instanceof View)) {
4430 break;
4431 }
Romain Guy8506ab42009-06-11 17:35:47 -07004432
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004433 View parentView = (View) parent;
4434
4435 position.offset(-parentView.getScrollX(), -parentView.getScrollY());
4436
4437 child = parentView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 parent = child.getParent();
4439 }
Svetoslav Ganovee6c6ae2012-09-10 19:56:06 -07004440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 return scrolled;
4442 }
4443
4444 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004445 * Called when this view wants to give up focus. If focus is cleared
4446 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4447 * <p>
4448 * <strong>Note:</strong> When a View clears focus the framework is trying
4449 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004450 * View is the first from the top that can take focus, then all callbacks
4451 * related to clearing focus will be invoked after wich the framework will
4452 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004453 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 */
4455 public void clearFocus() {
4456 if (DBG) {
4457 System.out.println(this + " clearFocus()");
4458 }
4459
Dianne Hackborn4702a852012-08-17 15:18:29 -07004460 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4461 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462
4463 if (mParent != null) {
4464 mParent.clearChildFocus(this);
4465 }
4466
4467 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004470
4471 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004472
4473 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4474 notifyAccessibilityStateChanged();
4475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 }
4477 }
4478
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004479 void ensureInputFocusOnFirstFocusable() {
4480 View root = getRootView();
4481 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004482 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 }
4484 }
4485
4486 /**
4487 * Called internally by the view system when a new view is getting focus.
4488 * This is what clears the old focus.
4489 */
4490 void unFocus() {
4491 if (DBG) {
4492 System.out.println(this + " unFocus()");
4493 }
4494
Dianne Hackborn4702a852012-08-17 15:18:29 -07004495 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
4496 mPrivateFlags &= ~PFLAG_FOCUSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004497
4498 onFocusChanged(false, 0, null);
4499 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004500
4501 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4502 notifyAccessibilityStateChanged();
4503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 }
4505 }
4506
4507 /**
4508 * Returns true if this view has focus iteself, or is the ancestor of the
4509 * view that has focus.
4510 *
4511 * @return True if this view has or contains focus, false otherwise.
4512 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004513 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 public boolean hasFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07004515 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 }
4517
4518 /**
4519 * Returns true if this view is focusable or if it contains a reachable View
4520 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4521 * is a View whose parents do not block descendants focus.
4522 *
4523 * Only {@link #VISIBLE} views are considered focusable.
4524 *
4525 * @return True if the view is focusable or if the view contains a focusable
4526 * View, false otherwise.
4527 *
4528 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4529 */
4530 public boolean hasFocusable() {
4531 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4532 }
4533
4534 /**
4535 * Called by the view system when the focus state of this view changes.
4536 * When the focus change event is caused by directional navigation, direction
4537 * and previouslyFocusedRect provide insight into where the focus is coming from.
4538 * When overriding, be sure to call up through to the super class so that
4539 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004540 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 * @param gainFocus True if the View has focus; false otherwise.
4542 * @param direction The direction focus has moved when requestFocus()
4543 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004544 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4545 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4546 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4548 * system, of the previously focused view. If applicable, this will be
4549 * passed in as finer grained information about where the focus is coming
4550 * from (in addition to direction). Will be <code>null</code> otherwise.
4551 */
4552 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004553 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004554 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4555 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004556 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004557 }
4558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004559 InputMethodManager imm = InputMethodManager.peekInstance();
4560 if (!gainFocus) {
4561 if (isPressed()) {
4562 setPressed(false);
4563 }
4564 if (imm != null && mAttachInfo != null
4565 && mAttachInfo.mHasWindowFocus) {
4566 imm.focusOut(this);
4567 }
Romain Guya2431d02009-04-30 16:30:00 -07004568 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 } else if (imm != null && mAttachInfo != null
4570 && mAttachInfo.mHasWindowFocus) {
4571 imm.focusIn(this);
4572 }
Romain Guy8506ab42009-06-11 17:35:47 -07004573
Romain Guy0fd89bf2011-01-26 15:41:30 -08004574 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004575 ListenerInfo li = mListenerInfo;
4576 if (li != null && li.mOnFocusChangeListener != null) {
4577 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004578 }
Joe Malin32736f02011-01-19 16:14:20 -08004579
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004580 if (mAttachInfo != null) {
4581 mAttachInfo.mKeyDispatchState.reset(this);
4582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 }
4584
4585 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07004586 * Sends an accessibility event of the given type. If accessibility is
Svetoslav Ganov30401322011-05-12 18:53:45 -07004587 * not enabled this method has no effect. The default implementation calls
4588 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4589 * to populate information about the event source (this View), then calls
4590 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4591 * populate the text content of the event source including its descendants,
4592 * and last calls
4593 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4594 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004595 * <p>
4596 * If an {@link AccessibilityDelegate} has been specified via calling
4597 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4598 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4599 * responsible for handling this call.
4600 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004601 *
Scott Mainb303d832011-10-12 16:45:18 -07004602 * @param eventType The type of the event to send, as defined by several types from
4603 * {@link android.view.accessibility.AccessibilityEvent}, such as
4604 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4605 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004606 *
4607 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4608 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4609 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004610 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004611 */
4612 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004613 if (mAccessibilityDelegate != null) {
4614 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4615 } else {
4616 sendAccessibilityEventInternal(eventType);
4617 }
4618 }
4619
4620 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004621 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4622 * {@link AccessibilityEvent} to make an announcement which is related to some
4623 * sort of a context change for which none of the events representing UI transitions
4624 * is a good fit. For example, announcing a new page in a book. If accessibility
4625 * is not enabled this method does nothing.
4626 *
4627 * @param text The announcement text.
4628 */
4629 public void announceForAccessibility(CharSequence text) {
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004630 if (AccessibilityManager.getInstance(mContext).isEnabled() && mParent != null) {
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004631 AccessibilityEvent event = AccessibilityEvent.obtain(
4632 AccessibilityEvent.TYPE_ANNOUNCEMENT);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004633 onInitializeAccessibilityEvent(event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004634 event.getText().add(text);
Svetoslav Ganov7a82b2b2012-07-02 18:33:23 -07004635 event.setContentDescription(null);
4636 mParent.requestSendAccessibilityEvent(this, event);
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004637 }
4638 }
4639
4640 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004641 * @see #sendAccessibilityEvent(int)
4642 *
4643 * Note: Called from the default {@link AccessibilityDelegate}.
4644 */
4645 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004646 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4647 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4648 }
4649 }
4650
4651 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004652 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4653 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004654 * perform a check whether accessibility is enabled.
4655 * <p>
4656 * If an {@link AccessibilityDelegate} has been specified via calling
4657 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4658 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4659 * is responsible for handling this call.
4660 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004661 *
4662 * @param event The event to send.
4663 *
4664 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004665 */
4666 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004667 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004668 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004669 } else {
4670 sendAccessibilityEventUncheckedInternal(event);
4671 }
4672 }
4673
4674 /**
4675 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4676 *
4677 * Note: Called from the default {@link AccessibilityDelegate}.
4678 */
4679 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004680 if (!isShown()) {
4681 return;
4682 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004683 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004684 // Only a subset of accessibility events populates text content.
4685 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4686 dispatchPopulateAccessibilityEvent(event);
4687 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004688 // In the beginning we called #isShown(), so we know that getParent() is not null.
4689 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004690 }
4691
4692 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004693 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4694 * to its children for adding their text content to the event. Note that the
4695 * event text is populated in a separate dispatch path since we add to the
4696 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004697 * A typical implementation will call
4698 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4699 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4700 * on each child. Override this method if custom population of the event text
4701 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004702 * <p>
4703 * If an {@link AccessibilityDelegate} has been specified via calling
4704 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4705 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4706 * is responsible for handling this call.
4707 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004708 * <p>
4709 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4710 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4711 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004712 *
4713 * @param event The event.
4714 *
4715 * @return True if the event population was completed.
4716 */
4717 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004718 if (mAccessibilityDelegate != null) {
4719 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4720 } else {
4721 return dispatchPopulateAccessibilityEventInternal(event);
4722 }
4723 }
4724
4725 /**
4726 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4727 *
4728 * Note: Called from the default {@link AccessibilityDelegate}.
4729 */
4730 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004731 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004732 return false;
4733 }
4734
4735 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004736 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004737 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004738 * text content. While this method is free to modify event
4739 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004740 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4741 * <p>
4742 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004743 * to the text added by the super implementation:
4744 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004745 * super.onPopulateAccessibilityEvent(event);
4746 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4747 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4748 * mCurrentDate.getTimeInMillis(), flags);
4749 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004750 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004751 * <p>
4752 * If an {@link AccessibilityDelegate} has been specified via calling
4753 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4754 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4755 * is responsible for handling this call.
4756 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004757 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4758 * information to the event, in case the default implementation has basic information to add.
4759 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004760 *
4761 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004762 *
4763 * @see #sendAccessibilityEvent(int)
4764 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004765 */
4766 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004767 if (mAccessibilityDelegate != null) {
4768 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4769 } else {
4770 onPopulateAccessibilityEventInternal(event);
4771 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004772 }
4773
4774 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004775 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4776 *
4777 * Note: Called from the default {@link AccessibilityDelegate}.
4778 */
4779 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4780
4781 }
4782
4783 /**
4784 * Initializes an {@link AccessibilityEvent} with information about
4785 * this View which is the event source. In other words, the source of
4786 * an accessibility event is the view whose state change triggered firing
4787 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004788 * <p>
4789 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004790 * to properties set by the super implementation:
4791 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4792 * super.onInitializeAccessibilityEvent(event);
4793 * event.setPassword(true);
4794 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004795 * <p>
4796 * If an {@link AccessibilityDelegate} has been specified via calling
4797 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4798 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4799 * is responsible for handling this call.
4800 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004801 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4802 * information to the event, in case the default implementation has basic information to add.
4803 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004804 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004805 *
4806 * @see #sendAccessibilityEvent(int)
4807 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4808 */
4809 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004810 if (mAccessibilityDelegate != null) {
4811 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4812 } else {
4813 onInitializeAccessibilityEventInternal(event);
4814 }
4815 }
4816
4817 /**
4818 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4819 *
4820 * Note: Called from the default {@link AccessibilityDelegate}.
4821 */
4822 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004823 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004824 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004825 event.setPackageName(getContext().getPackageName());
4826 event.setEnabled(isEnabled());
4827 event.setContentDescription(mContentDescription);
4828
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004829 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004830 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004831 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4832 FOCUSABLES_ALL);
4833 event.setItemCount(focusablesTempList.size());
4834 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4835 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004836 }
4837 }
4838
4839 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004840 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4841 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4842 * This method is responsible for obtaining an accessibility node info from a
4843 * pool of reusable instances and calling
4844 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4845 * initialize the former.
4846 * <p>
4847 * Note: The client is responsible for recycling the obtained instance by calling
4848 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4849 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004850 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004851 * @return A populated {@link AccessibilityNodeInfo}.
4852 *
4853 * @see AccessibilityNodeInfo
4854 */
4855 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004856 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4857 if (provider != null) {
4858 return provider.createAccessibilityNodeInfo(View.NO_ID);
4859 } else {
4860 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4861 onInitializeAccessibilityNodeInfo(info);
4862 return info;
4863 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004864 }
4865
4866 /**
4867 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4868 * The base implementation sets:
4869 * <ul>
4870 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004871 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4872 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004873 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4874 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4875 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4876 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4877 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4878 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4879 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4880 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4881 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4882 * </ul>
4883 * <p>
4884 * Subclasses should override this method, call the super implementation,
4885 * and set additional attributes.
4886 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004887 * <p>
4888 * If an {@link AccessibilityDelegate} has been specified via calling
4889 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4890 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4891 * is responsible for handling this call.
4892 * </p>
4893 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004894 * @param info The instance to initialize.
4895 */
4896 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004897 if (mAccessibilityDelegate != null) {
4898 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4899 } else {
4900 onInitializeAccessibilityNodeInfoInternal(info);
4901 }
4902 }
4903
4904 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004905 * Gets the location of this view in screen coordintates.
4906 *
4907 * @param outRect The output location
4908 */
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07004909 void getBoundsOnScreen(Rect outRect) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004910 if (mAttachInfo == null) {
4911 return;
4912 }
4913
4914 RectF position = mAttachInfo.mTmpTransformRect;
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004915 position.set(0, 0, mRight - mLeft, mBottom - mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004916
4917 if (!hasIdentityMatrix()) {
4918 getMatrix().mapRect(position);
4919 }
4920
Svetoslav Ganov14b2b742012-05-08 16:36:34 -07004921 position.offset(mLeft, mTop);
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004922
4923 ViewParent parent = mParent;
4924 while (parent instanceof View) {
4925 View parentView = (View) parent;
4926
4927 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4928
4929 if (!parentView.hasIdentityMatrix()) {
4930 parentView.getMatrix().mapRect(position);
4931 }
4932
4933 position.offset(parentView.mLeft, parentView.mTop);
4934
4935 parent = parentView.mParent;
4936 }
4937
4938 if (parent instanceof ViewRootImpl) {
4939 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4940 position.offset(0, -viewRootImpl.mCurScrollY);
4941 }
4942
4943 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4944
4945 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4946 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4947 }
4948
4949 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004950 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4951 *
4952 * Note: Called from the default {@link AccessibilityDelegate}.
4953 */
4954 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004955 Rect bounds = mAttachInfo.mTmpInvalRect;
Svetoslav Ganov983119a2012-07-03 21:04:10 -07004956
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004957 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004958 info.setBoundsInParent(bounds);
4959
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004960 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004961 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004962
Svetoslav Ganovc406be92012-05-11 16:12:32 -07004963 ViewParent parent = getParentForAccessibility();
4964 if (parent instanceof View) {
4965 info.setParent((View) parent);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004966 }
4967
Svetoslav Ganov33aef982012-09-13 12:49:03 -07004968 if (mID != View.NO_ID) {
4969 View rootView = getRootView();
4970 if (rootView == null) {
4971 rootView = this;
4972 }
4973 View label = rootView.findLabelForView(this, mID);
4974 if (label != null) {
4975 info.setLabeledBy(label);
4976 }
4977 }
4978
4979 if (mLabelForId != View.NO_ID) {
4980 View rootView = getRootView();
4981 if (rootView == null) {
4982 rootView = this;
4983 }
4984 View labeled = rootView.findViewInsideOutShouldExist(this, mLabelForId);
4985 if (labeled != null) {
4986 info.setLabelFor(labeled);
4987 }
4988 }
4989
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004990 info.setVisibleToUser(isVisibleToUser());
4991
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004992 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004993 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004994 info.setContentDescription(getContentDescription());
4995
4996 info.setEnabled(isEnabled());
4997 info.setClickable(isClickable());
4998 info.setFocusable(isFocusable());
4999 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07005000 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005001 info.setSelected(isSelected());
5002 info.setLongClickable(isLongClickable());
5003
5004 // TODO: These make sense only if we are in an AdapterView but all
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005005 // views can be selected. Maybe from accessibility perspective
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005006 // we should report as selectable view in an AdapterView.
5007 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
5008 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
5009
5010 if (isFocusable()) {
5011 if (isFocused()) {
5012 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
5013 } else {
5014 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
5015 }
5016 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005017
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005018 if (!isAccessibilityFocused()) {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07005019 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
Svetoslav Ganov02afe2c2012-05-07 17:51:59 -07005020 } else {
5021 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
5022 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005023
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005024 if (isClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005025 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
5026 }
5027
Svetoslav Ganovfb1e80a2012-05-16 17:33:19 -07005028 if (isLongClickable() && isEnabled()) {
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005029 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
5030 }
5031
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005032 if (mContentDescription != null && mContentDescription.length() > 0) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07005033 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
5034 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
5035 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07005036 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD
5037 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07005038 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005039 }
5040
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005041 private View findLabelForView(View view, int labeledId) {
5042 if (mMatchLabelForPredicate == null) {
5043 mMatchLabelForPredicate = new MatchLabelForPredicate();
5044 }
5045 mMatchLabelForPredicate.mLabeledId = labeledId;
5046 return findViewByPredicateInsideOut(view, mMatchLabelForPredicate);
5047 }
5048
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005049 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07005050 * Computes whether this view is visible to the user. Such a view is
5051 * attached, visible, all its predecessors are visible, it is not clipped
5052 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005053 *
5054 * @return Whether the view is visible on the screen.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005055 *
5056 * @hide
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005057 */
Guang Zhu0d607fb2012-05-11 19:34:56 -07005058 protected boolean isVisibleToUser() {
5059 return isVisibleToUser(null);
5060 }
5061
5062 /**
Romain Guyf0af1d52012-07-11 18:31:21 -07005063 * Computes whether the given portion of this view is visible to the user.
5064 * Such a view is attached, visible, all its predecessors are visible,
5065 * has an alpha greater than zero, and the specified portion is not
5066 * clipped entirely by its predecessors.
Guang Zhu0d607fb2012-05-11 19:34:56 -07005067 *
5068 * @param boundInView the portion of the view to test; coordinates should be relative; may be
5069 * <code>null</code>, and the entire view will be tested in this case.
5070 * When <code>true</code> is returned by the function, the actual visible
5071 * region will be stored in this parameter; that is, if boundInView is fully
5072 * contained within the view, no modification will be made, otherwise regions
5073 * outside of the visible area of the view will be clipped.
5074 *
5075 * @return Whether the specified portion of the view is visible on the screen.
5076 *
5077 * @hide
5078 */
5079 protected boolean isVisibleToUser(Rect boundInView) {
Romain Guyf0af1d52012-07-11 18:31:21 -07005080 if (mAttachInfo != null) {
5081 Rect visibleRect = mAttachInfo.mTmpInvalRect;
5082 Point offset = mAttachInfo.mPoint;
5083 // The first two checks are made also made by isShown() which
5084 // however traverses the tree up to the parent to catch that.
5085 // Therefore, we do some fail fast check to minimize the up
5086 // tree traversal.
5087 boolean isVisible = mAttachInfo.mWindowVisibility == View.VISIBLE
5088 && getAlpha() > 0
5089 && isShown()
5090 && getGlobalVisibleRect(visibleRect, offset);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005091 if (isVisible && boundInView != null) {
5092 visibleRect.offset(-offset.x, -offset.y);
Romain Guyf0af1d52012-07-11 18:31:21 -07005093 // isVisible is always true here, use a simple assignment
5094 isVisible = boundInView.intersect(visibleRect);
Guang Zhu0d607fb2012-05-11 19:34:56 -07005095 }
5096 return isVisible;
Romain Guyf0af1d52012-07-11 18:31:21 -07005097 }
5098
5099 return false;
Svetoslav Ganov749e7962012-04-19 17:13:46 -07005100 }
5101
5102 /**
alanvb72fe7a2012-08-27 16:44:25 -07005103 * Returns the delegate for implementing accessibility support via
5104 * composition. For more details see {@link AccessibilityDelegate}.
5105 *
5106 * @return The delegate, or null if none set.
5107 *
5108 * @hide
5109 */
5110 public AccessibilityDelegate getAccessibilityDelegate() {
5111 return mAccessibilityDelegate;
5112 }
5113
5114 /**
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07005115 * Sets a delegate for implementing accessibility support via composition as
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07005116 * opposed to inheritance. The delegate's primary use is for implementing
5117 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
5118 *
5119 * @param delegate The delegate instance.
5120 *
5121 * @see AccessibilityDelegate
5122 */
5123 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
5124 mAccessibilityDelegate = delegate;
5125 }
5126
5127 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07005128 * Gets the provider for managing a virtual view hierarchy rooted at this View
5129 * and reported to {@link android.accessibilityservice.AccessibilityService}s
5130 * that explore the window content.
5131 * <p>
5132 * If this method returns an instance, this instance is responsible for managing
5133 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
5134 * View including the one representing the View itself. Similarly the returned
5135 * instance is responsible for performing accessibility actions on any virtual
5136 * view or the root view itself.
5137 * </p>
5138 * <p>
5139 * If an {@link AccessibilityDelegate} has been specified via calling
5140 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
5141 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
5142 * is responsible for handling this call.
5143 * </p>
5144 *
5145 * @return The provider.
5146 *
5147 * @see AccessibilityNodeProvider
5148 */
5149 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
5150 if (mAccessibilityDelegate != null) {
5151 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
5152 } else {
5153 return null;
5154 }
5155 }
5156
5157 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005158 * Gets the unique identifier of this view on the screen for accessibility purposes.
5159 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
5160 *
5161 * @return The view accessibility id.
5162 *
5163 * @hide
5164 */
5165 public int getAccessibilityViewId() {
5166 if (mAccessibilityViewId == NO_ID) {
5167 mAccessibilityViewId = sNextAccessibilityViewId++;
5168 }
5169 return mAccessibilityViewId;
5170 }
5171
5172 /**
5173 * Gets the unique identifier of the window in which this View reseides.
5174 *
5175 * @return The window accessibility id.
5176 *
5177 * @hide
5178 */
5179 public int getAccessibilityWindowId() {
5180 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
5181 }
5182
5183 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07005184 * Gets the {@link View} description. It briefly describes the view and is
5185 * primarily used for accessibility support. Set this property to enable
5186 * better accessibility support for your application. This is especially
5187 * true for views that do not have textual representation (For example,
5188 * ImageButton).
5189 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07005190 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07005191 *
5192 * @attr ref android.R.styleable#View_contentDescription
5193 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07005194 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07005195 public CharSequence getContentDescription() {
5196 return mContentDescription;
5197 }
5198
5199 /**
5200 * Sets the {@link View} description. It briefly describes the view and is
5201 * primarily used for accessibility support. Set this property to enable
5202 * better accessibility support for your application. This is especially
5203 * true for views that do not have textual representation (For example,
5204 * ImageButton).
5205 *
5206 * @param contentDescription The content description.
5207 *
5208 * @attr ref android.R.styleable#View_contentDescription
5209 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07005210 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07005211 public void setContentDescription(CharSequence contentDescription) {
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005212 if (mContentDescription == null) {
5213 if (contentDescription == null) {
5214 return;
5215 }
5216 } else if (mContentDescription.equals(contentDescription)) {
5217 return;
5218 }
svetoslavganov75986cf2009-05-14 22:28:01 -07005219 mContentDescription = contentDescription;
Svetoslav Ganove47957a2012-06-05 14:46:50 -07005220 final boolean nonEmptyDesc = contentDescription != null && contentDescription.length() > 0;
5221 if (nonEmptyDesc && getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
5222 setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
5223 }
Svetoslav Ganovb708f772012-10-10 21:25:55 -07005224 notifyAccessibilityStateChanged();
svetoslavganov75986cf2009-05-14 22:28:01 -07005225 }
5226
5227 /**
Svetoslav Ganov33aef982012-09-13 12:49:03 -07005228 * Gets the id of a view for which this view serves as a label for
5229 * accessibility purposes.
5230 *
5231 * @return The labeled view id.
5232 */
5233 @ViewDebug.ExportedProperty(category = "accessibility")
5234 public int getLabelFor() {
5235 return mLabelForId;
5236 }
5237
5238 /**
5239 * Sets the id of a view for which this view serves as a label for
5240 * accessibility purposes.
5241 *
5242 * @param id The labeled view id.
5243 */
5244 @RemotableViewMethod
5245 public void setLabelFor(int id) {
5246 mLabelForId = id;
5247 if (mLabelForId != View.NO_ID
5248 && mID == View.NO_ID) {
5249 mID = generateViewId();
5250 }
5251 }
5252
5253 /**
Romain Guya2431d02009-04-30 16:30:00 -07005254 * Invoked whenever this view loses focus, either by losing window focus or by losing
5255 * focus within its window. This method can be used to clear any state tied to the
5256 * focus. For instance, if a button is held pressed with the trackball and the window
5257 * loses focus, this method can be used to cancel the press.
5258 *
5259 * Subclasses of View overriding this method should always call super.onFocusLost().
5260 *
5261 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07005262 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07005263 *
5264 * @hide pending API council approval
5265 */
5266 protected void onFocusLost() {
5267 resetPressedState();
5268 }
5269
5270 private void resetPressedState() {
5271 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5272 return;
5273 }
5274
5275 if (isPressed()) {
5276 setPressed(false);
5277
5278 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005279 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005280 }
5281 }
5282 }
5283
5284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 * Returns true if this view has focus
5286 *
5287 * @return True if this view has focus, false otherwise.
5288 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005289 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005290 public boolean isFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005291 return (mPrivateFlags & PFLAG_FOCUSED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 }
5293
5294 /**
5295 * Find the view in the hierarchy rooted at this view that currently has
5296 * focus.
5297 *
5298 * @return The view that currently has focus, or null if no focused view can
5299 * be found.
5300 */
5301 public View findFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005302 return (mPrivateFlags & PFLAG_FOCUSED) != 0 ? this : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 }
5304
5305 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07005306 * Indicates whether this view is one of the set of scrollable containers in
5307 * its window.
5308 *
5309 * @return whether this view is one of the set of scrollable containers in
5310 * its window
5311 *
5312 * @attr ref android.R.styleable#View_isScrollContainer
5313 */
5314 public boolean isScrollContainer() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005315 return (mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0;
Philip Milne6c8ea062012-04-03 17:38:43 -07005316 }
5317
5318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 * Change whether this view is one of the set of scrollable containers in
5320 * its window. This will be used to determine whether the window can
5321 * resize or must pan when a soft input area is open -- scrollable
5322 * containers allow the window to use resize mode since the container
5323 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07005324 *
5325 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 */
5327 public void setScrollContainer(boolean isScrollContainer) {
5328 if (isScrollContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005329 if (mAttachInfo != null && (mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -07005331 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005333 mPrivateFlags |= PFLAG_SCROLL_CONTAINER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005335 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 mAttachInfo.mScrollContainers.remove(this);
5337 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07005338 mPrivateFlags &= ~(PFLAG_SCROLL_CONTAINER|PFLAG_SCROLL_CONTAINER_ADDED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 }
5340 }
5341
5342 /**
5343 * Returns the quality of the drawing cache.
5344 *
5345 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5346 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5347 *
5348 * @see #setDrawingCacheQuality(int)
5349 * @see #setDrawingCacheEnabled(boolean)
5350 * @see #isDrawingCacheEnabled()
5351 *
5352 * @attr ref android.R.styleable#View_drawingCacheQuality
5353 */
5354 public int getDrawingCacheQuality() {
5355 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
5356 }
5357
5358 /**
5359 * Set the drawing cache quality of this view. This value is used only when the
5360 * drawing cache is enabled
5361 *
5362 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5363 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5364 *
5365 * @see #getDrawingCacheQuality()
5366 * @see #setDrawingCacheEnabled(boolean)
5367 * @see #isDrawingCacheEnabled()
5368 *
5369 * @attr ref android.R.styleable#View_drawingCacheQuality
5370 */
5371 public void setDrawingCacheQuality(int quality) {
5372 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5373 }
5374
5375 /**
5376 * Returns whether the screen should remain on, corresponding to the current
5377 * value of {@link #KEEP_SCREEN_ON}.
5378 *
5379 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5380 *
5381 * @see #setKeepScreenOn(boolean)
5382 *
5383 * @attr ref android.R.styleable#View_keepScreenOn
5384 */
5385 public boolean getKeepScreenOn() {
5386 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5387 }
5388
5389 /**
5390 * Controls whether the screen should remain on, modifying the
5391 * value of {@link #KEEP_SCREEN_ON}.
5392 *
5393 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5394 *
5395 * @see #getKeepScreenOn()
5396 *
5397 * @attr ref android.R.styleable#View_keepScreenOn
5398 */
5399 public void setKeepScreenOn(boolean keepScreenOn) {
5400 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5401 }
5402
5403 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005404 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5405 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 *
5407 * @attr ref android.R.styleable#View_nextFocusLeft
5408 */
5409 public int getNextFocusLeftId() {
5410 return mNextFocusLeftId;
5411 }
5412
5413 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005414 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5415 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5416 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 *
5418 * @attr ref android.R.styleable#View_nextFocusLeft
5419 */
5420 public void setNextFocusLeftId(int nextFocusLeftId) {
5421 mNextFocusLeftId = nextFocusLeftId;
5422 }
5423
5424 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005425 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5426 * @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 -08005427 *
5428 * @attr ref android.R.styleable#View_nextFocusRight
5429 */
5430 public int getNextFocusRightId() {
5431 return mNextFocusRightId;
5432 }
5433
5434 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005435 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5436 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5437 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005438 *
5439 * @attr ref android.R.styleable#View_nextFocusRight
5440 */
5441 public void setNextFocusRightId(int nextFocusRightId) {
5442 mNextFocusRightId = nextFocusRightId;
5443 }
5444
5445 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005446 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5447 * @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 -08005448 *
5449 * @attr ref android.R.styleable#View_nextFocusUp
5450 */
5451 public int getNextFocusUpId() {
5452 return mNextFocusUpId;
5453 }
5454
5455 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005456 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5457 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5458 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005459 *
5460 * @attr ref android.R.styleable#View_nextFocusUp
5461 */
5462 public void setNextFocusUpId(int nextFocusUpId) {
5463 mNextFocusUpId = nextFocusUpId;
5464 }
5465
5466 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005467 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5468 * @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 -08005469 *
5470 * @attr ref android.R.styleable#View_nextFocusDown
5471 */
5472 public int getNextFocusDownId() {
5473 return mNextFocusDownId;
5474 }
5475
5476 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005477 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5478 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5479 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005480 *
5481 * @attr ref android.R.styleable#View_nextFocusDown
5482 */
5483 public void setNextFocusDownId(int nextFocusDownId) {
5484 mNextFocusDownId = nextFocusDownId;
5485 }
5486
5487 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005488 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5489 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5490 *
5491 * @attr ref android.R.styleable#View_nextFocusForward
5492 */
5493 public int getNextFocusForwardId() {
5494 return mNextFocusForwardId;
5495 }
5496
5497 /**
5498 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5499 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5500 * decide automatically.
5501 *
5502 * @attr ref android.R.styleable#View_nextFocusForward
5503 */
5504 public void setNextFocusForwardId(int nextFocusForwardId) {
5505 mNextFocusForwardId = nextFocusForwardId;
5506 }
5507
5508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 * Returns the visibility of this view and all of its ancestors
5510 *
5511 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5512 */
5513 public boolean isShown() {
5514 View current = this;
5515 //noinspection ConstantConditions
5516 do {
5517 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5518 return false;
5519 }
5520 ViewParent parent = current.mParent;
5521 if (parent == null) {
5522 return false; // We are not attached to the view root
5523 }
5524 if (!(parent instanceof View)) {
5525 return true;
5526 }
5527 current = (View) parent;
5528 } while (current != null);
5529
5530 return false;
5531 }
5532
5533 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005534 * Called by the view hierarchy when the content insets for a window have
5535 * changed, to allow it to adjust its content to fit within those windows.
5536 * The content insets tell you the space that the status bar, input method,
5537 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005539 * <p>You do not normally need to deal with this function, since the default
5540 * window decoration given to applications takes care of applying it to the
5541 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5542 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5543 * and your content can be placed under those system elements. You can then
5544 * use this method within your view hierarchy if you have parts of your UI
5545 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005547 * <p>The default implementation of this method simply applies the content
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005548 * inset's to the view's padding, consuming that content (modifying the
5549 * insets to be 0), and returning true. This behavior is off by default, but can
5550 * be enabled through {@link #setFitsSystemWindows(boolean)}.
5551 *
5552 * <p>This function's traversal down the hierarchy is depth-first. The same content
5553 * insets object is propagated down the hierarchy, so any changes made to it will
5554 * be seen by all following views (including potentially ones above in
5555 * the hierarchy since this is a depth-first traversal). The first view
5556 * that returns true will abort the entire traversal.
5557 *
5558 * <p>The default implementation works well for a situation where it is
5559 * used with a container that covers the entire window, allowing it to
5560 * apply the appropriate insets to its content on all edges. If you need
5561 * a more complicated layout (such as two different views fitting system
5562 * windows, one on the top of the window, and one on the bottom),
5563 * you can override the method and handle the insets however you would like.
5564 * Note that the insets provided by the framework are always relative to the
5565 * far edges of the window, not accounting for the location of the called view
5566 * within that window. (In fact when this method is called you do not yet know
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005567 * where the layout will place the view, as it is done before layout happens.)
5568 *
5569 * <p>Note: unlike many View methods, there is no dispatch phase to this
5570 * call. If you are overriding it in a ViewGroup and want to allow the
5571 * call to continue to your children, you must be sure to call the super
5572 * implementation.
5573 *
Dianne Hackborncf675782012-05-10 15:07:24 -07005574 * <p>Here is a sample layout that makes use of fitting system windows
5575 * to have controls for a video view placed inside of the window decorations
5576 * that it hides and shows. This can be used with code like the second
5577 * sample (video player) shown in {@link #setSystemUiVisibility(int)}.
5578 *
5579 * {@sample development/samples/ApiDemos/res/layout/video_player.xml complete}
5580 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005581 * @param insets Current content insets of the window. Prior to
5582 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5583 * the insets or else you and Android will be unhappy.
5584 *
5585 * @return Return true if this view applied the insets and it should not
5586 * continue propagating further down the hierarchy, false otherwise.
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005587 * @see #getFitsSystemWindows()
Romain Guyf0af1d52012-07-11 18:31:21 -07005588 * @see #setFitsSystemWindows(boolean)
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005589 * @see #setSystemUiVisibility(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005590 */
5591 protected boolean fitSystemWindows(Rect insets) {
5592 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -07005593 mUserPaddingStart = UNDEFINED_PADDING;
5594 mUserPaddingEnd = UNDEFINED_PADDING;
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005595 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5596 || mAttachInfo == null
5597 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5598 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5599 return true;
5600 } else {
5601 internalSetPadding(0, 0, 0, 0);
5602 return false;
5603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 }
5605 return false;
5606 }
5607
5608 /**
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005609 * Sets whether or not this view should account for system screen decorations
5610 * such as the status bar and inset its content; that is, controlling whether
5611 * the default implementation of {@link #fitSystemWindows(Rect)} will be
5612 * executed. See that method for more details.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005613 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005614 * <p>Note that if you are providing your own implementation of
5615 * {@link #fitSystemWindows(Rect)}, then there is no need to set this
5616 * flag to true -- your implementation will be overriding the default
5617 * implementation that checks this flag.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005618 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005619 * @param fitSystemWindows If true, then the default implementation of
5620 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005621 *
5622 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005623 * @see #getFitsSystemWindows()
5624 * @see #fitSystemWindows(Rect)
5625 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005626 */
5627 public void setFitsSystemWindows(boolean fitSystemWindows) {
5628 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5629 }
5630
5631 /**
Dianne Hackborncf675782012-05-10 15:07:24 -07005632 * Check for state of {@link #setFitsSystemWindows(boolean). If this method
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005633 * returns true, the default implementation of {@link #fitSystemWindows(Rect)}
5634 * will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005635 *
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005636 * @return Returns true if the default implementation of
5637 * {@link #fitSystemWindows(Rect)} will be executed.
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005638 *
5639 * @attr ref android.R.styleable#View_fitsSystemWindows
Dianne Hackbornd5333f92012-05-18 10:51:35 -07005640 * @see #setFitsSystemWindows()
5641 * @see #fitSystemWindows(Rect)
5642 * @see #setSystemUiVisibility(int)
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005643 */
Dianne Hackborncf675782012-05-10 15:07:24 -07005644 public boolean getFitsSystemWindows() {
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005645 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5646 }
5647
Dianne Hackbornb1b55e62012-05-10 16:25:54 -07005648 /** @hide */
5649 public boolean fitsSystemWindows() {
5650 return getFitsSystemWindows();
5651 }
5652
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005653 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005654 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5655 */
5656 public void requestFitSystemWindows() {
5657 if (mParent != null) {
5658 mParent.requestFitSystemWindows();
5659 }
5660 }
5661
5662 /**
5663 * For use by PhoneWindow to make its own system window fitting optional.
5664 * @hide
5665 */
5666 public void makeOptionalFitsSystemWindows() {
5667 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5668 }
5669
5670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 * Returns the visibility status for this view.
5672 *
5673 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5674 * @attr ref android.R.styleable#View_visibility
5675 */
5676 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005677 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5678 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5679 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 })
5681 public int getVisibility() {
5682 return mViewFlags & VISIBILITY_MASK;
5683 }
5684
5685 /**
5686 * Set the enabled state of this view.
5687 *
5688 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5689 * @attr ref android.R.styleable#View_visibility
5690 */
5691 @RemotableViewMethod
5692 public void setVisibility(int visibility) {
5693 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005694 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005695 }
5696
5697 /**
5698 * Returns the enabled status for this view. The interpretation of the
5699 * enabled state varies by subclass.
5700 *
5701 * @return True if this view is enabled, false otherwise.
5702 */
5703 @ViewDebug.ExportedProperty
5704 public boolean isEnabled() {
5705 return (mViewFlags & ENABLED_MASK) == ENABLED;
5706 }
5707
5708 /**
5709 * Set the enabled state of this view. The interpretation of the enabled
5710 * state varies by subclass.
5711 *
5712 * @param enabled True if this view is enabled, false otherwise.
5713 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005714 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005716 if (enabled == isEnabled()) return;
5717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5719
5720 /*
5721 * The View most likely has to change its appearance, so refresh
5722 * the drawable state.
5723 */
5724 refreshDrawableState();
5725
5726 // Invalidate too, since the default behavior for views is to be
5727 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005728 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 }
5730
5731 /**
5732 * Set whether this view can receive the focus.
5733 *
5734 * Setting this to false will also ensure that this view is not focusable
5735 * in touch mode.
5736 *
5737 * @param focusable If true, this view can receive the focus.
5738 *
5739 * @see #setFocusableInTouchMode(boolean)
5740 * @attr ref android.R.styleable#View_focusable
5741 */
5742 public void setFocusable(boolean focusable) {
5743 if (!focusable) {
5744 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5745 }
5746 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5747 }
5748
5749 /**
5750 * Set whether this view can receive focus while in touch mode.
5751 *
5752 * Setting this to true will also ensure that this view is focusable.
5753 *
5754 * @param focusableInTouchMode If true, this view can receive the focus while
5755 * in touch mode.
5756 *
5757 * @see #setFocusable(boolean)
5758 * @attr ref android.R.styleable#View_focusableInTouchMode
5759 */
5760 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5761 // Focusable in touch mode should always be set before the focusable flag
5762 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5763 // which, in touch mode, will not successfully request focus on this view
5764 // because the focusable in touch mode flag is not set
5765 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5766 if (focusableInTouchMode) {
5767 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5768 }
5769 }
5770
5771 /**
5772 * Set whether this view should have sound effects enabled for events such as
5773 * clicking and touching.
5774 *
5775 * <p>You may wish to disable sound effects for a view if you already play sounds,
5776 * for instance, a dial key that plays dtmf tones.
5777 *
5778 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5779 * @see #isSoundEffectsEnabled()
5780 * @see #playSoundEffect(int)
5781 * @attr ref android.R.styleable#View_soundEffectsEnabled
5782 */
5783 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5784 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5785 }
5786
5787 /**
5788 * @return whether this view should have sound effects enabled for events such as
5789 * clicking and touching.
5790 *
5791 * @see #setSoundEffectsEnabled(boolean)
5792 * @see #playSoundEffect(int)
5793 * @attr ref android.R.styleable#View_soundEffectsEnabled
5794 */
5795 @ViewDebug.ExportedProperty
5796 public boolean isSoundEffectsEnabled() {
5797 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5798 }
5799
5800 /**
5801 * Set whether this view should have haptic feedback for events such as
5802 * long presses.
5803 *
5804 * <p>You may wish to disable haptic feedback if your view already controls
5805 * its own haptic feedback.
5806 *
5807 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5808 * @see #isHapticFeedbackEnabled()
5809 * @see #performHapticFeedback(int)
5810 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5811 */
5812 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5813 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5814 }
5815
5816 /**
5817 * @return whether this view should have haptic feedback enabled for events
5818 * long presses.
5819 *
5820 * @see #setHapticFeedbackEnabled(boolean)
5821 * @see #performHapticFeedback(int)
5822 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5823 */
5824 @ViewDebug.ExportedProperty
5825 public boolean isHapticFeedbackEnabled() {
5826 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5827 }
5828
5829 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005830 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005831 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005832 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5833 * {@link #LAYOUT_DIRECTION_RTL},
5834 * {@link #LAYOUT_DIRECTION_INHERIT} or
5835 * {@link #LAYOUT_DIRECTION_LOCALE}.
5836 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005837 *
5838 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005839 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005840 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005841 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5842 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5843 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5844 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005845 })
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005846 public int getRawLayoutDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005847 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >> PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005848 }
5849
5850 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005851 * Set the layout direction for this view. This will propagate a reset of layout direction
5852 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005853 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005854 * @param layoutDirection the layout direction to set. Should be one of:
5855 *
5856 * {@link #LAYOUT_DIRECTION_LTR},
5857 * {@link #LAYOUT_DIRECTION_RTL},
5858 * {@link #LAYOUT_DIRECTION_INHERIT},
5859 * {@link #LAYOUT_DIRECTION_LOCALE}.
5860 *
5861 * Resolution will be done if the value is set to LAYOUT_DIRECTION_INHERIT. The resolution
5862 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
5863 * will return the default {@link #LAYOUT_DIRECTION_LTR}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005864 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005865 * @attr ref android.R.styleable#View_layoutDirection
Cibu Johny7632cb92010-02-22 13:01:02 -08005866 */
5867 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005868 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005869 if (getRawLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005870 // Reset the current layout direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -07005871 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4457e852012-09-18 19:23:12 -07005872 resetRtlProperties();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005873 // Set the new layout direction (filtered)
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005874 mPrivateFlags2 |=
Dianne Hackborn4702a852012-08-17 15:18:29 -07005875 ((layoutDirection << PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) & PFLAG2_LAYOUT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005876 // We need to resolve all RTL properties as they all depend on layout direction
5877 resolveRtlPropertiesIfNeeded();
Fabrice Di Meglioacb1c122012-10-02 14:18:55 -07005878 requestLayout();
5879 invalidate(true);
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005880 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005881 }
5882
5883 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005884 * Returns the resolved layout direction for this view.
5885 *
5886 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005887 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005888 *
5889 * For compatibility, this will return {@link #LAYOUT_DIRECTION_LTR} if API version
5890 * is lower than {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005891 */
5892 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005893 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5894 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005895 })
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005896 public int getLayoutDirection() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005897 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
5898 if (targetSdkVersion < JELLY_BEAN_MR1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005899 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07005900 return LAYOUT_DIRECTION_LTR;
5901 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -07005902 return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
5903 PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005904 }
5905
5906 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005907 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5908 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005909 *
5910 * @return true if the layout is right-to-left.
Fabrice Di Meglio9a048562012-09-26 14:55:56 -07005911 *
5912 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005913 */
5914 @ViewDebug.ExportedProperty(category = "layout")
5915 public boolean isLayoutRtl() {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -07005916 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005917 }
5918
5919 /**
Adam Powell539ee872012-02-03 19:00:49 -08005920 * Indicates whether the view is currently tracking transient state that the
5921 * app should not need to concern itself with saving and restoring, but that
5922 * the framework should take special note to preserve when possible.
5923 *
Adam Powell785c4472012-05-02 21:25:39 -07005924 * <p>A view with transient state cannot be trivially rebound from an external
5925 * data source, such as an adapter binding item views in a list. This may be
5926 * because the view is performing an animation, tracking user selection
5927 * of content, or similar.</p>
5928 *
Adam Powell539ee872012-02-03 19:00:49 -08005929 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005930 */
5931 @ViewDebug.ExportedProperty(category = "layout")
5932 public boolean hasTransientState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07005933 return (mPrivateFlags2 & PFLAG2_HAS_TRANSIENT_STATE) == PFLAG2_HAS_TRANSIENT_STATE;
Adam Powell539ee872012-02-03 19:00:49 -08005934 }
5935
5936 /**
5937 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005938 * framework should attempt to preserve when possible. This flag is reference counted,
5939 * so every call to setHasTransientState(true) should be paired with a later call
5940 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005941 *
Adam Powell785c4472012-05-02 21:25:39 -07005942 * <p>A view with transient state cannot be trivially rebound from an external
5943 * data source, such as an adapter binding item views in a list. This may be
5944 * because the view is performing an animation, tracking user selection
5945 * of content, or similar.</p>
5946 *
Adam Powell539ee872012-02-03 19:00:49 -08005947 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005948 */
5949 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005950 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5951 mTransientStateCount - 1;
5952 if (mTransientStateCount < 0) {
5953 mTransientStateCount = 0;
5954 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5955 "unmatched pair of setHasTransientState calls");
5956 }
5957 if ((hasTransientState && mTransientStateCount == 1) ||
Adam Powell057a5852012-05-11 10:28:38 -07005958 (!hasTransientState && mTransientStateCount == 0)) {
Chet Haase563d4f22012-04-18 16:20:08 -07005959 // update flag if we've just incremented up from 0 or decremented down to 0
Dianne Hackborn4702a852012-08-17 15:18:29 -07005960 mPrivateFlags2 = (mPrivateFlags2 & ~PFLAG2_HAS_TRANSIENT_STATE) |
5961 (hasTransientState ? PFLAG2_HAS_TRANSIENT_STATE : 0);
Chet Haase563d4f22012-04-18 16:20:08 -07005962 if (mParent != null) {
5963 try {
5964 mParent.childHasTransientStateChanged(this, hasTransientState);
5965 } catch (AbstractMethodError e) {
5966 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5967 " does not fully implement ViewParent", e);
5968 }
Adam Powell539ee872012-02-03 19:00:49 -08005969 }
5970 }
5971 }
5972
5973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 * If this view doesn't do any drawing on its own, set this flag to
5975 * allow further optimizations. By default, this flag is not set on
5976 * View, but could be set on some View subclasses such as ViewGroup.
5977 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005978 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5979 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 *
5981 * @param willNotDraw whether or not this View draw on its own
5982 */
5983 public void setWillNotDraw(boolean willNotDraw) {
5984 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5985 }
5986
5987 /**
5988 * Returns whether or not this View draws on its own.
5989 *
5990 * @return true if this view has nothing to draw, false otherwise
5991 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005992 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 public boolean willNotDraw() {
5994 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5995 }
5996
5997 /**
5998 * When a View's drawing cache is enabled, drawing is redirected to an
5999 * offscreen bitmap. Some views, like an ImageView, must be able to
6000 * bypass this mechanism if they already draw a single bitmap, to avoid
6001 * unnecessary usage of the memory.
6002 *
6003 * @param willNotCacheDrawing true if this view does not cache its
6004 * drawing, false otherwise
6005 */
6006 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
6007 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
6008 }
6009
6010 /**
6011 * Returns whether or not this View can cache its drawing or not.
6012 *
6013 * @return true if this view does not cache its drawing, false otherwise
6014 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006015 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 public boolean willNotCacheDrawing() {
6017 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
6018 }
6019
6020 /**
6021 * Indicates whether this view reacts to click events or not.
6022 *
6023 * @return true if the view is clickable, false otherwise
6024 *
6025 * @see #setClickable(boolean)
6026 * @attr ref android.R.styleable#View_clickable
6027 */
6028 @ViewDebug.ExportedProperty
6029 public boolean isClickable() {
6030 return (mViewFlags & CLICKABLE) == CLICKABLE;
6031 }
6032
6033 /**
6034 * Enables or disables click events for this view. When a view
6035 * is clickable it will change its state to "pressed" on every click.
6036 * Subclasses should set the view clickable to visually react to
6037 * user's clicks.
6038 *
6039 * @param clickable true to make the view clickable, false otherwise
6040 *
6041 * @see #isClickable()
6042 * @attr ref android.R.styleable#View_clickable
6043 */
6044 public void setClickable(boolean clickable) {
6045 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
6046 }
6047
6048 /**
6049 * Indicates whether this view reacts to long click events or not.
6050 *
6051 * @return true if the view is long clickable, false otherwise
6052 *
6053 * @see #setLongClickable(boolean)
6054 * @attr ref android.R.styleable#View_longClickable
6055 */
6056 public boolean isLongClickable() {
6057 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6058 }
6059
6060 /**
6061 * Enables or disables long click events for this view. When a view is long
6062 * clickable it reacts to the user holding down the button for a longer
6063 * duration than a tap. This event can either launch the listener or a
6064 * context menu.
6065 *
6066 * @param longClickable true to make the view long clickable, false otherwise
6067 * @see #isLongClickable()
6068 * @attr ref android.R.styleable#View_longClickable
6069 */
6070 public void setLongClickable(boolean longClickable) {
6071 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
6072 }
6073
6074 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07006075 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 *
6077 * @see #isClickable()
6078 * @see #setClickable(boolean)
6079 *
6080 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
6081 * the View's internal state from a previously set "pressed" state.
6082 */
6083 public void setPressed(boolean pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006084 final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08006085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 if (pressed) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006087 mPrivateFlags |= PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006089 mPrivateFlags &= ~PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006090 }
Adam Powell035a1fc2012-02-27 15:23:50 -08006091
6092 if (needsRefresh) {
6093 refreshDrawableState();
6094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006095 dispatchSetPressed(pressed);
6096 }
6097
6098 /**
6099 * Dispatch setPressed to all of this View's children.
6100 *
6101 * @see #setPressed(boolean)
6102 *
6103 * @param pressed The new pressed state
6104 */
6105 protected void dispatchSetPressed(boolean pressed) {
6106 }
6107
6108 /**
6109 * Indicates whether the view is currently in pressed state. Unless
6110 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
6111 * the pressed state.
6112 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006113 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 * @see #isClickable()
6115 * @see #setClickable(boolean)
6116 *
6117 * @return true if the view is currently pressed, false otherwise
6118 */
6119 public boolean isPressed() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006120 return (mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 }
6122
6123 /**
6124 * Indicates whether this view will save its state (that is,
6125 * whether its {@link #onSaveInstanceState} method will be called).
6126 *
6127 * @return Returns true if the view state saving is enabled, else false.
6128 *
6129 * @see #setSaveEnabled(boolean)
6130 * @attr ref android.R.styleable#View_saveEnabled
6131 */
6132 public boolean isSaveEnabled() {
6133 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
6134 }
6135
6136 /**
6137 * Controls whether the saving of this view's state is
6138 * enabled (that is, whether its {@link #onSaveInstanceState} method
6139 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07006140 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 * for its state to be saved. This flag can only disable the
6142 * saving of this view; any child views may still have their state saved.
6143 *
6144 * @param enabled Set to false to <em>disable</em> state saving, or true
6145 * (the default) to allow it.
6146 *
6147 * @see #isSaveEnabled()
6148 * @see #setId(int)
6149 * @see #onSaveInstanceState()
6150 * @attr ref android.R.styleable#View_saveEnabled
6151 */
6152 public void setSaveEnabled(boolean enabled) {
6153 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
6154 }
6155
Jeff Brown85a31762010-09-01 17:01:00 -07006156 /**
6157 * Gets whether the framework should discard touches when the view's
6158 * window is obscured by another visible window.
6159 * Refer to the {@link View} security documentation for more details.
6160 *
6161 * @return True if touch filtering is enabled.
6162 *
6163 * @see #setFilterTouchesWhenObscured(boolean)
6164 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6165 */
6166 @ViewDebug.ExportedProperty
6167 public boolean getFilterTouchesWhenObscured() {
6168 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
6169 }
6170
6171 /**
6172 * Sets whether the framework should discard touches when the view's
6173 * window is obscured by another visible window.
6174 * Refer to the {@link View} security documentation for more details.
6175 *
6176 * @param enabled True if touch filtering should be enabled.
6177 *
6178 * @see #getFilterTouchesWhenObscured
6179 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
6180 */
6181 public void setFilterTouchesWhenObscured(boolean enabled) {
6182 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
6183 FILTER_TOUCHES_WHEN_OBSCURED);
6184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006185
6186 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07006187 * Indicates whether the entire hierarchy under this view will save its
6188 * state when a state saving traversal occurs from its parent. The default
6189 * is true; if false, these views will not be saved unless
6190 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6191 *
6192 * @return Returns true if the view state saving from parent is enabled, else false.
6193 *
6194 * @see #setSaveFromParentEnabled(boolean)
6195 */
6196 public boolean isSaveFromParentEnabled() {
6197 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
6198 }
6199
6200 /**
6201 * Controls whether the entire hierarchy under this view will save its
6202 * state when a state saving traversal occurs from its parent. The default
6203 * is true; if false, these views will not be saved unless
6204 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
6205 *
6206 * @param enabled Set to false to <em>disable</em> state saving, or true
6207 * (the default) to allow it.
6208 *
6209 * @see #isSaveFromParentEnabled()
6210 * @see #setId(int)
6211 * @see #onSaveInstanceState()
6212 */
6213 public void setSaveFromParentEnabled(boolean enabled) {
6214 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
6215 }
6216
6217
6218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 * Returns whether this View is able to take focus.
6220 *
6221 * @return True if this view can take focus, or false otherwise.
6222 * @attr ref android.R.styleable#View_focusable
6223 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006224 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 public final boolean isFocusable() {
6226 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
6227 }
6228
6229 /**
6230 * When a view is focusable, it may not want to take focus when in touch mode.
6231 * For example, a button would like focus when the user is navigating via a D-pad
6232 * so that the user can click on it, but once the user starts touching the screen,
6233 * the button shouldn't take focus
6234 * @return Whether the view is focusable in touch mode.
6235 * @attr ref android.R.styleable#View_focusableInTouchMode
6236 */
6237 @ViewDebug.ExportedProperty
6238 public final boolean isFocusableInTouchMode() {
6239 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
6240 }
6241
6242 /**
6243 * Find the nearest view in the specified direction that can take focus.
6244 * This does not actually give focus to that view.
6245 *
6246 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6247 *
6248 * @return The nearest focusable in the specified direction, or null if none
6249 * can be found.
6250 */
6251 public View focusSearch(int direction) {
6252 if (mParent != null) {
6253 return mParent.focusSearch(this, direction);
6254 } else {
6255 return null;
6256 }
6257 }
6258
6259 /**
6260 * This method is the last chance for the focused view and its ancestors to
6261 * respond to an arrow key. This is called when the focused view did not
6262 * consume the key internally, nor could the view system find a new view in
6263 * the requested direction to give focus to.
6264 *
6265 * @param focused The currently focused view.
6266 * @param direction The direction focus wants to move. One of FOCUS_UP,
6267 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
6268 * @return True if the this view consumed this unhandled move.
6269 */
6270 public boolean dispatchUnhandledMove(View focused, int direction) {
6271 return false;
6272 }
6273
6274 /**
6275 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08006276 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006277 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08006278 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
6279 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 * @return The user specified next view, or null if there is none.
6281 */
6282 View findUserSetNextFocus(View root, int direction) {
6283 switch (direction) {
6284 case FOCUS_LEFT:
6285 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006286 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287 case FOCUS_RIGHT:
6288 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006289 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 case FOCUS_UP:
6291 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006292 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 case FOCUS_DOWN:
6294 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006295 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006296 case FOCUS_FORWARD:
6297 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006298 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08006299 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08006300 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08006301 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07006302 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08006303 @Override
6304 public boolean apply(View t) {
6305 return t.mNextFocusForwardId == id;
6306 }
6307 });
6308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006309 }
6310 return null;
6311 }
6312
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006313 private View findViewInsideOutShouldExist(View root, int id) {
6314 if (mMatchIdPredicate == null) {
6315 mMatchIdPredicate = new MatchIdPredicate();
6316 }
6317 mMatchIdPredicate.mId = id;
6318 View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006319 if (result == null) {
Svetoslav Ganov33aef982012-09-13 12:49:03 -07006320 Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 }
6322 return result;
6323 }
6324
6325 /**
6326 * Find and return all focusable views that are descendants of this view,
6327 * possibly including this view if it is focusable itself.
6328 *
6329 * @param direction The direction of the focus
6330 * @return A list of focusable views
6331 */
6332 public ArrayList<View> getFocusables(int direction) {
6333 ArrayList<View> result = new ArrayList<View>(24);
6334 addFocusables(result, direction);
6335 return result;
6336 }
6337
6338 /**
6339 * Add any focusable views that are descendants of this view (possibly
6340 * including this view if it is focusable itself) to views. If we are in touch mode,
6341 * only add views that are also focusable in touch mode.
6342 *
6343 * @param views Focusable views found so far
6344 * @param direction The direction of the focus
6345 */
6346 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006347 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
6348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349
svetoslavganov75986cf2009-05-14 22:28:01 -07006350 /**
6351 * Adds any focusable views that are descendants of this view (possibly
6352 * including this view if it is focusable itself) to views. This method
6353 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07006354 * only views focusable in touch mode if we are in touch mode or
6355 * only views that can take accessibility focus if accessibility is enabeld
6356 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07006357 *
6358 * @param views Focusable views found so far or null if all we are interested is
6359 * the number of focusables.
6360 * @param direction The direction of the focus.
6361 * @param focusableMode The type of focusables to be added.
6362 *
6363 * @see #FOCUSABLES_ALL
6364 * @see #FOCUSABLES_TOUCH_MODE
6365 */
6366 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006367 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07006368 return;
6369 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006370 if (!isFocusable()) {
6371 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07006372 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07006373 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
6374 && isInTouchMode() && !isFocusableInTouchMode()) {
6375 return;
6376 }
6377 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 }
6379
6380 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006381 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006382 * The search is performed by either the text that the View renders or the content
6383 * description that describes the view for accessibility purposes and the view does
6384 * not render or both. Clients can specify how the search is to be performed via
6385 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
6386 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006387 *
6388 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006389 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07006390 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006391 * @see #FIND_VIEWS_WITH_TEXT
6392 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
6393 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006394 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006395 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07006396 if (getAccessibilityNodeProvider() != null) {
6397 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
6398 outViews.add(this);
6399 }
6400 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006401 && (searched != null && searched.length() > 0)
6402 && (mContentDescription != null && mContentDescription.length() > 0)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006403 String searchedLowerCase = searched.toString().toLowerCase();
6404 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6405 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6406 outViews.add(this);
6407 }
6408 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006409 }
6410
6411 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 * Find and return all touchable views that are descendants of this view,
6413 * possibly including this view if it is touchable itself.
6414 *
6415 * @return A list of touchable views
6416 */
6417 public ArrayList<View> getTouchables() {
6418 ArrayList<View> result = new ArrayList<View>();
6419 addTouchables(result);
6420 return result;
6421 }
6422
6423 /**
6424 * Add any touchable views that are descendants of this view (possibly
6425 * including this view if it is touchable itself) to views.
6426 *
6427 * @param views Touchable views found so far
6428 */
6429 public void addTouchables(ArrayList<View> views) {
6430 final int viewFlags = mViewFlags;
6431
6432 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6433 && (viewFlags & ENABLED_MASK) == ENABLED) {
6434 views.add(this);
6435 }
6436 }
6437
6438 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006439 * Returns whether this View is accessibility focused.
6440 *
6441 * @return True if this View is accessibility focused.
6442 */
6443 boolean isAccessibilityFocused() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006444 return (mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006445 }
6446
6447 /**
6448 * Call this to try to give accessibility focus to this view.
6449 *
6450 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6451 * returns false or the view is no visible or the view already has accessibility
6452 * focus.
6453 *
6454 * See also {@link #focusSearch(int)}, which is what you call to say that you
6455 * have focus, and you want your parent to look for the next one.
6456 *
6457 * @return Whether this view actually took accessibility focus.
6458 *
6459 * @hide
6460 */
6461 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006462 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6463 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006464 return false;
6465 }
6466 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6467 return false;
6468 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006469 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) == 0) {
6470 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006471 ViewRootImpl viewRootImpl = getViewRootImpl();
6472 if (viewRootImpl != null) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006473 viewRootImpl.setAccessibilityFocus(this, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07006474 }
6475 invalidate();
6476 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6477 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006478 return true;
6479 }
6480 return false;
6481 }
6482
6483 /**
6484 * Call this to try to clear accessibility focus of this view.
6485 *
6486 * See also {@link #focusSearch(int)}, which is what you call to say that you
6487 * have focus, and you want your parent to look for the next one.
6488 *
6489 * @hide
6490 */
6491 public void clearAccessibilityFocus() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006492 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6493 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006494 invalidate();
6495 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6496 notifyAccessibilityStateChanged();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006497 }
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006498 // Clear the global reference of accessibility focus if this
6499 // view or any of its descendants had accessibility focus.
6500 ViewRootImpl viewRootImpl = getViewRootImpl();
6501 if (viewRootImpl != null) {
6502 View focusHost = viewRootImpl.getAccessibilityFocusedHost();
6503 if (focusHost != null && ViewRootImpl.isViewDescendantOf(focusHost, this)) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07006504 viewRootImpl.setAccessibilityFocus(null, null);
Svetoslav Ganovc00d0082012-05-22 18:37:49 -07006505 }
6506 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006507 }
6508
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07006509 private void sendAccessibilityHoverEvent(int eventType) {
6510 // Since we are not delivering to a client accessibility events from not
6511 // important views (unless the clinet request that) we need to fire the
6512 // event from the deepest view exposed to the client. As a consequence if
6513 // the user crosses a not exposed view the client will see enter and exit
6514 // of the exposed predecessor followed by and enter and exit of that same
6515 // predecessor when entering and exiting the not exposed descendant. This
6516 // is fine since the client has a clear idea which view is hovered at the
6517 // price of a couple more events being sent. This is a simple and
6518 // working solution.
6519 View source = this;
6520 while (true) {
6521 if (source.includeForAccessibility()) {
6522 source.sendAccessibilityEvent(eventType);
6523 return;
6524 }
6525 ViewParent parent = source.getParent();
6526 if (parent instanceof View) {
6527 source = (View) parent;
6528 } else {
6529 return;
6530 }
6531 }
6532 }
6533
Svetoslav Ganov42138042012-03-20 11:51:39 -07006534 /**
6535 * Clears accessibility focus without calling any callback methods
6536 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6537 * is used for clearing accessibility focus when giving this focus to
6538 * another view.
6539 */
6540 void clearAccessibilityFocusNoCallbacks() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006541 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_FOCUSED) != 0) {
6542 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006543 invalidate();
6544 }
6545 }
6546
6547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 * Call this to try to give focus to a specific view or to one of its
6549 * descendants.
6550 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006551 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6552 * false), or if it is focusable and it is not focusable in touch mode
6553 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006555 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 * have focus, and you want your parent to look for the next one.
6557 *
6558 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6559 * {@link #FOCUS_DOWN} and <code>null</code>.
6560 *
6561 * @return Whether this view or one of its descendants actually took focus.
6562 */
6563 public final boolean requestFocus() {
6564 return requestFocus(View.FOCUS_DOWN);
6565 }
6566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 /**
6568 * Call this to try to give focus to a specific view or to one of its
6569 * descendants and give it a hint about what direction focus is heading.
6570 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006571 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6572 * false), or if it is focusable and it is not focusable in touch mode
6573 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006575 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 * have focus, and you want your parent to look for the next one.
6577 *
6578 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6579 * <code>null</code> set for the previously focused rectangle.
6580 *
6581 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6582 * @return Whether this view or one of its descendants actually took focus.
6583 */
6584 public final boolean requestFocus(int direction) {
6585 return requestFocus(direction, null);
6586 }
6587
6588 /**
6589 * Call this to try to give focus to a specific view or to one of its descendants
6590 * and give it hints about the direction and a specific rectangle that the focus
6591 * is coming from. The rectangle can help give larger views a finer grained hint
6592 * about where focus is coming from, and therefore, where to show selection, or
6593 * forward focus change internally.
6594 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006595 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6596 * false), or if it is focusable and it is not focusable in touch mode
6597 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 *
6599 * A View will not take focus if it is not visible.
6600 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006601 * A View will not take focus if one of its parents has
6602 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6603 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006605 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 * have focus, and you want your parent to look for the next one.
6607 *
6608 * You may wish to override this method if your custom {@link View} has an internal
6609 * {@link View} that it wishes to forward the request to.
6610 *
6611 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6612 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6613 * to give a finer grained hint about where focus is coming from. May be null
6614 * if there is no hint.
6615 * @return Whether this view or one of its descendants actually took focus.
6616 */
6617 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006618 return requestFocusNoSearch(direction, previouslyFocusedRect);
6619 }
6620
6621 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 // need to be focusable
6623 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6624 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6625 return false;
6626 }
6627
6628 // need to be focusable in touch mode if in touch mode
6629 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006630 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6631 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 }
6633
6634 // need to not have any parents blocking us
6635 if (hasAncestorThatBlocksDescendantFocus()) {
6636 return false;
6637 }
6638
6639 handleFocusGainInternal(direction, previouslyFocusedRect);
6640 return true;
6641 }
6642
6643 /**
6644 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6645 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6646 * touch mode to request focus when they are touched.
6647 *
6648 * @return Whether this view or one of its descendants actually took focus.
6649 *
6650 * @see #isInTouchMode()
6651 *
6652 */
6653 public final boolean requestFocusFromTouch() {
6654 // Leave touch mode if we need to
6655 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006656 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006657 if (viewRoot != null) {
6658 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 }
6660 }
6661 return requestFocus(View.FOCUS_DOWN);
6662 }
6663
6664 /**
6665 * @return Whether any ancestor of this view blocks descendant focus.
6666 */
6667 private boolean hasAncestorThatBlocksDescendantFocus() {
6668 ViewParent ancestor = mParent;
6669 while (ancestor instanceof ViewGroup) {
6670 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6671 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6672 return true;
6673 } else {
6674 ancestor = vgAncestor.getParent();
6675 }
6676 }
6677 return false;
6678 }
6679
6680 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006681 * Gets the mode for determining whether this View is important for accessibility
6682 * which is if it fires accessibility events and if it is reported to
6683 * accessibility services that query the screen.
6684 *
6685 * @return The mode for determining whether a View is important for accessibility.
6686 *
6687 * @attr ref android.R.styleable#View_importantForAccessibility
6688 *
6689 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6690 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6691 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6692 */
6693 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
Svetoslav Ganovf9817f72012-05-22 18:10:31 -07006694 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO, to = "auto"),
6695 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES, to = "yes"),
6696 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO, to = "no")
Svetoslav Ganov42138042012-03-20 11:51:39 -07006697 })
6698 public int getImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006699 return (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6700 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006701 }
6702
6703 /**
6704 * Sets how to determine whether this view is important for accessibility
6705 * which is if it fires accessibility events and if it is reported to
6706 * accessibility services that query the screen.
6707 *
6708 * @param mode How to determine whether this view is important for accessibility.
6709 *
6710 * @attr ref android.R.styleable#View_importantForAccessibility
6711 *
6712 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6713 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6714 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6715 */
6716 public void setImportantForAccessibility(int mode) {
6717 if (mode != getImportantForAccessibility()) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006718 mPrivateFlags2 &= ~PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
6719 mPrivateFlags2 |= (mode << PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6720 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006721 notifyAccessibilityStateChanged();
6722 }
6723 }
6724
6725 /**
6726 * Gets whether this view should be exposed for accessibility.
6727 *
6728 * @return Whether the view is exposed for accessibility.
6729 *
6730 * @hide
6731 */
6732 public boolean isImportantForAccessibility() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006733 final int mode = (mPrivateFlags2 & PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_MASK)
6734 >> PFLAG2_IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006735 switch (mode) {
6736 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6737 return true;
6738 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6739 return false;
6740 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
Svetoslav Ganov34caec92012-07-19 18:07:58 -07006741 return isActionableForAccessibility() || hasListenersForAccessibility()
6742 || getAccessibilityNodeProvider() != null;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006743 default:
6744 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6745 + mode);
6746 }
6747 }
6748
6749 /**
6750 * Gets the parent for accessibility purposes. Note that the parent for
6751 * accessibility is not necessary the immediate parent. It is the first
6752 * predecessor that is important for accessibility.
6753 *
6754 * @return The parent for accessibility purposes.
6755 */
6756 public ViewParent getParentForAccessibility() {
6757 if (mParent instanceof View) {
6758 View parentView = (View) mParent;
6759 if (parentView.includeForAccessibility()) {
6760 return mParent;
6761 } else {
6762 return mParent.getParentForAccessibility();
6763 }
6764 }
6765 return null;
6766 }
6767
6768 /**
6769 * Adds the children of a given View for accessibility. Since some Views are
6770 * not important for accessibility the children for accessibility are not
6771 * necessarily direct children of the riew, rather they are the first level of
6772 * descendants important for accessibility.
6773 *
6774 * @param children The list of children for accessibility.
6775 */
6776 public void addChildrenForAccessibility(ArrayList<View> children) {
6777 if (includeForAccessibility()) {
6778 children.add(this);
6779 }
6780 }
6781
6782 /**
6783 * Whether to regard this view for accessibility. A view is regarded for
6784 * accessibility if it is important for accessibility or the querying
6785 * accessibility service has explicitly requested that view not
6786 * important for accessibility are regarded.
6787 *
6788 * @return Whether to regard the view for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006789 *
6790 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006791 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006792 public boolean includeForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006793 if (mAttachInfo != null) {
Romain Guyf0af1d52012-07-11 18:31:21 -07006794 return mAttachInfo.mIncludeNotImportantViews || isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006795 }
6796 return false;
6797 }
6798
6799 /**
6800 * Returns whether the View is considered actionable from
6801 * accessibility perspective. Such view are important for
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006802 * accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006803 *
6804 * @return True if the view is actionable for accessibility.
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006805 *
6806 * @hide
Svetoslav Ganov42138042012-03-20 11:51:39 -07006807 */
Svetoslav Ganove5dfa47d2012-05-08 15:58:32 -07006808 public boolean isActionableForAccessibility() {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006809 return (isClickable() || isLongClickable() || isFocusable());
6810 }
6811
6812 /**
6813 * Returns whether the View has registered callbacks wich makes it
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006814 * important for accessibility.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006815 *
6816 * @return True if the view is actionable for accessibility.
6817 */
6818 private boolean hasListenersForAccessibility() {
6819 ListenerInfo info = getListenerInfo();
6820 return mTouchDelegate != null || info.mOnKeyListener != null
6821 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6822 || info.mOnHoverListener != null || info.mOnDragListener != null;
6823 }
6824
6825 /**
6826 * Notifies accessibility services that some view's important for
6827 * accessibility state has changed. Note that such notifications
6828 * are made at most once every
6829 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6830 * to avoid unnecessary load to the system. Also once a view has
6831 * made a notifucation this method is a NOP until the notification has
6832 * been sent to clients.
6833 *
6834 * @hide
6835 *
6836 * TODO: Makse sure this method is called for any view state change
6837 * that is interesting for accessilility purposes.
6838 */
6839 public void notifyAccessibilityStateChanged() {
Svetoslav Ganovc406be92012-05-11 16:12:32 -07006840 if (!AccessibilityManager.getInstance(mContext).isEnabled()) {
6841 return;
6842 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07006843 if ((mPrivateFlags2 & PFLAG2_ACCESSIBILITY_STATE_CHANGED) == 0) {
6844 mPrivateFlags2 |= PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006845 if (mParent != null) {
6846 mParent.childAccessibilityStateChanged(this);
6847 }
6848 }
6849 }
6850
6851 /**
6852 * Reset the state indicating the this view has requested clients
Fabrice Di Meglio69542e42012-09-18 19:34:34 -07006853 * interested in its accessibility state to be notified.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006854 *
6855 * @hide
6856 */
6857 public void resetAccessibilityStateChanged() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07006858 mPrivateFlags2 &= ~PFLAG2_ACCESSIBILITY_STATE_CHANGED;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006859 }
6860
6861 /**
6862 * Performs the specified accessibility action on the view. For
6863 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006864 * <p>
6865 * If an {@link AccessibilityDelegate} has been specified via calling
6866 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
6867 * {@link AccessibilityDelegate#performAccessibilityAction(View, int, Bundle)}
6868 * is responsible for handling this call.
6869 * </p>
Svetoslav Ganov42138042012-03-20 11:51:39 -07006870 *
6871 * @param action The action to perform.
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006872 * @param arguments Optional action arguments.
Svetoslav Ganov42138042012-03-20 11:51:39 -07006873 * @return Whether the action was performed.
6874 */
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006875 public boolean performAccessibilityAction(int action, Bundle arguments) {
alanv8eeefef2012-05-07 16:57:53 -07006876 if (mAccessibilityDelegate != null) {
6877 return mAccessibilityDelegate.performAccessibilityAction(this, action, arguments);
6878 } else {
6879 return performAccessibilityActionInternal(action, arguments);
6880 }
6881 }
6882
6883 /**
6884 * @see #performAccessibilityAction(int, Bundle)
6885 *
6886 * Note: Called from the default {@link AccessibilityDelegate}.
6887 */
6888 boolean performAccessibilityActionInternal(int action, Bundle arguments) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006889 switch (action) {
6890 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006891 if (isClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006892 performClick();
6893 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006894 }
6895 } break;
6896 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6897 if (isLongClickable()) {
Svetoslav Ganov96985fc2012-10-09 16:25:54 -07006898 performLongClick();
6899 return true;
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006900 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006901 } break;
6902 case AccessibilityNodeInfo.ACTION_FOCUS: {
6903 if (!hasFocus()) {
6904 // Get out of touch mode since accessibility
6905 // wants to move focus around.
6906 getViewRootImpl().ensureTouchMode(false);
6907 return requestFocus();
6908 }
6909 } break;
6910 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6911 if (hasFocus()) {
6912 clearFocus();
6913 return !isFocused();
6914 }
6915 } break;
6916 case AccessibilityNodeInfo.ACTION_SELECT: {
6917 if (!isSelected()) {
6918 setSelected(true);
6919 return isSelected();
6920 }
6921 } break;
6922 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6923 if (isSelected()) {
6924 setSelected(false);
6925 return !isSelected();
6926 }
6927 } break;
6928 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
Svetoslav Ganov27e2da72012-07-02 18:12:00 -07006929 if (!isAccessibilityFocused()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006930 return requestAccessibilityFocus();
6931 }
6932 } break;
6933 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6934 if (isAccessibilityFocused()) {
6935 clearAccessibilityFocus();
6936 return true;
6937 }
6938 } break;
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006939 case AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY: {
6940 if (arguments != null) {
6941 final int granularity = arguments.getInt(
6942 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6943 return nextAtGranularity(granularity);
6944 }
6945 } break;
6946 case AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY: {
6947 if (arguments != null) {
6948 final int granularity = arguments.getInt(
6949 AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
6950 return previousAtGranularity(granularity);
6951 }
6952 } break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006953 }
6954 return false;
6955 }
6956
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006957 private boolean nextAtGranularity(int granularity) {
6958 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006959 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006960 return false;
6961 }
6962 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6963 if (iterator == null) {
6964 return false;
6965 }
6966 final int current = getAccessibilityCursorPosition();
6967 final int[] range = iterator.following(current);
6968 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006969 return false;
6970 }
6971 final int start = range[0];
6972 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006973 setAccessibilityCursorPosition(end);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006974 sendViewTextTraversedAtGranularityEvent(
6975 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY,
6976 granularity, start, end);
6977 return true;
6978 }
6979
6980 private boolean previousAtGranularity(int granularity) {
6981 CharSequence text = getIterableTextForAccessibility();
Svetoslav Ganov64899e52012-05-15 21:09:30 -07006982 if (text == null || text.length() == 0) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006983 return false;
6984 }
6985 TextSegmentIterator iterator = getIteratorForGranularity(granularity);
6986 if (iterator == null) {
6987 return false;
6988 }
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07006989 int current = getAccessibilityCursorPosition();
6990 if (current == ACCESSIBILITY_CURSOR_POSITION_UNDEFINED) {
6991 current = text.length();
6992 } else if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
6993 // When traversing by character we always put the cursor after the character
6994 // to ease edit and have to compensate before asking the for previous segment.
6995 current--;
6996 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006997 final int[] range = iterator.preceding(current);
6998 if (range == null) {
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07006999 return false;
7000 }
7001 final int start = range[0];
7002 final int end = range[1];
Svetoslav Ganov39f2aee2012-05-29 09:15:30 -07007003 // Always put the cursor after the character to ease edit.
7004 if (granularity == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER) {
7005 setAccessibilityCursorPosition(end);
7006 } else {
7007 setAccessibilityCursorPosition(start);
7008 }
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007009 sendViewTextTraversedAtGranularityEvent(
7010 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
7011 granularity, start, end);
7012 return true;
7013 }
7014
7015 /**
7016 * Gets the text reported for accessibility purposes.
7017 *
7018 * @return The accessibility text.
7019 *
7020 * @hide
7021 */
7022 public CharSequence getIterableTextForAccessibility() {
Svetoslav Ganov05282aa2012-09-06 18:59:29 -07007023 return getContentDescription();
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007024 }
7025
7026 /**
7027 * @hide
7028 */
7029 public int getAccessibilityCursorPosition() {
7030 return mAccessibilityCursorPosition;
7031 }
7032
7033 /**
7034 * @hide
7035 */
7036 public void setAccessibilityCursorPosition(int position) {
7037 mAccessibilityCursorPosition = position;
7038 }
7039
7040 private void sendViewTextTraversedAtGranularityEvent(int action, int granularity,
7041 int fromIndex, int toIndex) {
7042 if (mParent == null) {
7043 return;
7044 }
7045 AccessibilityEvent event = AccessibilityEvent.obtain(
7046 AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY);
7047 onInitializeAccessibilityEvent(event);
7048 onPopulateAccessibilityEvent(event);
7049 event.setFromIndex(fromIndex);
7050 event.setToIndex(toIndex);
7051 event.setAction(action);
7052 event.setMovementGranularity(granularity);
7053 mParent.requestSendAccessibilityEvent(this, event);
7054 }
7055
7056 /**
7057 * @hide
7058 */
7059 public TextSegmentIterator getIteratorForGranularity(int granularity) {
7060 switch (granularity) {
7061 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER: {
7062 CharSequence text = getIterableTextForAccessibility();
7063 if (text != null && text.length() > 0) {
7064 CharacterTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007065 CharacterTextSegmentIterator.getInstance(
7066 mContext.getResources().getConfiguration().locale);
Svetoslav Ganov6d17a932012-04-27 19:30:38 -07007067 iterator.initialize(text.toString());
7068 return iterator;
7069 }
7070 } break;
7071 case AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD: {
7072 CharSequence text = getIterableTextForAccessibility();
7073 if (text != null && text.length() > 0) {
7074 WordTextSegmentIterator iterator =
Svetoslav Ganovbbd31552012-06-11 12:08:18 -07007075 WordTextSegmentIterator.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_PARAGRAPH: {
7082 CharSequence text = getIterableTextForAccessibility();
7083 if (text != null && text.length() > 0) {
7084 ParagraphTextSegmentIterator iterator =
7085 ParagraphTextSegmentIterator.getInstance();
7086 iterator.initialize(text.toString());
7087 return iterator;
7088 }
7089 } break;
7090 }
7091 return null;
7092 }
7093
Svetoslav Ganov42138042012-03-20 11:51:39 -07007094 /**
Romain Guya440b002010-02-24 15:57:54 -08007095 * @hide
7096 */
7097 public void dispatchStartTemporaryDetach() {
Svetoslav Ganov961bf0e2012-05-08 09:40:03 -07007098 clearAccessibilityFocus();
Romain Guy38c2ece2012-05-24 14:20:56 -07007099 clearDisplayList();
7100
Romain Guya440b002010-02-24 15:57:54 -08007101 onStartTemporaryDetach();
7102 }
7103
7104 /**
7105 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
7107 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08007108 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007109 */
7110 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08007111 removeUnsetPressCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007112 mPrivateFlags |= PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08007113 }
7114
7115 /**
7116 * @hide
7117 */
7118 public void dispatchFinishTemporaryDetach() {
7119 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 }
Romain Guy8506ab42009-06-11 17:35:47 -07007121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 /**
7123 * Called after {@link #onStartTemporaryDetach} when the container is done
7124 * changing the view.
7125 */
7126 public void onFinishTemporaryDetach() {
7127 }
Romain Guy8506ab42009-06-11 17:35:47 -07007128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007129 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007130 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
7131 * for this view's window. Returns null if the view is not currently attached
7132 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07007133 * just use the standard high-level event callbacks like
7134 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007135 */
7136 public KeyEvent.DispatcherState getKeyDispatcherState() {
7137 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
7138 }
Joe Malin32736f02011-01-19 16:14:20 -08007139
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 * Dispatch a key event before it is processed by any input method
7142 * associated with the view hierarchy. This can be used to intercept
7143 * key events in special situations before the IME consumes them; a
7144 * typical example would be handling the BACK key to update the application's
7145 * UI instead of allowing the IME to see it and close itself.
7146 *
7147 * @param event The key event to be dispatched.
7148 * @return True if the event was handled, false otherwise.
7149 */
7150 public boolean dispatchKeyEventPreIme(KeyEvent event) {
7151 return onKeyPreIme(event.getKeyCode(), event);
7152 }
7153
7154 /**
7155 * Dispatch a key event to the next view on the focus path. This path runs
7156 * from the top of the view tree down to the currently focused view. If this
7157 * view has focus, it will dispatch to itself. Otherwise it will dispatch
7158 * the next node down the focus path. This method also fires any key
7159 * listeners.
7160 *
7161 * @param event The key event to be dispatched.
7162 * @return True if the event was handled, false otherwise.
7163 */
7164 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007165 if (mInputEventConsistencyVerifier != null) {
7166 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
7167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168
Jeff Brown21bc5c92011-02-28 18:27:14 -08007169 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07007170 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007171 ListenerInfo li = mListenerInfo;
7172 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7173 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 return true;
7175 }
7176
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007177 if (event.dispatch(this, mAttachInfo != null
7178 ? mAttachInfo.mKeyDispatchState : null, this)) {
7179 return true;
7180 }
7181
7182 if (mInputEventConsistencyVerifier != null) {
7183 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7184 }
7185 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 }
7187
7188 /**
7189 * Dispatches a key shortcut event.
7190 *
7191 * @param event The key event to be dispatched.
7192 * @return True if the event was handled by the view, false otherwise.
7193 */
7194 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
7195 return onKeyShortcut(event.getKeyCode(), event);
7196 }
7197
7198 /**
7199 * Pass the touch screen motion event down to the target view, or this
7200 * view if it is the target.
7201 *
7202 * @param event The motion event to be dispatched.
7203 * @return True if the event was handled by the view, false otherwise.
7204 */
7205 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007206 if (mInputEventConsistencyVerifier != null) {
7207 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
7208 }
7209
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007210 if (onFilterTouchEventForSecurity(event)) {
7211 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007212 ListenerInfo li = mListenerInfo;
7213 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
7214 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007215 return true;
7216 }
7217
7218 if (onTouchEvent(event)) {
7219 return true;
7220 }
Jeff Brown85a31762010-09-01 17:01:00 -07007221 }
7222
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007223 if (mInputEventConsistencyVerifier != null) {
7224 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007226 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 }
7228
7229 /**
Jeff Brown85a31762010-09-01 17:01:00 -07007230 * Filter the touch event to apply security policies.
7231 *
7232 * @param event The motion event to be filtered.
7233 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08007234 *
Jeff Brown85a31762010-09-01 17:01:00 -07007235 * @see #getFilterTouchesWhenObscured
7236 */
7237 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07007238 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07007239 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
7240 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
7241 // Window is obscured, drop this touch.
7242 return false;
7243 }
7244 return true;
7245 }
7246
7247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 * Pass a trackball motion event down to the focused view.
7249 *
7250 * @param event The motion event to be dispatched.
7251 * @return True if the event was handled by the view, false otherwise.
7252 */
7253 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007254 if (mInputEventConsistencyVerifier != null) {
7255 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
7256 }
7257
Romain Guy02ccac62011-06-24 13:20:23 -07007258 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 }
7260
7261 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007262 * Dispatch a generic motion event.
7263 * <p>
7264 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7265 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08007266 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07007267 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007268 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08007269 *
7270 * @param event The motion event to be dispatched.
7271 * @return True if the event was handled by the view, false otherwise.
7272 */
7273 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08007274 if (mInputEventConsistencyVerifier != null) {
7275 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
7276 }
7277
Jeff Browna032cc02011-03-07 16:56:21 -08007278 final int source = event.getSource();
7279 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
7280 final int action = event.getAction();
7281 if (action == MotionEvent.ACTION_HOVER_ENTER
7282 || action == MotionEvent.ACTION_HOVER_MOVE
7283 || action == MotionEvent.ACTION_HOVER_EXIT) {
7284 if (dispatchHoverEvent(event)) {
7285 return true;
7286 }
7287 } else if (dispatchGenericPointerEvent(event)) {
7288 return true;
7289 }
7290 } else if (dispatchGenericFocusedEvent(event)) {
7291 return true;
7292 }
7293
Jeff Brown10b62902011-06-20 16:40:37 -07007294 if (dispatchGenericMotionEventInternal(event)) {
7295 return true;
7296 }
7297
7298 if (mInputEventConsistencyVerifier != null) {
7299 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7300 }
7301 return false;
7302 }
7303
7304 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07007305 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007306 ListenerInfo li = mListenerInfo;
7307 if (li != null && li.mOnGenericMotionListener != null
7308 && (mViewFlags & ENABLED_MASK) == ENABLED
7309 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007310 return true;
7311 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07007312
7313 if (onGenericMotionEvent(event)) {
7314 return true;
7315 }
7316
7317 if (mInputEventConsistencyVerifier != null) {
7318 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
7319 }
7320 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08007321 }
7322
7323 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007324 * Dispatch a hover event.
7325 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07007326 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07007327 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007328 * </p>
7329 *
7330 * @param event The motion event to be dispatched.
7331 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007332 */
7333 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07007334 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007335 ListenerInfo li = mListenerInfo;
7336 if (li != null && li.mOnHoverListener != null
7337 && (mViewFlags & ENABLED_MASK) == ENABLED
7338 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07007339 return true;
7340 }
7341
Jeff Browna032cc02011-03-07 16:56:21 -08007342 return onHoverEvent(event);
7343 }
7344
7345 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007346 * Returns true if the view has a child to which it has recently sent
7347 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
7348 * it does not have a hovered child, then it must be the innermost hovered view.
7349 * @hide
7350 */
7351 protected boolean hasHoveredChild() {
7352 return false;
7353 }
7354
7355 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007356 * Dispatch a generic motion event to the view under the first pointer.
7357 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007358 * Do not call this method directly.
7359 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007360 * </p>
7361 *
7362 * @param event The motion event to be dispatched.
7363 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007364 */
7365 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
7366 return false;
7367 }
7368
7369 /**
7370 * Dispatch a generic motion event to the currently focused view.
7371 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007372 * Do not call this method directly.
7373 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08007374 * </p>
7375 *
7376 * @param event The motion event to be dispatched.
7377 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08007378 */
7379 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
7380 return false;
7381 }
7382
7383 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08007384 * Dispatch a pointer event.
7385 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07007386 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
7387 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
7388 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08007389 * and should not be expected to handle other pointing device features.
7390 * </p>
7391 *
7392 * @param event The motion event to be dispatched.
7393 * @return True if the event was handled by the view, false otherwise.
7394 * @hide
7395 */
7396 public final boolean dispatchPointerEvent(MotionEvent event) {
7397 if (event.isTouchEvent()) {
7398 return dispatchTouchEvent(event);
7399 } else {
7400 return dispatchGenericMotionEvent(event);
7401 }
7402 }
7403
7404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 * Called when the window containing this view gains or loses window focus.
7406 * ViewGroups should override to route to their children.
7407 *
7408 * @param hasFocus True if the window containing this view now has focus,
7409 * false otherwise.
7410 */
7411 public void dispatchWindowFocusChanged(boolean hasFocus) {
7412 onWindowFocusChanged(hasFocus);
7413 }
7414
7415 /**
7416 * Called when the window containing this view gains or loses focus. Note
7417 * that this is separate from view focus: to receive key events, both
7418 * your view and its window must have focus. If a window is displayed
7419 * on top of yours that takes input focus, then your own window will lose
7420 * focus but the view focus will remain unchanged.
7421 *
7422 * @param hasWindowFocus True if the window containing this view now has
7423 * focus, false otherwise.
7424 */
7425 public void onWindowFocusChanged(boolean hasWindowFocus) {
7426 InputMethodManager imm = InputMethodManager.peekInstance();
7427 if (!hasWindowFocus) {
7428 if (isPressed()) {
7429 setPressed(false);
7430 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07007431 if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 imm.focusOut(this);
7433 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007434 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08007435 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07007436 onFocusLost();
Dianne Hackborn4702a852012-08-17 15:18:29 -07007437 } else if (imm != null && (mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 imm.focusIn(this);
7439 }
7440 refreshDrawableState();
7441 }
7442
7443 /**
7444 * Returns true if this view is in a window that currently has window focus.
7445 * Note that this is not the same as the view itself having focus.
7446 *
7447 * @return True if this view is in a window that currently has window focus.
7448 */
7449 public boolean hasWindowFocus() {
7450 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
7451 }
7452
7453 /**
Adam Powell326d8082009-12-09 15:10:07 -08007454 * Dispatch a view visibility change down the view hierarchy.
7455 * ViewGroups should override to route to their children.
7456 * @param changedView The view whose visibility changed. Could be 'this' or
7457 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007458 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7459 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007460 */
7461 protected void dispatchVisibilityChanged(View changedView, int visibility) {
7462 onVisibilityChanged(changedView, visibility);
7463 }
7464
7465 /**
7466 * Called when the visibility of the view or an ancestor of the view is changed.
7467 * @param changedView The view whose visibility changed. Could be 'this' or
7468 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08007469 * @param visibility The new visibility of changedView: {@link #VISIBLE},
7470 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08007471 */
7472 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007473 if (visibility == VISIBLE) {
7474 if (mAttachInfo != null) {
7475 initialAwakenScrollBars();
7476 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07007477 mPrivateFlags |= PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -07007478 }
7479 }
Adam Powell326d8082009-12-09 15:10:07 -08007480 }
7481
7482 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08007483 * Dispatch a hint about whether this view is displayed. For instance, when
7484 * a View moves out of the screen, it might receives a display hint indicating
7485 * the view is not displayed. Applications should not <em>rely</em> on this hint
7486 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007487 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007488 * @param hint A hint about whether or not this view is displayed:
7489 * {@link #VISIBLE} or {@link #INVISIBLE}.
7490 */
7491 public void dispatchDisplayHint(int hint) {
7492 onDisplayHint(hint);
7493 }
7494
7495 /**
7496 * Gives this view a hint about whether is displayed or not. For instance, when
7497 * a View moves out of the screen, it might receives a display hint indicating
7498 * the view is not displayed. Applications should not <em>rely</em> on this hint
7499 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08007500 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08007501 * @param hint A hint about whether or not this view is displayed:
7502 * {@link #VISIBLE} or {@link #INVISIBLE}.
7503 */
7504 protected void onDisplayHint(int hint) {
7505 }
7506
7507 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 * Dispatch a window visibility change down the view hierarchy.
7509 * ViewGroups should override to route to their children.
7510 *
7511 * @param visibility The new visibility of the window.
7512 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007513 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 */
7515 public void dispatchWindowVisibilityChanged(int visibility) {
7516 onWindowVisibilityChanged(visibility);
7517 }
7518
7519 /**
7520 * Called when the window containing has change its visibility
7521 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
7522 * that this tells you whether or not your window is being made visible
7523 * to the window manager; this does <em>not</em> tell you whether or not
7524 * your window is obscured by other windows on the screen, even if it
7525 * is itself visible.
7526 *
7527 * @param visibility The new visibility of the window.
7528 */
7529 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07007530 if (visibility == VISIBLE) {
7531 initialAwakenScrollBars();
7532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007533 }
7534
7535 /**
7536 * Returns the current visibility of the window this view is attached to
7537 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
7538 *
7539 * @return Returns the current visibility of the view's window.
7540 */
7541 public int getWindowVisibility() {
7542 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
7543 }
7544
7545 /**
7546 * Retrieve the overall visible display size in which the window this view is
7547 * attached to has been positioned in. This takes into account screen
7548 * decorations above the window, for both cases where the window itself
7549 * is being position inside of them or the window is being placed under
7550 * then and covered insets are used for the window to position its content
7551 * inside. In effect, this tells you the available area where content can
7552 * be placed and remain visible to users.
7553 *
7554 * <p>This function requires an IPC back to the window manager to retrieve
7555 * the requested information, so should not be used in performance critical
7556 * code like drawing.
7557 *
7558 * @param outRect Filled in with the visible display frame. If the view
7559 * is not attached to a window, this is simply the raw display size.
7560 */
7561 public void getWindowVisibleDisplayFrame(Rect outRect) {
7562 if (mAttachInfo != null) {
7563 try {
7564 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7565 } catch (RemoteException e) {
7566 return;
7567 }
7568 // XXX This is really broken, and probably all needs to be done
7569 // in the window manager, and we need to know more about whether
7570 // we want the area behind or in front of the IME.
7571 final Rect insets = mAttachInfo.mVisibleInsets;
7572 outRect.left += insets.left;
7573 outRect.top += insets.top;
7574 outRect.right -= insets.right;
7575 outRect.bottom -= insets.bottom;
7576 return;
7577 }
Jeff Brownbd6e1502012-08-28 03:27:37 -07007578 // The view is not attached to a display so we don't have a context.
7579 // Make a best guess about the display size.
7580 Display d = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007581 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007582 }
7583
7584 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007585 * Dispatch a notification about a resource configuration change down
7586 * the view hierarchy.
7587 * ViewGroups should override to route to their children.
7588 *
7589 * @param newConfig The new resource configuration.
7590 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007591 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007592 */
7593 public void dispatchConfigurationChanged(Configuration newConfig) {
7594 onConfigurationChanged(newConfig);
7595 }
7596
7597 /**
7598 * Called when the current configuration of the resources being used
7599 * by the application have changed. You can use this to decide when
7600 * to reload resources that can changed based on orientation and other
7601 * configuration characterstics. You only need to use this if you are
7602 * not relying on the normal {@link android.app.Activity} mechanism of
7603 * recreating the activity instance upon a configuration change.
7604 *
7605 * @param newConfig The new resource configuration.
7606 */
7607 protected void onConfigurationChanged(Configuration newConfig) {
7608 }
7609
7610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007611 * Private function to aggregate all per-view attributes in to the view
7612 * root.
7613 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007614 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7615 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 }
7617
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007618 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7619 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007620 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007621 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007622 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007623 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007624 ListenerInfo li = mListenerInfo;
7625 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007626 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007628 }
7629 }
7630
7631 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007632 final AttachInfo ai = mAttachInfo;
Craig Mautner7eac0f52012-09-13 13:14:14 -07007633 if (ai != null && !ai.mRecomputeGlobalAttributes) {
Joe Onorato664644d2011-01-23 17:53:23 -08007634 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7635 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 ai.mRecomputeGlobalAttributes = true;
7637 }
7638 }
7639 }
7640
7641 /**
7642 * Returns whether the device is currently in touch mode. Touch mode is entered
7643 * once the user begins interacting with the device by touch, and affects various
7644 * things like whether focus is always visible to the user.
7645 *
7646 * @return Whether the device is in touch mode.
7647 */
7648 @ViewDebug.ExportedProperty
7649 public boolean isInTouchMode() {
7650 if (mAttachInfo != null) {
7651 return mAttachInfo.mInTouchMode;
7652 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007653 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 }
7655 }
7656
7657 /**
7658 * Returns the context the view is running in, through which it can
7659 * access the current theme, resources, etc.
7660 *
7661 * @return The view's Context.
7662 */
7663 @ViewDebug.CapturedViewProperty
7664 public final Context getContext() {
7665 return mContext;
7666 }
7667
7668 /**
7669 * Handle a key event before it is processed by any input method
7670 * associated with the view hierarchy. This can be used to intercept
7671 * key events in special situations before the IME consumes them; a
7672 * typical example would be handling the BACK key to update the application's
7673 * UI instead of allowing the IME to see it and close itself.
7674 *
7675 * @param keyCode The value in event.getKeyCode().
7676 * @param event Description of the key event.
7677 * @return If you handled the event, return true. If you want to allow the
7678 * event to be handled by the next receiver, return false.
7679 */
7680 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7681 return false;
7682 }
7683
7684 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007685 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7686 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7688 * is released, if the view is enabled and clickable.
7689 *
Jean Chalard405bc512012-05-29 19:12:34 +09007690 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7691 * although some may elect to do so in some situations. Do not rely on this to
7692 * catch software key presses.
7693 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 * @param keyCode A key code that represents the button pressed, from
7695 * {@link android.view.KeyEvent}.
7696 * @param event The KeyEvent object that defines the button action.
7697 */
7698 public boolean onKeyDown(int keyCode, KeyEvent event) {
7699 boolean result = false;
7700
7701 switch (keyCode) {
7702 case KeyEvent.KEYCODE_DPAD_CENTER:
7703 case KeyEvent.KEYCODE_ENTER: {
7704 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7705 return true;
7706 }
7707 // Long clickable items don't necessarily have to be clickable
7708 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7709 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7710 (event.getRepeatCount() == 0)) {
7711 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007712 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 return true;
7714 }
7715 break;
7716 }
7717 }
7718 return result;
7719 }
7720
7721 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007722 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7723 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7724 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007725 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7726 * although some may elect to do so in some situations. Do not rely on this to
7727 * catch software key presses.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007728 */
7729 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7730 return false;
7731 }
7732
7733 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007734 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7735 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7737 * {@link KeyEvent#KEYCODE_ENTER} is released.
Jean Chalard405bc512012-05-29 19:12:34 +09007738 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7739 * although some may elect to do so in some situations. Do not rely on this to
7740 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 *
7742 * @param keyCode A key code that represents the button pressed, from
7743 * {@link android.view.KeyEvent}.
7744 * @param event The KeyEvent object that defines the button action.
7745 */
7746 public boolean onKeyUp(int keyCode, KeyEvent event) {
7747 boolean result = false;
7748
7749 switch (keyCode) {
7750 case KeyEvent.KEYCODE_DPAD_CENTER:
7751 case KeyEvent.KEYCODE_ENTER: {
7752 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7753 return true;
7754 }
7755 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7756 setPressed(false);
7757
7758 if (!mHasPerformedLongPress) {
7759 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007760 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761
7762 result = performClick();
7763 }
7764 }
7765 break;
7766 }
7767 }
7768 return result;
7769 }
7770
7771 /**
7772 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7773 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7774 * the event).
Jean Chalard405bc512012-05-29 19:12:34 +09007775 * <p>Key presses in software keyboards will generally NOT trigger this listener,
7776 * although some may elect to do so in some situations. Do not rely on this to
7777 * catch software key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 *
7779 * @param keyCode A key code that represents the button pressed, from
7780 * {@link android.view.KeyEvent}.
7781 * @param repeatCount The number of times the action was made.
7782 * @param event The KeyEvent object that defines the button action.
7783 */
7784 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7785 return false;
7786 }
7787
7788 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007789 * Called on the focused view when a key shortcut event is not handled.
7790 * Override this method to implement local key shortcuts for the View.
7791 * Key shortcuts can also be implemented by setting the
7792 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 *
7794 * @param keyCode The value in event.getKeyCode().
7795 * @param event Description of the key event.
7796 * @return If you handled the event, return true. If you want to allow the
7797 * event to be handled by the next receiver, return false.
7798 */
7799 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7800 return false;
7801 }
7802
7803 /**
7804 * Check whether the called view is a text editor, in which case it
7805 * would make sense to automatically display a soft input window for
7806 * it. Subclasses should override this if they implement
7807 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007808 * a call on that method would return a non-null InputConnection, and
7809 * they are really a first-class editor that the user would normally
7810 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007811 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007812 * <p>The default implementation always returns false. This does
7813 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7814 * will not be called or the user can not otherwise perform edits on your
7815 * view; it is just a hint to the system that this is not the primary
7816 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007817 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 * @return Returns true if this view is a text editor, else false.
7819 */
7820 public boolean onCheckIsTextEditor() {
7821 return false;
7822 }
Romain Guy8506ab42009-06-11 17:35:47 -07007823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 /**
7825 * Create a new InputConnection for an InputMethod to interact
7826 * with the view. The default implementation returns null, since it doesn't
7827 * support input methods. You can override this to implement such support.
7828 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007829 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 * <p>When implementing this, you probably also want to implement
7831 * {@link #onCheckIsTextEditor()} to indicate you will return a
7832 * non-null InputConnection.
7833 *
7834 * @param outAttrs Fill in with attribute information about the connection.
7835 */
7836 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7837 return null;
7838 }
7839
7840 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007841 * Called by the {@link android.view.inputmethod.InputMethodManager}
7842 * when a view who is not the current
7843 * input connection target is trying to make a call on the manager. The
7844 * default implementation returns false; you can override this to return
7845 * true for certain views if you are performing InputConnection proxying
7846 * to them.
7847 * @param view The View that is making the InputMethodManager call.
7848 * @return Return true to allow the call, false to reject.
7849 */
7850 public boolean checkInputConnectionProxy(View view) {
7851 return false;
7852 }
Romain Guy8506ab42009-06-11 17:35:47 -07007853
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007855 * Show the context menu for this view. It is not safe to hold on to the
7856 * menu after returning from this method.
7857 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007858 * You should normally not overload this method. Overload
7859 * {@link #onCreateContextMenu(ContextMenu)} or define an
7860 * {@link OnCreateContextMenuListener} to add items to the context menu.
7861 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 * @param menu The context menu to populate
7863 */
7864 public void createContextMenu(ContextMenu menu) {
7865 ContextMenuInfo menuInfo = getContextMenuInfo();
7866
7867 // Sets the current menu info so all items added to menu will have
7868 // my extra info set.
7869 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7870
7871 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007872 ListenerInfo li = mListenerInfo;
7873 if (li != null && li.mOnCreateContextMenuListener != null) {
7874 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007875 }
7876
7877 // Clear the extra information so subsequent items that aren't mine don't
7878 // have my extra info.
7879 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7880
7881 if (mParent != null) {
7882 mParent.createContextMenu(menu);
7883 }
7884 }
7885
7886 /**
7887 * Views should implement this if they have extra information to associate
7888 * with the context menu. The return result is supplied as a parameter to
7889 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7890 * callback.
7891 *
7892 * @return Extra information about the item for which the context menu
7893 * should be shown. This information will vary across different
7894 * subclasses of View.
7895 */
7896 protected ContextMenuInfo getContextMenuInfo() {
7897 return null;
7898 }
7899
7900 /**
7901 * Views should implement this if the view itself is going to add items to
7902 * the context menu.
7903 *
7904 * @param menu the context menu to populate
7905 */
7906 protected void onCreateContextMenu(ContextMenu menu) {
7907 }
7908
7909 /**
7910 * Implement this method to handle trackball motion events. The
7911 * <em>relative</em> movement of the trackball since the last event
7912 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7913 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7914 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7915 * they will often be fractional values, representing the more fine-grained
7916 * movement information available from a trackball).
7917 *
7918 * @param event The motion event.
7919 * @return True if the event was handled, false otherwise.
7920 */
7921 public boolean onTrackballEvent(MotionEvent event) {
7922 return false;
7923 }
7924
7925 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007926 * Implement this method to handle generic motion events.
7927 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007928 * Generic motion events describe joystick movements, mouse hovers, track pad
7929 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007930 * {@link MotionEvent#getSource() source} of the motion event specifies
7931 * the class of input that was received. Implementations of this method
7932 * must examine the bits in the source before processing the event.
7933 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007934 * </p><p>
7935 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7936 * are delivered to the view under the pointer. All other generic motion events are
7937 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007938 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007939 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007940 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007941 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7942 * // process the joystick movement...
7943 * return true;
7944 * }
7945 * }
7946 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7947 * switch (event.getAction()) {
7948 * case MotionEvent.ACTION_HOVER_MOVE:
7949 * // process the mouse hover movement...
7950 * return true;
7951 * case MotionEvent.ACTION_SCROLL:
7952 * // process the scroll wheel movement...
7953 * return true;
7954 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007955 * }
7956 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007957 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007958 *
7959 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007960 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007961 */
7962 public boolean onGenericMotionEvent(MotionEvent event) {
7963 return false;
7964 }
7965
7966 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007967 * Implement this method to handle hover events.
7968 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007969 * This method is called whenever a pointer is hovering into, over, or out of the
7970 * bounds of a view and the view is not currently being touched.
7971 * Hover events are represented as pointer events with action
7972 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7973 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7974 * </p>
7975 * <ul>
7976 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7977 * when the pointer enters the bounds of the view.</li>
7978 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7979 * when the pointer has already entered the bounds of the view and has moved.</li>
7980 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7981 * when the pointer has exited the bounds of the view or when the pointer is
7982 * about to go down due to a button click, tap, or similar user action that
7983 * causes the view to be touched.</li>
7984 * </ul>
7985 * <p>
7986 * The view should implement this method to return true to indicate that it is
7987 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007988 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007989 * The default implementation calls {@link #setHovered} to update the hovered state
7990 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007991 * is enabled and is clickable. The default implementation also sends hover
7992 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007993 * </p>
7994 *
7995 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007996 * @return True if the view handled the hover event.
7997 *
7998 * @see #isHovered
7999 * @see #setHovered
8000 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008001 */
8002 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008003 // The root view may receive hover (or touch) events that are outside the bounds of
8004 // the window. This code ensures that we only send accessibility events for
8005 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07008006 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008007 if (!mSendingHoverAccessibilityEvents) {
8008 if ((action == MotionEvent.ACTION_HOVER_ENTER
8009 || action == MotionEvent.ACTION_HOVER_MOVE)
8010 && !hasHoveredChild()
8011 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008012 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008013 mSendingHoverAccessibilityEvents = true;
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008014 }
8015 } else {
8016 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07008017 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008018 && !pointInView(event.getX(), event.getY()))) {
8019 mSendingHoverAccessibilityEvents = false;
Svetoslav Ganov8ffe8b32012-06-15 10:31:31 -07008020 sendAccessibilityHoverEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008021 // If the window does not have input focus we take away accessibility
8022 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07008023 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov45a02e02012-06-17 15:07:29 -07008024 getViewRootImpl().setAccessibilityFocus(null, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07008025 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008026 }
Jeff Browna1b24182011-07-28 13:38:24 -07008027 }
8028
Jeff Brown87b7f802011-06-21 18:35:45 -07008029 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07008030 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07008031 case MotionEvent.ACTION_HOVER_ENTER:
8032 setHovered(true);
8033 break;
8034 case MotionEvent.ACTION_HOVER_EXIT:
8035 setHovered(false);
8036 break;
8037 }
Jeff Browna1b24182011-07-28 13:38:24 -07008038
8039 // Dispatch the event to onGenericMotionEvent before returning true.
8040 // This is to provide compatibility with existing applications that
8041 // handled HOVER_MOVE events in onGenericMotionEvent and that would
8042 // break because of the new default handling for hoverable views
8043 // in onHoverEvent.
8044 // Note that onGenericMotionEvent will be called by default when
8045 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
8046 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07008047 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08008048 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008049
Svetoslav Ganov736c2752011-04-22 18:30:36 -07008050 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08008051 }
8052
8053 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07008054 * Returns true if the view should handle {@link #onHoverEvent}
8055 * by calling {@link #setHovered} to change its hovered state.
8056 *
8057 * @return True if the view is hoverable.
8058 */
8059 private boolean isHoverable() {
8060 final int viewFlags = mViewFlags;
8061 if ((viewFlags & ENABLED_MASK) == DISABLED) {
8062 return false;
8063 }
8064
8065 return (viewFlags & CLICKABLE) == CLICKABLE
8066 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
8067 }
8068
8069 /**
Jeff Browna032cc02011-03-07 16:56:21 -08008070 * Returns true if the view is currently hovered.
8071 *
8072 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008073 *
8074 * @see #setHovered
8075 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008076 */
Jeff Brown10b62902011-06-20 16:40:37 -07008077 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08008078 public boolean isHovered() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008079 return (mPrivateFlags & PFLAG_HOVERED) != 0;
Jeff Browna032cc02011-03-07 16:56:21 -08008080 }
8081
8082 /**
8083 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008084 * <p>
8085 * Calling this method also changes the drawable state of the view. This
8086 * enables the view to react to hover by using different drawable resources
8087 * to change its appearance.
8088 * </p><p>
8089 * The {@link #onHoverChanged} method is called when the hovered state changes.
8090 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08008091 *
8092 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07008093 *
8094 * @see #isHovered
8095 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08008096 */
8097 public void setHovered(boolean hovered) {
8098 if (hovered) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008099 if ((mPrivateFlags & PFLAG_HOVERED) == 0) {
8100 mPrivateFlags |= PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008101 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008102 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08008103 }
8104 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008105 if ((mPrivateFlags & PFLAG_HOVERED) != 0) {
8106 mPrivateFlags &= ~PFLAG_HOVERED;
Jeff Browna032cc02011-03-07 16:56:21 -08008107 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07008108 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08008109 }
8110 }
8111 }
8112
8113 /**
Jeff Brown10b62902011-06-20 16:40:37 -07008114 * Implement this method to handle hover state changes.
8115 * <p>
8116 * This method is called whenever the hover state changes as a result of a
8117 * call to {@link #setHovered}.
8118 * </p>
8119 *
8120 * @param hovered The current hover state, as returned by {@link #isHovered}.
8121 *
8122 * @see #isHovered
8123 * @see #setHovered
8124 */
8125 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07008126 }
8127
8128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 * Implement this method to handle touch screen motion events.
8130 *
8131 * @param event The motion event.
8132 * @return True if the event was handled, false otherwise.
8133 */
8134 public boolean onTouchEvent(MotionEvent event) {
8135 final int viewFlags = mViewFlags;
8136
8137 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008138 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08008139 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07008140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 // A disabled view that is clickable still consumes the touch
8142 // events, it just doesn't respond to them.
8143 return (((viewFlags & CLICKABLE) == CLICKABLE ||
8144 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
8145 }
8146
8147 if (mTouchDelegate != null) {
8148 if (mTouchDelegate.onTouchEvent(event)) {
8149 return true;
8150 }
8151 }
8152
8153 if (((viewFlags & CLICKABLE) == CLICKABLE ||
8154 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
8155 switch (event.getAction()) {
8156 case MotionEvent.ACTION_UP:
Dianne Hackborn4702a852012-08-17 15:18:29 -07008157 boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
8158 if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 // take focus if we don't have it already and we should in
8160 // touch mode.
8161 boolean focusTaken = false;
8162 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
8163 focusTaken = requestFocus();
8164 }
8165
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008166 if (prepressed) {
8167 // The button is being released before we actually
8168 // showed it as pressed. Make it show the pressed
8169 // state now (before scheduling the click) to ensure
8170 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08008171 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08008172 }
Joe Malin32736f02011-01-19 16:14:20 -08008173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 if (!mHasPerformedLongPress) {
8175 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05008176 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008177
8178 // Only perform take click actions if we were in the pressed state
8179 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08008180 // Use a Runnable and post this rather than calling
8181 // performClick directly. This lets other visual state
8182 // of the view update before click actions start.
8183 if (mPerformClick == null) {
8184 mPerformClick = new PerformClick();
8185 }
8186 if (!post(mPerformClick)) {
8187 performClick();
8188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 }
8190 }
8191
8192 if (mUnsetPressedState == null) {
8193 mUnsetPressedState = new UnsetPressedState();
8194 }
8195
Adam Powelle14579b2009-12-16 18:39:52 -08008196 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08008197 postDelayed(mUnsetPressedState,
8198 ViewConfiguration.getPressedStateDuration());
8199 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 // If the post failed, unpress right now
8201 mUnsetPressedState.run();
8202 }
Adam Powelle14579b2009-12-16 18:39:52 -08008203 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 }
8205 break;
8206
8207 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08008208 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008209
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07008210 if (performButtonActionOnTouchDown(event)) {
8211 break;
8212 }
8213
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008214 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07008215 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008216
8217 // For views inside a scrolling container, delay the pressed feedback for
8218 // a short period in case this is a scroll.
8219 if (isInScrollingContainer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008220 mPrivateFlags |= PFLAG_PREPRESSED;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008221 if (mPendingCheckForTap == null) {
8222 mPendingCheckForTap = new CheckForTap();
8223 }
8224 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
8225 } else {
8226 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08008227 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07008228 checkForLongClick(0);
8229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 break;
8231
8232 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08008233 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08008234 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 break;
8236
8237 case MotionEvent.ACTION_MOVE:
8238 final int x = (int) event.getX();
8239 final int y = (int) event.getY();
8240
8241 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07008242 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08008244 removeTapCallback();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008245 if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08008246 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05008247 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248
Adam Powell4d6f0662012-02-21 15:11:11 -08008249 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 }
8252 break;
8253 }
8254 return true;
8255 }
8256
8257 return false;
8258 }
8259
8260 /**
Adam Powell10298662011-08-14 18:26:30 -07008261 * @hide
8262 */
8263 public boolean isInScrollingContainer() {
8264 ViewParent p = getParent();
8265 while (p != null && p instanceof ViewGroup) {
8266 if (((ViewGroup) p).shouldDelayChildPressedState()) {
8267 return true;
8268 }
8269 p = p.getParent();
8270 }
8271 return false;
8272 }
8273
8274 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05008275 * Remove the longpress detection timer.
8276 */
8277 private void removeLongPressCallback() {
8278 if (mPendingCheckForLongPress != null) {
8279 removeCallbacks(mPendingCheckForLongPress);
8280 }
8281 }
Adam Powell3cb8b632011-01-21 15:34:14 -08008282
8283 /**
8284 * Remove the pending click action
8285 */
8286 private void removePerformClickCallback() {
8287 if (mPerformClick != null) {
8288 removeCallbacks(mPerformClick);
8289 }
8290 }
8291
Adam Powelle14579b2009-12-16 18:39:52 -08008292 /**
Romain Guya440b002010-02-24 15:57:54 -08008293 * Remove the prepress detection timer.
8294 */
8295 private void removeUnsetPressCallback() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008296 if ((mPrivateFlags & PFLAG_PRESSED) != 0 && mUnsetPressedState != null) {
Romain Guya440b002010-02-24 15:57:54 -08008297 setPressed(false);
8298 removeCallbacks(mUnsetPressedState);
8299 }
8300 }
8301
8302 /**
Adam Powelle14579b2009-12-16 18:39:52 -08008303 * Remove the tap detection timer.
8304 */
8305 private void removeTapCallback() {
8306 if (mPendingCheckForTap != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008307 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powelle14579b2009-12-16 18:39:52 -08008308 removeCallbacks(mPendingCheckForTap);
8309 }
8310 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05008311
8312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008313 * Cancels a pending long press. Your subclass can use this if you
8314 * want the context menu to come up if the user presses and holds
8315 * at the same place, but you don't want it to come up if they press
8316 * and then move around enough to cause scrolling.
8317 */
8318 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05008319 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08008320
8321 /*
8322 * The prepressed state handled by the tap callback is a display
8323 * construct, but the tap callback will post a long press callback
8324 * less its own timeout. Remove it here.
8325 */
8326 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327 }
8328
8329 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008330 * Remove the pending callback for sending a
8331 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
8332 */
8333 private void removeSendViewScrolledAccessibilityEventCallback() {
8334 if (mSendViewScrolledAccessibilityEvent != null) {
8335 removeCallbacks(mSendViewScrolledAccessibilityEvent);
Svetoslav Ganov4a812ae2012-05-29 16:46:10 -07008336 mSendViewScrolledAccessibilityEvent.mIsPending = false;
Svetoslav Ganova0156172011-06-26 17:55:44 -07008337 }
8338 }
8339
8340 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 * Sets the TouchDelegate for this View.
8342 */
8343 public void setTouchDelegate(TouchDelegate delegate) {
8344 mTouchDelegate = delegate;
8345 }
8346
8347 /**
8348 * Gets the TouchDelegate for this View.
8349 */
8350 public TouchDelegate getTouchDelegate() {
8351 return mTouchDelegate;
8352 }
8353
8354 /**
8355 * Set flags controlling behavior of this view.
8356 *
8357 * @param flags Constant indicating the value which should be set
8358 * @param mask Constant indicating the bit range that should be changed
8359 */
8360 void setFlags(int flags, int mask) {
8361 int old = mViewFlags;
8362 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
8363
8364 int changed = mViewFlags ^ old;
8365 if (changed == 0) {
8366 return;
8367 }
8368 int privateFlags = mPrivateFlags;
8369
8370 /* Check if the FOCUSABLE bit has changed */
8371 if (((changed & FOCUSABLE_MASK) != 0) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -07008372 ((privateFlags & PFLAG_HAS_BOUNDS) !=0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008373 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008374 && ((privateFlags & PFLAG_FOCUSED) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 /* Give up focus if we are no longer focusable */
8376 clearFocus();
8377 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
Dianne Hackborn4702a852012-08-17 15:18:29 -07008378 && ((privateFlags & PFLAG_FOCUSED) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 /*
8380 * Tell the view system that we are now available to take focus
8381 * if no one else already has it.
8382 */
8383 if (mParent != null) mParent.focusableViewAvailable(this);
8384 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008385 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8386 notifyAccessibilityStateChanged();
8387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 }
8389
8390 if ((flags & VISIBILITY_MASK) == VISIBLE) {
8391 if ((changed & VISIBILITY_MASK) != 0) {
8392 /*
Chet Haase4324ead2011-08-24 21:31:03 -07008393 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07008394 * it was not visible. Marking it drawn ensures that the invalidation will
8395 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008397 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07008398 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399
8400 needGlobalAttributesUpdate(true);
8401
8402 // a view becoming visible is worth notifying the parent
8403 // about in case nothing has focus. even if this specific view
8404 // isn't focusable, it may contain something that is, so let
8405 // the root view try to give this focus if nothing else does.
8406 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
8407 mParent.focusableViewAvailable(this);
8408 }
8409 }
8410 }
8411
8412 /* Check if the GONE bit has changed */
8413 if ((changed & GONE) != 0) {
8414 needGlobalAttributesUpdate(false);
8415 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008416
Romain Guyecd80ee2009-12-03 17:13:02 -08008417 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
8418 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008419 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08008420 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07008421 if (mParent instanceof View) {
8422 // GONE views noop invalidation, so invalidate the parent
8423 ((View) mParent).invalidate(true);
8424 }
8425 // Mark the view drawn to ensure that it gets invalidated properly the next
8426 // time it is visible and gets invalidated
Dianne Hackborn4702a852012-08-17 15:18:29 -07008427 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 }
8429 if (mAttachInfo != null) {
8430 mAttachInfo.mViewVisibilityChanged = true;
8431 }
8432 }
8433
8434 /* Check if the VISIBLE bit has changed */
8435 if ((changed & INVISIBLE) != 0) {
8436 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07008437 /*
8438 * If this view is becoming invisible, set the DRAWN flag so that
8439 * the next invalidate() will not be skipped.
8440 */
Dianne Hackborn4702a852012-08-17 15:18:29 -07008441 mPrivateFlags |= PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442
8443 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008444 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 if (getRootView() != this) {
8446 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07008447 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 }
8449 }
8450 if (mAttachInfo != null) {
8451 mAttachInfo.mViewVisibilityChanged = true;
8452 }
8453 }
8454
Adam Powell326d8082009-12-09 15:10:07 -08008455 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07008456 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08008457 ((ViewGroup) mParent).onChildVisibilityChanged(this,
8458 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08008459 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07008460 } else if (mParent != null) {
8461 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07008462 }
Adam Powell326d8082009-12-09 15:10:07 -08008463 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
8464 }
8465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
8467 destroyDrawingCache();
8468 }
8469
8470 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
8471 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008472 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008473 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 }
8475
8476 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
8477 destroyDrawingCache();
Dianne Hackborn4702a852012-08-17 15:18:29 -07008478 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 }
8480
8481 if ((changed & DRAW_MASK) != 0) {
8482 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07008483 if (mBackground != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008484 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
8485 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008487 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 }
8489 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07008490 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008491 }
8492 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08008493 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 }
8495
8496 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08008497 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 mParent.recomputeViewAttributes(this);
8499 }
8500 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07008501
8502 if (AccessibilityManager.getInstance(mContext).isEnabled()
8503 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
8504 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
8505 notifyAccessibilityStateChanged();
8506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 }
8508
8509 /**
8510 * Change the view's z order in the tree, so it's on top of other sibling
8511 * views
8512 */
8513 public void bringToFront() {
8514 if (mParent != null) {
8515 mParent.bringChildToFront(this);
8516 }
8517 }
8518
8519 /**
8520 * This is called in response to an internal scroll in this view (i.e., the
8521 * view scrolled its own contents). This is typically as a result of
8522 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
8523 * called.
8524 *
8525 * @param l Current horizontal scroll origin.
8526 * @param t Current vertical scroll origin.
8527 * @param oldl Previous horizontal scroll origin.
8528 * @param oldt Previous vertical scroll origin.
8529 */
8530 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07008531 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
8532 postSendViewScrolledAccessibilityEventCallback();
8533 }
8534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008535 mBackgroundSizeChanged = true;
8536
8537 final AttachInfo ai = mAttachInfo;
8538 if (ai != null) {
8539 ai.mViewScrollChanged = true;
8540 }
8541 }
8542
8543 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008544 * Interface definition for a callback to be invoked when the layout bounds of a view
8545 * changes due to layout processing.
8546 */
8547 public interface OnLayoutChangeListener {
8548 /**
8549 * Called when the focus state of a view has changed.
8550 *
8551 * @param v The view whose state has changed.
8552 * @param left The new value of the view's left property.
8553 * @param top The new value of the view's top property.
8554 * @param right The new value of the view's right property.
8555 * @param bottom The new value of the view's bottom property.
8556 * @param oldLeft The previous value of the view's left property.
8557 * @param oldTop The previous value of the view's top property.
8558 * @param oldRight The previous value of the view's right property.
8559 * @param oldBottom The previous value of the view's bottom property.
8560 */
8561 void onLayoutChange(View v, int left, int top, int right, int bottom,
8562 int oldLeft, int oldTop, int oldRight, int oldBottom);
8563 }
8564
8565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008566 * This is called during layout when the size of this view has changed. If
8567 * you were just added to the view hierarchy, you're called with the old
8568 * values of 0.
8569 *
8570 * @param w Current width of this view.
8571 * @param h Current height of this view.
8572 * @param oldw Old width of this view.
8573 * @param oldh Old height of this view.
8574 */
8575 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8576 }
8577
8578 /**
8579 * Called by draw to draw the child views. This may be overridden
8580 * by derived classes to gain control just before its children are drawn
8581 * (but after its own view has been drawn).
8582 * @param canvas the canvas on which to draw the view
8583 */
8584 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 }
8587
8588 /**
8589 * Gets the parent of this view. Note that the parent is a
8590 * ViewParent and not necessarily a View.
8591 *
8592 * @return Parent of this view.
8593 */
8594 public final ViewParent getParent() {
8595 return mParent;
8596 }
8597
8598 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008599 * Set the horizontal scrolled position of your view. This will cause a call to
8600 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8601 * invalidated.
8602 * @param value the x position to scroll to
8603 */
8604 public void setScrollX(int value) {
8605 scrollTo(value, mScrollY);
8606 }
8607
8608 /**
8609 * Set the vertical scrolled position of your view. This will cause a call to
8610 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8611 * invalidated.
8612 * @param value the y position to scroll to
8613 */
8614 public void setScrollY(int value) {
8615 scrollTo(mScrollX, value);
8616 }
8617
8618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 * Return the scrolled left position of this view. This is the left edge of
8620 * the displayed part of your view. You do not need to draw any pixels
8621 * farther left, since those are outside of the frame of your view on
8622 * screen.
8623 *
8624 * @return The left edge of the displayed part of your view, in pixels.
8625 */
8626 public final int getScrollX() {
8627 return mScrollX;
8628 }
8629
8630 /**
8631 * Return the scrolled top position of this view. This is the top edge of
8632 * the displayed part of your view. You do not need to draw any pixels above
8633 * it, since those are outside of the frame of your view on screen.
8634 *
8635 * @return The top edge of the displayed part of your view, in pixels.
8636 */
8637 public final int getScrollY() {
8638 return mScrollY;
8639 }
8640
8641 /**
8642 * Return the width of the your view.
8643 *
8644 * @return The width of your view, in pixels.
8645 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008646 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 public final int getWidth() {
8648 return mRight - mLeft;
8649 }
8650
8651 /**
8652 * Return the height of your view.
8653 *
8654 * @return The height of your view, in pixels.
8655 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008656 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008657 public final int getHeight() {
8658 return mBottom - mTop;
8659 }
8660
8661 /**
8662 * Return the visible drawing bounds of your view. Fills in the output
8663 * rectangle with the values from getScrollX(), getScrollY(),
Svetoslav Ganov78bd9832012-10-15 19:12:29 -07008664 * getWidth(), and getHeight(). These bounds do not account for any
8665 * transformation properties currently set on the view, such as
8666 * {@link #setScaleX(float)} or {@link #setRotation(float)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 *
8668 * @param outRect The (scrolled) drawing bounds of the view.
8669 */
8670 public void getDrawingRect(Rect outRect) {
8671 outRect.left = mScrollX;
8672 outRect.top = mScrollY;
8673 outRect.right = mScrollX + (mRight - mLeft);
8674 outRect.bottom = mScrollY + (mBottom - mTop);
8675 }
8676
8677 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008678 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8679 * raw width component (that is the result is masked by
8680 * {@link #MEASURED_SIZE_MASK}).
8681 *
8682 * @return The raw measured width of this view.
8683 */
8684 public final int getMeasuredWidth() {
8685 return mMeasuredWidth & MEASURED_SIZE_MASK;
8686 }
8687
8688 /**
8689 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008690 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008691 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008692 * This should be used during measurement and layout calculations only. Use
8693 * {@link #getWidth()} to see how wide a view is after layout.
8694 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008695 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008696 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008697 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008698 return mMeasuredWidth;
8699 }
8700
8701 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008702 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8703 * raw width component (that is the result is masked by
8704 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008706 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008707 */
8708 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008709 return mMeasuredHeight & MEASURED_SIZE_MASK;
8710 }
8711
8712 /**
8713 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008714 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008715 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8716 * This should be used during measurement and layout calculations only. Use
8717 * {@link #getHeight()} to see how wide a view is after layout.
8718 *
8719 * @return The measured width of this view as a bit mask.
8720 */
8721 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 return mMeasuredHeight;
8723 }
8724
8725 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008726 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8727 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8728 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8729 * and the height component is at the shifted bits
8730 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8731 */
8732 public final int getMeasuredState() {
8733 return (mMeasuredWidth&MEASURED_STATE_MASK)
8734 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8735 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8736 }
8737
8738 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008739 * The transform matrix of this view, which is calculated based on the current
8740 * roation, scale, and pivot properties.
8741 *
8742 * @see #getRotation()
8743 * @see #getScaleX()
8744 * @see #getScaleY()
8745 * @see #getPivotX()
8746 * @see #getPivotY()
8747 * @return The current transform matrix for the view
8748 */
8749 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008750 if (mTransformationInfo != null) {
8751 updateMatrix();
8752 return mTransformationInfo.mMatrix;
8753 }
8754 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008755 }
8756
8757 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008758 * Utility function to determine if the value is far enough away from zero to be
8759 * considered non-zero.
8760 * @param value A floating point value to check for zero-ness
8761 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8762 */
8763 private static boolean nonzero(float value) {
8764 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8765 }
8766
8767 /**
Jeff Brown86671742010-09-30 20:00:15 -07008768 * Returns true if the transform matrix is the identity matrix.
8769 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008770 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008771 * @return True if the transform matrix is the identity matrix, false otherwise.
8772 */
Jeff Brown86671742010-09-30 20:00:15 -07008773 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008774 if (mTransformationInfo != null) {
8775 updateMatrix();
8776 return mTransformationInfo.mMatrixIsIdentity;
8777 }
8778 return true;
8779 }
8780
8781 void ensureTransformationInfo() {
8782 if (mTransformationInfo == null) {
8783 mTransformationInfo = new TransformationInfo();
8784 }
Jeff Brown86671742010-09-30 20:00:15 -07008785 }
8786
8787 /**
8788 * Recomputes the transform matrix if necessary.
8789 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008790 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008791 final TransformationInfo info = mTransformationInfo;
8792 if (info == null) {
8793 return;
8794 }
8795 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008796 // transform-related properties have changed since the last time someone
8797 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008798
8799 // Figure out if we need to update the pivot point
Dianne Hackborn4702a852012-08-17 15:18:29 -07008800 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008801 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8802 info.mPrevWidth = mRight - mLeft;
8803 info.mPrevHeight = mBottom - mTop;
8804 info.mPivotX = info.mPrevWidth / 2f;
8805 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008806 }
8807 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008808 info.mMatrix.reset();
8809 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8810 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8811 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8812 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008813 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008814 if (info.mCamera == null) {
8815 info.mCamera = new Camera();
8816 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008817 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008818 info.mCamera.save();
8819 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8820 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8821 info.mCamera.getMatrix(info.matrix3D);
8822 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8823 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8824 info.mPivotY + info.mTranslationY);
8825 info.mMatrix.postConcat(info.matrix3D);
8826 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008827 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008828 info.mMatrixDirty = false;
8829 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8830 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008831 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008832 }
8833
Tobias Duboisdefdb1e2010-12-15 11:35:30 +01008834 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008835 * Utility method to retrieve the inverse of the current mMatrix property.
8836 * We cache the matrix to avoid recalculating it when transform properties
8837 * have not changed.
8838 *
8839 * @return The inverse of the current matrix of this view.
8840 */
Jeff Brown86671742010-09-30 20:00:15 -07008841 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008842 final TransformationInfo info = mTransformationInfo;
8843 if (info != null) {
8844 updateMatrix();
8845 if (info.mInverseMatrixDirty) {
8846 if (info.mInverseMatrix == null) {
8847 info.mInverseMatrix = new Matrix();
8848 }
8849 info.mMatrix.invert(info.mInverseMatrix);
8850 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008851 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008852 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008853 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008854 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008855 }
8856
8857 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008858 * Gets the distance along the Z axis from the camera to this view.
8859 *
8860 * @see #setCameraDistance(float)
8861 *
8862 * @return The distance along the Z axis.
8863 */
8864 public float getCameraDistance() {
8865 ensureTransformationInfo();
8866 final float dpi = mResources.getDisplayMetrics().densityDpi;
8867 final TransformationInfo info = mTransformationInfo;
8868 if (info.mCamera == null) {
8869 info.mCamera = new Camera();
8870 info.matrix3D = new Matrix();
8871 }
8872 return -(info.mCamera.getLocationZ() * dpi);
8873 }
8874
8875 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008876 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8877 * views are drawn) from the camera to this view. The camera's distance
8878 * affects 3D transformations, for instance rotations around the X and Y
8879 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008880 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008881 * use a camera distance that's greater than the height (X axis rotation) or
8882 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008883 *
Romain Guya5364ee2011-02-24 14:46:04 -08008884 * <p>The distance of the camera from the view plane can have an affect on the
8885 * perspective distortion of the view when it is rotated around the x or y axis.
8886 * For example, a large distance will result in a large viewing angle, and there
8887 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008888 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008889 * also result in some drawing artifacts if the rotated view ends up partially
8890 * behind the camera (which is why the recommendation is to use a distance at
8891 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008892 *
Romain Guya5364ee2011-02-24 14:46:04 -08008893 * <p>The distance is expressed in "depth pixels." The default distance depends
8894 * on the screen density. For instance, on a medium density display, the
8895 * default distance is 1280. On a high density display, the default distance
8896 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008897 *
Romain Guya5364ee2011-02-24 14:46:04 -08008898 * <p>If you want to specify a distance that leads to visually consistent
8899 * results across various densities, use the following formula:</p>
8900 * <pre>
8901 * float scale = context.getResources().getDisplayMetrics().density;
8902 * view.setCameraDistance(distance * scale);
8903 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008904 *
Romain Guya5364ee2011-02-24 14:46:04 -08008905 * <p>The density scale factor of a high density display is 1.5,
8906 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008907 *
Romain Guya5364ee2011-02-24 14:46:04 -08008908 * @param distance The distance in "depth pixels", if negative the opposite
8909 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008910 *
8911 * @see #setRotationX(float)
8912 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008913 */
8914 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008915 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008916
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008917 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008918 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008919 final TransformationInfo info = mTransformationInfo;
8920 if (info.mCamera == null) {
8921 info.mCamera = new Camera();
8922 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008923 }
8924
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008925 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8926 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008927
Chet Haase9d1992d2012-03-13 11:03:25 -07008928 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008929 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008930 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008931 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008932 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008933 // View was rejected last time it was drawn by its parent; this may have changed
8934 invalidateParentIfNeeded();
8935 }
Romain Guya5364ee2011-02-24 14:46:04 -08008936 }
8937
8938 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008939 * The degrees that the view is rotated around the pivot point.
8940 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008941 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008942 * @see #getPivotX()
8943 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008944 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008945 * @return The degrees of rotation.
8946 */
Chet Haasea5531132012-02-02 13:41:44 -08008947 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008948 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008949 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008950 }
8951
8952 /**
Chet Haase897247b2010-09-09 14:54:47 -07008953 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8954 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008955 *
8956 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008957 *
8958 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008959 * @see #getPivotX()
8960 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008961 * @see #setRotationX(float)
8962 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008963 *
8964 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008965 */
8966 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008967 ensureTransformationInfo();
8968 final TransformationInfo info = mTransformationInfo;
8969 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008970 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008971 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008972 info.mRotation = rotation;
8973 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008974 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008975 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008976 mDisplayList.setRotation(rotation);
8977 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07008978 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07008979 // View was rejected last time it was drawn by its parent; this may have changed
8980 invalidateParentIfNeeded();
8981 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008982 }
8983 }
8984
8985 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008986 * The degrees that the view is rotated around the vertical axis through the pivot point.
8987 *
8988 * @see #getPivotX()
8989 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008990 * @see #setRotationY(float)
8991 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008992 * @return The degrees of Y rotation.
8993 */
Chet Haasea5531132012-02-02 13:41:44 -08008994 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008995 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008996 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008997 }
8998
8999 /**
Chet Haase897247b2010-09-09 14:54:47 -07009000 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
9001 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
9002 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009003 *
Romain Guya5364ee2011-02-24 14:46:04 -08009004 * When rotating large views, it is recommended to adjust the camera distance
9005 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009006 *
9007 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009008 *
9009 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07009010 * @see #getPivotX()
9011 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009012 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009013 * @see #setRotationX(float)
9014 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009015 *
9016 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07009017 */
9018 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009019 ensureTransformationInfo();
9020 final TransformationInfo info = mTransformationInfo;
9021 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009022 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009023 info.mRotationY = rotationY;
9024 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009025 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009026 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009027 mDisplayList.setRotationY(rotationY);
9028 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009029 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009030 // View was rejected last time it was drawn by its parent; this may have changed
9031 invalidateParentIfNeeded();
9032 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009033 }
9034 }
9035
9036 /**
9037 * The degrees that the view is rotated around the horizontal axis through the pivot point.
9038 *
9039 * @see #getPivotX()
9040 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07009041 * @see #setRotationX(float)
9042 *
Chet Haasefd2b0022010-08-06 13:08:56 -07009043 * @return The degrees of X rotation.
9044 */
Chet Haasea5531132012-02-02 13:41:44 -08009045 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07009046 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009047 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07009048 }
9049
9050 /**
Chet Haase897247b2010-09-09 14:54:47 -07009051 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
9052 * Increasing values result in clockwise rotation from the viewpoint of looking down the
9053 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07009054 *
Romain Guya5364ee2011-02-24 14:46:04 -08009055 * When rotating large views, it is recommended to adjust the camera distance
9056 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07009057 *
9058 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07009059 *
9060 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07009061 * @see #getPivotX()
9062 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08009063 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07009064 * @see #setRotationY(float)
9065 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08009066 *
9067 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07009068 */
9069 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009070 ensureTransformationInfo();
9071 final TransformationInfo info = mTransformationInfo;
9072 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009073 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009074 info.mRotationX = rotationX;
9075 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009076 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009077 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009078 mDisplayList.setRotationX(rotationX);
9079 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009080 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009081 // View was rejected last time it was drawn by its parent; this may have changed
9082 invalidateParentIfNeeded();
9083 }
Chet Haasefd2b0022010-08-06 13:08:56 -07009084 }
9085 }
9086
9087 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009088 * The amount that the view is scaled in x around the pivot point, as a proportion of
9089 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
9090 *
Joe Onorato93162322010-09-16 15:42:01 -04009091 * <p>By default, this is 1.0f.
9092 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009093 * @see #getPivotX()
9094 * @see #getPivotY()
9095 * @return The scaling factor.
9096 */
Chet Haasea5531132012-02-02 13:41:44 -08009097 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009098 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009099 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009100 }
9101
9102 /**
9103 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
9104 * the view's unscaled width. A value of 1 means that no scaling is applied.
9105 *
9106 * @param scaleX The scaling factor.
9107 * @see #getPivotX()
9108 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009109 *
9110 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07009111 */
9112 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009113 ensureTransformationInfo();
9114 final TransformationInfo info = mTransformationInfo;
9115 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009116 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009117 info.mScaleX = scaleX;
9118 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009119 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009120 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009121 mDisplayList.setScaleX(scaleX);
9122 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009123 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009124 // View was rejected last time it was drawn by its parent; this may have changed
9125 invalidateParentIfNeeded();
9126 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009127 }
9128 }
9129
9130 /**
9131 * The amount that the view is scaled in y around the pivot point, as a proportion of
9132 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
9133 *
Joe Onorato93162322010-09-16 15:42:01 -04009134 * <p>By default, this is 1.0f.
9135 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009136 * @see #getPivotX()
9137 * @see #getPivotY()
9138 * @return The scaling factor.
9139 */
Chet Haasea5531132012-02-02 13:41:44 -08009140 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009141 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009142 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009143 }
9144
9145 /**
9146 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
9147 * the view's unscaled width. A value of 1 means that no scaling is applied.
9148 *
9149 * @param scaleY The scaling factor.
9150 * @see #getPivotX()
9151 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009152 *
9153 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07009154 */
9155 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009156 ensureTransformationInfo();
9157 final TransformationInfo info = mTransformationInfo;
9158 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009159 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009160 info.mScaleY = scaleY;
9161 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009162 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009163 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009164 mDisplayList.setScaleY(scaleY);
9165 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009166 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009167 // View was rejected last time it was drawn by its parent; this may have changed
9168 invalidateParentIfNeeded();
9169 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009170 }
9171 }
9172
9173 /**
9174 * The x location of the point around which the view is {@link #setRotation(float) rotated}
9175 * and {@link #setScaleX(float) scaled}.
9176 *
9177 * @see #getRotation()
9178 * @see #getScaleX()
9179 * @see #getScaleY()
9180 * @see #getPivotY()
9181 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009182 *
9183 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009184 */
Chet Haasea5531132012-02-02 13:41:44 -08009185 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009186 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009187 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009188 }
9189
9190 /**
9191 * Sets the x location of the point around which the view is
9192 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07009193 * By default, the pivot point is centered on the object.
9194 * Setting this property disables this behavior and causes the view to use only the
9195 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009196 *
9197 * @param pivotX The x location of the pivot point.
9198 * @see #getRotation()
9199 * @see #getScaleX()
9200 * @see #getScaleY()
9201 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009202 *
9203 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07009204 */
9205 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009206 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009207 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009208 final TransformationInfo info = mTransformationInfo;
9209 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009210 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009211 info.mPivotX = pivotX;
9212 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009213 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009214 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009215 mDisplayList.setPivotX(pivotX);
9216 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009217 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009218 // View was rejected last time it was drawn by its parent; this may have changed
9219 invalidateParentIfNeeded();
9220 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009221 }
9222 }
9223
9224 /**
9225 * The y location of the point around which the view is {@link #setRotation(float) rotated}
9226 * and {@link #setScaleY(float) scaled}.
9227 *
9228 * @see #getRotation()
9229 * @see #getScaleX()
9230 * @see #getScaleY()
9231 * @see #getPivotY()
9232 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07009233 *
9234 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009235 */
Chet Haasea5531132012-02-02 13:41:44 -08009236 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009237 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009238 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009239 }
9240
9241 /**
9242 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07009243 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
9244 * Setting this property disables this behavior and causes the view to use only the
9245 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07009246 *
9247 * @param pivotY The y location of the pivot point.
9248 * @see #getRotation()
9249 * @see #getScaleX()
9250 * @see #getScaleY()
9251 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08009252 *
9253 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07009254 */
9255 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009256 ensureTransformationInfo();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009257 mPrivateFlags |= PFLAG_PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009258 final TransformationInfo info = mTransformationInfo;
9259 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009260 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009261 info.mPivotY = pivotY;
9262 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009263 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009264 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009265 mDisplayList.setPivotY(pivotY);
9266 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009267 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009268 // View was rejected last time it was drawn by its parent; this may have changed
9269 invalidateParentIfNeeded();
9270 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009271 }
9272 }
9273
9274 /**
9275 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
9276 * completely transparent and 1 means the view is completely opaque.
9277 *
Joe Onorato93162322010-09-16 15:42:01 -04009278 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07009279 * @return The opacity of the view.
9280 */
Chet Haasea5531132012-02-02 13:41:44 -08009281 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07009282 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009283 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07009284 }
9285
9286 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07009287 * Returns whether this View has content which overlaps. This function, intended to be
9288 * overridden by specific View types, is an optimization when alpha is set on a view. If
9289 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
9290 * and then composited it into place, which can be expensive. If the view has no overlapping
9291 * rendering, the view can draw each primitive with the appropriate alpha value directly.
9292 * An example of overlapping rendering is a TextView with a background image, such as a
9293 * Button. An example of non-overlapping rendering is a TextView with no background, or
9294 * an ImageView with only the foreground image. The default implementation returns true;
9295 * subclasses should override if they have cases which can be optimized.
9296 *
9297 * @return true if the content in this view might overlap, false otherwise.
9298 */
9299 public boolean hasOverlappingRendering() {
9300 return true;
9301 }
9302
9303 /**
Romain Guy171c5922011-01-06 10:04:23 -08009304 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
9305 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009306 *
Romain Guy171c5922011-01-06 10:04:23 -08009307 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
9308 * responsible for applying the opacity itself. Otherwise, calling this method is
9309 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08009310 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07009311 *
Chet Haasea5531132012-02-02 13:41:44 -08009312 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
9313 * performance implications. It is generally best to use the alpha property sparingly and
9314 * transiently, as in the case of fading animations.</p>
9315 *
Chet Haasec3aa3612010-06-17 08:50:37 -07009316 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08009317 *
Joe Malin32736f02011-01-19 16:14:20 -08009318 * @see #setLayerType(int, android.graphics.Paint)
9319 *
Chet Haase73066682010-11-29 15:55:32 -08009320 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07009321 */
9322 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009323 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009324 if (mTransformationInfo.mAlpha != alpha) {
9325 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009326 if (onSetAlpha((int) (alpha * 255))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009327 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009328 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07009329 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009330 invalidate(true);
9331 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009332 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07009333 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07009334 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009335 mDisplayList.setAlpha(alpha);
9336 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009337 }
Chet Haaseed032702010-10-01 14:05:54 -07009338 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009339 }
9340
9341 /**
Chet Haasea00f3862011-02-22 06:34:40 -08009342 * Faster version of setAlpha() which performs the same steps except there are
9343 * no calls to invalidate(). The caller of this function should perform proper invalidation
9344 * on the parent and this object. The return value indicates whether the subclass handles
9345 * alpha (the return value for onSetAlpha()).
9346 *
9347 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009348 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
9349 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08009350 */
9351 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009352 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009353 if (mTransformationInfo.mAlpha != alpha) {
9354 mTransformationInfo.mAlpha = alpha;
9355 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
9356 if (subclassHandlesAlpha) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009357 mPrivateFlags |= PFLAG_ALPHA_SET;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009358 return true;
9359 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009360 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07009361 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009362 mDisplayList.setAlpha(alpha);
9363 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009364 }
Chet Haasea00f3862011-02-22 06:34:40 -08009365 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08009366 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08009367 }
9368
9369 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009370 * Top position of this view relative to its parent.
9371 *
9372 * @return The top of this view, in pixels.
9373 */
9374 @ViewDebug.CapturedViewProperty
9375 public final int getTop() {
9376 return mTop;
9377 }
9378
9379 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009380 * Sets the top position of this view relative to its parent. This method is meant to be called
9381 * by the layout system and should not generally be called otherwise, because the property
9382 * may be changed at any time by the layout.
9383 *
9384 * @param top The top of this view, in pixels.
9385 */
9386 public final void setTop(int top) {
9387 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07009388 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009389 final boolean matrixIsIdentity = mTransformationInfo == null
9390 || mTransformationInfo.mMatrixIsIdentity;
9391 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009392 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009393 int minTop;
9394 int yLoc;
9395 if (top < mTop) {
9396 minTop = top;
9397 yLoc = top - mTop;
9398 } else {
9399 minTop = mTop;
9400 yLoc = 0;
9401 }
Chet Haasee9140a72011-02-16 16:23:29 -08009402 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009403 }
9404 } else {
9405 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009406 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009407 }
9408
Chet Haaseed032702010-10-01 14:05:54 -07009409 int width = mRight - mLeft;
9410 int oldHeight = mBottom - mTop;
9411
Chet Haase21cd1382010-09-01 17:42:29 -07009412 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07009413 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009414 mDisplayList.setTop(mTop);
9415 }
Chet Haase21cd1382010-09-01 17:42:29 -07009416
Chet Haaseed032702010-10-01 14:05:54 -07009417 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9418
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009419 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009420 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009421 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009422 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009423 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009424 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009425 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009426 }
Chet Haase55dbb652010-12-21 20:15:08 -08009427 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009428 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009429 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009430 // View was rejected last time it was drawn by its parent; this may have changed
9431 invalidateParentIfNeeded();
9432 }
Chet Haase21cd1382010-09-01 17:42:29 -07009433 }
9434 }
9435
9436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009437 * Bottom position of this view relative to its parent.
9438 *
9439 * @return The bottom of this view, in pixels.
9440 */
9441 @ViewDebug.CapturedViewProperty
9442 public final int getBottom() {
9443 return mBottom;
9444 }
9445
9446 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08009447 * True if this view has changed since the last time being drawn.
9448 *
9449 * @return The dirty state of this view.
9450 */
9451 public boolean isDirty() {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009452 return (mPrivateFlags & PFLAG_DIRTY_MASK) != 0;
Michael Jurkadab559a2011-01-04 20:31:51 -08009453 }
9454
9455 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009456 * Sets the bottom position of this view relative to its parent. This method is meant to be
9457 * called by the layout system and should not generally be called otherwise, because the
9458 * property may be changed at any time by the layout.
9459 *
9460 * @param bottom The bottom of this view, in pixels.
9461 */
9462 public final void setBottom(int bottom) {
9463 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07009464 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009465 final boolean matrixIsIdentity = mTransformationInfo == null
9466 || mTransformationInfo.mMatrixIsIdentity;
9467 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009468 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009469 int maxBottom;
9470 if (bottom < mBottom) {
9471 maxBottom = mBottom;
9472 } else {
9473 maxBottom = bottom;
9474 }
Chet Haasee9140a72011-02-16 16:23:29 -08009475 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009476 }
9477 } else {
9478 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009479 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009480 }
9481
Chet Haaseed032702010-10-01 14:05:54 -07009482 int width = mRight - mLeft;
9483 int oldHeight = mBottom - mTop;
9484
Chet Haase21cd1382010-09-01 17:42:29 -07009485 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07009486 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009487 mDisplayList.setBottom(mBottom);
9488 }
Chet Haase21cd1382010-09-01 17:42:29 -07009489
Chet Haaseed032702010-10-01 14:05:54 -07009490 onSizeChanged(width, mBottom - mTop, width, oldHeight);
9491
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009492 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009493 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009494 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009495 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009496 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009497 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009498 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009499 }
Chet Haase55dbb652010-12-21 20:15:08 -08009500 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009501 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009502 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009503 // View was rejected last time it was drawn by its parent; this may have changed
9504 invalidateParentIfNeeded();
9505 }
Chet Haase21cd1382010-09-01 17:42:29 -07009506 }
9507 }
9508
9509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009510 * Left position of this view relative to its parent.
9511 *
9512 * @return The left edge of this view, in pixels.
9513 */
9514 @ViewDebug.CapturedViewProperty
9515 public final int getLeft() {
9516 return mLeft;
9517 }
9518
9519 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009520 * Sets the left position of this view relative to its parent. This method is meant to be called
9521 * by the layout system and should not generally be called otherwise, because the property
9522 * may be changed at any time by the layout.
9523 *
9524 * @param left The bottom of this view, in pixels.
9525 */
9526 public final void setLeft(int left) {
9527 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07009528 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009529 final boolean matrixIsIdentity = mTransformationInfo == null
9530 || mTransformationInfo.mMatrixIsIdentity;
9531 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009532 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009533 int minLeft;
9534 int xLoc;
9535 if (left < mLeft) {
9536 minLeft = left;
9537 xLoc = left - mLeft;
9538 } else {
9539 minLeft = mLeft;
9540 xLoc = 0;
9541 }
Chet Haasee9140a72011-02-16 16:23:29 -08009542 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009543 }
9544 } else {
9545 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009546 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009547 }
9548
Chet Haaseed032702010-10-01 14:05:54 -07009549 int oldWidth = mRight - mLeft;
9550 int height = mBottom - mTop;
9551
Chet Haase21cd1382010-09-01 17:42:29 -07009552 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07009553 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009554 mDisplayList.setLeft(left);
9555 }
Chet Haase21cd1382010-09-01 17:42:29 -07009556
Chet Haaseed032702010-10-01 14:05:54 -07009557 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9558
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009559 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009560 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009561 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009562 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009563 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009564 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009565 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009566 }
Chet Haase55dbb652010-12-21 20:15:08 -08009567 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009568 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009569 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009570 // View was rejected last time it was drawn by its parent; this may have changed
9571 invalidateParentIfNeeded();
9572 }
Chet Haase21cd1382010-09-01 17:42:29 -07009573 }
9574 }
9575
9576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009577 * Right position of this view relative to its parent.
9578 *
9579 * @return The right edge of this view, in pixels.
9580 */
9581 @ViewDebug.CapturedViewProperty
9582 public final int getRight() {
9583 return mRight;
9584 }
9585
9586 /**
Chet Haase21cd1382010-09-01 17:42:29 -07009587 * Sets the right position of this view relative to its parent. This method is meant to be called
9588 * by the layout system and should not generally be called otherwise, because the property
9589 * may be changed at any time by the layout.
9590 *
9591 * @param right The bottom of this view, in pixels.
9592 */
9593 public final void setRight(int right) {
9594 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07009595 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009596 final boolean matrixIsIdentity = mTransformationInfo == null
9597 || mTransformationInfo.mMatrixIsIdentity;
9598 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08009599 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07009600 int maxRight;
9601 if (right < mRight) {
9602 maxRight = mRight;
9603 } else {
9604 maxRight = right;
9605 }
Chet Haasee9140a72011-02-16 16:23:29 -08009606 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009607 }
9608 } else {
9609 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009610 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009611 }
9612
Chet Haaseed032702010-10-01 14:05:54 -07009613 int oldWidth = mRight - mLeft;
9614 int height = mBottom - mTop;
9615
Chet Haase21cd1382010-09-01 17:42:29 -07009616 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009617 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009618 mDisplayList.setRight(mRight);
9619 }
Chet Haase21cd1382010-09-01 17:42:29 -07009620
Chet Haaseed032702010-10-01 14:05:54 -07009621 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9622
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009623 if (!matrixIsIdentity) {
Dianne Hackborn4702a852012-08-17 15:18:29 -07009624 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009625 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009626 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009627 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009628 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009629 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009630 }
Chet Haase55dbb652010-12-21 20:15:08 -08009631 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009632 invalidateParentIfNeeded();
Dianne Hackborn4702a852012-08-17 15:18:29 -07009633 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009634 // View was rejected last time it was drawn by its parent; this may have changed
9635 invalidateParentIfNeeded();
9636 }
Chet Haase21cd1382010-09-01 17:42:29 -07009637 }
9638 }
9639
9640 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009641 * The visual x position of this view, in pixels. This is equivalent to the
9642 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009643 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009644 *
Chet Haasedf030d22010-07-30 17:22:38 -07009645 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009646 */
Chet Haasea5531132012-02-02 13:41:44 -08009647 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009648 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009649 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009650 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009651
Chet Haasedf030d22010-07-30 17:22:38 -07009652 /**
9653 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9654 * {@link #setTranslationX(float) translationX} property to be the difference between
9655 * the x value passed in and the current {@link #getLeft() left} property.
9656 *
9657 * @param x The visual x position of this view, in pixels.
9658 */
9659 public void setX(float x) {
9660 setTranslationX(x - mLeft);
9661 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009662
Chet Haasedf030d22010-07-30 17:22:38 -07009663 /**
9664 * The visual y position of this view, in pixels. This is equivalent to the
9665 * {@link #setTranslationY(float) translationY} property plus the current
9666 * {@link #getTop() top} property.
9667 *
9668 * @return The visual y position of this view, in pixels.
9669 */
Chet Haasea5531132012-02-02 13:41:44 -08009670 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009671 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009672 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009673 }
9674
9675 /**
9676 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9677 * {@link #setTranslationY(float) translationY} property to be the difference between
9678 * the y value passed in and the current {@link #getTop() top} property.
9679 *
9680 * @param y The visual y position of this view, in pixels.
9681 */
9682 public void setY(float y) {
9683 setTranslationY(y - mTop);
9684 }
9685
9686
9687 /**
9688 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9689 * This position is post-layout, in addition to wherever the object's
9690 * layout placed it.
9691 *
9692 * @return The horizontal position of this view relative to its left position, in pixels.
9693 */
Chet Haasea5531132012-02-02 13:41:44 -08009694 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009695 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009696 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009697 }
9698
9699 /**
9700 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9701 * This effectively positions the object post-layout, in addition to wherever the object's
9702 * layout placed it.
9703 *
9704 * @param translationX The horizontal position of this view relative to its left position,
9705 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009706 *
9707 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009708 */
9709 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009710 ensureTransformationInfo();
9711 final TransformationInfo info = mTransformationInfo;
9712 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009713 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009714 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009715 info.mTranslationX = translationX;
9716 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009717 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009718 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009719 mDisplayList.setTranslationX(translationX);
9720 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009721 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009722 // View was rejected last time it was drawn by its parent; this may have changed
9723 invalidateParentIfNeeded();
9724 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009725 }
9726 }
9727
9728 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009729 * The horizontal location of this view relative to its {@link #getTop() top} position.
9730 * This position is post-layout, in addition to wherever the object's
9731 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009732 *
Chet Haasedf030d22010-07-30 17:22:38 -07009733 * @return The vertical position of this view relative to its top position,
9734 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009735 */
Chet Haasea5531132012-02-02 13:41:44 -08009736 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009737 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009738 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009739 }
9740
9741 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009742 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9743 * This effectively positions the object post-layout, in addition to wherever the object's
9744 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009745 *
Chet Haasedf030d22010-07-30 17:22:38 -07009746 * @param translationY The vertical position of this view relative to its top position,
9747 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009748 *
9749 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009750 */
Chet Haasedf030d22010-07-30 17:22:38 -07009751 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009752 ensureTransformationInfo();
9753 final TransformationInfo info = mTransformationInfo;
9754 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009755 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009756 info.mTranslationY = translationY;
9757 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009758 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009759 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009760 mDisplayList.setTranslationY(translationY);
9761 }
Dianne Hackborn4702a852012-08-17 15:18:29 -07009762 if ((mPrivateFlags2 & PFLAG2_VIEW_QUICK_REJECTED) == PFLAG2_VIEW_QUICK_REJECTED) {
Chet Haase1a3ab172012-05-11 08:41:20 -07009763 // View was rejected last time it was drawn by its parent; this may have changed
9764 invalidateParentIfNeeded();
9765 }
Chet Haasedf030d22010-07-30 17:22:38 -07009766 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009767 }
9768
9769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 * Hit rectangle in parent's coordinates
9771 *
9772 * @param outRect The hit rectangle of the view.
9773 */
9774 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009775 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009776 final TransformationInfo info = mTransformationInfo;
9777 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009778 outRect.set(mLeft, mTop, mRight, mBottom);
9779 } else {
9780 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009781 tmpRect.set(-info.mPivotX, -info.mPivotY,
9782 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9783 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009784 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9785 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009786 }
9787 }
9788
9789 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009790 * Determines whether the given point, in local coordinates is inside the view.
9791 */
9792 /*package*/ final boolean pointInView(float localX, float localY) {
9793 return localX >= 0 && localX < (mRight - mLeft)
9794 && localY >= 0 && localY < (mBottom - mTop);
9795 }
9796
9797 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009798 * Utility method to determine whether the given point, in local coordinates,
9799 * is inside the view, where the area of the view is expanded by the slop factor.
9800 * This method is called while processing touch-move events to determine if the event
9801 * is still within the view.
9802 */
9803 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009804 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009805 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 }
9807
9808 /**
9809 * When a view has focus and the user navigates away from it, the next view is searched for
9810 * starting from the rectangle filled in by this method.
9811 *
Fabrice Di Meglioc11f77f2012-09-18 15:33:07 -07009812 * By default, the rectangle is the {@link #getDrawingRect(android.graphics.Rect)})
Romain Guy5c22a8c2011-05-13 11:48:45 -07009813 * of the view. However, if your view maintains some idea of internal selection,
9814 * such as a cursor, or a selected row or column, you should override this method and
9815 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 *
9817 * @param r The rectangle to fill in, in this view's coordinates.
9818 */
9819 public void getFocusedRect(Rect r) {
9820 getDrawingRect(r);
9821 }
9822
9823 /**
9824 * If some part of this view is not clipped by any of its parents, then
9825 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009826 * coordinates (without taking possible View rotations into account), offset
9827 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9828 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829 *
9830 * @param r If true is returned, r holds the global coordinates of the
9831 * visible portion of this view.
9832 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9833 * between this view and its root. globalOffet may be null.
9834 * @return true if r is non-empty (i.e. part of the view is visible at the
9835 * root level.
9836 */
9837 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9838 int width = mRight - mLeft;
9839 int height = mBottom - mTop;
9840 if (width > 0 && height > 0) {
9841 r.set(0, 0, width, height);
9842 if (globalOffset != null) {
9843 globalOffset.set(-mScrollX, -mScrollY);
9844 }
9845 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9846 }
9847 return false;
9848 }
9849
9850 public final boolean getGlobalVisibleRect(Rect r) {
9851 return getGlobalVisibleRect(r, null);
9852 }
9853
9854 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009855 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 if (getGlobalVisibleRect(r, offset)) {
9857 r.offset(-offset.x, -offset.y); // make r local
9858 return true;
9859 }
9860 return false;
9861 }
9862
9863 /**
9864 * Offset this view's vertical location by the specified number of pixels.
9865 *
9866 * @param offset the number of pixels to offset the view by
9867 */
9868 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009869 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009870 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009871 final boolean matrixIsIdentity = mTransformationInfo == null
9872 || mTransformationInfo.mMatrixIsIdentity;
9873 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009874 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009875 invalidateViewProperty(false, false);
9876 } else {
9877 final ViewParent p = mParent;
9878 if (p != null && mAttachInfo != null) {
9879 final Rect r = mAttachInfo.mTmpInvalRect;
9880 int minTop;
9881 int maxBottom;
9882 int yLoc;
9883 if (offset < 0) {
9884 minTop = mTop + offset;
9885 maxBottom = mBottom;
9886 yLoc = offset;
9887 } else {
9888 minTop = mTop;
9889 maxBottom = mBottom + offset;
9890 yLoc = 0;
9891 }
9892 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9893 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009894 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009895 }
9896 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009897 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009898 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009899
Chet Haasec3aa3612010-06-17 08:50:37 -07009900 mTop += offset;
9901 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009902 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009903 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009904 invalidateViewProperty(false, false);
9905 } else {
9906 if (!matrixIsIdentity) {
9907 invalidateViewProperty(false, true);
9908 }
9909 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009910 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 }
9913
9914 /**
9915 * Offset this view's horizontal location by the specified amount of pixels.
9916 *
9917 * @param offset the numer of pixels to offset the view by
9918 */
9919 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009920 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009921 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009922 final boolean matrixIsIdentity = mTransformationInfo == null
9923 || mTransformationInfo.mMatrixIsIdentity;
9924 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009925 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009926 invalidateViewProperty(false, false);
9927 } else {
9928 final ViewParent p = mParent;
9929 if (p != null && mAttachInfo != null) {
9930 final Rect r = mAttachInfo.mTmpInvalRect;
9931 int minLeft;
9932 int maxRight;
9933 if (offset < 0) {
9934 minLeft = mLeft + offset;
9935 maxRight = mRight;
9936 } else {
9937 minLeft = mLeft;
9938 maxRight = mRight + offset;
9939 }
9940 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9941 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009942 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009943 }
9944 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009945 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009946 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009947
Chet Haasec3aa3612010-06-17 08:50:37 -07009948 mLeft += offset;
9949 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009950 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009951 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009952 invalidateViewProperty(false, false);
9953 } else {
9954 if (!matrixIsIdentity) {
9955 invalidateViewProperty(false, true);
9956 }
9957 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009958 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 }
9961
9962 /**
9963 * Get the LayoutParams associated with this view. All views should have
9964 * layout parameters. These supply parameters to the <i>parent</i> of this
9965 * view specifying how it should be arranged. There are many subclasses of
9966 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9967 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009968 *
9969 * This method may return null if this View is not attached to a parent
9970 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9971 * was not invoked successfully. When a View is attached to a parent
9972 * ViewGroup, this method must not return null.
9973 *
9974 * @return The LayoutParams associated with this view, or null if no
9975 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009976 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009977 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009978 public ViewGroup.LayoutParams getLayoutParams() {
9979 return mLayoutParams;
9980 }
9981
9982 /**
9983 * Set the layout parameters associated with this view. These supply
9984 * parameters to the <i>parent</i> of this view specifying how it should be
9985 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9986 * correspond to the different subclasses of ViewGroup that are responsible
9987 * for arranging their children.
9988 *
Romain Guy01c174b2011-02-22 11:51:06 -08009989 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990 */
9991 public void setLayoutParams(ViewGroup.LayoutParams params) {
9992 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009993 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994 }
9995 mLayoutParams = params;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -07009996 resolveLayoutParams();
Philip Milned7dd8902012-01-26 16:55:30 -08009997 if (mParent instanceof ViewGroup) {
9998 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 requestLayout();
10001 }
10002
10003 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010004 * Resolve the layout parameters depending on the resolved layout direction
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010005 *
10006 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010007 */
Fabrice Di Megliofcc33482012-10-18 11:11:51 -070010008 public void resolveLayoutParams() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010009 if (mLayoutParams != null) {
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -070010010 mLayoutParams.resolveLayoutDirection(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070010011 }
10012 }
10013
10014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 * Set the scrolled position of your view. This will cause a call to
10016 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10017 * invalidated.
10018 * @param x the x position to scroll to
10019 * @param y the y position to scroll to
10020 */
10021 public void scrollTo(int x, int y) {
10022 if (mScrollX != x || mScrollY != y) {
10023 int oldX = mScrollX;
10024 int oldY = mScrollY;
10025 mScrollX = x;
10026 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010027 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -070010029 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010030 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -070010031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 }
10033 }
10034
10035 /**
10036 * Move the scrolled position of your view. This will cause a call to
10037 * {@link #onScrollChanged(int, int, int, int)} and the view will be
10038 * invalidated.
10039 * @param x the amount of pixels to scroll by horizontally
10040 * @param y the amount of pixels to scroll by vertically
10041 */
10042 public void scrollBy(int x, int y) {
10043 scrollTo(mScrollX + x, mScrollY + y);
10044 }
10045
10046 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010047 * <p>Trigger the scrollbars to draw. When invoked this method starts an
10048 * animation to fade the scrollbars out after a default delay. If a subclass
10049 * provides animated scrolling, the start delay should equal the duration
10050 * of the scrolling animation.</p>
10051 *
10052 * <p>The animation starts only if at least one of the scrollbars is
10053 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
10054 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10055 * this method returns true, and false otherwise. If the animation is
10056 * started, this method calls {@link #invalidate()}; in that case the
10057 * caller should not call {@link #invalidate()}.</p>
10058 *
10059 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -070010060 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -070010061 *
10062 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
10063 * and {@link #scrollTo(int, int)}.</p>
10064 *
10065 * @return true if the animation is played, false otherwise
10066 *
10067 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -070010068 * @see #scrollBy(int, int)
10069 * @see #scrollTo(int, int)
10070 * @see #isHorizontalScrollBarEnabled()
10071 * @see #isVerticalScrollBarEnabled()
10072 * @see #setHorizontalScrollBarEnabled(boolean)
10073 * @see #setVerticalScrollBarEnabled(boolean)
10074 */
10075 protected boolean awakenScrollBars() {
10076 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -070010077 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -070010078 }
10079
10080 /**
Adam Powell8568c3a2010-04-19 14:26:11 -070010081 * Trigger the scrollbars to draw.
10082 * This method differs from awakenScrollBars() only in its default duration.
10083 * initialAwakenScrollBars() will show the scroll bars for longer than
10084 * usual to give the user more of a chance to notice them.
10085 *
10086 * @return true if the animation is played, false otherwise.
10087 */
10088 private boolean initialAwakenScrollBars() {
10089 return mScrollCache != null &&
10090 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
10091 }
10092
10093 /**
Mike Cleronf116bf82009-09-27 19:14:12 -070010094 * <p>
10095 * Trigger the scrollbars to draw. When invoked this method starts an
10096 * animation to fade the scrollbars out after a fixed delay. If a subclass
10097 * provides animated scrolling, the start delay should equal the duration of
10098 * the scrolling animation.
10099 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010100 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010101 * <p>
10102 * The animation starts only if at least one of the scrollbars is enabled,
10103 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10104 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10105 * this method returns true, and false otherwise. If the animation is
10106 * started, this method calls {@link #invalidate()}; in that case the caller
10107 * should not call {@link #invalidate()}.
10108 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010109 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010110 * <p>
10111 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -070010112 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -070010113 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010114 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010115 * @param startDelay the delay, in milliseconds, after which the animation
10116 * should start; when the delay is 0, the animation starts
10117 * immediately
10118 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010119 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010120 * @see #scrollBy(int, int)
10121 * @see #scrollTo(int, int)
10122 * @see #isHorizontalScrollBarEnabled()
10123 * @see #isVerticalScrollBarEnabled()
10124 * @see #setHorizontalScrollBarEnabled(boolean)
10125 * @see #setVerticalScrollBarEnabled(boolean)
10126 */
10127 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -070010128 return awakenScrollBars(startDelay, true);
10129 }
Joe Malin32736f02011-01-19 16:14:20 -080010130
Mike Cleron290947b2009-09-29 18:34:32 -070010131 /**
10132 * <p>
10133 * Trigger the scrollbars to draw. When invoked this method starts an
10134 * animation to fade the scrollbars out after a fixed delay. If a subclass
10135 * provides animated scrolling, the start delay should equal the duration of
10136 * the scrolling animation.
10137 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010138 *
Mike Cleron290947b2009-09-29 18:34:32 -070010139 * <p>
10140 * The animation starts only if at least one of the scrollbars is enabled,
10141 * as specified by {@link #isHorizontalScrollBarEnabled()} and
10142 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
10143 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -080010144 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -070010145 * is set to true; in that case the caller
10146 * should not call {@link #invalidate()}.
10147 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010148 *
Mike Cleron290947b2009-09-29 18:34:32 -070010149 * <p>
10150 * This method should be invoked everytime a subclass directly updates the
10151 * scroll parameters.
10152 * </p>
Joe Malin32736f02011-01-19 16:14:20 -080010153 *
Mike Cleron290947b2009-09-29 18:34:32 -070010154 * @param startDelay the delay, in milliseconds, after which the animation
10155 * should start; when the delay is 0, the animation starts
10156 * immediately
Joe Malin32736f02011-01-19 16:14:20 -080010157 *
Mike Cleron290947b2009-09-29 18:34:32 -070010158 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -080010159 *
Mike Cleron290947b2009-09-29 18:34:32 -070010160 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -080010161 *
Mike Cleron290947b2009-09-29 18:34:32 -070010162 * @see #scrollBy(int, int)
10163 * @see #scrollTo(int, int)
10164 * @see #isHorizontalScrollBarEnabled()
10165 * @see #isVerticalScrollBarEnabled()
10166 * @see #setHorizontalScrollBarEnabled(boolean)
10167 * @see #setVerticalScrollBarEnabled(boolean)
10168 */
10169 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -070010170 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -080010171
Mike Cleronf116bf82009-09-27 19:14:12 -070010172 if (scrollCache == null || !scrollCache.fadeScrollBars) {
10173 return false;
10174 }
10175
10176 if (scrollCache.scrollBar == null) {
10177 scrollCache.scrollBar = new ScrollBarDrawable();
10178 }
10179
10180 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
10181
Mike Cleron290947b2009-09-29 18:34:32 -070010182 if (invalidate) {
10183 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -070010184 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -070010185 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010186
10187 if (scrollCache.state == ScrollabilityCache.OFF) {
10188 // FIXME: this is copied from WindowManagerService.
10189 // We should get this value from the system when it
10190 // is possible to do so.
10191 final int KEY_REPEAT_FIRST_DELAY = 750;
10192 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
10193 }
10194
10195 // Tell mScrollCache when we should start fading. This may
10196 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -070010197 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -070010198 scrollCache.fadeStartTime = fadeStartTime;
10199 scrollCache.state = ScrollabilityCache.ON;
10200
10201 // Schedule our fader to run, unscheduling any old ones first
10202 if (mAttachInfo != null) {
10203 mAttachInfo.mHandler.removeCallbacks(scrollCache);
10204 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
10205 }
10206
10207 return true;
10208 }
10209
10210 return false;
10211 }
10212
10213 /**
Chet Haaseaceafe62011-08-26 15:44:33 -070010214 * Do not invalidate views which are not visible and which are not running an animation. They
10215 * will not get drawn and they should not set dirty flags as if they will be drawn
10216 */
10217 private boolean skipInvalidate() {
10218 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
10219 (!(mParent instanceof ViewGroup) ||
10220 !((ViewGroup) mParent).isViewTransitioning(this));
10221 }
10222 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010223 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010224 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
10225 * in the future. This must be called from a UI thread. To call from a non-UI
10226 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 *
10228 * WARNING: This method is destructive to dirty.
10229 * @param dirty the rectangle representing the bounds of the dirty region
10230 */
10231 public void invalidate(Rect dirty) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010232 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010233 return;
10234 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010235 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10236 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10237 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10238 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10239 mPrivateFlags |= PFLAG_INVALIDATED;
10240 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 final ViewParent p = mParent;
10242 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010243 //noinspection PointlessBooleanExpression,ConstantConditions
10244 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10245 if (p != null && ai != null && ai.mHardwareAccelerated) {
10246 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010247 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010248 p.invalidateChild(this, null);
10249 return;
10250 }
Romain Guyaf636eb2010-12-09 17:47:21 -080010251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010252 if (p != null && ai != null) {
10253 final int scrollX = mScrollX;
10254 final int scrollY = mScrollY;
10255 final Rect r = ai.mTmpInvalRect;
10256 r.set(dirty.left - scrollX, dirty.top - scrollY,
10257 dirty.right - scrollX, dirty.bottom - scrollY);
10258 mParent.invalidateChild(this, r);
10259 }
10260 }
10261 }
10262
10263 /**
Joe Fernandez558459f2011-10-13 16:47:36 -070010264 * 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 -080010265 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -070010266 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
10267 * will be called at some point in the future. This must be called from
10268 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 * @param l the left position of the dirty region
10270 * @param t the top position of the dirty region
10271 * @param r the right position of the dirty region
10272 * @param b the bottom position of the dirty region
10273 */
10274 public void invalidate(int l, int t, int r, int b) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010275 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010276 return;
10277 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010278 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10279 (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID ||
10280 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED) {
10281 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
10282 mPrivateFlags |= PFLAG_INVALIDATED;
10283 mPrivateFlags |= PFLAG_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010284 final ViewParent p = mParent;
10285 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -080010286 //noinspection PointlessBooleanExpression,ConstantConditions
10287 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10288 if (p != null && ai != null && ai.mHardwareAccelerated) {
10289 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010290 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010291 p.invalidateChild(this, null);
10292 return;
10293 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 if (p != null && ai != null && l < r && t < b) {
10296 final int scrollX = mScrollX;
10297 final int scrollY = mScrollY;
10298 final Rect tmpr = ai.mTmpInvalRect;
10299 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
10300 p.invalidateChild(this, tmpr);
10301 }
10302 }
10303 }
10304
10305 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070010306 * Invalidate the whole view. If the view is visible,
10307 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
10308 * the future. This must be called from a UI thread. To call from a non-UI thread,
10309 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 */
10311 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -070010312 invalidate(true);
10313 }
Joe Malin32736f02011-01-19 16:14:20 -080010314
Chet Haaseed032702010-10-01 14:05:54 -070010315 /**
10316 * This is where the invalidate() work actually happens. A full invalidate()
10317 * causes the drawing cache to be invalidated, but this function can be called with
10318 * invalidateCache set to false to skip that invalidation step for cases that do not
10319 * need it (for example, a component that remains at the same dimensions with the same
10320 * content).
10321 *
10322 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
10323 * well. This is usually true for a full invalidate, but may be set to false if the
10324 * View's contents or dimensions have not changed.
10325 */
Romain Guy849d0a32011-02-01 17:20:48 -080010326 void invalidate(boolean invalidateCache) {
Chet Haaseaceafe62011-08-26 15:44:33 -070010327 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -070010328 return;
10329 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070010330 if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) ||
10331 (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) ||
10332 (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || isOpaque() != mLastIsOpaque) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010333 mLastIsOpaque = isOpaque();
Dianne Hackborn4702a852012-08-17 15:18:29 -070010334 mPrivateFlags &= ~PFLAG_DRAWN;
10335 mPrivateFlags |= PFLAG_DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -070010336 if (invalidateCache) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010337 mPrivateFlags |= PFLAG_INVALIDATED;
10338 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Chet Haaseed032702010-10-01 14:05:54 -070010339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -070010341 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -080010342 //noinspection PointlessBooleanExpression,ConstantConditions
10343 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
10344 if (p != null && ai != null && ai.mHardwareAccelerated) {
10345 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010346 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -080010347 p.invalidateChild(this, null);
10348 return;
10349 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -080010350 }
Michael Jurkaebefea42010-11-15 16:04:01 -080010351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010352 if (p != null && ai != null) {
10353 final Rect r = ai.mTmpInvalRect;
10354 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10355 // Don't call invalidate -- we don't want to internally scroll
10356 // our own bounds
10357 p.invalidateChild(this, r);
10358 }
10359 }
10360 }
10361
10362 /**
Chet Haase9d1992d2012-03-13 11:03:25 -070010363 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
10364 * set any flags or handle all of the cases handled by the default invalidation methods.
10365 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
10366 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
10367 * walk up the hierarchy, transforming the dirty rect as necessary.
10368 *
10369 * The method also handles normal invalidation logic if display list properties are not
10370 * being used in this view. The invalidateParent and forceRedraw flags are used by that
10371 * backup approach, to handle these cases used in the various property-setting methods.
10372 *
10373 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
10374 * are not being used in this view
10375 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
10376 * list properties are not being used in this view
10377 */
10378 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010379 if (mDisplayList == null || (mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -070010380 if (invalidateParent) {
10381 invalidateParentCaches();
10382 }
10383 if (forceRedraw) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010384 mPrivateFlags |= PFLAG_DRAWN; // force another invalidation with the new orientation
Chet Haase9d1992d2012-03-13 11:03:25 -070010385 }
10386 invalidate(false);
10387 } else {
10388 final AttachInfo ai = mAttachInfo;
10389 final ViewParent p = mParent;
10390 if (p != null && ai != null) {
10391 final Rect r = ai.mTmpInvalRect;
10392 r.set(0, 0, mRight - mLeft, mBottom - mTop);
10393 if (mParent instanceof ViewGroup) {
10394 ((ViewGroup) mParent).invalidateChildFast(this, r);
10395 } else {
10396 mParent.invalidateChild(this, r);
10397 }
10398 }
10399 }
10400 }
10401
10402 /**
10403 * Utility method to transform a given Rect by the current matrix of this view.
10404 */
10405 void transformRect(final Rect rect) {
10406 if (!getMatrix().isIdentity()) {
10407 RectF boundingRect = mAttachInfo.mTmpTransformRect;
10408 boundingRect.set(rect);
10409 getMatrix().mapRect(boundingRect);
10410 rect.set((int) (boundingRect.left - 0.5f),
10411 (int) (boundingRect.top - 0.5f),
10412 (int) (boundingRect.right + 0.5f),
10413 (int) (boundingRect.bottom + 0.5f));
10414 }
10415 }
10416
10417 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -080010418 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -080010419 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10420 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -080010421 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
10422 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -080010423 *
10424 * @hide
10425 */
Romain Guy0fd89bf2011-01-26 15:41:30 -080010426 protected void invalidateParentCaches() {
10427 if (mParent instanceof View) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010428 ((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010429 }
10430 }
Joe Malin32736f02011-01-19 16:14:20 -080010431
Romain Guy0fd89bf2011-01-26 15:41:30 -080010432 /**
10433 * Used to indicate that the parent of this view should be invalidated. This functionality
10434 * is used to force the parent to rebuild its display list (when hardware-accelerated),
10435 * which is necessary when various parent-managed properties of the view change, such as
10436 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
10437 * an invalidation event to the parent.
10438 *
10439 * @hide
10440 */
10441 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -080010442 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -080010443 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -080010444 }
10445 }
10446
10447 /**
Romain Guy24443ea2009-05-11 11:56:30 -070010448 * Indicates whether this View is opaque. An opaque View guarantees that it will
10449 * draw all the pixels overlapping its bounds using a fully opaque color.
10450 *
10451 * Subclasses of View should override this method whenever possible to indicate
10452 * whether an instance is opaque. Opaque Views are treated in a special way by
10453 * the View hierarchy, possibly allowing it to perform optimizations during
10454 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -070010455 *
Romain Guy24443ea2009-05-11 11:56:30 -070010456 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -070010457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010458 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -070010459 public boolean isOpaque() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010460 return (mPrivateFlags & PFLAG_OPAQUE_MASK) == PFLAG_OPAQUE_MASK &&
Romain Guyf8773082012-07-12 18:01:00 -070010461 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1.0f) >= 1.0f);
Romain Guy8f1344f52009-05-15 16:03:59 -070010462 }
10463
Adam Powell20232d02010-12-08 21:08:53 -080010464 /**
10465 * @hide
10466 */
10467 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -070010468 // Opaque if:
10469 // - Has a background
10470 // - Background is opaque
10471 // - Doesn't have scrollbars or scrollbars are inside overlay
10472
Philip Milne6c8ea062012-04-03 17:38:43 -070010473 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010474 mPrivateFlags |= PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010475 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010476 mPrivateFlags &= ~PFLAG_OPAQUE_BACKGROUND;
Romain Guy8f1344f52009-05-15 16:03:59 -070010477 }
10478
10479 final int flags = mViewFlags;
10480 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
10481 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010482 mPrivateFlags |= PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010483 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010484 mPrivateFlags &= ~PFLAG_OPAQUE_SCROLLBARS;
Romain Guy8f1344f52009-05-15 16:03:59 -070010485 }
10486 }
10487
10488 /**
10489 * @hide
10490 */
10491 protected boolean hasOpaqueScrollbars() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070010492 return (mPrivateFlags & PFLAG_OPAQUE_SCROLLBARS) == PFLAG_OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -070010493 }
10494
10495 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 * @return A handler associated with the thread running the View. This
10497 * handler can be used to pump events in the UI events queue.
10498 */
10499 public Handler getHandler() {
10500 if (mAttachInfo != null) {
10501 return mAttachInfo.mHandler;
10502 }
10503 return null;
10504 }
10505
10506 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080010507 * Gets the view root associated with the View.
10508 * @return The view root, or null if none.
10509 * @hide
10510 */
10511 public ViewRootImpl getViewRootImpl() {
10512 if (mAttachInfo != null) {
10513 return mAttachInfo.mViewRootImpl;
10514 }
10515 return null;
10516 }
10517
10518 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010519 * <p>Causes the Runnable to be added to the message queue.
10520 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010521 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 * @param action The Runnable that will be executed.
10523 *
10524 * @return Returns true if the Runnable was successfully placed in to the
10525 * message queue. Returns false on failure, usually because the
10526 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010527 *
10528 * @see #postDelayed
10529 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 */
10531 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010532 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010533 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010534 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010535 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010536 // Assume that post will succeed later
10537 ViewRootImpl.getRunQueue().post(action);
10538 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 }
10540
10541 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010542 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -070010544 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 * @param action The Runnable that will be executed.
10547 * @param delayMillis The delay (in milliseconds) until the Runnable
10548 * will be executed.
10549 *
10550 * @return true if the Runnable was successfully placed in to the
10551 * message queue. Returns false on failure, usually because the
10552 * looper processing the message queue is exiting. Note that a
10553 * result of true does not mean the Runnable will be processed --
10554 * if the looper is quit before the delivery time of the message
10555 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010556 *
10557 * @see #post
10558 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 */
10560 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010561 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010562 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010563 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 }
Jeff Browna175a5b2012-02-15 19:18:31 -080010565 // Assume that post will succeed later
10566 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10567 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010568 }
10569
10570 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010571 * <p>Causes the Runnable to execute on the next animation time step.
10572 * The runnable will be run on the user interface thread.</p>
10573 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010574 * @param action The Runnable that will be executed.
10575 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010576 * @see #postOnAnimationDelayed
10577 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010578 */
10579 public void postOnAnimation(Runnable action) {
10580 final AttachInfo attachInfo = mAttachInfo;
10581 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010582 attachInfo.mViewRootImpl.mChoreographer.postCallback(
10583 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010584 } else {
10585 // Assume that post will succeed later
10586 ViewRootImpl.getRunQueue().post(action);
10587 }
10588 }
10589
10590 /**
10591 * <p>Causes the Runnable to execute on the next animation time step,
10592 * after the specified amount of time elapses.
10593 * The runnable will be run on the user interface thread.</p>
10594 *
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010595 * @param action The Runnable that will be executed.
10596 * @param delayMillis The delay (in milliseconds) until the Runnable
10597 * will be executed.
10598 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010599 * @see #postOnAnimation
10600 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010601 */
10602 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
10603 final AttachInfo attachInfo = mAttachInfo;
10604 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010605 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10606 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010607 } else {
10608 // Assume that post will succeed later
10609 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10610 }
10611 }
10612
10613 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010614 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010615 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 * @param action The Runnable to remove from the message handling queue
10617 *
10618 * @return true if this view could ask the Handler to remove the Runnable,
10619 * false otherwise. When the returned value is true, the Runnable
10620 * may or may not have been actually removed from the message queue
10621 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010622 *
10623 * @see #post
10624 * @see #postDelayed
10625 * @see #postOnAnimation
10626 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 */
10628 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010629 if (action != null) {
10630 final AttachInfo attachInfo = mAttachInfo;
10631 if (attachInfo != null) {
10632 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010633 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10634 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010635 } else {
10636 // Assume that post will succeed later
10637 ViewRootImpl.getRunQueue().removeCallbacks(action);
10638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 return true;
10641 }
10642
10643 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010644 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10645 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 *
Romain Guye63a4f32011-08-11 11:33:31 -070010647 * <p>This method can be invoked from outside of the UI thread
10648 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010650 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010651 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 */
10653 public void postInvalidate() {
10654 postInvalidateDelayed(0);
10655 }
10656
10657 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010658 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10659 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010660 *
Romain Guye63a4f32011-08-11 11:33:31 -070010661 * <p>This method can be invoked from outside of the UI thread
10662 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 *
10664 * @param left The left coordinate of the rectangle to invalidate.
10665 * @param top The top coordinate of the rectangle to invalidate.
10666 * @param right The right coordinate of the rectangle to invalidate.
10667 * @param bottom The bottom coordinate of the rectangle to invalidate.
10668 *
10669 * @see #invalidate(int, int, int, int)
10670 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010671 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 */
10673 public void postInvalidate(int left, int top, int right, int bottom) {
10674 postInvalidateDelayed(0, left, top, right, bottom);
10675 }
10676
10677 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010678 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10679 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010680 *
Romain Guye63a4f32011-08-11 11:33:31 -070010681 * <p>This method can be invoked from outside of the UI thread
10682 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683 *
10684 * @param delayMilliseconds the duration in milliseconds to delay the
10685 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010686 *
10687 * @see #invalidate()
10688 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 */
10690 public void postInvalidateDelayed(long delayMilliseconds) {
10691 // We try only with the AttachInfo because there's no point in invalidating
10692 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010693 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010694 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010695 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 }
10697 }
10698
10699 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010700 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10701 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010702 *
Romain Guye63a4f32011-08-11 11:33:31 -070010703 * <p>This method can be invoked from outside of the UI thread
10704 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010705 *
10706 * @param delayMilliseconds the duration in milliseconds to delay the
10707 * invalidation by
10708 * @param left The left coordinate of the rectangle to invalidate.
10709 * @param top The top coordinate of the rectangle to invalidate.
10710 * @param right The right coordinate of the rectangle to invalidate.
10711 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010712 *
10713 * @see #invalidate(int, int, int, int)
10714 * @see #invalidate(Rect)
10715 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010716 */
10717 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10718 int right, int bottom) {
10719
10720 // We try only with the AttachInfo because there's no point in invalidating
10721 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010722 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010723 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010724 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10725 info.target = this;
10726 info.left = left;
10727 info.top = top;
10728 info.right = right;
10729 info.bottom = bottom;
10730
Jeff Browna175a5b2012-02-15 19:18:31 -080010731 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 }
10733 }
10734
10735 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010736 * <p>Cause an invalidate to happen on the next animation time step, typically the
10737 * next display frame.</p>
10738 *
10739 * <p>This method can be invoked from outside of the UI thread
10740 * only when this View is attached to a window.</p>
10741 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010742 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010743 */
10744 public void postInvalidateOnAnimation() {
10745 // We try only with the AttachInfo because there's no point in invalidating
10746 // if we are not attached to our window
10747 final AttachInfo attachInfo = mAttachInfo;
10748 if (attachInfo != null) {
10749 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10750 }
10751 }
10752
10753 /**
10754 * <p>Cause an invalidate of the specified area to happen on the next animation
10755 * time step, typically the next display frame.</p>
10756 *
10757 * <p>This method can be invoked from outside of the UI thread
10758 * only when this View is attached to a window.</p>
10759 *
10760 * @param left The left coordinate of the rectangle to invalidate.
10761 * @param top The top coordinate of the rectangle to invalidate.
10762 * @param right The right coordinate of the rectangle to invalidate.
10763 * @param bottom The bottom coordinate of the rectangle to invalidate.
10764 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010765 * @see #invalidate(int, int, int, int)
10766 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010767 */
10768 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10769 // We try only with the AttachInfo because there's no point in invalidating
10770 // if we are not attached to our window
10771 final AttachInfo attachInfo = mAttachInfo;
10772 if (attachInfo != null) {
10773 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10774 info.target = this;
10775 info.left = left;
10776 info.top = top;
10777 info.right = right;
10778 info.bottom = bottom;
10779
10780 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10781 }
10782 }
10783
10784 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010785 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10786 * This event is sent at most once every
10787 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10788 */
10789 private void postSendViewScrolledAccessibilityEventCallback() {
10790 if (mSendViewScrolledAccessibilityEvent == null) {
10791 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10792 }
10793 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10794 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10795 postDelayed(mSendViewScrolledAccessibilityEvent,
10796 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10797 }
10798 }
10799
10800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 * Called by a parent to request that a child update its values for mScrollX
10802 * and mScrollY if necessary. This will typically be done if the child is
10803 * animating a scroll using a {@link android.widget.Scroller Scroller}
10804 * object.
10805 */
10806 public void computeScroll() {
10807 }
10808
10809 /**
10810 * <p>Indicate whether the horizontal edges are faded when the view is
10811 * scrolled horizontally.</p>
10812 *
10813 * @return true if the horizontal edges should are faded on scroll, false
10814 * otherwise
10815 *
10816 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010817 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010818 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 */
10820 public boolean isHorizontalFadingEdgeEnabled() {
10821 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10822 }
10823
10824 /**
10825 * <p>Define whether the horizontal edges should be faded when this view
10826 * is scrolled horizontally.</p>
10827 *
10828 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10829 * be faded when the view is scrolled
10830 * horizontally
10831 *
10832 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010833 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010834 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 */
10836 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10837 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10838 if (horizontalFadingEdgeEnabled) {
10839 initScrollCache();
10840 }
10841
10842 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10843 }
10844 }
10845
10846 /**
10847 * <p>Indicate whether the vertical edges are faded when the view is
10848 * scrolled horizontally.</p>
10849 *
10850 * @return true if the vertical edges should are faded on scroll, false
10851 * otherwise
10852 *
10853 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010854 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010855 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 */
10857 public boolean isVerticalFadingEdgeEnabled() {
10858 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10859 }
10860
10861 /**
10862 * <p>Define whether the vertical edges should be faded when this view
10863 * is scrolled vertically.</p>
10864 *
10865 * @param verticalFadingEdgeEnabled true if the vertical edges should
10866 * be faded when the view is scrolled
10867 * vertically
10868 *
10869 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010870 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010871 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010872 */
10873 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10874 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10875 if (verticalFadingEdgeEnabled) {
10876 initScrollCache();
10877 }
10878
10879 mViewFlags ^= FADING_EDGE_VERTICAL;
10880 }
10881 }
10882
10883 /**
10884 * Returns the strength, or intensity, of the top faded edge. The strength is
10885 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10886 * returns 0.0 or 1.0 but no value in between.
10887 *
10888 * Subclasses should override this method to provide a smoother fade transition
10889 * when scrolling occurs.
10890 *
10891 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10892 */
10893 protected float getTopFadingEdgeStrength() {
10894 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10895 }
10896
10897 /**
10898 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10899 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10900 * returns 0.0 or 1.0 but no value in between.
10901 *
10902 * Subclasses should override this method to provide a smoother fade transition
10903 * when scrolling occurs.
10904 *
10905 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10906 */
10907 protected float getBottomFadingEdgeStrength() {
10908 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10909 computeVerticalScrollRange() ? 1.0f : 0.0f;
10910 }
10911
10912 /**
10913 * Returns the strength, or intensity, of the left faded edge. The strength is
10914 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10915 * returns 0.0 or 1.0 but no value in between.
10916 *
10917 * Subclasses should override this method to provide a smoother fade transition
10918 * when scrolling occurs.
10919 *
10920 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10921 */
10922 protected float getLeftFadingEdgeStrength() {
10923 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10924 }
10925
10926 /**
10927 * Returns the strength, or intensity, of the right faded edge. The strength is
10928 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10929 * returns 0.0 or 1.0 but no value in between.
10930 *
10931 * Subclasses should override this method to provide a smoother fade transition
10932 * when scrolling occurs.
10933 *
10934 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10935 */
10936 protected float getRightFadingEdgeStrength() {
10937 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10938 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10939 }
10940
10941 /**
10942 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10943 * scrollbar is not drawn by default.</p>
10944 *
10945 * @return true if the horizontal scrollbar should be painted, false
10946 * otherwise
10947 *
10948 * @see #setHorizontalScrollBarEnabled(boolean)
10949 */
10950 public boolean isHorizontalScrollBarEnabled() {
10951 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10952 }
10953
10954 /**
10955 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10956 * scrollbar is not drawn by default.</p>
10957 *
10958 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10959 * be painted
10960 *
10961 * @see #isHorizontalScrollBarEnabled()
10962 */
10963 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10964 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10965 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010966 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010967 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 }
10969 }
10970
10971 /**
10972 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10973 * scrollbar is not drawn by default.</p>
10974 *
10975 * @return true if the vertical scrollbar should be painted, false
10976 * otherwise
10977 *
10978 * @see #setVerticalScrollBarEnabled(boolean)
10979 */
10980 public boolean isVerticalScrollBarEnabled() {
10981 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10982 }
10983
10984 /**
10985 * <p>Define whether the vertical scrollbar should be drawn or not. The
10986 * scrollbar is not drawn by default.</p>
10987 *
10988 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10989 * be painted
10990 *
10991 * @see #isVerticalScrollBarEnabled()
10992 */
10993 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10994 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10995 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010996 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010997 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 }
10999 }
11000
Adam Powell20232d02010-12-08 21:08:53 -080011001 /**
11002 * @hide
11003 */
11004 protected void recomputePadding() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070011005 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 }
Joe Malin32736f02011-01-19 16:14:20 -080011007
Mike Cleronfe81d382009-09-28 14:22:16 -070011008 /**
11009 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080011010 *
Mike Cleronfe81d382009-09-28 14:22:16 -070011011 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080011012 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011013 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070011014 */
11015 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
11016 initScrollCache();
11017 final ScrollabilityCache scrollabilityCache = mScrollCache;
11018 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011019 if (fadeScrollbars) {
11020 scrollabilityCache.state = ScrollabilityCache.OFF;
11021 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070011022 scrollabilityCache.state = ScrollabilityCache.ON;
11023 }
11024 }
Joe Malin32736f02011-01-19 16:14:20 -080011025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 /**
Joe Malin32736f02011-01-19 16:14:20 -080011027 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011028 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080011029 *
Mike Cleron52f0a642009-09-28 18:21:37 -070011030 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070011031 *
11032 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070011033 */
11034 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080011035 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070011036 }
Joe Malin32736f02011-01-19 16:14:20 -080011037
Mike Cleron52f0a642009-09-28 18:21:37 -070011038 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070011039 *
11040 * Returns the delay before scrollbars fade.
11041 *
11042 * @return the delay before scrollbars fade
11043 *
11044 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11045 */
11046 public int getScrollBarDefaultDelayBeforeFade() {
11047 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
11048 mScrollCache.scrollBarDefaultDelayBeforeFade;
11049 }
11050
11051 /**
11052 * Define the delay before scrollbars fade.
11053 *
11054 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
11055 *
11056 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
11057 */
11058 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
11059 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
11060 }
11061
11062 /**
11063 *
11064 * Returns the scrollbar fade duration.
11065 *
11066 * @return the scrollbar fade duration
11067 *
11068 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11069 */
11070 public int getScrollBarFadeDuration() {
11071 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
11072 mScrollCache.scrollBarFadeDuration;
11073 }
11074
11075 /**
11076 * Define the scrollbar fade duration.
11077 *
11078 * @param scrollBarFadeDuration - the scrollbar fade duration
11079 *
11080 * @attr ref android.R.styleable#View_scrollbarFadeDuration
11081 */
11082 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
11083 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
11084 }
11085
11086 /**
11087 *
11088 * Returns the scrollbar size.
11089 *
11090 * @return the scrollbar size
11091 *
11092 * @attr ref android.R.styleable#View_scrollbarSize
11093 */
11094 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070011095 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070011096 mScrollCache.scrollBarSize;
11097 }
11098
11099 /**
11100 * Define the scrollbar size.
11101 *
11102 * @param scrollBarSize - the scrollbar size
11103 *
11104 * @attr ref android.R.styleable#View_scrollbarSize
11105 */
11106 public void setScrollBarSize(int scrollBarSize) {
11107 getScrollCache().scrollBarSize = scrollBarSize;
11108 }
11109
11110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
11112 * inset. When inset, they add to the padding of the view. And the scrollbars
11113 * can be drawn inside the padding area or on the edge of the view. For example,
11114 * if a view has a background drawable and you want to draw the scrollbars
11115 * inside the padding specified by the drawable, you can use
11116 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
11117 * appear at the edge of the view, ignoring the padding, then you can use
11118 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
11119 * @param style the style of the scrollbars. Should be one of
11120 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
11121 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
11122 * @see #SCROLLBARS_INSIDE_OVERLAY
11123 * @see #SCROLLBARS_INSIDE_INSET
11124 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11125 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011126 *
11127 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 */
11129 public void setScrollBarStyle(int style) {
11130 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
11131 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070011132 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011133 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 }
11135 }
11136
11137 /**
11138 * <p>Returns the current scrollbar style.</p>
11139 * @return the current scrollbar style
11140 * @see #SCROLLBARS_INSIDE_OVERLAY
11141 * @see #SCROLLBARS_INSIDE_INSET
11142 * @see #SCROLLBARS_OUTSIDE_OVERLAY
11143 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070011144 *
11145 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070011147 @ViewDebug.ExportedProperty(mapping = {
11148 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
11149 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
11150 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
11151 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
11152 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011153 public int getScrollBarStyle() {
11154 return mViewFlags & SCROLLBARS_STYLE_MASK;
11155 }
11156
11157 /**
11158 * <p>Compute the horizontal range that the horizontal scrollbar
11159 * represents.</p>
11160 *
11161 * <p>The range is expressed in arbitrary units that must be the same as the
11162 * units used by {@link #computeHorizontalScrollExtent()} and
11163 * {@link #computeHorizontalScrollOffset()}.</p>
11164 *
11165 * <p>The default range is the drawing width of this view.</p>
11166 *
11167 * @return the total horizontal range represented by the horizontal
11168 * scrollbar
11169 *
11170 * @see #computeHorizontalScrollExtent()
11171 * @see #computeHorizontalScrollOffset()
11172 * @see android.widget.ScrollBarDrawable
11173 */
11174 protected int computeHorizontalScrollRange() {
11175 return getWidth();
11176 }
11177
11178 /**
11179 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
11180 * within the horizontal range. This value is used to compute the position
11181 * of the thumb within the scrollbar's track.</p>
11182 *
11183 * <p>The range is expressed in arbitrary units that must be the same as the
11184 * units used by {@link #computeHorizontalScrollRange()} and
11185 * {@link #computeHorizontalScrollExtent()}.</p>
11186 *
11187 * <p>The default offset is the scroll offset of this view.</p>
11188 *
11189 * @return the horizontal offset of the scrollbar's thumb
11190 *
11191 * @see #computeHorizontalScrollRange()
11192 * @see #computeHorizontalScrollExtent()
11193 * @see android.widget.ScrollBarDrawable
11194 */
11195 protected int computeHorizontalScrollOffset() {
11196 return mScrollX;
11197 }
11198
11199 /**
11200 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
11201 * within the horizontal range. This value is used to compute the length
11202 * of the thumb within the scrollbar's track.</p>
11203 *
11204 * <p>The range is expressed in arbitrary units that must be the same as the
11205 * units used by {@link #computeHorizontalScrollRange()} and
11206 * {@link #computeHorizontalScrollOffset()}.</p>
11207 *
11208 * <p>The default extent is the drawing width of this view.</p>
11209 *
11210 * @return the horizontal extent of the scrollbar's thumb
11211 *
11212 * @see #computeHorizontalScrollRange()
11213 * @see #computeHorizontalScrollOffset()
11214 * @see android.widget.ScrollBarDrawable
11215 */
11216 protected int computeHorizontalScrollExtent() {
11217 return getWidth();
11218 }
11219
11220 /**
11221 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
11222 *
11223 * <p>The range is expressed in arbitrary units that must be the same as the
11224 * units used by {@link #computeVerticalScrollExtent()} and
11225 * {@link #computeVerticalScrollOffset()}.</p>
11226 *
11227 * @return the total vertical range represented by the vertical scrollbar
11228 *
11229 * <p>The default range is the drawing height of this view.</p>
11230 *
11231 * @see #computeVerticalScrollExtent()
11232 * @see #computeVerticalScrollOffset()
11233 * @see android.widget.ScrollBarDrawable
11234 */
11235 protected int computeVerticalScrollRange() {
11236 return getHeight();
11237 }
11238
11239 /**
11240 * <p>Compute the vertical offset of the vertical scrollbar's thumb
11241 * within the horizontal range. This value is used to compute the position
11242 * of the thumb within the scrollbar's track.</p>
11243 *
11244 * <p>The range is expressed in arbitrary units that must be the same as the
11245 * units used by {@link #computeVerticalScrollRange()} and
11246 * {@link #computeVerticalScrollExtent()}.</p>
11247 *
11248 * <p>The default offset is the scroll offset of this view.</p>
11249 *
11250 * @return the vertical offset of the scrollbar's thumb
11251 *
11252 * @see #computeVerticalScrollRange()
11253 * @see #computeVerticalScrollExtent()
11254 * @see android.widget.ScrollBarDrawable
11255 */
11256 protected int computeVerticalScrollOffset() {
11257 return mScrollY;
11258 }
11259
11260 /**
11261 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
11262 * within the vertical range. This value is used to compute the length
11263 * of the thumb within the scrollbar's track.</p>
11264 *
11265 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080011266 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 * {@link #computeVerticalScrollOffset()}.</p>
11268 *
11269 * <p>The default extent is the drawing height of this view.</p>
11270 *
11271 * @return the vertical extent of the scrollbar's thumb
11272 *
11273 * @see #computeVerticalScrollRange()
11274 * @see #computeVerticalScrollOffset()
11275 * @see android.widget.ScrollBarDrawable
11276 */
11277 protected int computeVerticalScrollExtent() {
11278 return getHeight();
11279 }
11280
11281 /**
Adam Powell69159442011-06-13 17:53:06 -070011282 * Check if this view can be scrolled horizontally in a certain direction.
11283 *
11284 * @param direction Negative to check scrolling left, positive to check scrolling right.
11285 * @return true if this view can be scrolled in the specified direction, false otherwise.
11286 */
11287 public boolean canScrollHorizontally(int direction) {
11288 final int offset = computeHorizontalScrollOffset();
11289 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
11290 if (range == 0) return false;
11291 if (direction < 0) {
11292 return offset > 0;
11293 } else {
11294 return offset < range - 1;
11295 }
11296 }
11297
11298 /**
11299 * Check if this view can be scrolled vertically in a certain direction.
11300 *
11301 * @param direction Negative to check scrolling up, positive to check scrolling down.
11302 * @return true if this view can be scrolled in the specified direction, false otherwise.
11303 */
11304 public boolean canScrollVertically(int direction) {
11305 final int offset = computeVerticalScrollOffset();
11306 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
11307 if (range == 0) return false;
11308 if (direction < 0) {
11309 return offset > 0;
11310 } else {
11311 return offset < range - 1;
11312 }
11313 }
11314
11315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
11317 * scrollbars are painted only if they have been awakened first.</p>
11318 *
11319 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080011320 *
Mike Cleronf116bf82009-09-27 19:14:12 -070011321 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080011323 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 // scrollbars are drawn only when the animation is running
11325 final ScrollabilityCache cache = mScrollCache;
11326 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080011327
Mike Cleronf116bf82009-09-27 19:14:12 -070011328 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080011329
Mike Cleronf116bf82009-09-27 19:14:12 -070011330 if (state == ScrollabilityCache.OFF) {
11331 return;
11332 }
Joe Malin32736f02011-01-19 16:14:20 -080011333
Mike Cleronf116bf82009-09-27 19:14:12 -070011334 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080011335
Mike Cleronf116bf82009-09-27 19:14:12 -070011336 if (state == ScrollabilityCache.FADING) {
11337 // We're fading -- get our fade interpolation
11338 if (cache.interpolatorValues == null) {
11339 cache.interpolatorValues = new float[1];
11340 }
Joe Malin32736f02011-01-19 16:14:20 -080011341
Mike Cleronf116bf82009-09-27 19:14:12 -070011342 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080011343
Mike Cleronf116bf82009-09-27 19:14:12 -070011344 // Stops the animation if we're done
11345 if (cache.scrollBarInterpolator.timeToValues(values) ==
11346 Interpolator.Result.FREEZE_END) {
11347 cache.state = ScrollabilityCache.OFF;
11348 } else {
11349 cache.scrollBar.setAlpha(Math.round(values[0]));
11350 }
Joe Malin32736f02011-01-19 16:14:20 -080011351
11352 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070011353 // drawing. We only want this when we're fading so that
11354 // we prevent excessive redraws
11355 invalidate = true;
11356 } else {
11357 // We're just on -- but we may have been fading before so
11358 // reset alpha
11359 cache.scrollBar.setAlpha(255);
11360 }
11361
Joe Malin32736f02011-01-19 16:14:20 -080011362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 final int viewFlags = mViewFlags;
11364
11365 final boolean drawHorizontalScrollBar =
11366 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
11367 final boolean drawVerticalScrollBar =
11368 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
11369 && !isVerticalScrollBarHidden();
11370
11371 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
11372 final int width = mRight - mLeft;
11373 final int height = mBottom - mTop;
11374
11375 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376
Mike Reede8853fc2009-09-04 14:01:48 -040011377 final int scrollX = mScrollX;
11378 final int scrollY = mScrollY;
11379 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
11380
Mike Cleronf116bf82009-09-27 19:14:12 -070011381 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080011382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011384 int size = scrollBar.getSize(false);
11385 if (size <= 0) {
11386 size = cache.scrollBarSize;
11387 }
11388
Mike Cleronf116bf82009-09-27 19:14:12 -070011389 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040011390 computeHorizontalScrollOffset(),
11391 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040011392 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070011393 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080011394 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070011395 left = scrollX + (mPaddingLeft & inside);
11396 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
11397 bottom = top + size;
11398 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
11399 if (invalidate) {
11400 invalidate(left, top, right, bottom);
11401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011402 }
11403
11404 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080011405 int size = scrollBar.getSize(true);
11406 if (size <= 0) {
11407 size = cache.scrollBarSize;
11408 }
11409
Mike Reede8853fc2009-09-04 14:01:48 -040011410 scrollBar.setParameters(computeVerticalScrollRange(),
11411 computeVerticalScrollOffset(),
11412 computeVerticalScrollExtent(), true);
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070011413 int verticalScrollbarPosition = mVerticalScrollbarPosition;
11414 if (verticalScrollbarPosition == SCROLLBAR_POSITION_DEFAULT) {
11415 verticalScrollbarPosition = isLayoutRtl() ?
11416 SCROLLBAR_POSITION_LEFT : SCROLLBAR_POSITION_RIGHT;
11417 }
11418 switch (verticalScrollbarPosition) {
Adam Powell20232d02010-12-08 21:08:53 -080011419 default:
Adam Powell20232d02010-12-08 21:08:53 -080011420 case SCROLLBAR_POSITION_RIGHT:
11421 left = scrollX + width - size - (mUserPaddingRight & inside);
11422 break;
11423 case SCROLLBAR_POSITION_LEFT:
11424 left = scrollX + (mUserPaddingLeft & inside);
11425 break;
11426 }
Mike Cleronf116bf82009-09-27 19:14:12 -070011427 top = scrollY + (mPaddingTop & inside);
11428 right = left + size;
11429 bottom = scrollY + height - (mUserPaddingBottom & inside);
11430 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
11431 if (invalidate) {
11432 invalidate(left, top, right, bottom);
11433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011434 }
11435 }
11436 }
11437 }
Romain Guy8506ab42009-06-11 17:35:47 -070011438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011440 * 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 -080011441 * FastScroller is visible.
11442 * @return whether to temporarily hide the vertical scrollbar
11443 * @hide
11444 */
11445 protected boolean isVerticalScrollBarHidden() {
11446 return false;
11447 }
11448
11449 /**
11450 * <p>Draw the horizontal scrollbar if
11451 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
11452 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011453 * @param canvas the canvas on which to draw the scrollbar
11454 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011455 *
11456 * @see #isHorizontalScrollBarEnabled()
11457 * @see #computeHorizontalScrollRange()
11458 * @see #computeHorizontalScrollExtent()
11459 * @see #computeHorizontalScrollOffset()
11460 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070011461 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011462 */
Romain Guy8fb95422010-08-17 18:38:51 -070011463 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
11464 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011465 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011466 scrollBar.draw(canvas);
11467 }
Mike Reede8853fc2009-09-04 14:01:48 -040011468
Mike Reed4d6fe5f2009-09-03 13:29:05 -040011469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011470 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
11471 * returns true.</p>
11472 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011473 * @param canvas the canvas on which to draw the scrollbar
11474 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011475 *
11476 * @see #isVerticalScrollBarEnabled()
11477 * @see #computeVerticalScrollRange()
11478 * @see #computeVerticalScrollExtent()
11479 * @see #computeVerticalScrollOffset()
11480 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040011481 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011482 */
Romain Guy8fb95422010-08-17 18:38:51 -070011483 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
11484 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040011485 scrollBar.setBounds(l, t, r, b);
11486 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011487 }
11488
11489 /**
11490 * Implement this to do your drawing.
11491 *
11492 * @param canvas the canvas on which the background will be drawn
11493 */
11494 protected void onDraw(Canvas canvas) {
11495 }
11496
11497 /*
11498 * Caller is responsible for calling requestLayout if necessary.
11499 * (This allows addViewInLayout to not request a new layout.)
11500 */
11501 void assignParent(ViewParent parent) {
11502 if (mParent == null) {
11503 mParent = parent;
11504 } else if (parent == null) {
11505 mParent = null;
11506 } else {
11507 throw new RuntimeException("view " + this + " being added, but"
11508 + " it already has a parent");
11509 }
11510 }
11511
11512 /**
11513 * This is called when the view is attached to a window. At this point it
11514 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070011515 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
11516 * however it may be called any time before the first onDraw -- including
11517 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 *
11519 * @see #onDetachedFromWindow()
11520 */
11521 protected void onAttachedToWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011522 if ((mPrivateFlags & PFLAG_REQUEST_TRANSPARENT_REGIONS) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 mParent.requestTransparentRegion(this);
11524 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011525
Dianne Hackborn4702a852012-08-17 15:18:29 -070011526 if ((mPrivateFlags & PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
Adam Powell8568c3a2010-04-19 14:26:11 -070011527 initialAwakenScrollBars();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011528 mPrivateFlags &= ~PFLAG_AWAKEN_SCROLL_BARS_ON_ATTACH;
Adam Powell8568c3a2010-04-19 14:26:11 -070011529 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011530
Chet Haasea9b61ac2010-12-20 07:40:25 -080011531 jumpDrawablesToCurrentState();
Romain Guy2a0f2282012-05-08 14:43:12 -070011532
Svetoslav Ganov42138042012-03-20 11:51:39 -070011533 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070011534 if (isFocused()) {
11535 InputMethodManager imm = InputMethodManager.peekInstance();
11536 imm.focusIn(this);
11537 }
Romain Guy2a0f2282012-05-08 14:43:12 -070011538
11539 if (mAttachInfo != null && mDisplayList != null) {
11540 mAttachInfo.mViewRootImpl.dequeueDisplayList(mDisplayList);
11541 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011542 }
Cibu Johny86666632010-02-22 13:01:02 -080011543
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011544 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011545 * Resolve all RTL related properties.
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011546 *
11547 * @hide
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011548 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011549 public void resolveRtlPropertiesIfNeeded() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011550 if (!needRtlPropertiesResolution()) return;
11551
11552 // Order is important here: LayoutDirection MUST be resolved first
11553 if (!isLayoutDirectionResolved()) {
11554 resolveLayoutDirection();
11555 resolveLayoutParams();
11556 }
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011557 // ... then we can resolve the others properties depending on the resolved LayoutDirection.
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011558 if (!isTextDirectionResolved()) {
11559 resolveTextDirection();
11560 }
11561 if (!isTextAlignmentResolved()) {
11562 resolveTextAlignment();
11563 }
11564 if (!isPaddingResolved()) {
11565 resolvePadding();
11566 }
11567 if (!isDrawablesResolved()) {
11568 resolveDrawables();
11569 }
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011570 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011571 }
11572
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070011573 /**
11574 * Reset resolution of all RTL related properties.
11575 *
11576 * @hide
11577 */
11578 public void resetRtlProperties() {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011579 resetResolvedLayoutDirection();
11580 resetResolvedTextDirection();
11581 resetResolvedTextAlignment();
11582 resetResolvedPadding();
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011583 resetResolvedDrawables();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011584 }
11585
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011586 /**
Romain Guybb9908b2012-03-08 11:14:07 -080011587 * @see #onScreenStateChanged(int)
11588 */
11589 void dispatchScreenStateChanged(int screenState) {
11590 onScreenStateChanged(screenState);
11591 }
11592
11593 /**
11594 * This method is called whenever the state of the screen this view is
11595 * attached to changes. A state change will usually occurs when the screen
11596 * turns on or off (whether it happens automatically or the user does it
11597 * manually.)
11598 *
11599 * @param screenState The new state of the screen. Can be either
11600 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
11601 */
11602 public void onScreenStateChanged(int screenState) {
11603 }
11604
11605 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011606 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
11607 */
11608 private boolean hasRtlSupport() {
11609 return mContext.getApplicationInfo().hasRtlSupport();
11610 }
11611
11612 /**
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011613 * Return true if we are in RTL compatibility mode (either before Jelly Bean MR1 or
11614 * RTL not supported)
11615 */
11616 private boolean isRtlCompatibilityMode() {
11617 final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
11618 return targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport();
11619 }
11620
11621 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011622 * @return true if RTL properties need resolution.
11623 */
11624 private boolean needRtlPropertiesResolution() {
11625 return (mPrivateFlags2 & ALL_RTL_PROPERTIES_RESOLVED) != ALL_RTL_PROPERTIES_RESOLVED;
11626 }
11627
11628 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011629 * Called when any RTL property (layout direction or text direction or text alignment) has
11630 * been changed.
11631 *
11632 * Subclasses need to override this method to take care of cached information that depends on the
11633 * resolved layout direction, or to inform child views that inherit their layout direction.
11634 *
11635 * The default implementation does nothing.
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011636 *
11637 * @param layoutDirection the direction of the layout
11638 *
11639 * @see #LAYOUT_DIRECTION_LTR
11640 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011641 */
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011642 public void onRtlPropertiesChanged(int layoutDirection) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011643 }
11644
11645 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011646 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
11647 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011648 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011649 * @return true if resolution has been done, false otherwise.
11650 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011651 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070011652 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011653 public boolean resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011654 // Clear any previous layout direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070011655 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011656
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011657 if (hasRtlSupport()) {
11658 // Set resolved depending on layout direction
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011659 switch ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_MASK) >>
11660 PFLAG2_LAYOUT_DIRECTION_MASK_SHIFT) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011661 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011662 // We cannot resolve yet. LTR is by default and let the resolution happen again
11663 // later to get the correct resolved value
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011664 if (!canResolveLayoutDirection()) return false;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011665
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011666 View parent = ((View) mParent);
11667 // Parent has not yet resolved, LTR is still the default
11668 if (!parent.isLayoutDirectionResolved()) return false;
Fabrice Di Megliob93911f2012-06-26 19:43:15 -070011669
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011670 if (parent.getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011671 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011672 }
11673 break;
11674 case LAYOUT_DIRECTION_RTL:
Dianne Hackborn4702a852012-08-17 15:18:29 -070011675 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011676 break;
11677 case LAYOUT_DIRECTION_LOCALE:
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011678 if((LAYOUT_DIRECTION_RTL ==
11679 TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()))) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011680 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011681 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011682 break;
11683 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011684 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070011685 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011686 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011687
11688 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070011689 mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011690 return true;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011691 }
11692
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011693 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011694 * Check if layout direction resolution can be done.
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011695 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011696 * @return true if layout direction resolution can be done otherwise return false.
11697 *
11698 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011699 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011700 public boolean canResolveLayoutDirection() {
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011701 switch (getRawLayoutDirection()) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011702 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070011703 return (mParent != null) && (mParent instanceof ViewGroup) &&
11704 ((ViewGroup) mParent).canResolveLayoutDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011705 default:
11706 return true;
11707 }
11708 }
11709
11710 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011711 * Reset the resolved layout direction. Layout direction will be resolved during a call to
11712 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011713 *
11714 * @hide
11715 */
11716 public void resetResolvedLayoutDirection() {
11717 // Reset the current resolved bits
11718 mPrivateFlags2 &= ~PFLAG2_LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011719 }
11720
11721 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011722 * @return true if the layout direction is inherited.
11723 *
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011724 * @hide
11725 */
11726 public boolean isLayoutDirectionInherited() {
11727 return (getRawLayoutDirection() == LAYOUT_DIRECTION_INHERIT);
11728 }
11729
11730 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011731 * @return true if layout direction has been resolved.
11732 */
11733 private boolean isLayoutDirectionResolved() {
11734 return (mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED) == PFLAG2_LAYOUT_DIRECTION_RESOLVED;
11735 }
11736
11737 /**
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011738 * Return if padding has been resolved
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011739 *
11740 * @hide
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011741 */
11742 boolean isPaddingResolved() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070011743 return (mPrivateFlags2 & PFLAG2_PADDING_RESOLVED) == PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011744 }
11745
11746 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011747 * Resolve padding depending on layout direction.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011748 *
11749 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011750 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011751 public void resolvePadding() {
Fabrice Di Megliobbd10292012-09-27 14:08:49 -070011752 if (!isRtlCompatibilityMode()) {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011753 // Post Jelly Bean MR1 case: we need to take the resolved layout direction into account.
11754 // If start / end padding are defined, they will be resolved (hence overriding) to
11755 // left / right or right / left depending on the resolved layout direction.
11756 // If start / end padding are not defined, use the left / right ones.
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070011757 int resolvedLayoutDirection = getLayoutDirection();
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011758 // Set user padding to initial values ...
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070011759 mUserPaddingLeft = mUserPaddingLeftInitial;
11760 mUserPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070011761 // ... then resolve it.
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070011762 switch (resolvedLayoutDirection) {
11763 case LAYOUT_DIRECTION_RTL:
11764 if (mUserPaddingStart != UNDEFINED_PADDING) {
11765 mUserPaddingRight = mUserPaddingStart;
11766 }
11767 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11768 mUserPaddingLeft = mUserPaddingEnd;
11769 }
11770 break;
11771 case LAYOUT_DIRECTION_LTR:
11772 default:
11773 if (mUserPaddingStart != UNDEFINED_PADDING) {
11774 mUserPaddingLeft = mUserPaddingStart;
11775 }
11776 if (mUserPaddingEnd != UNDEFINED_PADDING) {
11777 mUserPaddingRight = mUserPaddingEnd;
11778 }
11779 }
11780
11781 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11782
11783 internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight,
11784 mUserPaddingBottom);
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011785 onRtlPropertiesChanged(resolvedLayoutDirection);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011786 }
11787
Dianne Hackborn4702a852012-08-17 15:18:29 -070011788 mPrivateFlags2 |= PFLAG2_PADDING_RESOLVED;
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011789 }
11790
11791 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011792 * Reset the resolved layout direction.
11793 *
11794 * @hide
11795 */
11796 public void resetResolvedPadding() {
11797 mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;
11798 }
11799
11800 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011801 * This is called when the view is detached from a window. At this point it
11802 * no longer has a surface for drawing.
11803 *
11804 * @see #onAttachedToWindow()
11805 */
11806 protected void onDetachedFromWindow() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011807 mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011808
Romain Guya440b002010-02-24 15:57:54 -080011809 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011810 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011811 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011812 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011814 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011815
Romain Guya998dff2012-03-23 18:58:36 -070011816 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011817
11818 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011819 if (mDisplayList != null) {
Romain Guy2a0f2282012-05-08 14:43:12 -070011820 mAttachInfo.mViewRootImpl.enqueueDisplayList(mDisplayList);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011821 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011822 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011823 } else {
Romain Guy38c2ece2012-05-24 14:20:56 -070011824 // Should never happen
11825 clearDisplayList();
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011826 }
11827
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011828 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011829
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070011830 resetRtlProperties();
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070011831 onRtlPropertiesChanged(LAYOUT_DIRECTION_DEFAULT);
Svetoslav Ganov42138042012-03-20 11:51:39 -070011832 resetAccessibilityStateChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011833 }
11834
11835 /**
11836 * @return The number of times this view has been attached to a window
11837 */
11838 protected int getWindowAttachCount() {
11839 return mWindowAttachCount;
11840 }
11841
11842 /**
11843 * Retrieve a unique token identifying the window this view is attached to.
11844 * @return Return the window's token for use in
11845 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11846 */
11847 public IBinder getWindowToken() {
11848 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11849 }
11850
11851 /**
11852 * Retrieve a unique token identifying the top-level "real" window of
11853 * the window that this view is attached to. That is, this is like
11854 * {@link #getWindowToken}, except if the window this view in is a panel
11855 * window (attached to another containing window), then the token of
11856 * the containing window is returned instead.
11857 *
11858 * @return Returns the associated window token, either
11859 * {@link #getWindowToken()} or the containing window's token.
11860 */
11861 public IBinder getApplicationWindowToken() {
11862 AttachInfo ai = mAttachInfo;
11863 if (ai != null) {
11864 IBinder appWindowToken = ai.mPanelParentWindowToken;
11865 if (appWindowToken == null) {
11866 appWindowToken = ai.mWindowToken;
11867 }
11868 return appWindowToken;
11869 }
11870 return null;
11871 }
11872
11873 /**
Jeff Brown98365d72012-08-19 20:30:52 -070011874 * Gets the logical display to which the view's window has been attached.
11875 *
11876 * @return The logical display, or null if the view is not currently attached to a window.
11877 */
11878 public Display getDisplay() {
11879 return mAttachInfo != null ? mAttachInfo.mDisplay : null;
11880 }
11881
11882 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011883 * Retrieve private session object this view hierarchy is using to
11884 * communicate with the window manager.
11885 * @return the session object to communicate with the window manager
11886 */
11887 /*package*/ IWindowSession getWindowSession() {
11888 return mAttachInfo != null ? mAttachInfo.mSession : null;
11889 }
11890
11891 /**
11892 * @param info the {@link android.view.View.AttachInfo} to associated with
11893 * this view
11894 */
11895 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11896 //System.out.println("Attached! " + this);
11897 mAttachInfo = info;
11898 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011899 // We will need to evaluate the drawable state at least once.
Dianne Hackborn4702a852012-08-17 15:18:29 -070011900 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 if (mFloatingTreeObserver != null) {
11902 info.mTreeObserver.merge(mFloatingTreeObserver);
11903 mFloatingTreeObserver = null;
11904 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011905 if ((mPrivateFlags&PFLAG_SCROLL_CONTAINER) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906 mAttachInfo.mScrollContainers.add(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011907 mPrivateFlags |= PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011908 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011909 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011911
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011912 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011913 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011914 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011915 if (listeners != null && listeners.size() > 0) {
11916 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11917 // perform the dispatching. The iterator is a safe guard against listeners that
11918 // could mutate the list by calling the various add/remove methods. This prevents
11919 // the array from being modified while we iterate it.
11920 for (OnAttachStateChangeListener listener : listeners) {
11921 listener.onViewAttachedToWindow(this);
11922 }
11923 }
11924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 int vis = info.mWindowVisibility;
11926 if (vis != GONE) {
11927 onWindowVisibilityChanged(vis);
11928 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070011929 if ((mPrivateFlags&PFLAG_DRAWABLE_STATE_DIRTY) != 0) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011930 // If nobody has evaluated the drawable state yet, then do it now.
11931 refreshDrawableState();
11932 }
Craig Mautner7eac0f52012-09-13 13:14:14 -070011933 needGlobalAttributesUpdate(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 }
11935
11936 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 AttachInfo info = mAttachInfo;
11938 if (info != null) {
11939 int vis = info.mWindowVisibility;
11940 if (vis != GONE) {
11941 onWindowVisibilityChanged(GONE);
11942 }
11943 }
11944
11945 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011946
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011947 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011948 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011949 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011950 if (listeners != null && listeners.size() > 0) {
11951 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11952 // perform the dispatching. The iterator is a safe guard against listeners that
11953 // could mutate the list by calling the various add/remove methods. This prevents
11954 // the array from being modified while we iterate it.
11955 for (OnAttachStateChangeListener listener : listeners) {
11956 listener.onViewDetachedFromWindow(this);
11957 }
11958 }
11959
Dianne Hackborn4702a852012-08-17 15:18:29 -070011960 if ((mPrivateFlags & PFLAG_SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961 mAttachInfo.mScrollContainers.remove(this);
Dianne Hackborn4702a852012-08-17 15:18:29 -070011962 mPrivateFlags &= ~PFLAG_SCROLL_CONTAINER_ADDED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011963 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965 mAttachInfo = null;
11966 }
11967
11968 /**
11969 * Store this view hierarchy's frozen state into the given container.
11970 *
11971 * @param container The SparseArray in which to save the view's state.
11972 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011973 * @see #restoreHierarchyState(android.util.SparseArray)
11974 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11975 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 */
11977 public void saveHierarchyState(SparseArray<Parcelable> container) {
11978 dispatchSaveInstanceState(container);
11979 }
11980
11981 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011982 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11983 * this view and its children. May be overridden to modify how freezing happens to a
11984 * 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 -080011985 *
11986 * @param container The SparseArray in which to save the view's state.
11987 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011988 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11989 * @see #saveHierarchyState(android.util.SparseArray)
11990 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011991 */
11992 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11993 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070011994 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011995 Parcelable state = onSaveInstanceState();
Dianne Hackborn4702a852012-08-17 15:18:29 -070011996 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 throw new IllegalStateException(
11998 "Derived class did not call super.onSaveInstanceState()");
11999 }
12000 if (state != null) {
12001 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
12002 // + ": " + state);
12003 container.put(mID, state);
12004 }
12005 }
12006 }
12007
12008 /**
12009 * Hook allowing a view to generate a representation of its internal state
12010 * that can later be used to create a new instance with that same state.
12011 * This state should only contain information that is not persistent or can
12012 * not be reconstructed later. For example, you will never store your
12013 * current position on screen because that will be computed again when a
12014 * new instance of the view is placed in its view hierarchy.
12015 * <p>
12016 * Some examples of things you may store here: the current cursor position
12017 * in a text view (but usually not the text itself since that is stored in a
12018 * content provider or other persistent storage), the currently selected
12019 * item in a list view.
12020 *
12021 * @return Returns a Parcelable object containing the view's current dynamic
12022 * state, or null if there is nothing interesting to save. The
12023 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070012024 * @see #onRestoreInstanceState(android.os.Parcelable)
12025 * @see #saveHierarchyState(android.util.SparseArray)
12026 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012027 * @see #setSaveEnabled(boolean)
12028 */
12029 protected Parcelable onSaveInstanceState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012030 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012031 return BaseSavedState.EMPTY_STATE;
12032 }
12033
12034 /**
12035 * Restore this view hierarchy's frozen state from the given container.
12036 *
12037 * @param container The SparseArray which holds previously frozen states.
12038 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012039 * @see #saveHierarchyState(android.util.SparseArray)
12040 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
12041 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 */
12043 public void restoreHierarchyState(SparseArray<Parcelable> container) {
12044 dispatchRestoreInstanceState(container);
12045 }
12046
12047 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070012048 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
12049 * state for this view and its children. May be overridden to modify how restoring
12050 * happens to a view's children; for example, some views may want to not store state
12051 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012052 *
12053 * @param container The SparseArray which holds previously saved state.
12054 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012055 * @see #dispatchSaveInstanceState(android.util.SparseArray)
12056 * @see #restoreHierarchyState(android.util.SparseArray)
12057 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012058 */
12059 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
12060 if (mID != NO_ID) {
12061 Parcelable state = container.get(mID);
12062 if (state != null) {
12063 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
12064 // + ": " + state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012065 mPrivateFlags &= ~PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012066 onRestoreInstanceState(state);
Dianne Hackborn4702a852012-08-17 15:18:29 -070012067 if ((mPrivateFlags & PFLAG_SAVE_STATE_CALLED) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 throw new IllegalStateException(
12069 "Derived class did not call super.onRestoreInstanceState()");
12070 }
12071 }
12072 }
12073 }
12074
12075 /**
12076 * Hook allowing a view to re-apply a representation of its internal state that had previously
12077 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
12078 * null state.
12079 *
12080 * @param state The frozen state that had previously been returned by
12081 * {@link #onSaveInstanceState}.
12082 *
Philip Milne6c8ea062012-04-03 17:38:43 -070012083 * @see #onSaveInstanceState()
12084 * @see #restoreHierarchyState(android.util.SparseArray)
12085 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 */
12087 protected void onRestoreInstanceState(Parcelable state) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012088 mPrivateFlags |= PFLAG_SAVE_STATE_CALLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012089 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080012090 throw new IllegalArgumentException("Wrong state class, expecting View State but "
12091 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080012092 + "when two views of different type have the same id in the same hierarchy. "
12093 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080012094 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 }
12096 }
12097
12098 /**
12099 * <p>Return the time at which the drawing of the view hierarchy started.</p>
12100 *
12101 * @return the drawing start time in milliseconds
12102 */
12103 public long getDrawingTime() {
12104 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
12105 }
12106
12107 /**
12108 * <p>Enables or disables the duplication of the parent's state into this view. When
12109 * duplication is enabled, this view gets its drawable state from its parent rather
12110 * than from its own internal properties.</p>
12111 *
12112 * <p>Note: in the current implementation, setting this property to true after the
12113 * view was added to a ViewGroup might have no effect at all. This property should
12114 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
12115 *
12116 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
12117 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012118 *
Gilles Debunnefb817032011-01-13 13:52:49 -080012119 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
12120 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012121 *
12122 * @param enabled True to enable duplication of the parent's drawable state, false
12123 * to disable it.
12124 *
12125 * @see #getDrawableState()
12126 * @see #isDuplicateParentStateEnabled()
12127 */
12128 public void setDuplicateParentStateEnabled(boolean enabled) {
12129 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
12130 }
12131
12132 /**
12133 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
12134 *
12135 * @return True if this view's drawable state is duplicated from the parent,
12136 * false otherwise
12137 *
12138 * @see #getDrawableState()
12139 * @see #setDuplicateParentStateEnabled(boolean)
12140 */
12141 public boolean isDuplicateParentStateEnabled() {
12142 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
12143 }
12144
12145 /**
Romain Guy171c5922011-01-06 10:04:23 -080012146 * <p>Specifies the type of layer backing this view. The layer can be
12147 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
12148 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012149 *
Romain Guy171c5922011-01-06 10:04:23 -080012150 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12151 * instance that controls how the layer is composed on screen. The following
12152 * properties of the paint are taken into account when composing the layer:</p>
12153 * <ul>
12154 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12155 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12156 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12157 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080012158 *
Romain Guy171c5922011-01-06 10:04:23 -080012159 * <p>If this view has an alpha value set to < 1.0 by calling
12160 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12161 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12162 * equivalent to setting a hardware layer on this view and providing a paint with
Chet Haased15ebf22012-09-05 11:40:29 -070012163 * the desired alpha value.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012164 *
Romain Guy171c5922011-01-06 10:04:23 -080012165 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
12166 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
12167 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012168 *
Chet Haased15ebf22012-09-05 11:40:29 -070012169 * @param layerType The type of layer to use with this view, must be one of
Romain Guy171c5922011-01-06 10:04:23 -080012170 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12171 * {@link #LAYER_TYPE_HARDWARE}
12172 * @param paint The paint used to compose the layer. This argument is optional
12173 * and can be null. It is ignored when the layer type is
12174 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080012175 *
12176 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080012177 * @see #LAYER_TYPE_NONE
12178 * @see #LAYER_TYPE_SOFTWARE
12179 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080012180 * @see #setAlpha(float)
12181 *
Romain Guy171c5922011-01-06 10:04:23 -080012182 * @attr ref android.R.styleable#View_layerType
12183 */
12184 public void setLayerType(int layerType, Paint paint) {
12185 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080012186 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080012187 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
12188 }
Chet Haasedaf98e92011-01-10 14:10:36 -080012189
Romain Guyd6cd5722011-01-17 14:42:41 -080012190 if (layerType == mLayerType) {
12191 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
12192 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012193 invalidateParentCaches();
12194 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080012195 }
12196 return;
12197 }
Romain Guy171c5922011-01-06 10:04:23 -080012198
12199 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080012200 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070012201 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070012202 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012203 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080012204 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070012205 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080012206 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080012207 default:
12208 break;
Romain Guy171c5922011-01-06 10:04:23 -080012209 }
12210
12211 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080012212 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
12213 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
12214 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080012215
Romain Guy0fd89bf2011-01-26 15:41:30 -080012216 invalidateParentCaches();
12217 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080012218 }
12219
12220 /**
Chet Haased15ebf22012-09-05 11:40:29 -070012221 * Updates the {@link Paint} object used with the current layer (used only if the current
12222 * layer type is not set to {@link #LAYER_TYPE_NONE}). Changed properties of the Paint
12223 * provided to {@link #setLayerType(int, android.graphics.Paint)} will be used the next time
12224 * the View is redrawn, but {@link #setLayerPaint(android.graphics.Paint)} must be called to
12225 * ensure that the view gets redrawn immediately.
12226 *
12227 * <p>A layer is associated with an optional {@link android.graphics.Paint}
12228 * instance that controls how the layer is composed on screen. The following
12229 * properties of the paint are taken into account when composing the layer:</p>
12230 * <ul>
12231 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
12232 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
12233 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
12234 * </ul>
12235 *
12236 * <p>If this view has an alpha value set to < 1.0 by calling
12237 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
12238 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
12239 * equivalent to setting a hardware layer on this view and providing a paint with
12240 * the desired alpha value.</p>
12241 *
12242 * @param paint The paint used to compose the layer. This argument is optional
12243 * and can be null. It is ignored when the layer type is
12244 * {@link #LAYER_TYPE_NONE}
12245 *
12246 * @see #setLayerType(int, android.graphics.Paint)
12247 */
12248 public void setLayerPaint(Paint paint) {
12249 int layerType = getLayerType();
12250 if (layerType != LAYER_TYPE_NONE) {
12251 mLayerPaint = paint == null ? new Paint() : paint;
12252 if (layerType == LAYER_TYPE_HARDWARE) {
12253 HardwareLayer layer = getHardwareLayer();
12254 if (layer != null) {
12255 layer.setLayerPaint(paint);
12256 }
12257 invalidateViewProperty(false, false);
12258 } else {
12259 invalidate();
12260 }
12261 }
12262 }
12263
12264 /**
Romain Guy59c7f802011-09-29 17:21:45 -070012265 * Indicates whether this view has a static layer. A view with layer type
12266 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
12267 * dynamic.
12268 */
12269 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080012270 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070012271 }
12272
12273 /**
Romain Guy171c5922011-01-06 10:04:23 -080012274 * Indicates what type of layer is currently associated with this view. By default
12275 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
12276 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
12277 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080012278 *
Romain Guy171c5922011-01-06 10:04:23 -080012279 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
12280 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080012281 *
12282 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070012283 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080012284 * @see #LAYER_TYPE_NONE
12285 * @see #LAYER_TYPE_SOFTWARE
12286 * @see #LAYER_TYPE_HARDWARE
12287 */
12288 public int getLayerType() {
12289 return mLayerType;
12290 }
Joe Malin32736f02011-01-19 16:14:20 -080012291
Romain Guy6c319ca2011-01-11 14:29:25 -080012292 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080012293 * Forces this view's layer to be created and this view to be rendered
12294 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
12295 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070012296 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012297 * This method can for instance be used to render a view into its layer before
12298 * starting an animation. If this view is complex, rendering into the layer
12299 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070012300 *
Romain Guyf1ae1062011-03-02 18:16:04 -080012301 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070012302 *
12303 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080012304 */
12305 public void buildLayer() {
12306 if (mLayerType == LAYER_TYPE_NONE) return;
12307
12308 if (mAttachInfo == null) {
12309 throw new IllegalStateException("This view must be attached to a window first");
12310 }
12311
12312 switch (mLayerType) {
12313 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080012314 if (mAttachInfo.mHardwareRenderer != null &&
12315 mAttachInfo.mHardwareRenderer.isEnabled() &&
12316 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012317 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080012318 }
Romain Guyf1ae1062011-03-02 18:16:04 -080012319 break;
12320 case LAYER_TYPE_SOFTWARE:
12321 buildDrawingCache(true);
12322 break;
12323 }
12324 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012325
Romain Guyf1ae1062011-03-02 18:16:04 -080012326 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080012327 * <p>Returns a hardware layer that can be used to draw this view again
12328 * without executing its draw method.</p>
12329 *
12330 * @return A HardwareLayer ready to render, or null if an error occurred.
12331 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080012332 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070012333 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
12334 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012335 return null;
12336 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012337
Romain Guy9c4b79a2011-11-10 19:23:58 -080012338 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080012339
12340 final int width = mRight - mLeft;
12341 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080012342
Romain Guy6c319ca2011-01-11 14:29:25 -080012343 if (width == 0 || height == 0) {
12344 return null;
12345 }
12346
Dianne Hackborn4702a852012-08-17 15:18:29 -070012347 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
Romain Guy6c319ca2011-01-11 14:29:25 -080012348 if (mHardwareLayer == null) {
12349 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
12350 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070012351 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy846a5332012-07-11 17:44:57 -070012352 } else {
12353 if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
Chet Haase603f6de2012-09-14 15:31:25 -070012354 if (mHardwareLayer.resize(width, height)) {
12355 mLocalDirtyRect.set(0, 0, width, height);
12356 }
Romain Guy846a5332012-07-11 17:44:57 -070012357 }
12358
12359 // This should not be necessary but applications that change
12360 // the parameters of their background drawable without calling
12361 // this.setBackground(Drawable) can leave the view in a bad state
12362 // (for instance isOpaque() returns true, but the background is
12363 // not opaque.)
12364 computeOpaqueFlags();
12365
12366 final boolean opaque = isOpaque();
Chet Haase603f6de2012-09-14 15:31:25 -070012367 if (mHardwareLayer.isValid() && mHardwareLayer.isOpaque() != opaque) {
Romain Guy846a5332012-07-11 17:44:57 -070012368 mHardwareLayer.setOpaque(opaque);
12369 mLocalDirtyRect.set(0, 0, width, height);
12370 }
Romain Guy6c319ca2011-01-11 14:29:25 -080012371 }
12372
Romain Guy5cd5c3f2011-10-17 17:10:02 -070012373 // The layer is not valid if the underlying GPU resources cannot be allocated
12374 if (!mHardwareLayer.isValid()) {
12375 return null;
12376 }
12377
Romain Guy11cb6422012-09-21 00:39:43 -070012378 mHardwareLayer.setLayerPaint(mLayerPaint);
12379 mHardwareLayer.redrawLater(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
12380 ViewRootImpl viewRoot = getViewRootImpl();
12381 if (viewRoot != null) viewRoot.pushHardwareLayerUpdate(mHardwareLayer);
12382
Michael Jurka7e52caf2012-03-06 15:57:06 -080012383 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080012384 }
12385
12386 return mHardwareLayer;
12387 }
Romain Guy171c5922011-01-06 10:04:23 -080012388
Romain Guy589b0bb2011-10-10 13:57:47 -070012389 /**
12390 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070012391 *
Romain Guy589b0bb2011-10-10 13:57:47 -070012392 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070012393 *
12394 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070012395 * @see #LAYER_TYPE_HARDWARE
12396 */
Romain Guya998dff2012-03-23 18:58:36 -070012397 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070012398 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012399 AttachInfo info = mAttachInfo;
12400 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070012401 info.mHardwareRenderer.isEnabled() &&
12402 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080012403 mHardwareLayer.destroy();
12404 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080012405
Chet Haase6a2d17f2012-09-30 12:14:13 -070012406 if (mDisplayList != null) {
12407 mDisplayList.reset();
12408 }
Romain Guy9c4b79a2011-11-10 19:23:58 -080012409 invalidate(true);
12410 invalidateParentCaches();
12411 }
Romain Guy65b345f2011-07-27 18:51:50 -070012412 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070012413 }
Romain Guy65b345f2011-07-27 18:51:50 -070012414 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070012415 }
12416
Romain Guy171c5922011-01-06 10:04:23 -080012417 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080012418 * Destroys all hardware rendering resources. This method is invoked
12419 * when the system needs to reclaim resources. Upon execution of this
12420 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070012421 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012422 * Note: you <strong>must</strong> call
12423 * <code>super.destroyHardwareResources()</code> when overriding
12424 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070012425 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080012426 * @hide
12427 */
12428 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070012429 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080012430 }
12431
12432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012433 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
12434 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
12435 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
12436 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
12437 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
12438 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012439 *
Romain Guy171c5922011-01-06 10:04:23 -080012440 * <p>Enabling the drawing cache is similar to
12441 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080012442 * acceleration is turned off. When hardware acceleration is turned on, enabling the
12443 * drawing cache has no effect on rendering because the system uses a different mechanism
12444 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
12445 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
12446 * for information on how to enable software and hardware layers.</p>
12447 *
12448 * <p>This API can be used to manually generate
12449 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
12450 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451 *
12452 * @param enabled true to enable the drawing cache, false otherwise
12453 *
12454 * @see #isDrawingCacheEnabled()
12455 * @see #getDrawingCache()
12456 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080012457 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012458 */
12459 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012460 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
12462 }
12463
12464 /**
12465 * <p>Indicates whether the drawing cache is enabled for this view.</p>
12466 *
12467 * @return true if the drawing cache is enabled
12468 *
12469 * @see #setDrawingCacheEnabled(boolean)
12470 * @see #getDrawingCache()
12471 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012472 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012473 public boolean isDrawingCacheEnabled() {
12474 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
12475 }
12476
12477 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080012478 * Debugging utility which recursively outputs the dirty state of a view and its
12479 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080012480 *
Chet Haasedaf98e92011-01-10 14:10:36 -080012481 * @hide
12482 */
Romain Guy676b1732011-02-14 14:45:33 -080012483 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080012484 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012485 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.PFLAG_DIRTY_MASK) +
12486 ") DRAWN(" + (mPrivateFlags & PFLAG_DRAWN) + ")" + " CACHE_VALID(" +
12487 (mPrivateFlags & View.PFLAG_DRAWING_CACHE_VALID) +
12488 ") INVALIDATED(" + (mPrivateFlags & PFLAG_INVALIDATED) + ")");
Chet Haasedaf98e92011-01-10 14:10:36 -080012489 if (clear) {
12490 mPrivateFlags &= clearMask;
12491 }
12492 if (this instanceof ViewGroup) {
12493 ViewGroup parent = (ViewGroup) this;
12494 final int count = parent.getChildCount();
12495 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080012496 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080012497 child.outputDirtyFlags(indent + " ", clear, clearMask);
12498 }
12499 }
12500 }
12501
12502 /**
12503 * This method is used by ViewGroup to cause its children to restore or recreate their
12504 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
12505 * to recreate its own display list, which would happen if it went through the normal
12506 * draw/dispatchDraw mechanisms.
12507 *
12508 * @hide
12509 */
12510 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080012511
12512 /**
12513 * A view that is not attached or hardware accelerated cannot create a display list.
12514 * This method checks these conditions and returns the appropriate result.
12515 *
12516 * @return true if view has the ability to create a display list, false otherwise.
12517 *
12518 * @hide
12519 */
12520 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080012521 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080012522 }
Joe Malin32736f02011-01-19 16:14:20 -080012523
Chet Haasedaf98e92011-01-10 14:10:36 -080012524 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080012525 * @return The HardwareRenderer associated with that view or null if hardware rendering
12526 * is not supported or this this has not been attached to a window.
12527 *
12528 * @hide
12529 */
12530 public HardwareRenderer getHardwareRenderer() {
12531 if (mAttachInfo != null) {
12532 return mAttachInfo.mHardwareRenderer;
12533 }
12534 return null;
12535 }
12536
12537 /**
Chet Haasea1cff502012-02-21 13:43:44 -080012538 * Returns a DisplayList. If the incoming displayList is null, one will be created.
12539 * Otherwise, the same display list will be returned (after having been rendered into
12540 * along the way, depending on the invalidation state of the view).
12541 *
12542 * @param displayList The previous version of this displayList, could be null.
12543 * @param isLayer Whether the requester of the display list is a layer. If so,
12544 * the view will avoid creating a layer inside the resulting display list.
12545 * @return A new or reused DisplayList object.
12546 */
12547 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
12548 if (!canHaveDisplayList()) {
12549 return null;
12550 }
12551
Dianne Hackborn4702a852012-08-17 15:18:29 -070012552 if (((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 ||
Chet Haasea1cff502012-02-21 13:43:44 -080012553 displayList == null || !displayList.isValid() ||
12554 (!isLayer && mRecreateDisplayList))) {
12555 // Don't need to recreate the display list, just need to tell our
12556 // children to restore/recreate theirs
12557 if (displayList != null && displayList.isValid() &&
12558 !isLayer && !mRecreateDisplayList) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012559 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12560 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012561 dispatchGetDisplayList();
12562
12563 return displayList;
12564 }
12565
12566 if (!isLayer) {
12567 // If we got here, we're recreating it. Mark it as such to ensure that
12568 // we copy in child display lists into ours in drawChild()
12569 mRecreateDisplayList = true;
12570 }
12571 if (displayList == null) {
12572 final String name = getClass().getSimpleName();
12573 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
12574 // If we're creating a new display list, make sure our parent gets invalidated
12575 // since they will need to recreate their display list to account for this
12576 // new child display list.
12577 invalidateParentCaches();
12578 }
12579
12580 boolean caching = false;
12581 final HardwareCanvas canvas = displayList.start();
Chet Haasea1cff502012-02-21 13:43:44 -080012582 int width = mRight - mLeft;
12583 int height = mBottom - mTop;
12584
12585 try {
12586 canvas.setViewport(width, height);
12587 // The dirty rect should always be null for a display list
12588 canvas.onPreDraw(null);
Michael Jurkaba649742012-06-28 19:12:58 -070012589 int layerType = getLayerType();
Chet Haase1271e2c2012-04-20 09:54:27 -070012590 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070012591 if (layerType == LAYER_TYPE_HARDWARE) {
12592 final HardwareLayer layer = getHardwareLayer();
12593 if (layer != null && layer.isValid()) {
12594 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
12595 } else {
12596 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
12597 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
12598 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12599 }
12600 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080012601 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070012602 buildDrawingCache(true);
12603 Bitmap cache = getDrawingCache(true);
12604 if (cache != null) {
12605 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
12606 caching = true;
12607 }
Chet Haasea1cff502012-02-21 13:43:44 -080012608 }
Chet Haasea1cff502012-02-21 13:43:44 -080012609 } else {
12610
12611 computeScroll();
12612
Chet Haasea1cff502012-02-21 13:43:44 -080012613 canvas.translate(-mScrollX, -mScrollY);
12614 if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012615 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12616 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012617 }
12618
12619 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012620 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Chet Haasea1cff502012-02-21 13:43:44 -080012621 dispatchDraw(canvas);
12622 } else {
12623 draw(canvas);
12624 }
12625 }
12626 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080012627 canvas.onPostDraw();
12628
12629 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070012630 displayList.setCaching(caching);
12631 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080012632 displayList.setLeftTopRightBottom(0, 0, width, height);
12633 } else {
12634 setDisplayListProperties(displayList);
12635 }
12636 }
12637 } else if (!isLayer) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012638 mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
12639 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haasea1cff502012-02-21 13:43:44 -080012640 }
12641
12642 return displayList;
12643 }
12644
12645 /**
12646 * Get the DisplayList for the HardwareLayer
12647 *
12648 * @param layer The HardwareLayer whose DisplayList we want
12649 * @return A DisplayList fopr the specified HardwareLayer
12650 */
12651 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
12652 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
12653 layer.setDisplayList(displayList);
12654 return displayList;
12655 }
12656
12657
12658 /**
Romain Guyb051e892010-09-28 19:09:36 -070012659 * <p>Returns a display list that can be used to draw this view again
12660 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012661 *
Romain Guyb051e892010-09-28 19:09:36 -070012662 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080012663 *
12664 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070012665 */
Chet Haasedaf98e92011-01-10 14:10:36 -080012666 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080012667 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070012668 return mDisplayList;
12669 }
12670
Romain Guy38c2ece2012-05-24 14:20:56 -070012671 private void clearDisplayList() {
12672 if (mDisplayList != null) {
12673 mDisplayList.invalidate();
12674 mDisplayList.clear();
12675 }
12676 }
12677
Romain Guyb051e892010-09-28 19:09:36 -070012678 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012679 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012680 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012681 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012682 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012683 * @see #getDrawingCache(boolean)
12684 */
12685 public Bitmap getDrawingCache() {
12686 return getDrawingCache(false);
12687 }
12688
12689 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012690 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
12691 * is null when caching is disabled. If caching is enabled and the cache is not ready,
12692 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
12693 * draw from the cache when the cache is enabled. To benefit from the cache, you must
12694 * request the drawing cache by calling this method and draw it on screen if the
12695 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012696 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012697 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12698 * this method will create a bitmap of the same size as this view. Because this bitmap
12699 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12700 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12701 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12702 * size than the view. This implies that your application must be able to handle this
12703 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012704 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012705 * @param autoScale Indicates whether the generated bitmap should be scaled based on
12706 * the current density of the screen when the application is in compatibility
12707 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012708 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012709 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080012710 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012711 * @see #setDrawingCacheEnabled(boolean)
12712 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070012713 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012714 * @see #destroyDrawingCache()
12715 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012716 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012717 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
12718 return null;
12719 }
12720 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012721 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012722 }
Romain Guy02890fd2010-08-06 17:58:44 -070012723 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 }
12725
12726 /**
12727 * <p>Frees the resources used by the drawing cache. If you call
12728 * {@link #buildDrawingCache()} manually without calling
12729 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12730 * should cleanup the cache with this method afterwards.</p>
12731 *
12732 * @see #setDrawingCacheEnabled(boolean)
12733 * @see #buildDrawingCache()
12734 * @see #getDrawingCache()
12735 */
12736 public void destroyDrawingCache() {
12737 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012738 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012739 mDrawingCache = null;
12740 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012741 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012742 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012743 mUnscaledDrawingCache = null;
12744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012745 }
12746
12747 /**
12748 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012749 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012750 * view will always be drawn on top of a solid color.
12751 *
12752 * @param color The background color to use for the drawing cache's bitmap
12753 *
12754 * @see #setDrawingCacheEnabled(boolean)
12755 * @see #buildDrawingCache()
12756 * @see #getDrawingCache()
12757 */
12758 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012759 if (color != mDrawingCacheBackgroundColor) {
12760 mDrawingCacheBackgroundColor = color;
Dianne Hackborn4702a852012-08-17 15:18:29 -070012761 mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;
Romain Guy52e2ef82010-01-14 12:11:48 -080012762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 }
12764
12765 /**
12766 * @see #setDrawingCacheBackgroundColor(int)
12767 *
12768 * @return The background color to used for the drawing cache's bitmap
12769 */
12770 public int getDrawingCacheBackgroundColor() {
12771 return mDrawingCacheBackgroundColor;
12772 }
12773
12774 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012775 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012776 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012777 * @see #buildDrawingCache(boolean)
12778 */
12779 public void buildDrawingCache() {
12780 buildDrawingCache(false);
12781 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012782
Romain Guyfbd8f692009-06-26 14:51:58 -070012783 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012784 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12785 *
12786 * <p>If you call {@link #buildDrawingCache()} manually without calling
12787 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12788 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012789 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012790 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12791 * this method will create a bitmap of the same size as this view. Because this bitmap
12792 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12793 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12794 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12795 * size than the view. This implies that your application must be able to handle this
12796 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012797 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012798 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12799 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012800 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012801 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012802 *
12803 * @see #getDrawingCache()
12804 * @see #destroyDrawingCache()
12805 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012806 public void buildDrawingCache(boolean autoScale) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012807 if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012808 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012809 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810
Romain Guy8506ab42009-06-11 17:35:47 -070012811 int width = mRight - mLeft;
12812 int height = mBottom - mTop;
12813
12814 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012815 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012816
Romain Guye1123222009-06-29 14:24:56 -070012817 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012818 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12819 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012821
12822 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012823 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012824 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012825
Romain Guyaf61cc42012-09-30 10:49:15 -070012826 final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
12827 final long drawingCacheSize =
Chris Craik10e9d1d2012-09-06 14:42:44 -070012828 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
12829 if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
Chris Craik3667aa32012-09-06 14:56:39 -070012830 if (width > 0 && height > 0) {
12831 Log.w(VIEW_LOG_TAG, "View too large to fit into drawing cache, needs "
12832 + projectedBitmapSize + " bytes, only "
12833 + drawingCacheSize + " available");
12834 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012835 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012836 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012837 return;
12838 }
12839
12840 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012841 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842
12843 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012844 Bitmap.Config quality;
12845 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012846 // Never pick ARGB_4444 because it looks awful
12847 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012848 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12849 case DRAWING_CACHE_QUALITY_AUTO:
12850 quality = Bitmap.Config.ARGB_8888;
12851 break;
12852 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012853 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 break;
12855 case DRAWING_CACHE_QUALITY_HIGH:
12856 quality = Bitmap.Config.ARGB_8888;
12857 break;
12858 default:
12859 quality = Bitmap.Config.ARGB_8888;
12860 break;
12861 }
12862 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012863 // Optimization for translucent windows
12864 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012865 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012866 }
12867
12868 // Try to cleanup memory
12869 if (bitmap != null) bitmap.recycle();
12870
12871 try {
Dianne Hackborndde331c2012-08-03 14:01:57 -070012872 bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12873 width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012874 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012875 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012876 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012877 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012878 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012879 }
Adam Powell26153a32010-11-08 15:22:27 -080012880 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012881 } catch (OutOfMemoryError e) {
12882 // If there is not enough memory to create the bitmap cache, just
12883 // ignore the issue as bitmap caches are not required to draw the
12884 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012885 if (autoScale) {
12886 mDrawingCache = null;
12887 } else {
12888 mUnscaledDrawingCache = null;
12889 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012890 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012891 return;
12892 }
12893
12894 clear = drawingCacheBackgroundColor != 0;
12895 }
12896
12897 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012898 if (attachInfo != null) {
12899 canvas = attachInfo.mCanvas;
12900 if (canvas == null) {
12901 canvas = new Canvas();
12902 }
12903 canvas.setBitmap(bitmap);
12904 // Temporarily clobber the cached Canvas in case one of our children
12905 // is also using a drawing cache. Without this, the children would
12906 // steal the canvas by attaching their own bitmap to it and bad, bad
12907 // thing would happen (invisible views, corrupted drawings, etc.)
12908 attachInfo.mCanvas = null;
12909 } else {
12910 // This case should hopefully never or seldom happen
12911 canvas = new Canvas(bitmap);
12912 }
12913
12914 if (clear) {
12915 bitmap.eraseColor(drawingCacheBackgroundColor);
12916 }
12917
12918 computeScroll();
12919 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012920
Romain Guye1123222009-06-29 14:24:56 -070012921 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012922 final float scale = attachInfo.mApplicationScale;
12923 canvas.scale(scale, scale);
12924 }
Joe Malin32736f02011-01-19 16:14:20 -080012925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012926 canvas.translate(-mScrollX, -mScrollY);
12927
Dianne Hackborn4702a852012-08-17 15:18:29 -070012928 mPrivateFlags |= PFLAG_DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012929 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12930 mLayerType != LAYER_TYPE_NONE) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070012931 mPrivateFlags |= PFLAG_DRAWING_CACHE_VALID;
Romain Guy0d9275e2010-10-26 14:22:30 -070012932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012933
12934 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070012935 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
12936 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012937 dispatchDraw(canvas);
12938 } else {
12939 draw(canvas);
12940 }
12941
12942 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012943 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012944
12945 if (attachInfo != null) {
12946 // Restore the cached Canvas for our siblings
12947 attachInfo.mCanvas = canvas;
12948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012949 }
12950 }
12951
12952 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012953 * Create a snapshot of the view into a bitmap. We should probably make
12954 * some form of this public, but should think about the API.
12955 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012956 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012957 int width = mRight - mLeft;
12958 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012959
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012960 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012961 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012962 width = (int) ((width * scale) + 0.5f);
12963 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012964
Dianne Hackborndde331c2012-08-03 14:01:57 -070012965 Bitmap bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
12966 width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012967 if (bitmap == null) {
12968 throw new OutOfMemoryError();
12969 }
12970
Romain Guyc529d8d2011-09-06 15:01:39 -070012971 Resources resources = getResources();
12972 if (resources != null) {
12973 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12974 }
Joe Malin32736f02011-01-19 16:14:20 -080012975
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012976 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012977 if (attachInfo != null) {
12978 canvas = attachInfo.mCanvas;
12979 if (canvas == null) {
12980 canvas = new Canvas();
12981 }
12982 canvas.setBitmap(bitmap);
12983 // Temporarily clobber the cached Canvas in case one of our children
12984 // is also using a drawing cache. Without this, the children would
12985 // steal the canvas by attaching their own bitmap to it and bad, bad
12986 // things would happen (invisible views, corrupted drawings, etc.)
12987 attachInfo.mCanvas = null;
12988 } else {
12989 // This case should hopefully never or seldom happen
12990 canvas = new Canvas(bitmap);
12991 }
12992
Romain Guy5bcdff42009-05-14 21:27:18 -070012993 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012994 bitmap.eraseColor(backgroundColor);
12995 }
12996
12997 computeScroll();
12998 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012999 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013000 canvas.translate(-mScrollX, -mScrollY);
13001
Romain Guy5bcdff42009-05-14 21:27:18 -070013002 // Temporarily remove the dirty mask
13003 int flags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013004 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Romain Guy5bcdff42009-05-14 21:27:18 -070013005
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013006 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013007 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013008 dispatchDraw(canvas);
13009 } else {
13010 draw(canvas);
13011 }
13012
Romain Guy5bcdff42009-05-14 21:27:18 -070013013 mPrivateFlags = flags;
13014
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013015 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070013016 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013017
13018 if (attachInfo != null) {
13019 // Restore the cached Canvas for our siblings
13020 attachInfo.mCanvas = canvas;
13021 }
Romain Guy8506ab42009-06-11 17:35:47 -070013022
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070013023 return bitmap;
13024 }
13025
13026 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013027 * Indicates whether this View is currently in edit mode. A View is usually
13028 * in edit mode when displayed within a developer tool. For instance, if
13029 * this View is being drawn by a visual user interface builder, this method
13030 * should return true.
13031 *
13032 * Subclasses should check the return value of this method to provide
13033 * different behaviors if their normal behavior might interfere with the
13034 * host environment. For instance: the class spawns a thread in its
13035 * constructor, the drawing code relies on device-specific features, etc.
13036 *
13037 * This method is usually checked in the drawing code of custom widgets.
13038 *
13039 * @return True if this View is in edit mode, false otherwise.
13040 */
13041 public boolean isInEditMode() {
13042 return false;
13043 }
13044
13045 /**
13046 * If the View draws content inside its padding and enables fading edges,
13047 * it needs to support padding offsets. Padding offsets are added to the
13048 * fading edges to extend the length of the fade so that it covers pixels
13049 * drawn inside the padding.
13050 *
13051 * Subclasses of this class should override this method if they need
13052 * to draw content inside the padding.
13053 *
13054 * @return True if padding offset must be applied, false otherwise.
13055 *
13056 * @see #getLeftPaddingOffset()
13057 * @see #getRightPaddingOffset()
13058 * @see #getTopPaddingOffset()
13059 * @see #getBottomPaddingOffset()
13060 *
13061 * @since CURRENT
13062 */
13063 protected boolean isPaddingOffsetRequired() {
13064 return false;
13065 }
13066
13067 /**
13068 * Amount by which to extend the left fading region. Called only when
13069 * {@link #isPaddingOffsetRequired()} returns true.
13070 *
13071 * @return The left padding offset in pixels.
13072 *
13073 * @see #isPaddingOffsetRequired()
13074 *
13075 * @since CURRENT
13076 */
13077 protected int getLeftPaddingOffset() {
13078 return 0;
13079 }
13080
13081 /**
13082 * Amount by which to extend the right fading region. Called only when
13083 * {@link #isPaddingOffsetRequired()} returns true.
13084 *
13085 * @return The right padding offset in pixels.
13086 *
13087 * @see #isPaddingOffsetRequired()
13088 *
13089 * @since CURRENT
13090 */
13091 protected int getRightPaddingOffset() {
13092 return 0;
13093 }
13094
13095 /**
13096 * Amount by which to extend the top fading region. Called only when
13097 * {@link #isPaddingOffsetRequired()} returns true.
13098 *
13099 * @return The top padding offset in pixels.
13100 *
13101 * @see #isPaddingOffsetRequired()
13102 *
13103 * @since CURRENT
13104 */
13105 protected int getTopPaddingOffset() {
13106 return 0;
13107 }
13108
13109 /**
13110 * Amount by which to extend the bottom fading region. Called only when
13111 * {@link #isPaddingOffsetRequired()} returns true.
13112 *
13113 * @return The bottom padding offset in pixels.
13114 *
13115 * @see #isPaddingOffsetRequired()
13116 *
13117 * @since CURRENT
13118 */
13119 protected int getBottomPaddingOffset() {
13120 return 0;
13121 }
13122
13123 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070013124 * @hide
13125 * @param offsetRequired
13126 */
13127 protected int getFadeTop(boolean offsetRequired) {
13128 int top = mPaddingTop;
13129 if (offsetRequired) top += getTopPaddingOffset();
13130 return top;
13131 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013132
Romain Guyf2fc4602011-07-19 15:20:03 -070013133 /**
13134 * @hide
13135 * @param offsetRequired
13136 */
13137 protected int getFadeHeight(boolean offsetRequired) {
13138 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070013139 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070013140 return mBottom - mTop - mPaddingBottom - padding;
13141 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013142
Romain Guyf2fc4602011-07-19 15:20:03 -070013143 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013144 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070013145 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013146 *
Romain Guy2bffd262010-09-12 17:40:02 -070013147 * <p>Even if this method returns true, it does not mean that every call
13148 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
13149 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090013150 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070013151 * window is hardware accelerated,
13152 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
13153 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080013154 *
Romain Guy2bffd262010-09-12 17:40:02 -070013155 * @return True if the view is attached to a window and the window is
13156 * hardware accelerated; false in any other case.
13157 */
13158 public boolean isHardwareAccelerated() {
13159 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
13160 }
Joe Malin32736f02011-01-19 16:14:20 -080013161
Romain Guy2bffd262010-09-12 17:40:02 -070013162 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080013163 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
13164 * case of an active Animation being run on the view.
13165 */
13166 private boolean drawAnimation(ViewGroup parent, long drawingTime,
13167 Animation a, boolean scalingRequired) {
13168 Transformation invalidationTransform;
13169 final int flags = parent.mGroupFlags;
13170 final boolean initialized = a.isInitialized();
13171 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070013172 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080013173 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
Romain Guy393a52c2012-05-22 20:21:08 -070013174 if (mAttachInfo != null) a.setListenerHandler(mAttachInfo.mHandler);
Chet Haasebcca79a2012-02-14 08:45:14 -080013175 onAnimationStart();
13176 }
13177
13178 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
13179 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
13180 if (parent.mInvalidationTransformation == null) {
13181 parent.mInvalidationTransformation = new Transformation();
13182 }
13183 invalidationTransform = parent.mInvalidationTransformation;
13184 a.getTransformation(drawingTime, invalidationTransform, 1f);
13185 } else {
13186 invalidationTransform = parent.mChildTransformation;
13187 }
Romain Guy393a52c2012-05-22 20:21:08 -070013188
Chet Haasebcca79a2012-02-14 08:45:14 -080013189 if (more) {
13190 if (!a.willChangeBounds()) {
Romain Guyf0af1d52012-07-11 18:31:21 -070013191 if ((flags & (ViewGroup.FLAG_OPTIMIZE_INVALIDATE | ViewGroup.FLAG_ANIMATION_DONE)) ==
13192 ViewGroup.FLAG_OPTIMIZE_INVALIDATE) {
13193 parent.mGroupFlags |= ViewGroup.FLAG_INVALIDATE_REQUIRED;
13194 } else if ((flags & ViewGroup.FLAG_INVALIDATE_REQUIRED) == 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013195 // The child need to draw an animation, potentially offscreen, so
13196 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013197 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013198 parent.invalidate(mLeft, mTop, mRight, mBottom);
13199 }
13200 } else {
13201 if (parent.mInvalidateRegion == null) {
13202 parent.mInvalidateRegion = new RectF();
13203 }
13204 final RectF region = parent.mInvalidateRegion;
13205 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
13206 invalidationTransform);
13207
13208 // The child need to draw an animation, potentially offscreen, so
13209 // make sure we do not cancel invalidate requests
Dianne Hackborn4702a852012-08-17 15:18:29 -070013210 parent.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
Chet Haasebcca79a2012-02-14 08:45:14 -080013211
13212 final int left = mLeft + (int) region.left;
13213 final int top = mTop + (int) region.top;
13214 parent.invalidate(left, top, left + (int) (region.width() + .5f),
13215 top + (int) (region.height() + .5f));
13216 }
13217 }
13218 return more;
13219 }
13220
Chet Haasea1cff502012-02-21 13:43:44 -080013221 /**
13222 * This method is called by getDisplayList() when a display list is created or re-rendered.
13223 * It sets or resets the current value of all properties on that display list (resetting is
13224 * necessary when a display list is being re-created, because we need to make sure that
13225 * previously-set transform values
13226 */
13227 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013228 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013229 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070013230 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080013231 if (mParent instanceof ViewGroup) {
13232 displayList.setClipChildren(
13233 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
13234 }
Chet Haase9420abd2012-03-29 16:28:32 -070013235 float alpha = 1;
13236 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
13237 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13238 ViewGroup parentVG = (ViewGroup) mParent;
13239 final boolean hasTransform =
13240 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
13241 if (hasTransform) {
13242 Transformation transform = parentVG.mChildTransformation;
13243 final int transformType = parentVG.mChildTransformation.getTransformationType();
13244 if (transformType != Transformation.TYPE_IDENTITY) {
13245 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
13246 alpha = transform.getAlpha();
13247 }
13248 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
13249 displayList.setStaticMatrix(transform.getMatrix());
13250 }
13251 }
13252 }
Chet Haasea1cff502012-02-21 13:43:44 -080013253 }
13254 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070013255 alpha *= mTransformationInfo.mAlpha;
13256 if (alpha < 1) {
13257 final int multipliedAlpha = (int) (255 * alpha);
13258 if (onSetAlpha(multipliedAlpha)) {
13259 alpha = 1;
13260 }
13261 }
13262 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080013263 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
13264 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
13265 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
13266 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070013267 if (mTransformationInfo.mCamera == null) {
13268 mTransformationInfo.mCamera = new Camera();
13269 mTransformationInfo.matrix3D = new Matrix();
13270 }
13271 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Dianne Hackborn4702a852012-08-17 15:18:29 -070013272 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == PFLAG_PIVOT_EXPLICITLY_SET) {
Chet Haasea1cff502012-02-21 13:43:44 -080013273 displayList.setPivotX(getPivotX());
13274 displayList.setPivotY(getPivotY());
13275 }
Chet Haase9420abd2012-03-29 16:28:32 -070013276 } else if (alpha < 1) {
13277 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080013278 }
13279 }
13280 }
13281
Chet Haasebcca79a2012-02-14 08:45:14 -080013282 /**
Chet Haase64a48c12012-02-13 16:33:29 -080013283 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
13284 * This draw() method is an implementation detail and is not intended to be overridden or
13285 * to be called from anywhere else other than ViewGroup.drawChild().
13286 */
13287 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070013288 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080013289 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080013290 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080013291 final int flags = parent.mGroupFlags;
13292
Chet Haasea1cff502012-02-21 13:43:44 -080013293 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080013294 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080013295 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013296 }
13297
13298 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080013299 boolean concatMatrix = false;
13300
13301 boolean scalingRequired = false;
13302 boolean caching;
Michael Jurkaba649742012-06-28 19:12:58 -070013303 int layerType = getLayerType();
Chet Haase64a48c12012-02-13 16:33:29 -080013304
13305 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080013306 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
13307 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013308 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070013309 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080013310 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
13311 } else {
13312 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
13313 }
13314
Chet Haasebcca79a2012-02-14 08:45:14 -080013315 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080013316 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013317 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080013318 concatMatrix = a.willChangeTransformationMatrix();
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013319 if (concatMatrix) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013320 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013321 }
Chet Haasebcca79a2012-02-14 08:45:14 -080013322 transformToApply = parent.mChildTransformation;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013323 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013324 if ((mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_TRANSFORM) == PFLAG3_VIEW_IS_ANIMATING_TRANSFORM &&
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013325 mDisplayList != null) {
13326 // No longer animating: clear out old animation matrix
13327 mDisplayList.setAnimationMatrix(null);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013328 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_TRANSFORM;
Chet Haaseafd5c3e2012-05-10 13:21:10 -070013329 }
13330 if (!useDisplayListProperties &&
13331 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
13332 final boolean hasTransform =
13333 parent.getChildStaticTransformation(this, parent.mChildTransformation);
13334 if (hasTransform) {
13335 final int transformType = parent.mChildTransformation.getTransformationType();
13336 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
13337 parent.mChildTransformation : null;
13338 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
13339 }
Chet Haase64a48c12012-02-13 16:33:29 -080013340 }
13341 }
13342
13343 concatMatrix |= !childHasIdentityMatrix;
13344
13345 // Sets the flag as early as possible to allow draw() implementations
13346 // to call invalidate() successfully when doing animations
Dianne Hackborn4702a852012-08-17 15:18:29 -070013347 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase64a48c12012-02-13 16:33:29 -080013348
Romain Guyfbb43212012-08-30 15:19:27 -070013349 if (!concatMatrix &&
13350 (flags & (ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
13351 ViewGroup.FLAG_CLIP_CHILDREN)) == ViewGroup.FLAG_CLIP_CHILDREN &&
Chet Haase599913d2012-07-23 16:22:05 -070013352 canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Dianne Hackborn4702a852012-08-17 15:18:29 -070013353 (mPrivateFlags & PFLAG_DRAW_ANIMATION) == 0) {
13354 mPrivateFlags2 |= PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013355 return more;
13356 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013357 mPrivateFlags2 &= ~PFLAG2_VIEW_QUICK_REJECTED;
Chet Haase64a48c12012-02-13 16:33:29 -080013358
13359 if (hardwareAccelerated) {
13360 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
13361 // retain the flag's value temporarily in the mRecreateDisplayList flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070013362 mRecreateDisplayList = (mPrivateFlags & PFLAG_INVALIDATED) == PFLAG_INVALIDATED;
13363 mPrivateFlags &= ~PFLAG_INVALIDATED;
Chet Haase64a48c12012-02-13 16:33:29 -080013364 }
13365
Chet Haase64a48c12012-02-13 16:33:29 -080013366 DisplayList displayList = null;
13367 Bitmap cache = null;
13368 boolean hasDisplayList = false;
13369 if (caching) {
13370 if (!hardwareAccelerated) {
13371 if (layerType != LAYER_TYPE_NONE) {
13372 layerType = LAYER_TYPE_SOFTWARE;
13373 buildDrawingCache(true);
13374 }
13375 cache = getDrawingCache(true);
13376 } else {
13377 switch (layerType) {
13378 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070013379 if (useDisplayListProperties) {
13380 hasDisplayList = canHaveDisplayList();
13381 } else {
13382 buildDrawingCache(true);
13383 cache = getDrawingCache(true);
13384 }
Chet Haase64a48c12012-02-13 16:33:29 -080013385 break;
Chet Haasea1cff502012-02-21 13:43:44 -080013386 case LAYER_TYPE_HARDWARE:
13387 if (useDisplayListProperties) {
13388 hasDisplayList = canHaveDisplayList();
13389 }
13390 break;
Chet Haase64a48c12012-02-13 16:33:29 -080013391 case LAYER_TYPE_NONE:
13392 // Delay getting the display list until animation-driven alpha values are
13393 // set up and possibly passed on to the view
13394 hasDisplayList = canHaveDisplayList();
13395 break;
13396 }
13397 }
13398 }
Chet Haasea1cff502012-02-21 13:43:44 -080013399 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070013400 if (useDisplayListProperties) {
13401 displayList = getDisplayList();
13402 if (!displayList.isValid()) {
13403 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13404 // to getDisplayList(), the display list will be marked invalid and we should not
13405 // try to use it again.
13406 displayList = null;
13407 hasDisplayList = false;
13408 useDisplayListProperties = false;
13409 }
13410 }
Chet Haase64a48c12012-02-13 16:33:29 -080013411
Chet Haase526057b2012-07-12 17:50:41 -070013412 int sx = 0;
13413 int sy = 0;
13414 if (!hasDisplayList) {
13415 computeScroll();
13416 sx = mScrollX;
13417 sy = mScrollY;
13418 }
13419
Chet Haase64a48c12012-02-13 16:33:29 -080013420 final boolean hasNoCache = cache == null || hasDisplayList;
13421 final boolean offsetForScroll = cache == null && !hasDisplayList &&
13422 layerType != LAYER_TYPE_HARDWARE;
13423
Chet Haasea1cff502012-02-21 13:43:44 -080013424 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070013425 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013426 restoreTo = canvas.save();
13427 }
Chet Haase64a48c12012-02-13 16:33:29 -080013428 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013429 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080013430 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080013431 if (!useDisplayListProperties) {
13432 canvas.translate(mLeft, mTop);
13433 }
Chet Haase64a48c12012-02-13 16:33:29 -080013434 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080013435 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070013436 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080013437 restoreTo = canvas.save();
13438 }
Chet Haase64a48c12012-02-13 16:33:29 -080013439 // mAttachInfo cannot be null, otherwise scalingRequired == false
13440 final float scale = 1.0f / mAttachInfo.mApplicationScale;
13441 canvas.scale(scale, scale);
13442 }
13443 }
13444
Chet Haasea1cff502012-02-21 13:43:44 -080013445 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase21433372012-06-05 07:54:09 -070013446 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix() ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013447 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase64a48c12012-02-13 16:33:29 -080013448 if (transformToApply != null || !childHasIdentityMatrix) {
13449 int transX = 0;
13450 int transY = 0;
13451
13452 if (offsetForScroll) {
13453 transX = -sx;
13454 transY = -sy;
13455 }
13456
13457 if (transformToApply != null) {
13458 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070013459 if (useDisplayListProperties) {
13460 displayList.setAnimationMatrix(transformToApply.getMatrix());
13461 } else {
13462 // Undo the scroll translation, apply the transformation matrix,
13463 // then redo the scroll translate to get the correct result.
13464 canvas.translate(-transX, -transY);
13465 canvas.concat(transformToApply.getMatrix());
13466 canvas.translate(transX, transY);
13467 }
Chet Haasea1cff502012-02-21 13:43:44 -080013468 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013469 }
13470
13471 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070013472 if (transformAlpha < 1) {
Chet Haase21433372012-06-05 07:54:09 -070013473 alpha *= transformAlpha;
Chet Haasea1cff502012-02-21 13:43:44 -080013474 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013475 }
13476 }
13477
Chet Haasea1cff502012-02-21 13:43:44 -080013478 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013479 canvas.translate(-transX, -transY);
13480 canvas.concat(getMatrix());
13481 canvas.translate(transX, transY);
13482 }
13483 }
13484
Chet Haase21433372012-06-05 07:54:09 -070013485 // Deal with alpha if it is or used to be <1
13486 if (alpha < 1 ||
Dianne Hackborn4702a852012-08-17 15:18:29 -070013487 (mPrivateFlags3 & PFLAG3_VIEW_IS_ANIMATING_ALPHA) == PFLAG3_VIEW_IS_ANIMATING_ALPHA) {
Chet Haase21433372012-06-05 07:54:09 -070013488 if (alpha < 1) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013489 mPrivateFlags3 |= PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013490 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013491 mPrivateFlags3 &= ~PFLAG3_VIEW_IS_ANIMATING_ALPHA;
Chet Haase21433372012-06-05 07:54:09 -070013492 }
Chet Haasea1cff502012-02-21 13:43:44 -080013493 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080013494 if (hasNoCache) {
13495 final int multipliedAlpha = (int) (255 * alpha);
13496 if (!onSetAlpha(multipliedAlpha)) {
13497 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080013498 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080013499 layerType != LAYER_TYPE_NONE) {
13500 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
13501 }
Chet Haase9420abd2012-03-29 16:28:32 -070013502 if (useDisplayListProperties) {
13503 displayList.setAlpha(alpha * getAlpha());
13504 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070013505 final int scrollX = hasDisplayList ? 0 : sx;
13506 final int scrollY = hasDisplayList ? 0 : sy;
13507 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
13508 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080013509 }
13510 } else {
13511 // Alpha is handled by the child directly, clobber the layer's alpha
Dianne Hackborn4702a852012-08-17 15:18:29 -070013512 mPrivateFlags |= PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013513 }
13514 }
13515 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013516 } else if ((mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013517 onSetAlpha(255);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013518 mPrivateFlags &= ~PFLAG_ALPHA_SET;
Chet Haase64a48c12012-02-13 16:33:29 -080013519 }
13520
Chet Haasea1cff502012-02-21 13:43:44 -080013521 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
13522 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080013523 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013524 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080013525 } else {
13526 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080013527 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080013528 } else {
13529 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
13530 }
13531 }
13532 }
13533
Chet Haase9420abd2012-03-29 16:28:32 -070013534 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080013535 displayList = getDisplayList();
13536 if (!displayList.isValid()) {
13537 // Uncommon, but possible. If a view is removed from the hierarchy during the call
13538 // to getDisplayList(), the display list will be marked invalid and we should not
13539 // try to use it again.
13540 displayList = null;
13541 hasDisplayList = false;
13542 }
13543 }
13544
13545 if (hasNoCache) {
13546 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080013547 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080013548 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080013549 if (layer != null && layer.isValid()) {
13550 mLayerPaint.setAlpha((int) (alpha * 255));
13551 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
13552 layerRendered = true;
13553 } else {
13554 final int scrollX = hasDisplayList ? 0 : sx;
13555 final int scrollY = hasDisplayList ? 0 : sy;
13556 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080013557 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080013558 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
13559 }
13560 }
13561
13562 if (!layerRendered) {
13563 if (!hasDisplayList) {
13564 // Fast path for layouts with no backgrounds
Dianne Hackborn4702a852012-08-17 15:18:29 -070013565 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
13566 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013567 dispatchDraw(canvas);
13568 } else {
13569 draw(canvas);
13570 }
13571 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013572 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070013573 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080013574 }
13575 }
13576 } else if (cache != null) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013577 mPrivateFlags &= ~PFLAG_DIRTY_MASK;
Chet Haase64a48c12012-02-13 16:33:29 -080013578 Paint cachePaint;
13579
13580 if (layerType == LAYER_TYPE_NONE) {
13581 cachePaint = parent.mCachePaint;
13582 if (cachePaint == null) {
13583 cachePaint = new Paint();
13584 cachePaint.setDither(false);
13585 parent.mCachePaint = cachePaint;
13586 }
Chet Haase9420abd2012-03-29 16:28:32 -070013587 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080013588 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080013589 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
13590 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080013591 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080013592 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080013593 }
13594 } else {
13595 cachePaint = mLayerPaint;
13596 cachePaint.setAlpha((int) (alpha * 255));
13597 }
13598 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
13599 }
13600
Chet Haasea1cff502012-02-21 13:43:44 -080013601 if (restoreTo >= 0) {
13602 canvas.restoreToCount(restoreTo);
13603 }
Chet Haase64a48c12012-02-13 16:33:29 -080013604
13605 if (a != null && !more) {
13606 if (!hardwareAccelerated && !a.getFillAfter()) {
13607 onSetAlpha(255);
13608 }
13609 parent.finishAnimatingView(this, a);
13610 }
13611
13612 if (more && hardwareAccelerated) {
13613 // invalidation is the trigger to recreate display lists, so if we're using
13614 // display lists to render, force an invalidate to allow the animation to
13615 // continue drawing another frame
13616 parent.invalidate(true);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013617 if (a.hasAlpha() && (mPrivateFlags & PFLAG_ALPHA_SET) == PFLAG_ALPHA_SET) {
Chet Haase64a48c12012-02-13 16:33:29 -080013618 // alpha animations should cause the child to recreate its display list
13619 invalidate(true);
13620 }
13621 }
13622
13623 mRecreateDisplayList = false;
13624
13625 return more;
13626 }
13627
13628 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013629 * Manually render this view (and all of its children) to the given Canvas.
13630 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070013631 * called. When implementing a view, implement
13632 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
13633 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013634 *
13635 * @param canvas The Canvas to which the View is rendered.
13636 */
13637 public void draw(Canvas canvas) {
Romain Guy5bcdff42009-05-14 21:27:18 -070013638 final int privateFlags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070013639 final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
Romain Guy5bcdff42009-05-14 21:27:18 -070013640 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013641 mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070013642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013643 /*
13644 * Draw traversal performs several drawing steps which must be executed
13645 * in the appropriate order:
13646 *
13647 * 1. Draw the background
13648 * 2. If necessary, save the canvas' layers to prepare for fading
13649 * 3. Draw view's content
13650 * 4. Draw children
13651 * 5. If necessary, draw the fading edges and restore layers
13652 * 6. Draw decorations (scrollbars for instance)
13653 */
13654
13655 // Step 1, draw the background, if needed
13656 int saveCount;
13657
Romain Guy24443ea2009-05-11 11:56:30 -070013658 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013659 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070013660 if (background != null) {
13661 final int scrollX = mScrollX;
13662 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663
Romain Guy24443ea2009-05-11 11:56:30 -070013664 if (mBackgroundSizeChanged) {
13665 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
13666 mBackgroundSizeChanged = false;
13667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013668
Romain Guy24443ea2009-05-11 11:56:30 -070013669 if ((scrollX | scrollY) == 0) {
13670 background.draw(canvas);
13671 } else {
13672 canvas.translate(scrollX, scrollY);
13673 background.draw(canvas);
13674 canvas.translate(-scrollX, -scrollY);
13675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013676 }
13677 }
13678
13679 // skip step 2 & 5 if possible (common case)
13680 final int viewFlags = mViewFlags;
13681 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
13682 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
13683 if (!verticalEdges && !horizontalEdges) {
13684 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013685 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013686
13687 // Step 4, draw the children
13688 dispatchDraw(canvas);
13689
13690 // Step 6, draw decorations (scrollbars)
13691 onDrawScrollBars(canvas);
13692
13693 // we're done...
13694 return;
13695 }
13696
13697 /*
13698 * Here we do the full fledged routine...
13699 * (this is an uncommon case where speed matters less,
13700 * this is why we repeat some of the tests that have been
13701 * done above)
13702 */
13703
13704 boolean drawTop = false;
13705 boolean drawBottom = false;
13706 boolean drawLeft = false;
13707 boolean drawRight = false;
13708
13709 float topFadeStrength = 0.0f;
13710 float bottomFadeStrength = 0.0f;
13711 float leftFadeStrength = 0.0f;
13712 float rightFadeStrength = 0.0f;
13713
13714 // Step 2, save the canvas' layers
13715 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013716
13717 final boolean offsetRequired = isPaddingOffsetRequired();
13718 if (offsetRequired) {
13719 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720 }
13721
13722 int left = mScrollX + paddingLeft;
13723 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070013724 int top = mScrollY + getFadeTop(offsetRequired);
13725 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726
13727 if (offsetRequired) {
13728 right += getRightPaddingOffset();
13729 bottom += getBottomPaddingOffset();
13730 }
13731
13732 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070013733 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013734 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013735
13736 // clip the fade length if top and bottom fades overlap
13737 // overlapping fades produce odd-looking artifacts
13738 if (verticalEdges && (top + length > bottom - length)) {
13739 length = (bottom - top) / 2;
13740 }
13741
13742 // also clip horizontal fades if necessary
13743 if (horizontalEdges && (left + length > right - length)) {
13744 length = (right - left) / 2;
13745 }
13746
13747 if (verticalEdges) {
13748 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013749 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013750 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013751 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013752 }
13753
13754 if (horizontalEdges) {
13755 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013756 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013757 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013758 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013759 }
13760
13761 saveCount = canvas.getSaveCount();
13762
13763 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013764 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13766
13767 if (drawTop) {
13768 canvas.saveLayer(left, top, right, top + length, null, flags);
13769 }
13770
13771 if (drawBottom) {
13772 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13773 }
13774
13775 if (drawLeft) {
13776 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13777 }
13778
13779 if (drawRight) {
13780 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13781 }
13782 } else {
13783 scrollabilityCache.setFadeColor(solidColor);
13784 }
13785
13786 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013787 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013788
13789 // Step 4, draw the children
13790 dispatchDraw(canvas);
13791
13792 // Step 5, draw the fade effect and restore layers
13793 final Paint p = scrollabilityCache.paint;
13794 final Matrix matrix = scrollabilityCache.matrix;
13795 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013796
13797 if (drawTop) {
13798 matrix.setScale(1, fadeHeight * topFadeStrength);
13799 matrix.postTranslate(left, top);
13800 fade.setLocalMatrix(matrix);
13801 canvas.drawRect(left, top, right, top + length, p);
13802 }
13803
13804 if (drawBottom) {
13805 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13806 matrix.postRotate(180);
13807 matrix.postTranslate(left, bottom);
13808 fade.setLocalMatrix(matrix);
13809 canvas.drawRect(left, bottom - length, right, bottom, p);
13810 }
13811
13812 if (drawLeft) {
13813 matrix.setScale(1, fadeHeight * leftFadeStrength);
13814 matrix.postRotate(-90);
13815 matrix.postTranslate(left, top);
13816 fade.setLocalMatrix(matrix);
13817 canvas.drawRect(left, top, left + length, bottom, p);
13818 }
13819
13820 if (drawRight) {
13821 matrix.setScale(1, fadeHeight * rightFadeStrength);
13822 matrix.postRotate(90);
13823 matrix.postTranslate(right, top);
13824 fade.setLocalMatrix(matrix);
13825 canvas.drawRect(right - length, top, right, bottom, p);
13826 }
13827
13828 canvas.restoreToCount(saveCount);
13829
13830 // Step 6, draw decorations (scrollbars)
13831 onDrawScrollBars(canvas);
13832 }
13833
13834 /**
13835 * Override this if your view is known to always be drawn on top of a solid color background,
13836 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13837 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13838 * should be set to 0xFF.
13839 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013840 * @see #setVerticalFadingEdgeEnabled(boolean)
13841 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013842 *
13843 * @return The known solid color background for this view, or 0 if the color may vary
13844 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013845 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013846 public int getSolidColor() {
13847 return 0;
13848 }
13849
13850 /**
13851 * Build a human readable string representation of the specified view flags.
13852 *
13853 * @param flags the view flags to convert to a string
13854 * @return a String representing the supplied flags
13855 */
13856 private static String printFlags(int flags) {
13857 String output = "";
13858 int numFlags = 0;
13859 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13860 output += "TAKES_FOCUS";
13861 numFlags++;
13862 }
13863
13864 switch (flags & VISIBILITY_MASK) {
13865 case INVISIBLE:
13866 if (numFlags > 0) {
13867 output += " ";
13868 }
13869 output += "INVISIBLE";
13870 // USELESS HERE numFlags++;
13871 break;
13872 case GONE:
13873 if (numFlags > 0) {
13874 output += " ";
13875 }
13876 output += "GONE";
13877 // USELESS HERE numFlags++;
13878 break;
13879 default:
13880 break;
13881 }
13882 return output;
13883 }
13884
13885 /**
13886 * Build a human readable string representation of the specified private
13887 * view flags.
13888 *
13889 * @param privateFlags the private view flags to convert to a string
13890 * @return a String representing the supplied flags
13891 */
13892 private static String printPrivateFlags(int privateFlags) {
13893 String output = "";
13894 int numFlags = 0;
13895
Dianne Hackborn4702a852012-08-17 15:18:29 -070013896 if ((privateFlags & PFLAG_WANTS_FOCUS) == PFLAG_WANTS_FOCUS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013897 output += "WANTS_FOCUS";
13898 numFlags++;
13899 }
13900
Dianne Hackborn4702a852012-08-17 15:18:29 -070013901 if ((privateFlags & PFLAG_FOCUSED) == PFLAG_FOCUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013902 if (numFlags > 0) {
13903 output += " ";
13904 }
13905 output += "FOCUSED";
13906 numFlags++;
13907 }
13908
Dianne Hackborn4702a852012-08-17 15:18:29 -070013909 if ((privateFlags & PFLAG_SELECTED) == PFLAG_SELECTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 if (numFlags > 0) {
13911 output += " ";
13912 }
13913 output += "SELECTED";
13914 numFlags++;
13915 }
13916
Dianne Hackborn4702a852012-08-17 15:18:29 -070013917 if ((privateFlags & PFLAG_IS_ROOT_NAMESPACE) == PFLAG_IS_ROOT_NAMESPACE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918 if (numFlags > 0) {
13919 output += " ";
13920 }
13921 output += "IS_ROOT_NAMESPACE";
13922 numFlags++;
13923 }
13924
Dianne Hackborn4702a852012-08-17 15:18:29 -070013925 if ((privateFlags & PFLAG_HAS_BOUNDS) == PFLAG_HAS_BOUNDS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926 if (numFlags > 0) {
13927 output += " ";
13928 }
13929 output += "HAS_BOUNDS";
13930 numFlags++;
13931 }
13932
Dianne Hackborn4702a852012-08-17 15:18:29 -070013933 if ((privateFlags & PFLAG_DRAWN) == PFLAG_DRAWN) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 if (numFlags > 0) {
13935 output += " ";
13936 }
13937 output += "DRAWN";
13938 // USELESS HERE numFlags++;
13939 }
13940 return output;
13941 }
13942
13943 /**
13944 * <p>Indicates whether or not this view's layout will be requested during
13945 * the next hierarchy layout pass.</p>
13946 *
13947 * @return true if the layout will be forced during next layout pass
13948 */
13949 public boolean isLayoutRequested() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070013950 return (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 }
13952
13953 /**
13954 * Assign a size and position to a view and all of its
13955 * descendants
13956 *
13957 * <p>This is the second phase of the layout mechanism.
13958 * (The first is measuring). In this phase, each parent calls
13959 * layout on all of its children to position them.
13960 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013961 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013962 *
Chet Haase9c087442011-01-12 16:20:16 -080013963 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013964 * Derived classes with children should override
13965 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013966 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013967 *
13968 * @param l Left position, relative to parent
13969 * @param t Top position, relative to parent
13970 * @param r Right position, relative to parent
13971 * @param b Bottom position, relative to parent
13972 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013973 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013974 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013975 int oldL = mLeft;
13976 int oldT = mTop;
13977 int oldB = mBottom;
13978 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013979 boolean changed = setFrame(l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013980 if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 onLayout(changed, l, t, r, b);
Dianne Hackborn4702a852012-08-17 15:18:29 -070013982 mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013983
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013984 ListenerInfo li = mListenerInfo;
13985 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013986 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013987 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013988 int numListeners = listenersCopy.size();
13989 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013990 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013991 }
13992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013993 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070013994 mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013995 }
13996
13997 /**
13998 * Called from layout when this view should
13999 * assign a size and position to each of its children.
14000 *
14001 * Derived classes with children should override
14002 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070014003 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 * @param changed This is a new size or position for this view
14005 * @param left Left position, relative to parent
14006 * @param top Top position, relative to parent
14007 * @param right Right position, relative to parent
14008 * @param bottom Bottom position, relative to parent
14009 */
14010 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
14011 }
14012
14013 /**
14014 * Assign a size and position to this view.
14015 *
14016 * This is called from layout.
14017 *
14018 * @param left Left position, relative to parent
14019 * @param top Top position, relative to parent
14020 * @param right Right position, relative to parent
14021 * @param bottom Bottom position, relative to parent
14022 * @return true if the new size and position are different than the
14023 * previous ones
14024 * {@hide}
14025 */
14026 protected boolean setFrame(int left, int top, int right, int bottom) {
14027 boolean changed = false;
14028
14029 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070014030 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 + right + "," + bottom + ")");
14032 }
14033
14034 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
14035 changed = true;
14036
14037 // Remember our drawn bit
Dianne Hackborn4702a852012-08-17 15:18:29 -070014038 int drawn = mPrivateFlags & PFLAG_DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014040 int oldWidth = mRight - mLeft;
14041 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070014042 int newWidth = right - left;
14043 int newHeight = bottom - top;
14044 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
14045
14046 // Invalidate our old position
14047 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014048
14049 mLeft = left;
14050 mTop = top;
14051 mRight = right;
14052 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070014053 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080014054 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
14055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014056
Dianne Hackborn4702a852012-08-17 15:18:29 -070014057 mPrivateFlags |= PFLAG_HAS_BOUNDS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014059
Chet Haase75755e22011-07-18 17:48:25 -070014060 if (sizeChanged) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014061 if ((mPrivateFlags & PFLAG_PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014062 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014063 if (mTransformationInfo != null) {
14064 mTransformationInfo.mMatrixDirty = true;
14065 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014066 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014067 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
14068 }
14069
14070 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
14071 // If we are visible, force the DRAWN bit to on so that
14072 // this invalidate will go through (at least to our parent).
14073 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080014074 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 // the DRAWN bit.
Dianne Hackborn4702a852012-08-17 15:18:29 -070014076 mPrivateFlags |= PFLAG_DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070014077 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080014078 // parent display list may need to be recreated based on a change in the bounds
14079 // of any child
14080 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014081 }
14082
14083 // Reset drawn bit to original value (invalidate turns it off)
14084 mPrivateFlags |= drawn;
14085
14086 mBackgroundSizeChanged = true;
14087 }
14088 return changed;
14089 }
14090
14091 /**
14092 * Finalize inflating a view from XML. This is called as the last phase
14093 * of inflation, after all child views have been added.
14094 *
14095 * <p>Even if the subclass overrides onFinishInflate, they should always be
14096 * sure to call the super method, so that we get called.
14097 */
14098 protected void onFinishInflate() {
14099 }
14100
14101 /**
14102 * Returns the resources associated with this view.
14103 *
14104 * @return Resources object.
14105 */
14106 public Resources getResources() {
14107 return mResources;
14108 }
14109
14110 /**
14111 * Invalidates the specified Drawable.
14112 *
14113 * @param drawable the drawable to invalidate
14114 */
14115 public void invalidateDrawable(Drawable drawable) {
14116 if (verifyDrawable(drawable)) {
14117 final Rect dirty = drawable.getBounds();
14118 final int scrollX = mScrollX;
14119 final int scrollY = mScrollY;
14120
14121 invalidate(dirty.left + scrollX, dirty.top + scrollY,
14122 dirty.right + scrollX, dirty.bottom + scrollY);
14123 }
14124 }
14125
14126 /**
14127 * Schedules an action on a drawable to occur at a specified time.
14128 *
14129 * @param who the recipient of the action
14130 * @param what the action to run on the drawable
14131 * @param when the time at which the action must occur. Uses the
14132 * {@link SystemClock#uptimeMillis} timebase.
14133 */
14134 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080014135 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014136 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080014137 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014138 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
14139 Choreographer.CALLBACK_ANIMATION, what, who,
14140 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080014141 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080014142 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080014143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014144 }
14145 }
14146
14147 /**
14148 * Cancels a scheduled action on a drawable.
14149 *
14150 * @param who the recipient of the action
14151 * @param what the action to cancel
14152 */
14153 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080014154 if (verifyDrawable(who) && what != null) {
14155 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014156 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14157 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080014158 } else {
14159 ViewRootImpl.getRunQueue().removeCallbacks(what);
14160 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014161 }
14162 }
14163
14164 /**
14165 * Unschedule any events associated with the given Drawable. This can be
14166 * used when selecting a new Drawable into a view, so that the previous
14167 * one is completely unscheduled.
14168 *
14169 * @param who The Drawable to unschedule.
14170 *
14171 * @see #drawableStateChanged
14172 */
14173 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080014174 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070014175 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
14176 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014177 }
14178 }
14179
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070014180 /**
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014181 * Resolve the Drawables depending on the layout direction. This is implicitly supposing
14182 * that the View directionality can and will be resolved before its Drawables.
14183 *
14184 * Will call {@link View#onResolveDrawables} when resolution is done.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014185 *
14186 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014187 */
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014188 protected void resolveDrawables() {
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014189 if (mBackground != null) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014190 mBackground.setLayoutDirection(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014191 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014192 mPrivateFlags2 |= PFLAG2_DRAWABLE_RESOLVED;
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014193 onResolveDrawables(getLayoutDirection());
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014194 }
14195
14196 /**
14197 * Called when layout direction has been resolved.
14198 *
14199 * The default implementation does nothing.
14200 *
14201 * @param layoutDirection The resolved layout direction.
14202 *
Fabrice Di Meglio49b0a9b2012-09-18 12:06:13 -070014203 * @see #LAYOUT_DIRECTION_LTR
14204 * @see #LAYOUT_DIRECTION_RTL
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014205 *
14206 * @hide
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014207 */
14208 public void onResolveDrawables(int layoutDirection) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070014209 }
14210
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014211 /**
14212 * @hide
14213 */
14214 protected void resetResolvedDrawables() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014215 mPrivateFlags2 &= ~PFLAG2_DRAWABLE_RESOLVED;
14216 }
14217
14218 private boolean isDrawablesResolved() {
14219 return (mPrivateFlags2 & PFLAG2_DRAWABLE_RESOLVED) == PFLAG2_DRAWABLE_RESOLVED;
14220 }
14221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014222 /**
14223 * If your view subclass is displaying its own Drawable objects, it should
14224 * override this function and return true for any Drawable it is
14225 * displaying. This allows animations for those drawables to be
14226 * scheduled.
14227 *
14228 * <p>Be sure to call through to the super class when overriding this
14229 * function.
14230 *
14231 * @param who The Drawable to verify. Return true if it is one you are
14232 * displaying, else return the result of calling through to the
14233 * super class.
14234 *
14235 * @return boolean If true than the Drawable is being displayed in the
14236 * view; else false and it is not allowed to animate.
14237 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014238 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
14239 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014240 */
14241 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014242 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014243 }
14244
14245 /**
14246 * This function is called whenever the state of the view changes in such
14247 * a way that it impacts the state of drawables being shown.
14248 *
14249 * <p>Be sure to call through to the superclass when overriding this
14250 * function.
14251 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014252 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014253 */
14254 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014255 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 if (d != null && d.isStateful()) {
14257 d.setState(getDrawableState());
14258 }
14259 }
14260
14261 /**
14262 * Call this to force a view to update its drawable state. This will cause
14263 * drawableStateChanged to be called on this view. Views that are interested
14264 * in the new state should call getDrawableState.
14265 *
14266 * @see #drawableStateChanged
14267 * @see #getDrawableState
14268 */
14269 public void refreshDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014270 mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014271 drawableStateChanged();
14272
14273 ViewParent parent = mParent;
14274 if (parent != null) {
14275 parent.childDrawableStateChanged(this);
14276 }
14277 }
14278
14279 /**
14280 * Return an array of resource IDs of the drawable states representing the
14281 * current state of the view.
14282 *
14283 * @return The current drawable state
14284 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014285 * @see Drawable#setState(int[])
14286 * @see #drawableStateChanged()
14287 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014288 */
14289 public final int[] getDrawableState() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014290 if ((mDrawableState != null) && ((mPrivateFlags & PFLAG_DRAWABLE_STATE_DIRTY) == 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014291 return mDrawableState;
14292 } else {
14293 mDrawableState = onCreateDrawableState(0);
Dianne Hackborn4702a852012-08-17 15:18:29 -070014294 mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014295 return mDrawableState;
14296 }
14297 }
14298
14299 /**
14300 * Generate the new {@link android.graphics.drawable.Drawable} state for
14301 * this view. This is called by the view
14302 * system when the cached Drawable state is determined to be invalid. To
14303 * retrieve the current state, you should use {@link #getDrawableState}.
14304 *
14305 * @param extraSpace if non-zero, this is the number of extra entries you
14306 * would like in the returned array in which you can place your own
14307 * states.
14308 *
14309 * @return Returns an array holding the current {@link Drawable} state of
14310 * the view.
14311 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014312 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014313 */
14314 protected int[] onCreateDrawableState(int extraSpace) {
14315 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
14316 mParent instanceof View) {
14317 return ((View) mParent).onCreateDrawableState(extraSpace);
14318 }
14319
14320 int[] drawableState;
14321
14322 int privateFlags = mPrivateFlags;
14323
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014324 int viewStateIndex = 0;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014325 if ((privateFlags & PFLAG_PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014326 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
14327 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014328 if ((privateFlags & PFLAG_SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014329 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014330 if ((privateFlags & PFLAG_ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070014331 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
14332 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080014333 // This is set if HW acceleration is requested, even if the current
14334 // process doesn't allow it. This is just to allow app preview
14335 // windows to better match their app.
14336 viewStateIndex |= VIEW_STATE_ACCELERATED;
14337 }
Dianne Hackborn4702a852012-08-17 15:18:29 -070014338 if ((privateFlags & PFLAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014339
Christopher Tate3d4bf172011-03-28 16:16:46 -070014340 final int privateFlags2 = mPrivateFlags2;
Dianne Hackborn4702a852012-08-17 15:18:29 -070014341 if ((privateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
14342 if ((privateFlags2 & PFLAG2_DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
Christopher Tate3d4bf172011-03-28 16:16:46 -070014343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014344 drawableState = VIEW_STATE_SETS[viewStateIndex];
14345
14346 //noinspection ConstantIfStatement
14347 if (false) {
14348 Log.i("View", "drawableStateIndex=" + viewStateIndex);
14349 Log.i("View", toString()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014350 + " pressed=" + ((privateFlags & PFLAG_PRESSED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014351 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
14352 + " fo=" + hasFocus()
Dianne Hackborn4702a852012-08-17 15:18:29 -070014353 + " sl=" + ((privateFlags & PFLAG_SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014354 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014355 + ": " + Arrays.toString(drawableState));
14356 }
14357
14358 if (extraSpace == 0) {
14359 return drawableState;
14360 }
14361
14362 final int[] fullState;
14363 if (drawableState != null) {
14364 fullState = new int[drawableState.length + extraSpace];
14365 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
14366 } else {
14367 fullState = new int[extraSpace];
14368 }
14369
14370 return fullState;
14371 }
14372
14373 /**
14374 * Merge your own state values in <var>additionalState</var> into the base
14375 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014376 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014377 *
14378 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070014379 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014380 * own additional state values.
14381 *
14382 * @param additionalState The additional state values you would like
14383 * added to <var>baseState</var>; this array is not modified.
14384 *
14385 * @return As a convenience, the <var>baseState</var> array you originally
14386 * passed into the function is returned.
14387 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014388 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014389 */
14390 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
14391 final int N = baseState.length;
14392 int i = N - 1;
14393 while (i >= 0 && baseState[i] == 0) {
14394 i--;
14395 }
14396 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
14397 return baseState;
14398 }
14399
14400 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070014401 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
14402 * on all Drawable objects associated with this view.
14403 */
14404 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014405 if (mBackground != null) {
14406 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070014407 }
14408 }
14409
14410 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 * Sets the background color for this view.
14412 * @param color the color of the background
14413 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014414 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014415 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070014416 if (mBackground instanceof ColorDrawable) {
Romain Guy5f49c302012-09-06 16:33:31 -070014417 ((ColorDrawable) mBackground.mutate()).setColor(color);
Romain Guy7d3082a2012-07-11 17:52:54 -070014418 computeOpaqueFlags();
Chet Haase70d4ba12010-10-06 09:46:45 -070014419 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070014420 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070014421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014422 }
14423
14424 /**
14425 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070014426 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014427 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070014428 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014429 * @attr ref android.R.styleable#View_background
14430 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000014431 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014432 public void setBackgroundResource(int resid) {
14433 if (resid != 0 && resid == mBackgroundResource) {
14434 return;
14435 }
14436
14437 Drawable d= null;
14438 if (resid != 0) {
14439 d = mResources.getDrawable(resid);
14440 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014441 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014442
14443 mBackgroundResource = resid;
14444 }
14445
14446 /**
14447 * Set the background to a given Drawable, or remove the background. If the
14448 * background has padding, this View's padding is set to the background's
14449 * padding. However, when a background is removed, this View's padding isn't
14450 * touched. If setting the padding is desired, please use
14451 * {@link #setPadding(int, int, int, int)}.
14452 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014453 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014454 * background
14455 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014456 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070014457 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070014458 setBackgroundDrawable(background);
14459 }
14460
14461 /**
14462 * @deprecated use {@link #setBackground(Drawable)} instead
14463 */
14464 @Deprecated
14465 public void setBackgroundDrawable(Drawable background) {
Romain Guy846a5332012-07-11 17:44:57 -070014466 computeOpaqueFlags();
14467
Philip Milne6c8ea062012-04-03 17:38:43 -070014468 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070014469 return;
14470 }
14471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014472 boolean requestLayout = false;
14473
14474 mBackgroundResource = 0;
14475
14476 /*
14477 * Regardless of whether we're setting a new background or not, we want
14478 * to clear the previous drawable.
14479 */
Philip Milne6c8ea062012-04-03 17:38:43 -070014480 if (mBackground != null) {
14481 mBackground.setCallback(null);
14482 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014483 }
14484
Philip Milne6c8ea062012-04-03 17:38:43 -070014485 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014486 Rect padding = sThreadLocal.get();
14487 if (padding == null) {
14488 padding = new Rect();
14489 sThreadLocal.set(padding);
14490 }
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070014491 resetResolvedDrawables();
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014492 background.setLayoutDirection(getLayoutDirection());
Philip Milne6c8ea062012-04-03 17:38:43 -070014493 if (background.getPadding(padding)) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014494 resetResolvedPadding();
Fabrice Di Megliob03b4342012-06-04 12:55:30 -070014495 switch (background.getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014496 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014497 mUserPaddingLeftInitial = padding.right;
14498 mUserPaddingRightInitial = padding.left;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014499 internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014500 break;
14501 case LAYOUT_DIRECTION_LTR:
14502 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014503 mUserPaddingLeftInitial = padding.left;
14504 mUserPaddingRightInitial = padding.right;
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014505 internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014507 }
14508
14509 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
14510 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070014511 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
14512 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014513 requestLayout = true;
14514 }
14515
Philip Milne6c8ea062012-04-03 17:38:43 -070014516 background.setCallback(this);
14517 if (background.isStateful()) {
14518 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014519 }
Philip Milne6c8ea062012-04-03 17:38:43 -070014520 background.setVisible(getVisibility() == VISIBLE, false);
14521 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014522
Dianne Hackborn4702a852012-08-17 15:18:29 -070014523 if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
14524 mPrivateFlags &= ~PFLAG_SKIP_DRAW;
14525 mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014526 requestLayout = true;
14527 }
14528 } else {
14529 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070014530 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014531
Dianne Hackborn4702a852012-08-17 15:18:29 -070014532 if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014533 /*
14534 * This view ONLY drew the background before and we're removing
14535 * the background, so now it won't draw anything
14536 * (hence we SKIP_DRAW)
14537 */
Dianne Hackborn4702a852012-08-17 15:18:29 -070014538 mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
14539 mPrivateFlags |= PFLAG_SKIP_DRAW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014540 }
14541
14542 /*
14543 * When the background is set, we try to apply its padding to this
14544 * View. When the background is removed, we don't touch this View's
14545 * padding. This is noted in the Javadocs. Hence, we don't need to
14546 * requestLayout(), the invalidate() below is sufficient.
14547 */
14548
14549 // The old background's minimum size could have affected this
14550 // View's layout, so let's requestLayout
14551 requestLayout = true;
14552 }
14553
Romain Guy8f1344f52009-05-15 16:03:59 -070014554 computeOpaqueFlags();
14555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014556 if (requestLayout) {
14557 requestLayout();
14558 }
14559
14560 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080014561 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014562 }
14563
14564 /**
14565 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070014566 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014567 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070014568 *
14569 * @see #setBackground(Drawable)
14570 *
14571 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014572 */
14573 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014574 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014575 }
14576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014577 /**
14578 * Sets the padding. The view may add on the space required to display
14579 * the scrollbars, depending on the style and visibility of the scrollbars.
14580 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
14581 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
14582 * from the values set in this call.
14583 *
14584 * @attr ref android.R.styleable#View_padding
14585 * @attr ref android.R.styleable#View_paddingBottom
14586 * @attr ref android.R.styleable#View_paddingLeft
14587 * @attr ref android.R.styleable#View_paddingRight
14588 * @attr ref android.R.styleable#View_paddingTop
14589 * @param left the left padding in pixels
14590 * @param top the top padding in pixels
14591 * @param right the right padding in pixels
14592 * @param bottom the bottom padding in pixels
14593 */
14594 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014595 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014596
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014597 mUserPaddingStart = UNDEFINED_PADDING;
14598 mUserPaddingEnd = UNDEFINED_PADDING;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014599
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014600 mUserPaddingLeftInitial = left;
14601 mUserPaddingRightInitial = right;
14602
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014603 internalSetPadding(left, top, right, bottom);
14604 }
14605
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014606 /**
14607 * @hide
14608 */
14609 protected void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080014610 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014611 mUserPaddingRight = right;
14612 mUserPaddingBottom = bottom;
14613
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014614 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014615 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070014616
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014617 // Common case is there are no scroll bars.
14618 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014619 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080014620 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014621 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080014622 switch (mVerticalScrollbarPosition) {
14623 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Meglioc91b6ca2012-06-22 14:51:15 -070014624 if (isLayoutRtl()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014625 left += offset;
14626 } else {
14627 right += offset;
14628 }
14629 break;
Adam Powell20232d02010-12-08 21:08:53 -080014630 case SCROLLBAR_POSITION_RIGHT:
14631 right += offset;
14632 break;
14633 case SCROLLBAR_POSITION_LEFT:
14634 left += offset;
14635 break;
14636 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014637 }
Adam Powell20232d02010-12-08 21:08:53 -080014638 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014639 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
14640 ? 0 : getHorizontalScrollbarHeight();
14641 }
14642 }
Romain Guy8506ab42009-06-11 17:35:47 -070014643
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014644 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014645 changed = true;
14646 mPaddingLeft = left;
14647 }
14648 if (mPaddingTop != top) {
14649 changed = true;
14650 mPaddingTop = top;
14651 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014652 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014653 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014654 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014655 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014656 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014657 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070014658 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659 }
14660
14661 if (changed) {
14662 requestLayout();
14663 }
14664 }
14665
14666 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014667 * Sets the relative padding. The view may add on the space required to display
14668 * the scrollbars, depending on the style and visibility of the scrollbars.
14669 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
14670 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
14671 * from the values set in this call.
14672 *
14673 * @attr ref android.R.styleable#View_padding
14674 * @attr ref android.R.styleable#View_paddingBottom
14675 * @attr ref android.R.styleable#View_paddingStart
14676 * @attr ref android.R.styleable#View_paddingEnd
14677 * @attr ref android.R.styleable#View_paddingTop
14678 * @param start the start padding in pixels
14679 * @param top the top padding in pixels
14680 * @param end the end padding in pixels
14681 * @param bottom the bottom padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014682 */
14683 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070014684 resetResolvedPadding();
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014685
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070014686 mUserPaddingStart = start;
14687 mUserPaddingEnd = end;
14688
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014689 switch(getLayoutDirection()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014690 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014691 mUserPaddingLeftInitial = end;
14692 mUserPaddingRightInitial = start;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014693 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014694 break;
14695 case LAYOUT_DIRECTION_LTR:
14696 default:
Fabrice Di Meglio7e7786a2012-09-11 13:34:15 -070014697 mUserPaddingLeftInitial = start;
14698 mUserPaddingRightInitial = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080014699 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014700 }
14701 }
14702
14703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014704 * Returns the top padding of this view.
14705 *
14706 * @return the top padding in pixels
14707 */
14708 public int getPaddingTop() {
14709 return mPaddingTop;
14710 }
14711
14712 /**
14713 * Returns the bottom padding of this view. If there are inset and enabled
14714 * scrollbars, this value may include the space required to display the
14715 * scrollbars as well.
14716 *
14717 * @return the bottom padding in pixels
14718 */
14719 public int getPaddingBottom() {
14720 return mPaddingBottom;
14721 }
14722
14723 /**
14724 * Returns the left padding of this view. If there are inset and enabled
14725 * scrollbars, this value may include the space required to display the
14726 * scrollbars as well.
14727 *
14728 * @return the left padding in pixels
14729 */
14730 public int getPaddingLeft() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014731 if (!isPaddingResolved()) {
14732 resolvePadding();
14733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014734 return mPaddingLeft;
14735 }
14736
14737 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014738 * Returns the start padding of this view depending on its resolved layout direction.
14739 * If there are inset and enabled scrollbars, this value may include the space
14740 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014741 *
14742 * @return the start padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014743 */
14744 public int getPaddingStart() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014745 if (!isPaddingResolved()) {
14746 resolvePadding();
14747 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014748 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014749 mPaddingRight : mPaddingLeft;
14750 }
14751
14752 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014753 * Returns the right padding of this view. If there are inset and enabled
14754 * scrollbars, this value may include the space required to display the
14755 * scrollbars as well.
14756 *
14757 * @return the right padding in pixels
14758 */
14759 public int getPaddingRight() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014760 if (!isPaddingResolved()) {
14761 resolvePadding();
14762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014763 return mPaddingRight;
14764 }
14765
14766 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070014767 * Returns the end padding of this view depending on its resolved layout direction.
14768 * If there are inset and enabled scrollbars, this value may include the space
14769 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014770 *
14771 * @return the end padding in pixels
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014772 */
14773 public int getPaddingEnd() {
Fabrice Di Meglio23c89fd2012-08-13 12:17:42 -070014774 if (!isPaddingResolved()) {
14775 resolvePadding();
14776 }
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -070014777 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014778 mPaddingLeft : mPaddingRight;
14779 }
14780
14781 /**
14782 * Return if the padding as been set thru relative values
14783 * {@link #setPaddingRelative(int, int, int, int)} or thru
14784 * @attr ref android.R.styleable#View_paddingStart or
14785 * @attr ref android.R.styleable#View_paddingEnd
14786 *
14787 * @return true if the padding is relative or false if it is not.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014788 */
14789 public boolean isPaddingRelative() {
Fabrice Di Meglio016456e2012-07-17 20:35:48 -070014790 return (mUserPaddingStart != UNDEFINED_PADDING || mUserPaddingEnd != UNDEFINED_PADDING);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070014791 }
14792
14793 /**
Philip Milne1557fd72012-04-04 23:41:34 -070014794 * @hide
14795 */
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014796 public void resetPaddingToInitialValues() {
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014797 if (isRtlCompatibilityMode()) {
14798 mPaddingLeft = mUserPaddingLeftInitial;
14799 mPaddingRight = mUserPaddingRightInitial;
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014800 return;
14801 }
14802 if (isLayoutRtl()) {
14803 mPaddingLeft = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingLeftInitial;
14804 mPaddingRight = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014805 } else {
Fabrice Di Meglio84ebb352012-10-11 16:27:37 -070014806 mPaddingLeft = (mUserPaddingStart >= 0) ? mUserPaddingStart : mUserPaddingLeftInitial;
14807 mPaddingRight = (mUserPaddingEnd >= 0) ? mUserPaddingEnd : mUserPaddingRightInitial;
Fabrice Di Meglio80766f12012-10-01 13:13:12 -070014808 }
Fabrice Di Meglio47fb1912012-09-28 19:50:18 -070014809 }
14810
14811 /**
14812 * @hide
14813 */
Philip Milne7a23b492012-04-24 22:12:36 -070014814 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070014815 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070014816 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070014817 }
14818 return mLayoutInsets;
14819 }
14820
14821 /**
14822 * @hide
14823 */
14824 public void setLayoutInsets(Insets layoutInsets) {
14825 mLayoutInsets = layoutInsets;
14826 }
14827
14828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014829 * Changes the selection state of this view. A view can be selected or not.
14830 * Note that selection is not the same as focus. Views are typically
14831 * selected in the context of an AdapterView like ListView or GridView;
14832 * the selected view is the view that is highlighted.
14833 *
14834 * @param selected true if the view must be selected, false otherwise
14835 */
14836 public void setSelected(boolean selected) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014837 if (((mPrivateFlags & PFLAG_SELECTED) != 0) != selected) {
14838 mPrivateFlags = (mPrivateFlags & ~PFLAG_SELECTED) | (selected ? PFLAG_SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014839 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014840 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014841 refreshDrawableState();
14842 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014843 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14844 notifyAccessibilityStateChanged();
14845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 }
14847 }
14848
14849 /**
14850 * Dispatch setSelected to all of this View's children.
14851 *
14852 * @see #setSelected(boolean)
14853 *
14854 * @param selected The new selected state
14855 */
14856 protected void dispatchSetSelected(boolean selected) {
14857 }
14858
14859 /**
14860 * Indicates the selection state of this view.
14861 *
14862 * @return true if the view is selected, false otherwise
14863 */
14864 @ViewDebug.ExportedProperty
14865 public boolean isSelected() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014866 return (mPrivateFlags & PFLAG_SELECTED) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014867 }
14868
14869 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014870 * Changes the activated state of this view. A view can be activated or not.
14871 * Note that activation is not the same as selection. Selection is
14872 * a transient property, representing the view (hierarchy) the user is
14873 * currently interacting with. Activation is a longer-term state that the
14874 * user can move views in and out of. For example, in a list view with
14875 * single or multiple selection enabled, the views in the current selection
14876 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14877 * here.) The activated state is propagated down to children of the view it
14878 * is set on.
14879 *
14880 * @param activated true if the view must be activated, false otherwise
14881 */
14882 public void setActivated(boolean activated) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014883 if (((mPrivateFlags & PFLAG_ACTIVATED) != 0) != activated) {
14884 mPrivateFlags = (mPrivateFlags & ~PFLAG_ACTIVATED) | (activated ? PFLAG_ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014885 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014886 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014887 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014888 }
14889 }
14890
14891 /**
14892 * Dispatch setActivated to all of this View's children.
14893 *
14894 * @see #setActivated(boolean)
14895 *
14896 * @param activated The new activated state
14897 */
14898 protected void dispatchSetActivated(boolean activated) {
14899 }
14900
14901 /**
14902 * Indicates the activation state of this view.
14903 *
14904 * @return true if the view is activated, false otherwise
14905 */
14906 @ViewDebug.ExportedProperty
14907 public boolean isActivated() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070014908 return (mPrivateFlags & PFLAG_ACTIVATED) != 0;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014909 }
14910
14911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014912 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14913 * observer can be used to get notifications when global events, like
14914 * layout, happen.
14915 *
14916 * The returned ViewTreeObserver observer is not guaranteed to remain
14917 * valid for the lifetime of this View. If the caller of this method keeps
14918 * a long-lived reference to ViewTreeObserver, it should always check for
14919 * the return value of {@link ViewTreeObserver#isAlive()}.
14920 *
14921 * @return The ViewTreeObserver for this view's hierarchy.
14922 */
14923 public ViewTreeObserver getViewTreeObserver() {
14924 if (mAttachInfo != null) {
14925 return mAttachInfo.mTreeObserver;
14926 }
14927 if (mFloatingTreeObserver == null) {
14928 mFloatingTreeObserver = new ViewTreeObserver();
14929 }
14930 return mFloatingTreeObserver;
14931 }
14932
14933 /**
14934 * <p>Finds the topmost view in the current view hierarchy.</p>
14935 *
14936 * @return the topmost view containing this view
14937 */
14938 public View getRootView() {
14939 if (mAttachInfo != null) {
14940 final View v = mAttachInfo.mRootView;
14941 if (v != null) {
14942 return v;
14943 }
14944 }
Romain Guy8506ab42009-06-11 17:35:47 -070014945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014946 View parent = this;
14947
14948 while (parent.mParent != null && parent.mParent instanceof View) {
14949 parent = (View) parent.mParent;
14950 }
14951
14952 return parent;
14953 }
14954
14955 /**
14956 * <p>Computes the coordinates of this view on the screen. The argument
14957 * must be an array of two integers. After the method returns, the array
14958 * contains the x and y location in that order.</p>
14959 *
14960 * @param location an array of two integers in which to hold the coordinates
14961 */
14962 public void getLocationOnScreen(int[] location) {
14963 getLocationInWindow(location);
14964
14965 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014966 if (info != null) {
14967 location[0] += info.mWindowLeft;
14968 location[1] += info.mWindowTop;
14969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014970 }
14971
14972 /**
14973 * <p>Computes the coordinates of this view in its window. The argument
14974 * must be an array of two integers. After the method returns, the array
14975 * contains the x and y location in that order.</p>
14976 *
14977 * @param location an array of two integers in which to hold the coordinates
14978 */
14979 public void getLocationInWindow(int[] location) {
14980 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014981 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014982 }
14983
Gilles Debunne6583ce52011-12-06 18:09:02 -080014984 if (mAttachInfo == null) {
14985 // When the view is not attached to a window, this method does not make sense
14986 location[0] = location[1] = 0;
14987 return;
14988 }
14989
Gilles Debunnecea45132011-11-24 02:19:27 +010014990 float[] position = mAttachInfo.mTmpTransformLocation;
14991 position[0] = position[1] = 0.0f;
14992
14993 if (!hasIdentityMatrix()) {
14994 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014996
Gilles Debunnecea45132011-11-24 02:19:27 +010014997 position[0] += mLeft;
14998 position[1] += mTop;
14999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015000 ViewParent viewParent = mParent;
15001 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010015002 final View view = (View) viewParent;
15003
15004 position[0] -= view.mScrollX;
15005 position[1] -= view.mScrollY;
15006
15007 if (!view.hasIdentityMatrix()) {
15008 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070015009 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015010
15011 position[0] += view.mLeft;
15012 position[1] += view.mTop;
15013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015014 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070015015 }
Romain Guy8506ab42009-06-11 17:35:47 -070015016
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015017 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015018 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010015019 final ViewRootImpl vr = (ViewRootImpl) viewParent;
15020 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015021 }
Gilles Debunnecea45132011-11-24 02:19:27 +010015022
15023 location[0] = (int) (position[0] + 0.5f);
15024 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 }
15026
15027 /**
15028 * {@hide}
15029 * @param id the id of the view to be found
15030 * @return the view of the specified id, null if cannot be found
15031 */
15032 protected View findViewTraversal(int id) {
15033 if (id == mID) {
15034 return this;
15035 }
15036 return null;
15037 }
15038
15039 /**
15040 * {@hide}
15041 * @param tag the tag of the view to be found
15042 * @return the view of specified tag, null if cannot be found
15043 */
15044 protected View findViewWithTagTraversal(Object tag) {
15045 if (tag != null && tag.equals(mTag)) {
15046 return this;
15047 }
15048 return null;
15049 }
15050
15051 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015052 * {@hide}
15053 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070015054 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080015055 * @return The first view that matches the predicate or null.
15056 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070015057 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080015058 if (predicate.apply(this)) {
15059 return this;
15060 }
15061 return null;
15062 }
15063
15064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015065 * Look for a child view with the given id. If this view has the given
15066 * id, return this view.
15067 *
15068 * @param id The id to search for.
15069 * @return The view that has the given id in the hierarchy or null
15070 */
15071 public final View findViewById(int id) {
15072 if (id < 0) {
15073 return null;
15074 }
15075 return findViewTraversal(id);
15076 }
15077
15078 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070015079 * Finds a view by its unuque and stable accessibility id.
15080 *
15081 * @param accessibilityId The searched accessibility id.
15082 * @return The found view.
15083 */
15084 final View findViewByAccessibilityId(int accessibilityId) {
15085 if (accessibilityId < 0) {
15086 return null;
15087 }
15088 return findViewByAccessibilityIdTraversal(accessibilityId);
15089 }
15090
15091 /**
15092 * Performs the traversal to find a view by its unuque and stable accessibility id.
15093 *
15094 * <strong>Note:</strong>This method does not stop at the root namespace
15095 * boundary since the user can touch the screen at an arbitrary location
15096 * potentially crossing the root namespace bounday which will send an
15097 * accessibility event to accessibility services and they should be able
15098 * to obtain the event source. Also accessibility ids are guaranteed to be
15099 * unique in the window.
15100 *
15101 * @param accessibilityId The accessibility id.
15102 * @return The found view.
15103 */
15104 View findViewByAccessibilityIdTraversal(int accessibilityId) {
15105 if (getAccessibilityViewId() == accessibilityId) {
15106 return this;
15107 }
15108 return null;
15109 }
15110
15111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015112 * Look for a child view with the given tag. If this view has the given
15113 * tag, return this view.
15114 *
15115 * @param tag The tag to search for, using "tag.equals(getTag())".
15116 * @return The View that has the given tag in the hierarchy or null
15117 */
15118 public final View findViewWithTag(Object tag) {
15119 if (tag == null) {
15120 return null;
15121 }
15122 return findViewWithTagTraversal(tag);
15123 }
15124
15125 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080015126 * {@hide}
15127 * Look for a child view that matches the specified predicate.
15128 * If this view matches the predicate, return this view.
15129 *
15130 * @param predicate The predicate to evaluate.
15131 * @return The first view that matches the predicate or null.
15132 */
15133 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070015134 return findViewByPredicateTraversal(predicate, null);
15135 }
15136
15137 /**
15138 * {@hide}
15139 * Look for a child view that matches the specified predicate,
15140 * starting with the specified view and its descendents and then
15141 * recusively searching the ancestors and siblings of that view
15142 * until this view is reached.
15143 *
15144 * This method is useful in cases where the predicate does not match
15145 * a single unique view (perhaps multiple views use the same id)
15146 * and we are trying to find the view that is "closest" in scope to the
15147 * starting view.
15148 *
15149 * @param start The view to start from.
15150 * @param predicate The predicate to evaluate.
15151 * @return The first view that matches the predicate or null.
15152 */
15153 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
15154 View childToSkip = null;
15155 for (;;) {
15156 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
15157 if (view != null || start == this) {
15158 return view;
15159 }
15160
15161 ViewParent parent = start.getParent();
15162 if (parent == null || !(parent instanceof View)) {
15163 return null;
15164 }
15165
15166 childToSkip = start;
15167 start = (View) parent;
15168 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080015169 }
15170
15171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015172 * Sets the identifier for this view. The identifier does not have to be
15173 * unique in this view's hierarchy. The identifier should be a positive
15174 * number.
15175 *
15176 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070015177 * @see #getId()
15178 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015179 *
15180 * @param id a number used to identify the view
15181 *
15182 * @attr ref android.R.styleable#View_id
15183 */
15184 public void setId(int id) {
15185 mID = id;
Svetoslav Ganov33aef982012-09-13 12:49:03 -070015186 if (mID == View.NO_ID && mLabelForId != View.NO_ID) {
15187 mID = generateViewId();
15188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015189 }
15190
15191 /**
15192 * {@hide}
15193 *
15194 * @param isRoot true if the view belongs to the root namespace, false
15195 * otherwise
15196 */
15197 public void setIsRootNamespace(boolean isRoot) {
15198 if (isRoot) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015199 mPrivateFlags |= PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015200 } else {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015201 mPrivateFlags &= ~PFLAG_IS_ROOT_NAMESPACE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015202 }
15203 }
15204
15205 /**
15206 * {@hide}
15207 *
15208 * @return true if the view belongs to the root namespace, false otherwise
15209 */
15210 public boolean isRootNamespace() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015211 return (mPrivateFlags&PFLAG_IS_ROOT_NAMESPACE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015212 }
15213
15214 /**
15215 * Returns this view's identifier.
15216 *
15217 * @return a positive integer used to identify the view or {@link #NO_ID}
15218 * if the view has no ID
15219 *
Philip Milne6c8ea062012-04-03 17:38:43 -070015220 * @see #setId(int)
15221 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015222 * @attr ref android.R.styleable#View_id
15223 */
15224 @ViewDebug.CapturedViewProperty
15225 public int getId() {
15226 return mID;
15227 }
15228
15229 /**
15230 * Returns this view's tag.
15231 *
15232 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070015233 *
15234 * @see #setTag(Object)
15235 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015236 */
15237 @ViewDebug.ExportedProperty
15238 public Object getTag() {
15239 return mTag;
15240 }
15241
15242 /**
15243 * Sets the tag associated with this view. A tag can be used to mark
15244 * a view in its hierarchy and does not have to be unique within the
15245 * hierarchy. Tags can also be used to store data within a view without
15246 * resorting to another data structure.
15247 *
15248 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070015249 *
15250 * @see #getTag()
15251 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015252 */
15253 public void setTag(final Object tag) {
15254 mTag = tag;
15255 }
15256
15257 /**
Romain Guyd90a3312009-05-06 14:54:28 -070015258 * Returns the tag associated with this view and the specified key.
15259 *
15260 * @param key The key identifying the tag
15261 *
15262 * @return the Object stored in this view as a tag
15263 *
15264 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070015265 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070015266 */
15267 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015268 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070015269 return null;
15270 }
15271
15272 /**
15273 * Sets a tag associated with this view and a key. A tag can be used
15274 * to mark a view in its hierarchy and does not have to be unique within
15275 * the hierarchy. Tags can also be used to store data within a view
15276 * without resorting to another data structure.
15277 *
15278 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070015279 * application to ensure it is unique (see the <a
15280 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
15281 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070015282 * the Android framework or not associated with any package will cause
15283 * an {@link IllegalArgumentException} to be thrown.
15284 *
15285 * @param key The key identifying the tag
15286 * @param tag An Object to tag the view with
15287 *
15288 * @throws IllegalArgumentException If they specified key is not valid
15289 *
15290 * @see #setTag(Object)
15291 * @see #getTag(int)
15292 */
15293 public void setTag(int key, final Object tag) {
15294 // If the package id is 0x00 or 0x01, it's either an undefined package
15295 // or a framework id
15296 if ((key >>> 24) < 2) {
15297 throw new IllegalArgumentException("The key must be an application-specific "
15298 + "resource id.");
15299 }
15300
Adam Powell2b2f6d62011-09-23 11:15:39 -070015301 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015302 }
15303
15304 /**
15305 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
15306 * framework id.
15307 *
15308 * @hide
15309 */
15310 public void setTagInternal(int key, Object tag) {
15311 if ((key >>> 24) != 0x1) {
15312 throw new IllegalArgumentException("The key must be a framework-specific "
15313 + "resource id.");
15314 }
15315
Adam Powell2b2f6d62011-09-23 11:15:39 -070015316 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015317 }
15318
Adam Powell2b2f6d62011-09-23 11:15:39 -070015319 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070015320 if (mKeyedTags == null) {
15321 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070015322 }
15323
Adam Powell7db82ac2011-09-22 19:44:04 -070015324 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070015325 }
15326
15327 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015328 * Prints information about this view in the log output, with the tag
15329 * {@link #VIEW_LOG_TAG}.
15330 *
15331 * @hide
15332 */
15333 public void debug() {
15334 debug(0);
15335 }
15336
15337 /**
15338 * Prints information about this view in the log output, with the tag
15339 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
15340 * indentation defined by the <code>depth</code>.
15341 *
15342 * @param depth the indentation level
15343 *
15344 * @hide
15345 */
15346 protected void debug(int depth) {
15347 String output = debugIndent(depth - 1);
15348
15349 output += "+ " + this;
15350 int id = getId();
15351 if (id != -1) {
15352 output += " (id=" + id + ")";
15353 }
15354 Object tag = getTag();
15355 if (tag != null) {
15356 output += " (tag=" + tag + ")";
15357 }
15358 Log.d(VIEW_LOG_TAG, output);
15359
Dianne Hackborn4702a852012-08-17 15:18:29 -070015360 if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015361 output = debugIndent(depth) + " FOCUSED";
15362 Log.d(VIEW_LOG_TAG, output);
15363 }
15364
15365 output = debugIndent(depth);
15366 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
15367 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
15368 + "} ";
15369 Log.d(VIEW_LOG_TAG, output);
15370
15371 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
15372 || mPaddingBottom != 0) {
15373 output = debugIndent(depth);
15374 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
15375 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
15376 Log.d(VIEW_LOG_TAG, output);
15377 }
15378
15379 output = debugIndent(depth);
15380 output += "mMeasureWidth=" + mMeasuredWidth +
15381 " mMeasureHeight=" + mMeasuredHeight;
15382 Log.d(VIEW_LOG_TAG, output);
15383
15384 output = debugIndent(depth);
15385 if (mLayoutParams == null) {
15386 output += "BAD! no layout params";
15387 } else {
15388 output = mLayoutParams.debug(output);
15389 }
15390 Log.d(VIEW_LOG_TAG, output);
15391
15392 output = debugIndent(depth);
15393 output += "flags={";
15394 output += View.printFlags(mViewFlags);
15395 output += "}";
15396 Log.d(VIEW_LOG_TAG, output);
15397
15398 output = debugIndent(depth);
15399 output += "privateFlags={";
15400 output += View.printPrivateFlags(mPrivateFlags);
15401 output += "}";
15402 Log.d(VIEW_LOG_TAG, output);
15403 }
15404
15405 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090015406 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015407 *
15408 * @param depth the indentation level
15409 * @return a String containing (depth * 2 + 3) * 2 white spaces
15410 *
15411 * @hide
15412 */
15413 protected static String debugIndent(int depth) {
15414 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
15415 for (int i = 0; i < (depth * 2) + 3; i++) {
15416 spaces.append(' ').append(' ');
15417 }
15418 return spaces.toString();
15419 }
15420
15421 /**
15422 * <p>Return the offset of the widget's text baseline from the widget's top
15423 * boundary. If this widget does not support baseline alignment, this
15424 * method returns -1. </p>
15425 *
15426 * @return the offset of the baseline within the widget's bounds or -1
15427 * if baseline alignment is not supported
15428 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070015429 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015430 public int getBaseline() {
15431 return -1;
15432 }
15433
15434 /**
15435 * Call this when something has changed which has invalidated the
15436 * layout of this view. This will schedule a layout pass of the view
15437 * tree.
15438 */
15439 public void requestLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015440 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15441 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015442
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070015443 if (mParent != null && !mParent.isLayoutRequested()) {
15444 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015445 }
15446 }
15447
15448 /**
15449 * Forces this view to be laid out during the next layout pass.
15450 * This method does not call requestLayout() or forceLayout()
15451 * on the parent.
15452 */
15453 public void forceLayout() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015454 mPrivateFlags |= PFLAG_FORCE_LAYOUT;
15455 mPrivateFlags |= PFLAG_INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015456 }
15457
15458 /**
15459 * <p>
15460 * This is called to find out how big a view should be. The parent
15461 * supplies constraint information in the width and height parameters.
15462 * </p>
15463 *
15464 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080015465 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015466 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080015467 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015468 * </p>
15469 *
15470 *
15471 * @param widthMeasureSpec Horizontal space requirements as imposed by the
15472 * parent
15473 * @param heightMeasureSpec Vertical space requirements as imposed by the
15474 * parent
15475 *
15476 * @see #onMeasure(int, int)
15477 */
15478 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015479 if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015480 widthMeasureSpec != mOldWidthMeasureSpec ||
15481 heightMeasureSpec != mOldHeightMeasureSpec) {
15482
15483 // first clears the measured dimension flag
Dianne Hackborn4702a852012-08-17 15:18:29 -070015484 mPrivateFlags &= ~PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015485
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070015486 resolveRtlPropertiesIfNeeded();
Adam Powell0090f202012-08-07 17:15:30 -070015487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015488 // measure ourselves, this should set the measured dimension flag back
15489 onMeasure(widthMeasureSpec, heightMeasureSpec);
15490
15491 // flag not set, setMeasuredDimension() was not invoked, we raise
15492 // an exception to warn the developer
Dianne Hackborn4702a852012-08-17 15:18:29 -070015493 if ((mPrivateFlags & PFLAG_MEASURED_DIMENSION_SET) != PFLAG_MEASURED_DIMENSION_SET) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015494 throw new IllegalStateException("onMeasure() did not set the"
15495 + " measured dimension by calling"
15496 + " setMeasuredDimension()");
15497 }
15498
Dianne Hackborn4702a852012-08-17 15:18:29 -070015499 mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015500 }
15501
15502 mOldWidthMeasureSpec = widthMeasureSpec;
15503 mOldHeightMeasureSpec = heightMeasureSpec;
15504 }
15505
15506 /**
15507 * <p>
15508 * Measure the view and its content to determine the measured width and the
15509 * measured height. This method is invoked by {@link #measure(int, int)} and
15510 * should be overriden by subclasses to provide accurate and efficient
15511 * measurement of their contents.
15512 * </p>
15513 *
15514 * <p>
15515 * <strong>CONTRACT:</strong> When overriding this method, you
15516 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
15517 * measured width and height of this view. Failure to do so will trigger an
15518 * <code>IllegalStateException</code>, thrown by
15519 * {@link #measure(int, int)}. Calling the superclass'
15520 * {@link #onMeasure(int, int)} is a valid use.
15521 * </p>
15522 *
15523 * <p>
15524 * The base class implementation of measure defaults to the background size,
15525 * unless a larger size is allowed by the MeasureSpec. Subclasses should
15526 * override {@link #onMeasure(int, int)} to provide better measurements of
15527 * their content.
15528 * </p>
15529 *
15530 * <p>
15531 * If this method is overridden, it is the subclass's responsibility to make
15532 * sure the measured height and width are at least the view's minimum height
15533 * and width ({@link #getSuggestedMinimumHeight()} and
15534 * {@link #getSuggestedMinimumWidth()}).
15535 * </p>
15536 *
15537 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
15538 * The requirements are encoded with
15539 * {@link android.view.View.MeasureSpec}.
15540 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
15541 * The requirements are encoded with
15542 * {@link android.view.View.MeasureSpec}.
15543 *
15544 * @see #getMeasuredWidth()
15545 * @see #getMeasuredHeight()
15546 * @see #setMeasuredDimension(int, int)
15547 * @see #getSuggestedMinimumHeight()
15548 * @see #getSuggestedMinimumWidth()
15549 * @see android.view.View.MeasureSpec#getMode(int)
15550 * @see android.view.View.MeasureSpec#getSize(int)
15551 */
15552 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15553 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
15554 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
15555 }
15556
15557 /**
15558 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
15559 * measured width and measured height. Failing to do so will trigger an
15560 * exception at measurement time.</p>
15561 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080015562 * @param measuredWidth The measured width of this view. May be a complex
15563 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15564 * {@link #MEASURED_STATE_TOO_SMALL}.
15565 * @param measuredHeight The measured height of this view. May be a complex
15566 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
15567 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015568 */
15569 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
15570 mMeasuredWidth = measuredWidth;
15571 mMeasuredHeight = measuredHeight;
15572
Dianne Hackborn4702a852012-08-17 15:18:29 -070015573 mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015574 }
15575
15576 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080015577 * Merge two states as returned by {@link #getMeasuredState()}.
15578 * @param curState The current state as returned from a view or the result
15579 * of combining multiple views.
15580 * @param newState The new view state to combine.
15581 * @return Returns a new integer reflecting the combination of the two
15582 * states.
15583 */
15584 public static int combineMeasuredStates(int curState, int newState) {
15585 return curState | newState;
15586 }
15587
15588 /**
15589 * Version of {@link #resolveSizeAndState(int, int, int)}
15590 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
15591 */
15592 public static int resolveSize(int size, int measureSpec) {
15593 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
15594 }
15595
15596 /**
15597 * Utility to reconcile a desired size and state, with constraints imposed
15598 * by a MeasureSpec. Will take the desired size, unless a different size
15599 * is imposed by the constraints. The returned value is a compound integer,
15600 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
15601 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
15602 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015603 *
15604 * @param size How big the view wants to be
15605 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080015606 * @return Size information bit mask as defined by
15607 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015608 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080015609 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015610 int result = size;
15611 int specMode = MeasureSpec.getMode(measureSpec);
15612 int specSize = MeasureSpec.getSize(measureSpec);
15613 switch (specMode) {
15614 case MeasureSpec.UNSPECIFIED:
15615 result = size;
15616 break;
15617 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080015618 if (specSize < size) {
15619 result = specSize | MEASURED_STATE_TOO_SMALL;
15620 } else {
15621 result = size;
15622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015623 break;
15624 case MeasureSpec.EXACTLY:
15625 result = specSize;
15626 break;
15627 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080015628 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015629 }
15630
15631 /**
15632 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070015633 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015634 * by the MeasureSpec.
15635 *
15636 * @param size Default size for this view
15637 * @param measureSpec Constraints imposed by the parent
15638 * @return The size this view should be.
15639 */
15640 public static int getDefaultSize(int size, int measureSpec) {
15641 int result = size;
15642 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070015643 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015644
15645 switch (specMode) {
15646 case MeasureSpec.UNSPECIFIED:
15647 result = size;
15648 break;
15649 case MeasureSpec.AT_MOST:
15650 case MeasureSpec.EXACTLY:
15651 result = specSize;
15652 break;
15653 }
15654 return result;
15655 }
15656
15657 /**
15658 * Returns the suggested minimum height that the view should use. This
15659 * returns the maximum of the view's minimum height
15660 * and the background's minimum height
15661 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
15662 * <p>
15663 * When being used in {@link #onMeasure(int, int)}, the caller should still
15664 * ensure the returned height is within the requirements of the parent.
15665 *
15666 * @return The suggested minimum height of the view.
15667 */
15668 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015669 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015671 }
15672
15673 /**
15674 * Returns the suggested minimum width that the view should use. This
15675 * returns the maximum of the view's minimum width)
15676 * and the background's minimum width
15677 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
15678 * <p>
15679 * When being used in {@link #onMeasure(int, int)}, the caller should still
15680 * ensure the returned width is within the requirements of the parent.
15681 *
15682 * @return The suggested minimum width of the view.
15683 */
15684 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070015685 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
15686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015687
Philip Milne6c8ea062012-04-03 17:38:43 -070015688 /**
15689 * Returns the minimum height of the view.
15690 *
15691 * @return the minimum height the view will try to be.
15692 *
15693 * @see #setMinimumHeight(int)
15694 *
15695 * @attr ref android.R.styleable#View_minHeight
15696 */
15697 public int getMinimumHeight() {
15698 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015699 }
15700
15701 /**
15702 * Sets the minimum height of the view. It is not guaranteed the view will
15703 * be able to achieve this minimum height (for example, if its parent layout
15704 * constrains it with less available height).
15705 *
15706 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015707 *
15708 * @see #getMinimumHeight()
15709 *
15710 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015711 */
15712 public void setMinimumHeight(int minHeight) {
15713 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070015714 requestLayout();
15715 }
15716
15717 /**
15718 * Returns the minimum width of the view.
15719 *
15720 * @return the minimum width the view will try to be.
15721 *
15722 * @see #setMinimumWidth(int)
15723 *
15724 * @attr ref android.R.styleable#View_minWidth
15725 */
15726 public int getMinimumWidth() {
15727 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015728 }
15729
15730 /**
15731 * Sets the minimum width of the view. It is not guaranteed the view will
15732 * be able to achieve this minimum width (for example, if its parent layout
15733 * constrains it with less available width).
15734 *
15735 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070015736 *
15737 * @see #getMinimumWidth()
15738 *
15739 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015740 */
15741 public void setMinimumWidth(int minWidth) {
15742 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070015743 requestLayout();
15744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015745 }
15746
15747 /**
15748 * Get the animation currently associated with this view.
15749 *
15750 * @return The animation that is currently playing or
15751 * scheduled to play for this view.
15752 */
15753 public Animation getAnimation() {
15754 return mCurrentAnimation;
15755 }
15756
15757 /**
15758 * Start the specified animation now.
15759 *
15760 * @param animation the animation to start now
15761 */
15762 public void startAnimation(Animation animation) {
15763 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
15764 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080015765 invalidateParentCaches();
15766 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015767 }
15768
15769 /**
15770 * Cancels any animations for this view.
15771 */
15772 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080015773 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080015774 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080015775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015776 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015777 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015778 }
15779
15780 /**
15781 * Sets the next animation to play for this view.
15782 * If you want the animation to play immediately, use
Chet Haase42428932012-05-11 15:39:07 -070015783 * {@link #startAnimation(android.view.animation.Animation)} instead.
15784 * This method provides allows fine-grained
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015785 * control over the start time and invalidation, but you
15786 * must make sure that 1) the animation has a start time set, and
Chet Haase42428932012-05-11 15:39:07 -070015787 * 2) the view's parent (which controls animations on its children)
15788 * will be invalidated when the animation is supposed to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015789 * start.
15790 *
15791 * @param animation The next animation, or null.
15792 */
15793 public void setAnimation(Animation animation) {
15794 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015796 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015797 // If the screen is off assume the animation start time is now instead of
15798 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15799 // would cause the animation to start when the screen turns back on
15800 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15801 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15802 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015804 animation.reset();
15805 }
15806 }
15807
15808 /**
15809 * Invoked by a parent ViewGroup to notify the start of the animation
15810 * currently associated with this view. If you override this method,
15811 * always call super.onAnimationStart();
15812 *
15813 * @see #setAnimation(android.view.animation.Animation)
15814 * @see #getAnimation()
15815 */
15816 protected void onAnimationStart() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015817 mPrivateFlags |= PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015818 }
15819
15820 /**
15821 * Invoked by a parent ViewGroup to notify the end of the animation
15822 * currently associated with this view. If you override this method,
15823 * always call super.onAnimationEnd();
15824 *
15825 * @see #setAnimation(android.view.animation.Animation)
15826 * @see #getAnimation()
15827 */
15828 protected void onAnimationEnd() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070015829 mPrivateFlags &= ~PFLAG_ANIMATION_STARTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015830 }
15831
15832 /**
15833 * Invoked if there is a Transform that involves alpha. Subclass that can
15834 * draw themselves with the specified alpha should return true, and then
15835 * respect that alpha when their onDraw() is called. If this returns false
15836 * then the view may be redirected to draw into an offscreen buffer to
15837 * fulfill the request, which will look fine, but may be slower than if the
15838 * subclass handles it internally. The default implementation returns false.
15839 *
15840 * @param alpha The alpha (0..255) to apply to the view's drawing
15841 * @return true if the view can draw with the specified alpha.
15842 */
15843 protected boolean onSetAlpha(int alpha) {
15844 return false;
15845 }
15846
15847 /**
15848 * This is used by the RootView to perform an optimization when
15849 * the view hierarchy contains one or several SurfaceView.
15850 * SurfaceView is always considered transparent, but its children are not,
15851 * therefore all View objects remove themselves from the global transparent
15852 * region (passed as a parameter to this function).
15853 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015854 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015855 *
15856 * @return Returns true if the effective visibility of the view at this
15857 * point is opaque, regardless of the transparent region; returns false
15858 * if it is possible for underlying windows to be seen behind the view.
15859 *
15860 * {@hide}
15861 */
15862 public boolean gatherTransparentRegion(Region region) {
15863 final AttachInfo attachInfo = mAttachInfo;
15864 if (region != null && attachInfo != null) {
15865 final int pflags = mPrivateFlags;
Dianne Hackborn4702a852012-08-17 15:18:29 -070015866 if ((pflags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015867 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15868 // remove it from the transparent region.
15869 final int[] location = attachInfo.mTransparentLocation;
15870 getLocationInWindow(location);
15871 region.op(location[0], location[1], location[0] + mRight - mLeft,
15872 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Dianne Hackborn4702a852012-08-17 15:18:29 -070015873 } else if ((pflags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015874 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15875 // exists, so we remove the background drawable's non-transparent
15876 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015877 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015878 }
15879 }
15880 return true;
15881 }
15882
15883 /**
15884 * Play a sound effect for this view.
15885 *
15886 * <p>The framework will play sound effects for some built in actions, such as
15887 * clicking, but you may wish to play these effects in your widget,
15888 * for instance, for internal navigation.
15889 *
15890 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15891 * {@link #isSoundEffectsEnabled()} is true.
15892 *
15893 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15894 */
15895 public void playSoundEffect(int soundConstant) {
15896 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15897 return;
15898 }
15899 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15900 }
15901
15902 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015903 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015904 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015905 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015906 *
15907 * <p>The framework will provide haptic feedback for some built in actions,
15908 * such as long presses, but you may wish to provide feedback for your
15909 * own widget.
15910 *
15911 * <p>The feedback will only be performed if
15912 * {@link #isHapticFeedbackEnabled()} is true.
15913 *
15914 * @param feedbackConstant One of the constants defined in
15915 * {@link HapticFeedbackConstants}
15916 */
15917 public boolean performHapticFeedback(int feedbackConstant) {
15918 return performHapticFeedback(feedbackConstant, 0);
15919 }
15920
15921 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015922 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015923 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015924 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015925 *
15926 * @param feedbackConstant One of the constants defined in
15927 * {@link HapticFeedbackConstants}
15928 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15929 */
15930 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15931 if (mAttachInfo == null) {
15932 return false;
15933 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015934 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015935 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015936 && !isHapticFeedbackEnabled()) {
15937 return false;
15938 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015939 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15940 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015941 }
15942
15943 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015944 * Request that the visibility of the status bar or other screen/window
15945 * decorations be changed.
15946 *
15947 * <p>This method is used to put the over device UI into temporary modes
15948 * where the user's attention is focused more on the application content,
15949 * by dimming or hiding surrounding system affordances. This is typically
15950 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15951 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15952 * to be placed behind the action bar (and with these flags other system
15953 * affordances) so that smooth transitions between hiding and showing them
15954 * can be done.
15955 *
15956 * <p>Two representative examples of the use of system UI visibility is
15957 * implementing a content browsing application (like a magazine reader)
15958 * and a video playing application.
15959 *
15960 * <p>The first code shows a typical implementation of a View in a content
15961 * browsing application. In this implementation, the application goes
15962 * into a content-oriented mode by hiding the status bar and action bar,
15963 * and putting the navigation elements into lights out mode. The user can
15964 * then interact with content while in this mode. Such an application should
15965 * provide an easy way for the user to toggle out of the mode (such as to
15966 * check information in the status bar or access notifications). In the
15967 * implementation here, this is done simply by tapping on the content.
15968 *
15969 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15970 * content}
15971 *
15972 * <p>This second code sample shows a typical implementation of a View
15973 * in a video playing application. In this situation, while the video is
15974 * playing the application would like to go into a complete full-screen mode,
15975 * to use as much of the display as possible for the video. When in this state
15976 * the user can not interact with the application; the system intercepts
Dianne Hackborncf675782012-05-10 15:07:24 -070015977 * touching on the screen to pop the UI out of full screen mode. See
15978 * {@link #fitSystemWindows(Rect)} for a sample layout that goes with this code.
Dianne Hackborn98014352012-04-05 18:31:41 -070015979 *
15980 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15981 * content}
15982 *
15983 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15984 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15985 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15986 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015987 */
15988 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015989 if (visibility != mSystemUiVisibility) {
15990 mSystemUiVisibility = visibility;
15991 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15992 mParent.recomputeViewAttributes(this);
15993 }
Joe Onorato664644d2011-01-23 17:53:23 -080015994 }
15995 }
15996
15997 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015998 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15999 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
16000 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
16001 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
16002 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080016003 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080016004 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080016005 return mSystemUiVisibility;
16006 }
16007
Scott Mainec6331b2011-05-24 16:55:56 -070016008 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070016009 * Returns the current system UI visibility that is currently set for
16010 * the entire window. This is the combination of the
16011 * {@link #setSystemUiVisibility(int)} values supplied by all of the
16012 * views in the window.
16013 */
16014 public int getWindowSystemUiVisibility() {
16015 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
16016 }
16017
16018 /**
16019 * Override to find out when the window's requested system UI visibility
16020 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
16021 * This is different from the callbacks recieved through
16022 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
16023 * in that this is only telling you about the local request of the window,
16024 * not the actual values applied by the system.
16025 */
16026 public void onWindowSystemUiVisibilityChanged(int visible) {
16027 }
16028
16029 /**
16030 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
16031 * the view hierarchy.
16032 */
16033 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
16034 onWindowSystemUiVisibilityChanged(visible);
16035 }
16036
16037 /**
Scott Mainec6331b2011-05-24 16:55:56 -070016038 * Set a listener to receive callbacks when the visibility of the system bar changes.
16039 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
16040 */
Joe Onorato664644d2011-01-23 17:53:23 -080016041 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016042 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080016043 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
16044 mParent.recomputeViewAttributes(this);
16045 }
16046 }
16047
16048 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016049 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
16050 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080016051 */
16052 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016053 ListenerInfo li = mListenerInfo;
16054 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
16055 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080016056 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080016057 }
16058 }
16059
Dianne Hackborncf675782012-05-10 15:07:24 -070016060 boolean updateLocalSystemUiVisibility(int localValue, int localChanges) {
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016061 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
16062 if (val != mSystemUiVisibility) {
16063 setSystemUiVisibility(val);
Dianne Hackborncf675782012-05-10 15:07:24 -070016064 return true;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016065 }
Dianne Hackborncf675782012-05-10 15:07:24 -070016066 return false;
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016067 }
16068
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016069 /** @hide */
16070 public void setDisabledSystemUiVisibility(int flags) {
16071 if (mAttachInfo != null) {
16072 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
16073 mAttachInfo.mDisabledSystemUiVisibility = flags;
16074 if (mParent != null) {
16075 mParent.recomputeViewAttributes(this);
16076 }
16077 }
16078 }
16079 }
16080
Joe Onorato664644d2011-01-23 17:53:23 -080016081 /**
Joe Malin32736f02011-01-19 16:14:20 -080016082 * Creates an image that the system displays during the drag and drop
16083 * operation. This is called a &quot;drag shadow&quot;. The default implementation
16084 * for a DragShadowBuilder based on a View returns an image that has exactly the same
16085 * appearance as the given View. The default also positions the center of the drag shadow
16086 * directly under the touch point. If no View is provided (the constructor with no parameters
16087 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
16088 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
16089 * default is an invisible drag shadow.
16090 * <p>
16091 * You are not required to use the View you provide to the constructor as the basis of the
16092 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
16093 * anything you want as the drag shadow.
16094 * </p>
16095 * <p>
16096 * You pass a DragShadowBuilder object to the system when you start the drag. The system
16097 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
16098 * size and position of the drag shadow. It uses this data to construct a
16099 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
16100 * so that your application can draw the shadow image in the Canvas.
16101 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070016102 *
16103 * <div class="special reference">
16104 * <h3>Developer Guides</h3>
16105 * <p>For a guide to implementing drag and drop features, read the
16106 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16107 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070016108 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016109 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070016110 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070016111
16112 /**
Joe Malin32736f02011-01-19 16:14:20 -080016113 * Constructs a shadow image builder based on a View. By default, the resulting drag
16114 * shadow will have the same appearance and dimensions as the View, with the touch point
16115 * over the center of the View.
16116 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070016117 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016118 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070016119 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070016120 }
16121
Christopher Tate17ed60c2011-01-18 12:50:26 -080016122 /**
16123 * Construct a shadow builder object with no associated View. This
16124 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
16125 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
16126 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080016127 * reference to any View object. If they are not overridden, then the result is an
16128 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080016129 */
16130 public DragShadowBuilder() {
16131 mView = new WeakReference<View>(null);
16132 }
16133
16134 /**
16135 * Returns the View object that had been passed to the
16136 * {@link #View.DragShadowBuilder(View)}
16137 * constructor. If that View parameter was {@code null} or if the
16138 * {@link #View.DragShadowBuilder()}
16139 * constructor was used to instantiate the builder object, this method will return
16140 * null.
16141 *
16142 * @return The View object associate with this builder object.
16143 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070016144 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070016145 final public View getView() {
16146 return mView.get();
16147 }
16148
Christopher Tate2c095f32010-10-04 14:13:40 -070016149 /**
Joe Malin32736f02011-01-19 16:14:20 -080016150 * Provides the metrics for the shadow image. These include the dimensions of
16151 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070016152 * be centered under the touch location while dragging.
16153 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016154 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080016155 * same as the dimensions of the View itself and centers the shadow under
16156 * the touch point.
16157 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070016158 *
Joe Malin32736f02011-01-19 16:14:20 -080016159 * @param shadowSize A {@link android.graphics.Point} containing the width and height
16160 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
16161 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
16162 * image.
16163 *
16164 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
16165 * shadow image that should be underneath the touch point during the drag and drop
16166 * operation. Your application must set {@link android.graphics.Point#x} to the
16167 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070016168 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016169 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070016170 final View view = mView.get();
16171 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016172 shadowSize.set(view.getWidth(), view.getHeight());
16173 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070016174 } else {
16175 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
16176 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016177 }
16178
16179 /**
Joe Malin32736f02011-01-19 16:14:20 -080016180 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
16181 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016182 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070016183 *
Joe Malin32736f02011-01-19 16:14:20 -080016184 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070016185 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016186 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070016187 final View view = mView.get();
16188 if (view != null) {
16189 view.draw(canvas);
16190 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016191 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070016192 }
Christopher Tate2c095f32010-10-04 14:13:40 -070016193 }
16194 }
16195
16196 /**
Joe Malin32736f02011-01-19 16:14:20 -080016197 * Starts a drag and drop operation. When your application calls this method, it passes a
16198 * {@link android.view.View.DragShadowBuilder} object to the system. The
16199 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
16200 * to get metrics for the drag shadow, and then calls the object's
16201 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
16202 * <p>
16203 * Once the system has the drag shadow, it begins the drag and drop operation by sending
16204 * drag events to all the View objects in your application that are currently visible. It does
16205 * this either by calling the View object's drag listener (an implementation of
16206 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
16207 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
16208 * Both are passed a {@link android.view.DragEvent} object that has a
16209 * {@link android.view.DragEvent#getAction()} value of
16210 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
16211 * </p>
16212 * <p>
16213 * Your application can invoke startDrag() on any attached View object. The View object does not
16214 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
16215 * be related to the View the user selected for dragging.
16216 * </p>
16217 * @param data A {@link android.content.ClipData} object pointing to the data to be
16218 * transferred by the drag and drop operation.
16219 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
16220 * drag shadow.
16221 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
16222 * drop operation. This Object is put into every DragEvent object sent by the system during the
16223 * current drag.
16224 * <p>
16225 * myLocalState is a lightweight mechanism for the sending information from the dragged View
16226 * to the target Views. For example, it can contain flags that differentiate between a
16227 * a copy operation and a move operation.
16228 * </p>
16229 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
16230 * so the parameter should be set to 0.
16231 * @return {@code true} if the method completes successfully, or
16232 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
16233 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070016234 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016235 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016236 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070016237 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016238 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070016239 }
16240 boolean okay = false;
16241
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016242 Point shadowSize = new Point();
16243 Point shadowTouchPoint = new Point();
16244 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070016245
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016246 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
16247 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
16248 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070016249 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016250
Chris Tatea32dcf72010-10-14 12:13:50 -070016251 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016252 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
16253 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070016254 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016255 Surface surface = new Surface();
16256 try {
16257 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080016258 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070016259 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070016260 + " surface=" + surface);
16261 if (token != null) {
16262 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070016263 try {
Chris Tate6b391282010-10-14 15:48:59 -070016264 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016265 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070016266 } finally {
16267 surface.unlockCanvasAndPost(canvas);
16268 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016269
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070016270 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080016271
16272 // Cache the local state object for delivery with DragEvents
16273 root.setLocalDragState(myLocalState);
16274
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016275 // repurpose 'shadowSize' for the last touch point
16276 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070016277
Christopher Tatea53146c2010-09-07 11:57:52 -070016278 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080016279 shadowSize.x, shadowSize.y,
16280 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070016281 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070016282
16283 // Off and running! Release our local surface instance; the drag
16284 // shadow surface is now managed by the system process.
16285 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070016286 }
16287 } catch (Exception e) {
16288 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
16289 surface.destroy();
16290 }
16291
16292 return okay;
16293 }
16294
Christopher Tatea53146c2010-09-07 11:57:52 -070016295 /**
Joe Malin32736f02011-01-19 16:14:20 -080016296 * Handles drag events sent by the system following a call to
16297 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
16298 *<p>
16299 * When the system calls this method, it passes a
16300 * {@link android.view.DragEvent} object. A call to
16301 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
16302 * in DragEvent. The method uses these to determine what is happening in the drag and drop
16303 * operation.
16304 * @param event The {@link android.view.DragEvent} sent by the system.
16305 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
16306 * in DragEvent, indicating the type of drag event represented by this object.
16307 * @return {@code true} if the method was successful, otherwise {@code false}.
16308 * <p>
16309 * The method should return {@code true} in response to an action type of
16310 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
16311 * operation.
16312 * </p>
16313 * <p>
16314 * The method should also return {@code true} in response to an action type of
16315 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
16316 * {@code false} if it didn't.
16317 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016318 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070016319 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070016320 return false;
16321 }
16322
16323 /**
Joe Malin32736f02011-01-19 16:14:20 -080016324 * Detects if this View is enabled and has a drag event listener.
16325 * If both are true, then it calls the drag event listener with the
16326 * {@link android.view.DragEvent} it received. If the drag event listener returns
16327 * {@code true}, then dispatchDragEvent() returns {@code true}.
16328 * <p>
16329 * For all other cases, the method calls the
16330 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
16331 * method and returns its result.
16332 * </p>
16333 * <p>
16334 * This ensures that a drag event is always consumed, even if the View does not have a drag
16335 * event listener. However, if the View has a listener and the listener returns true, then
16336 * onDragEvent() is not called.
16337 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070016338 */
16339 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080016340 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070016341 ListenerInfo li = mListenerInfo;
16342 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
16343 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070016344 return true;
16345 }
Christopher Tatea53146c2010-09-07 11:57:52 -070016346 return onDragEvent(event);
16347 }
16348
Christopher Tate3d4bf172011-03-28 16:16:46 -070016349 boolean canAcceptDrag() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016350 return (mPrivateFlags2 & PFLAG2_DRAG_CAN_ACCEPT) != 0;
Christopher Tate3d4bf172011-03-28 16:16:46 -070016351 }
16352
Christopher Tatea53146c2010-09-07 11:57:52 -070016353 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070016354 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
16355 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070016356 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070016357 */
16358 public void onCloseSystemDialogs(String reason) {
16359 }
Joe Malin32736f02011-01-19 16:14:20 -080016360
Dianne Hackbornffa42482009-09-23 22:20:11 -070016361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016362 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070016363 * update a Region being computed for
16364 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016365 * that any non-transparent parts of the Drawable are removed from the
16366 * given transparent region.
16367 *
16368 * @param dr The Drawable whose transparency is to be applied to the region.
16369 * @param region A Region holding the current transparency information,
16370 * where any parts of the region that are set are considered to be
16371 * transparent. On return, this region will be modified to have the
16372 * transparency information reduced by the corresponding parts of the
16373 * Drawable that are not transparent.
16374 * {@hide}
16375 */
16376 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
16377 if (DBG) {
16378 Log.i("View", "Getting transparent region for: " + this);
16379 }
16380 final Region r = dr.getTransparentRegion();
16381 final Rect db = dr.getBounds();
16382 final AttachInfo attachInfo = mAttachInfo;
16383 if (r != null && attachInfo != null) {
16384 final int w = getRight()-getLeft();
16385 final int h = getBottom()-getTop();
16386 if (db.left > 0) {
16387 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
16388 r.op(0, 0, db.left, h, Region.Op.UNION);
16389 }
16390 if (db.right < w) {
16391 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
16392 r.op(db.right, 0, w, h, Region.Op.UNION);
16393 }
16394 if (db.top > 0) {
16395 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
16396 r.op(0, 0, w, db.top, Region.Op.UNION);
16397 }
16398 if (db.bottom < h) {
16399 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
16400 r.op(0, db.bottom, w, h, Region.Op.UNION);
16401 }
16402 final int[] location = attachInfo.mTransparentLocation;
16403 getLocationInWindow(location);
16404 r.translate(location[0], location[1]);
16405 region.op(r, Region.Op.INTERSECT);
16406 } else {
16407 region.op(db, Region.Op.DIFFERENCE);
16408 }
16409 }
16410
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016411 private void checkForLongClick(int delayOffset) {
16412 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
16413 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016414
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016415 if (mPendingCheckForLongPress == null) {
16416 mPendingCheckForLongPress = new CheckForLongPress();
16417 }
16418 mPendingCheckForLongPress.rememberWindowAttachCount();
16419 postDelayed(mPendingCheckForLongPress,
16420 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016422 }
16423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016424 /**
16425 * Inflate a view from an XML resource. This convenience method wraps the {@link
16426 * LayoutInflater} class, which provides a full range of options for view inflation.
16427 *
16428 * @param context The Context object for your activity or application.
16429 * @param resource The resource ID to inflate
16430 * @param root A view group that will be the parent. Used to properly inflate the
16431 * layout_* parameters.
16432 * @see LayoutInflater
16433 */
16434 public static View inflate(Context context, int resource, ViewGroup root) {
16435 LayoutInflater factory = LayoutInflater.from(context);
16436 return factory.inflate(resource, root);
16437 }
Romain Guy33e72ae2010-07-17 12:40:29 -070016438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016439 /**
Adam Powell637d3372010-08-25 14:37:03 -070016440 * Scroll the view with standard behavior for scrolling beyond the normal
16441 * content boundaries. Views that call this method should override
16442 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
16443 * results of an over-scroll operation.
16444 *
16445 * Views can use this method to handle any touch or fling-based scrolling.
16446 *
16447 * @param deltaX Change in X in pixels
16448 * @param deltaY Change in Y in pixels
16449 * @param scrollX Current X scroll value in pixels before applying deltaX
16450 * @param scrollY Current Y scroll value in pixels before applying deltaY
16451 * @param scrollRangeX Maximum content scroll range along the X axis
16452 * @param scrollRangeY Maximum content scroll range along the Y axis
16453 * @param maxOverScrollX Number of pixels to overscroll by in either direction
16454 * along the X axis.
16455 * @param maxOverScrollY Number of pixels to overscroll by in either direction
16456 * along the Y axis.
16457 * @param isTouchEvent true if this scroll operation is the result of a touch event.
16458 * @return true if scrolling was clamped to an over-scroll boundary along either
16459 * axis, false otherwise.
16460 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070016461 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070016462 protected boolean overScrollBy(int deltaX, int deltaY,
16463 int scrollX, int scrollY,
16464 int scrollRangeX, int scrollRangeY,
16465 int maxOverScrollX, int maxOverScrollY,
16466 boolean isTouchEvent) {
16467 final int overScrollMode = mOverScrollMode;
16468 final boolean canScrollHorizontal =
16469 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
16470 final boolean canScrollVertical =
16471 computeVerticalScrollRange() > computeVerticalScrollExtent();
16472 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
16473 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
16474 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
16475 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
16476
16477 int newScrollX = scrollX + deltaX;
16478 if (!overScrollHorizontal) {
16479 maxOverScrollX = 0;
16480 }
16481
16482 int newScrollY = scrollY + deltaY;
16483 if (!overScrollVertical) {
16484 maxOverScrollY = 0;
16485 }
16486
16487 // Clamp values if at the limits and record
16488 final int left = -maxOverScrollX;
16489 final int right = maxOverScrollX + scrollRangeX;
16490 final int top = -maxOverScrollY;
16491 final int bottom = maxOverScrollY + scrollRangeY;
16492
16493 boolean clampedX = false;
16494 if (newScrollX > right) {
16495 newScrollX = right;
16496 clampedX = true;
16497 } else if (newScrollX < left) {
16498 newScrollX = left;
16499 clampedX = true;
16500 }
16501
16502 boolean clampedY = false;
16503 if (newScrollY > bottom) {
16504 newScrollY = bottom;
16505 clampedY = true;
16506 } else if (newScrollY < top) {
16507 newScrollY = top;
16508 clampedY = true;
16509 }
16510
16511 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
16512
16513 return clampedX || clampedY;
16514 }
16515
16516 /**
16517 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
16518 * respond to the results of an over-scroll operation.
16519 *
16520 * @param scrollX New X scroll value in pixels
16521 * @param scrollY New Y scroll value in pixels
16522 * @param clampedX True if scrollX was clamped to an over-scroll boundary
16523 * @param clampedY True if scrollY was clamped to an over-scroll boundary
16524 */
16525 protected void onOverScrolled(int scrollX, int scrollY,
16526 boolean clampedX, boolean clampedY) {
16527 // Intentionally empty.
16528 }
16529
16530 /**
16531 * Returns the over-scroll mode for this view. The result will be
16532 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16533 * (allow over-scrolling only if the view content is larger than the container),
16534 * or {@link #OVER_SCROLL_NEVER}.
16535 *
16536 * @return This view's over-scroll mode.
16537 */
16538 public int getOverScrollMode() {
16539 return mOverScrollMode;
16540 }
16541
16542 /**
16543 * Set the over-scroll mode for this view. Valid over-scroll modes are
16544 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
16545 * (allow over-scrolling only if the view content is larger than the container),
16546 * or {@link #OVER_SCROLL_NEVER}.
16547 *
16548 * Setting the over-scroll mode of a view will have an effect only if the
16549 * view is capable of scrolling.
16550 *
16551 * @param overScrollMode The new over-scroll mode for this view.
16552 */
16553 public void setOverScrollMode(int overScrollMode) {
16554 if (overScrollMode != OVER_SCROLL_ALWAYS &&
16555 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
16556 overScrollMode != OVER_SCROLL_NEVER) {
16557 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
16558 }
16559 mOverScrollMode = overScrollMode;
16560 }
16561
16562 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016563 * Gets a scale factor that determines the distance the view should scroll
16564 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
16565 * @return The vertical scroll scale factor.
16566 * @hide
16567 */
16568 protected float getVerticalScrollFactor() {
16569 if (mVerticalScrollFactor == 0) {
16570 TypedValue outValue = new TypedValue();
16571 if (!mContext.getTheme().resolveAttribute(
16572 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
16573 throw new IllegalStateException(
16574 "Expected theme to define listPreferredItemHeight.");
16575 }
16576 mVerticalScrollFactor = outValue.getDimension(
16577 mContext.getResources().getDisplayMetrics());
16578 }
16579 return mVerticalScrollFactor;
16580 }
16581
16582 /**
16583 * Gets a scale factor that determines the distance the view should scroll
16584 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
16585 * @return The horizontal scroll scale factor.
16586 * @hide
16587 */
16588 protected float getHorizontalScrollFactor() {
16589 // TODO: Should use something else.
16590 return getVerticalScrollFactor();
16591 }
16592
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016593 /**
16594 * Return the value specifying the text direction or policy that was set with
16595 * {@link #setTextDirection(int)}.
16596 *
16597 * @return the defined text direction. It can be one of:
16598 *
16599 * {@link #TEXT_DIRECTION_INHERIT},
16600 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16601 * {@link #TEXT_DIRECTION_ANY_RTL},
16602 * {@link #TEXT_DIRECTION_LTR},
16603 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016604 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016605 *
16606 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016607 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016608 @ViewDebug.ExportedProperty(category = "text", mapping = {
16609 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
16610 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
16611 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
16612 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
16613 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
16614 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
16615 })
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016616 public int getRawTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016617 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_MASK) >> PFLAG2_TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016618 }
16619
16620 /**
16621 * Set the text direction.
16622 *
16623 * @param textDirection the direction to set. Should be one of:
16624 *
16625 * {@link #TEXT_DIRECTION_INHERIT},
16626 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16627 * {@link #TEXT_DIRECTION_ANY_RTL},
16628 * {@link #TEXT_DIRECTION_LTR},
16629 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016630 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016631 *
16632 * Resolution will be done if the value is set to TEXT_DIRECTION_INHERIT. The resolution
16633 * proceeds up the parent chain of the view to get the value. If there is no parent, then it will
16634 * return the default {@link #TEXT_DIRECTION_FIRST_STRONG}.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016635 */
16636 public void setTextDirection(int textDirection) {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016637 if (getRawTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016638 // Reset the current text direction and the resolved one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016639 mPrivateFlags2 &= ~PFLAG2_TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016640 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016641 // Set the new text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016642 mPrivateFlags2 |= ((textDirection << PFLAG2_TEXT_DIRECTION_MASK_SHIFT) & PFLAG2_TEXT_DIRECTION_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016643 // Do resolution
16644 resolveTextDirection();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016645 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016646 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016647 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016648 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070016649 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016650 }
16651 }
16652
16653 /**
16654 * Return the resolved text direction.
16655 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016656 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016657 *
Doug Feltcb3791202011-07-07 11:57:48 -070016658 * {@link #TEXT_DIRECTION_FIRST_STRONG}
16659 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016660 * {@link #TEXT_DIRECTION_LTR},
16661 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016662 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016663 */
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016664 public int getTextDirection() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016665 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED_MASK) >> PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016666 }
16667
16668 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016669 * Resolve the text direction.
16670 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016671 * @return true if resolution has been done, false otherwise.
16672 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016673 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016674 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016675 public boolean resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016676 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016677 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016678
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016679 if (hasRtlSupport()) {
16680 // Set resolved text direction flag depending on text direction flag
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016681 final int textDirection = getRawTextDirection();
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016682 switch(textDirection) {
16683 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016684 if (!canResolveTextDirection()) {
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016685 // We cannot do the resolution if there is no parent, so use the default one
Dianne Hackborn4702a852012-08-17 15:18:29 -070016686 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016687 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016688 return false;
16689 }
16690
16691 View parent = ((View) mParent);
16692 // Parent has not yet resolved, so we still return the default
16693 if (!parent.isTextDirectionResolved()) {
16694 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
16695 // Resolution will need to happen again later
16696 return false;
16697 }
16698
16699 // Set current resolved direction to the same value as the parent's one
16700 final int parentResolvedDirection = parent.getTextDirection();
16701 switch (parentResolvedDirection) {
16702 case TEXT_DIRECTION_FIRST_STRONG:
16703 case TEXT_DIRECTION_ANY_RTL:
16704 case TEXT_DIRECTION_LTR:
16705 case TEXT_DIRECTION_RTL:
16706 case TEXT_DIRECTION_LOCALE:
16707 mPrivateFlags2 |=
16708 (parentResolvedDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
16709 break;
16710 default:
16711 // Default resolved direction is "first strong" heuristic
16712 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016713 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016714 break;
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 // Resolved direction is the same as text direction
Dianne Hackborn4702a852012-08-17 15:18:29 -070016721 mPrivateFlags2 |= (textDirection << PFLAG2_TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016722 break;
16723 default:
16724 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016725 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070016726 }
16727 } else {
16728 // Default resolved direction is "first strong" heuristic
Dianne Hackborn4702a852012-08-17 15:18:29 -070016729 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016730 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016731
16732 // Set to resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016733 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016734 return true;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016735 }
16736
16737 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016738 * Check if text direction resolution can be done.
16739 *
16740 * @return true if text direction resolution can be done otherwise return false.
16741 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016742 private boolean canResolveTextDirection() {
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016743 switch (getRawTextDirection()) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016744 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016745 return (mParent != null) && (mParent instanceof View) &&
16746 ((View) mParent).canResolveTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070016747 default:
16748 return true;
16749 }
16750 }
16751
16752 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016753 * Reset resolved text direction. Text direction will be resolved during a call to
16754 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016755 *
16756 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080016757 */
16758 public void resetResolvedTextDirection() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016759 // Reset any previous text direction resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016760 mPrivateFlags2 &= ~(PFLAG2_TEXT_DIRECTION_RESOLVED | PFLAG2_TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016761 // Set to default value
16762 mPrivateFlags2 |= PFLAG2_TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070016763 }
16764
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016765 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016766 * @return true if text direction is inherited.
16767 *
Fabrice Di Meglio97e146c2012-09-23 15:45:16 -070016768 * @hide
16769 */
16770 public boolean isTextDirectionInherited() {
16771 return (getRawTextDirection() == TEXT_DIRECTION_INHERIT);
16772 }
16773
16774 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016775 * @return true if text direction is resolved.
16776 */
16777 private boolean isTextDirectionResolved() {
16778 return (mPrivateFlags2 & PFLAG2_TEXT_DIRECTION_RESOLVED) == PFLAG2_TEXT_DIRECTION_RESOLVED;
16779 }
16780
16781 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016782 * Return the value specifying the text alignment or policy that was set with
16783 * {@link #setTextAlignment(int)}.
16784 *
16785 * @return the defined text alignment. It can be one of:
16786 *
16787 * {@link #TEXT_ALIGNMENT_INHERIT},
16788 * {@link #TEXT_ALIGNMENT_GRAVITY},
16789 * {@link #TEXT_ALIGNMENT_CENTER},
16790 * {@link #TEXT_ALIGNMENT_TEXT_START},
16791 * {@link #TEXT_ALIGNMENT_TEXT_END},
16792 * {@link #TEXT_ALIGNMENT_VIEW_START},
16793 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016794 *
16795 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016796 */
16797 @ViewDebug.ExportedProperty(category = "text", mapping = {
16798 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16799 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16800 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16801 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16802 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16803 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16804 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16805 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016806 public int getRawTextAlignment() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070016807 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_MASK) >> PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016808 }
16809
16810 /**
16811 * Set the text alignment.
16812 *
16813 * @param textAlignment The text alignment to set. Should be one of
16814 *
16815 * {@link #TEXT_ALIGNMENT_INHERIT},
16816 * {@link #TEXT_ALIGNMENT_GRAVITY},
16817 * {@link #TEXT_ALIGNMENT_CENTER},
16818 * {@link #TEXT_ALIGNMENT_TEXT_START},
16819 * {@link #TEXT_ALIGNMENT_TEXT_END},
16820 * {@link #TEXT_ALIGNMENT_VIEW_START},
16821 * {@link #TEXT_ALIGNMENT_VIEW_END}
16822 *
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016823 * Resolution will be done if the value is set to TEXT_ALIGNMENT_INHERIT. The resolution
16824 * proceeds up the parent chain of the view to get the value. If there is no parent, then it
16825 * will return the default {@link #TEXT_ALIGNMENT_GRAVITY}.
16826 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016827 * @attr ref android.R.styleable#View_textAlignment
16828 */
16829 public void setTextAlignment(int textAlignment) {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016830 if (textAlignment != getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016831 // Reset the current and resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016832 mPrivateFlags2 &= ~PFLAG2_TEXT_ALIGNMENT_MASK;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016833 resetResolvedTextAlignment();
16834 // Set the new text alignment
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016835 mPrivateFlags2 |=
16836 ((textAlignment << PFLAG2_TEXT_ALIGNMENT_MASK_SHIFT) & PFLAG2_TEXT_ALIGNMENT_MASK);
16837 // Do resolution
16838 resolveTextAlignment();
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016839 // Notify change
Fabrice Di Meglio343e1132012-09-28 18:01:17 -070016840 onRtlPropertiesChanged(getLayoutDirection());
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016841 // Refresh
16842 requestLayout();
16843 invalidate(true);
16844 }
16845 }
16846
16847 /**
16848 * Return the resolved text alignment.
16849 *
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016850 * @return the resolved text alignment. Returns one of:
16851 *
16852 * {@link #TEXT_ALIGNMENT_GRAVITY},
16853 * {@link #TEXT_ALIGNMENT_CENTER},
16854 * {@link #TEXT_ALIGNMENT_TEXT_START},
16855 * {@link #TEXT_ALIGNMENT_TEXT_END},
16856 * {@link #TEXT_ALIGNMENT_VIEW_START},
16857 * {@link #TEXT_ALIGNMENT_VIEW_END}
16858 */
16859 @ViewDebug.ExportedProperty(category = "text", mapping = {
16860 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16861 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16862 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16863 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16864 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16865 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16866 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16867 })
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016868 public int getTextAlignment() {
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016869 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK) >>
16870 PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016871 }
16872
16873 /**
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016874 * Resolve the text alignment.
16875 *
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016876 * @return true if resolution has been done, false otherwise.
16877 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016878 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016879 */
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016880 public boolean resolveTextAlignment() {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016881 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016882 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016883
16884 if (hasRtlSupport()) {
16885 // Set resolved text alignment flag depending on text alignment flag
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016886 final int textAlignment = getRawTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016887 switch (textAlignment) {
16888 case TEXT_ALIGNMENT_INHERIT:
16889 // Check if we can resolve the text alignment
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016890 if (!canResolveTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016891 // We cannot do the resolution if there is no parent so use the default
Dianne Hackborn4702a852012-08-17 15:18:29 -070016892 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016893 // Resolution will need to happen again later
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016894 return false;
16895 }
16896 View parent = (View) mParent;
16897
16898 // Parent has not yet resolved, so we still return the default
16899 if (!parent.isTextAlignmentResolved()) {
16900 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16901 // Resolution will need to happen again later
16902 return false;
16903 }
16904
16905 final int parentResolvedTextAlignment = parent.getTextAlignment();
16906 switch (parentResolvedTextAlignment) {
16907 case TEXT_ALIGNMENT_GRAVITY:
16908 case TEXT_ALIGNMENT_TEXT_START:
16909 case TEXT_ALIGNMENT_TEXT_END:
16910 case TEXT_ALIGNMENT_CENTER:
16911 case TEXT_ALIGNMENT_VIEW_START:
16912 case TEXT_ALIGNMENT_VIEW_END:
16913 // Resolved text alignment is the same as the parent resolved
16914 // text alignment
16915 mPrivateFlags2 |=
16916 (parentResolvedTextAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16917 break;
16918 default:
16919 // Use default resolved text alignment
16920 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016921 }
16922 break;
16923 case TEXT_ALIGNMENT_GRAVITY:
16924 case TEXT_ALIGNMENT_TEXT_START:
16925 case TEXT_ALIGNMENT_TEXT_END:
16926 case TEXT_ALIGNMENT_CENTER:
16927 case TEXT_ALIGNMENT_VIEW_START:
16928 case TEXT_ALIGNMENT_VIEW_END:
16929 // Resolved text alignment is the same as text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016930 mPrivateFlags2 |= (textAlignment << PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016931 break;
16932 default:
16933 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016934 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016935 }
16936 } else {
16937 // Use default resolved text alignment
Dianne Hackborn4702a852012-08-17 15:18:29 -070016938 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016939 }
16940
16941 // Set the resolved
Dianne Hackborn4702a852012-08-17 15:18:29 -070016942 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED;
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016943 return true;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016944 }
16945
16946 /**
16947 * Check if text alignment resolution can be done.
16948 *
16949 * @return true if text alignment resolution can be done otherwise return false.
16950 */
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016951 private boolean canResolveTextAlignment() {
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016952 switch (getRawTextAlignment()) {
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016953 case TEXT_DIRECTION_INHERIT:
Fabrice Di Meglio9a048562012-09-26 14:55:56 -070016954 return (mParent != null) && (mParent instanceof View) &&
16955 ((View) mParent).canResolveTextAlignment();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016956 default:
16957 return true;
16958 }
16959 }
16960
16961 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016962 * Reset resolved text alignment. Text alignment will be resolved during a call to
16963 * {@link #onMeasure(int, int)}.
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016964 *
Fabrice Di Meglio4457e852012-09-18 19:23:12 -070016965 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016966 */
16967 public void resetResolvedTextAlignment() {
16968 // Reset any previous text alignment resolution
Dianne Hackborn4702a852012-08-17 15:18:29 -070016969 mPrivateFlags2 &= ~(PFLAG2_TEXT_ALIGNMENT_RESOLVED | PFLAG2_TEXT_ALIGNMENT_RESOLVED_MASK);
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016970 // Set to default
16971 mPrivateFlags2 |= PFLAG2_TEXT_ALIGNMENT_RESOLVED_DEFAULT;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016972 }
16973
Adam Powella9108a22012-07-18 11:18:09 -070016974 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016975 * @return true if text alignment is inherited.
16976 *
Fabrice Di Meglio1a7d4872012-09-23 16:19:58 -070016977 * @hide
16978 */
16979 public boolean isTextAlignmentInherited() {
16980 return (getRawTextAlignment() == TEXT_ALIGNMENT_INHERIT);
16981 }
16982
16983 /**
Fabrice Di Meglio1f88ba82012-09-24 14:56:49 -070016984 * @return true if text alignment is resolved.
16985 */
16986 private boolean isTextAlignmentResolved() {
16987 return (mPrivateFlags2 & PFLAG2_TEXT_ALIGNMENT_RESOLVED) == PFLAG2_TEXT_ALIGNMENT_RESOLVED;
16988 }
16989
16990 /**
Adam Powella9108a22012-07-18 11:18:09 -070016991 * Generate a value suitable for use in {@link #setId(int)}.
16992 * This value will not collide with ID values generated at build time by aapt for R.id.
16993 *
16994 * @return a generated ID value
16995 */
16996 public static int generateViewId() {
16997 for (;;) {
16998 final int result = sNextGeneratedId.get();
16999 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
17000 int newValue = result + 1;
17001 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
17002 if (sNextGeneratedId.compareAndSet(result, newValue)) {
17003 return result;
17004 }
17005 }
17006 }
17007
Chet Haaseb39f0512011-05-24 14:36:40 -070017008 //
17009 // Properties
17010 //
17011 /**
17012 * A Property wrapper around the <code>alpha</code> functionality handled by the
17013 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
17014 */
Chet Haased47f1532011-12-16 11:18:52 -080017015 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017016 @Override
17017 public void setValue(View object, float value) {
17018 object.setAlpha(value);
17019 }
17020
17021 @Override
17022 public Float get(View object) {
17023 return object.getAlpha();
17024 }
17025 };
17026
17027 /**
17028 * A Property wrapper around the <code>translationX</code> functionality handled by the
17029 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
17030 */
Chet Haased47f1532011-12-16 11:18:52 -080017031 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017032 @Override
17033 public void setValue(View object, float value) {
17034 object.setTranslationX(value);
17035 }
17036
17037 @Override
17038 public Float get(View object) {
17039 return object.getTranslationX();
17040 }
17041 };
17042
17043 /**
17044 * A Property wrapper around the <code>translationY</code> functionality handled by the
17045 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
17046 */
Chet Haased47f1532011-12-16 11:18:52 -080017047 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017048 @Override
17049 public void setValue(View object, float value) {
17050 object.setTranslationY(value);
17051 }
17052
17053 @Override
17054 public Float get(View object) {
17055 return object.getTranslationY();
17056 }
17057 };
17058
17059 /**
17060 * A Property wrapper around the <code>x</code> functionality handled by the
17061 * {@link View#setX(float)} and {@link View#getX()} methods.
17062 */
Chet Haased47f1532011-12-16 11:18:52 -080017063 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017064 @Override
17065 public void setValue(View object, float value) {
17066 object.setX(value);
17067 }
17068
17069 @Override
17070 public Float get(View object) {
17071 return object.getX();
17072 }
17073 };
17074
17075 /**
17076 * A Property wrapper around the <code>y</code> functionality handled by the
17077 * {@link View#setY(float)} and {@link View#getY()} methods.
17078 */
Chet Haased47f1532011-12-16 11:18:52 -080017079 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017080 @Override
17081 public void setValue(View object, float value) {
17082 object.setY(value);
17083 }
17084
17085 @Override
17086 public Float get(View object) {
17087 return object.getY();
17088 }
17089 };
17090
17091 /**
17092 * A Property wrapper around the <code>rotation</code> functionality handled by the
17093 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
17094 */
Chet Haased47f1532011-12-16 11:18:52 -080017095 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017096 @Override
17097 public void setValue(View object, float value) {
17098 object.setRotation(value);
17099 }
17100
17101 @Override
17102 public Float get(View object) {
17103 return object.getRotation();
17104 }
17105 };
17106
17107 /**
17108 * A Property wrapper around the <code>rotationX</code> functionality handled by the
17109 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
17110 */
Chet Haased47f1532011-12-16 11:18:52 -080017111 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017112 @Override
17113 public void setValue(View object, float value) {
17114 object.setRotationX(value);
17115 }
17116
17117 @Override
17118 public Float get(View object) {
17119 return object.getRotationX();
17120 }
17121 };
17122
17123 /**
17124 * A Property wrapper around the <code>rotationY</code> functionality handled by the
17125 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
17126 */
Chet Haased47f1532011-12-16 11:18:52 -080017127 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017128 @Override
17129 public void setValue(View object, float value) {
17130 object.setRotationY(value);
17131 }
17132
17133 @Override
17134 public Float get(View object) {
17135 return object.getRotationY();
17136 }
17137 };
17138
17139 /**
17140 * A Property wrapper around the <code>scaleX</code> functionality handled by the
17141 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
17142 */
Chet Haased47f1532011-12-16 11:18:52 -080017143 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017144 @Override
17145 public void setValue(View object, float value) {
17146 object.setScaleX(value);
17147 }
17148
17149 @Override
17150 public Float get(View object) {
17151 return object.getScaleX();
17152 }
17153 };
17154
17155 /**
17156 * A Property wrapper around the <code>scaleY</code> functionality handled by the
17157 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
17158 */
Chet Haased47f1532011-12-16 11:18:52 -080017159 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070017160 @Override
17161 public void setValue(View object, float value) {
17162 object.setScaleY(value);
17163 }
17164
17165 @Override
17166 public Float get(View object) {
17167 return object.getScaleY();
17168 }
17169 };
17170
Jeff Brown33bbfd22011-02-24 20:55:35 -080017171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017172 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
17173 * Each MeasureSpec represents a requirement for either the width or the height.
17174 * A MeasureSpec is comprised of a size and a mode. There are three possible
17175 * modes:
17176 * <dl>
17177 * <dt>UNSPECIFIED</dt>
17178 * <dd>
17179 * The parent has not imposed any constraint on the child. It can be whatever size
17180 * it wants.
17181 * </dd>
17182 *
17183 * <dt>EXACTLY</dt>
17184 * <dd>
17185 * The parent has determined an exact size for the child. The child is going to be
17186 * given those bounds regardless of how big it wants to be.
17187 * </dd>
17188 *
17189 * <dt>AT_MOST</dt>
17190 * <dd>
17191 * The child can be as large as it wants up to the specified size.
17192 * </dd>
17193 * </dl>
17194 *
17195 * MeasureSpecs are implemented as ints to reduce object allocation. This class
17196 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
17197 */
17198 public static class MeasureSpec {
17199 private static final int MODE_SHIFT = 30;
17200 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
17201
17202 /**
17203 * Measure specification mode: The parent has not imposed any constraint
17204 * on the child. It can be whatever size it wants.
17205 */
17206 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
17207
17208 /**
17209 * Measure specification mode: The parent has determined an exact size
17210 * for the child. The child is going to be given those bounds regardless
17211 * of how big it wants to be.
17212 */
17213 public static final int EXACTLY = 1 << MODE_SHIFT;
17214
17215 /**
17216 * Measure specification mode: The child can be as large as it wants up
17217 * to the specified size.
17218 */
17219 public static final int AT_MOST = 2 << MODE_SHIFT;
17220
17221 /**
17222 * Creates a measure specification based on the supplied size and mode.
17223 *
17224 * The mode must always be one of the following:
17225 * <ul>
17226 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
17227 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
17228 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
17229 * </ul>
17230 *
17231 * @param size the size of the measure specification
17232 * @param mode the mode of the measure specification
17233 * @return the measure specification based on size and mode
17234 */
17235 public static int makeMeasureSpec(int size, int mode) {
Adam Powell946d05b2012-10-01 15:06:20 -070017236 return size + mode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017237 }
17238
17239 /**
17240 * Extracts the mode from the supplied measure specification.
17241 *
17242 * @param measureSpec the measure specification to extract the mode from
17243 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
17244 * {@link android.view.View.MeasureSpec#AT_MOST} or
17245 * {@link android.view.View.MeasureSpec#EXACTLY}
17246 */
17247 public static int getMode(int measureSpec) {
17248 return (measureSpec & MODE_MASK);
17249 }
17250
17251 /**
17252 * Extracts the size from the supplied measure specification.
17253 *
17254 * @param measureSpec the measure specification to extract the size from
17255 * @return the size in pixels defined in the supplied measure specification
17256 */
17257 public static int getSize(int measureSpec) {
17258 return (measureSpec & ~MODE_MASK);
17259 }
17260
17261 /**
17262 * Returns a String representation of the specified measure
17263 * specification.
17264 *
17265 * @param measureSpec the measure specification to convert to a String
17266 * @return a String with the following format: "MeasureSpec: MODE SIZE"
17267 */
17268 public static String toString(int measureSpec) {
17269 int mode = getMode(measureSpec);
17270 int size = getSize(measureSpec);
17271
17272 StringBuilder sb = new StringBuilder("MeasureSpec: ");
17273
17274 if (mode == UNSPECIFIED)
17275 sb.append("UNSPECIFIED ");
17276 else if (mode == EXACTLY)
17277 sb.append("EXACTLY ");
17278 else if (mode == AT_MOST)
17279 sb.append("AT_MOST ");
17280 else
17281 sb.append(mode).append(" ");
17282
17283 sb.append(size);
17284 return sb.toString();
17285 }
17286 }
17287
17288 class CheckForLongPress implements Runnable {
17289
17290 private int mOriginalWindowAttachCount;
17291
17292 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070017293 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017294 && mOriginalWindowAttachCount == mWindowAttachCount) {
17295 if (performLongClick()) {
17296 mHasPerformedLongPress = true;
17297 }
17298 }
17299 }
17300
17301 public void rememberWindowAttachCount() {
17302 mOriginalWindowAttachCount = mWindowAttachCount;
17303 }
17304 }
Joe Malin32736f02011-01-19 16:14:20 -080017305
Adam Powelle14579b2009-12-16 18:39:52 -080017306 private final class CheckForTap implements Runnable {
17307 public void run() {
Dianne Hackborn4702a852012-08-17 15:18:29 -070017308 mPrivateFlags &= ~PFLAG_PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080017309 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070017310 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080017311 }
17312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017313
Adam Powella35d7682010-03-12 14:48:13 -080017314 private final class PerformClick implements Runnable {
17315 public void run() {
17316 performClick();
17317 }
17318 }
17319
Dianne Hackborn63042d62011-01-26 18:56:29 -080017320 /** @hide */
17321 public void hackTurnOffWindowResizeAnim(boolean off) {
17322 mAttachInfo.mTurnOffWindowResizeAnim = off;
17323 }
Joe Malin32736f02011-01-19 16:14:20 -080017324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017325 /**
Chet Haasea00f3862011-02-22 06:34:40 -080017326 * This method returns a ViewPropertyAnimator object, which can be used to animate
17327 * specific properties on this View.
17328 *
17329 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
17330 */
17331 public ViewPropertyAnimator animate() {
17332 if (mAnimator == null) {
17333 mAnimator = new ViewPropertyAnimator(this);
17334 }
17335 return mAnimator;
17336 }
17337
17338 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017339 * Interface definition for a callback to be invoked when a hardware key event is
17340 * dispatched to this view. The callback will be invoked before the key event is
17341 * given to the view. This is only useful for hardware keyboards; a software input
17342 * method has no obligation to trigger this listener.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017343 */
17344 public interface OnKeyListener {
17345 /**
Jean Chalard405bc512012-05-29 19:12:34 +090017346 * Called when a hardware key is dispatched to a view. This allows listeners to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017347 * get a chance to respond before the target view.
Jean Chalard405bc512012-05-29 19:12:34 +090017348 * <p>Key presses in software keyboards will generally NOT trigger this method,
17349 * although some may elect to do so in some situations. Do not assume a
17350 * software input method has to be key-based; even if it is, it may use key presses
17351 * in a different way than you expect, so there is no way to reliably catch soft
17352 * input key presses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017353 *
17354 * @param v The view the key has been dispatched to.
17355 * @param keyCode The code for the physical key that was pressed
17356 * @param event The KeyEvent object containing full information about
17357 * the event.
17358 * @return True if the listener has consumed the event, false otherwise.
17359 */
17360 boolean onKey(View v, int keyCode, KeyEvent event);
17361 }
17362
17363 /**
17364 * Interface definition for a callback to be invoked when a touch event is
17365 * dispatched to this view. The callback will be invoked before the touch
17366 * event is given to the view.
17367 */
17368 public interface OnTouchListener {
17369 /**
17370 * Called when a touch event is dispatched to a view. This allows listeners to
17371 * get a chance to respond before the target view.
17372 *
17373 * @param v The view the touch event has been dispatched to.
17374 * @param event The MotionEvent object containing full information about
17375 * the event.
17376 * @return True if the listener has consumed the event, false otherwise.
17377 */
17378 boolean onTouch(View v, MotionEvent event);
17379 }
17380
17381 /**
Jeff Brown10b62902011-06-20 16:40:37 -070017382 * Interface definition for a callback to be invoked when a hover event is
17383 * dispatched to this view. The callback will be invoked before the hover
17384 * event is given to the view.
17385 */
17386 public interface OnHoverListener {
17387 /**
17388 * Called when a hover event is dispatched to a view. This allows listeners to
17389 * get a chance to respond before the target view.
17390 *
17391 * @param v The view the hover event has been dispatched to.
17392 * @param event The MotionEvent object containing full information about
17393 * the event.
17394 * @return True if the listener has consumed the event, false otherwise.
17395 */
17396 boolean onHover(View v, MotionEvent event);
17397 }
17398
17399 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080017400 * Interface definition for a callback to be invoked when a generic motion event is
17401 * dispatched to this view. The callback will be invoked before the generic motion
17402 * event is given to the view.
17403 */
17404 public interface OnGenericMotionListener {
17405 /**
17406 * Called when a generic motion event is dispatched to a view. This allows listeners to
17407 * get a chance to respond before the target view.
17408 *
17409 * @param v The view the generic motion event has been dispatched to.
17410 * @param event The MotionEvent object containing full information about
17411 * the event.
17412 * @return True if the listener has consumed the event, false otherwise.
17413 */
17414 boolean onGenericMotion(View v, MotionEvent event);
17415 }
17416
17417 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017418 * Interface definition for a callback to be invoked when a view has been clicked and held.
17419 */
17420 public interface OnLongClickListener {
17421 /**
17422 * Called when a view has been clicked and held.
17423 *
17424 * @param v The view that was clicked and held.
17425 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080017426 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017427 */
17428 boolean onLongClick(View v);
17429 }
17430
17431 /**
Chris Tate32affef2010-10-18 15:29:21 -070017432 * Interface definition for a callback to be invoked when a drag is being dispatched
17433 * to this view. The callback will be invoked before the hosting view's own
17434 * onDrag(event) method. If the listener wants to fall back to the hosting view's
17435 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070017436 *
17437 * <div class="special reference">
17438 * <h3>Developer Guides</h3>
17439 * <p>For a guide to implementing drag and drop features, read the
17440 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
17441 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070017442 */
17443 public interface OnDragListener {
17444 /**
17445 * Called when a drag event is dispatched to a view. This allows listeners
17446 * to get a chance to override base View behavior.
17447 *
Joe Malin32736f02011-01-19 16:14:20 -080017448 * @param v The View that received the drag event.
17449 * @param event The {@link android.view.DragEvent} object for the drag event.
17450 * @return {@code true} if the drag event was handled successfully, or {@code false}
17451 * if the drag event was not handled. Note that {@code false} will trigger the View
17452 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070017453 */
17454 boolean onDrag(View v, DragEvent event);
17455 }
17456
17457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017458 * Interface definition for a callback to be invoked when the focus state of
17459 * a view changed.
17460 */
17461 public interface OnFocusChangeListener {
17462 /**
17463 * Called when the focus state of a view has changed.
17464 *
17465 * @param v The view whose state has changed.
17466 * @param hasFocus The new focus state of v.
17467 */
17468 void onFocusChange(View v, boolean hasFocus);
17469 }
17470
17471 /**
17472 * Interface definition for a callback to be invoked when a view is clicked.
17473 */
17474 public interface OnClickListener {
17475 /**
17476 * Called when a view has been clicked.
17477 *
17478 * @param v The view that was clicked.
17479 */
17480 void onClick(View v);
17481 }
17482
17483 /**
17484 * Interface definition for a callback to be invoked when the context menu
17485 * for this view is being built.
17486 */
17487 public interface OnCreateContextMenuListener {
17488 /**
17489 * Called when the context menu for this view is being built. It is not
17490 * safe to hold onto the menu after this method returns.
17491 *
17492 * @param menu The context menu that is being built
17493 * @param v The view for which the context menu is being built
17494 * @param menuInfo Extra information about the item for which the
17495 * context menu should be shown. This information will vary
17496 * depending on the class of v.
17497 */
17498 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
17499 }
17500
Joe Onorato664644d2011-01-23 17:53:23 -080017501 /**
17502 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017503 * visibility. This reports <strong>global</strong> changes to the system UI
Dianne Hackborncf675782012-05-10 15:07:24 -070017504 * state, not what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080017505 *
Philip Milne6c8ea062012-04-03 17:38:43 -070017506 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080017507 */
17508 public interface OnSystemUiVisibilityChangeListener {
17509 /**
17510 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070017511 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080017512 *
Dianne Hackborncf675782012-05-10 15:07:24 -070017513 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
17514 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, and {@link #SYSTEM_UI_FLAG_FULLSCREEN}.
17515 * This tells you the <strong>global</strong> state of these UI visibility
17516 * flags, not what your app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080017517 */
17518 public void onSystemUiVisibilityChange(int visibility);
17519 }
17520
Adam Powell4afd62b2011-02-18 15:02:18 -080017521 /**
17522 * Interface definition for a callback to be invoked when this view is attached
17523 * or detached from its window.
17524 */
17525 public interface OnAttachStateChangeListener {
17526 /**
17527 * Called when the view is attached to a window.
17528 * @param v The view that was attached
17529 */
17530 public void onViewAttachedToWindow(View v);
17531 /**
17532 * Called when the view is detached from a window.
17533 * @param v The view that was detached
17534 */
17535 public void onViewDetachedFromWindow(View v);
17536 }
17537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017538 private final class UnsetPressedState implements Runnable {
17539 public void run() {
17540 setPressed(false);
17541 }
17542 }
17543
17544 /**
17545 * Base class for derived classes that want to save and restore their own
17546 * state in {@link android.view.View#onSaveInstanceState()}.
17547 */
17548 public static class BaseSavedState extends AbsSavedState {
17549 /**
17550 * Constructor used when reading from a parcel. Reads the state of the superclass.
17551 *
17552 * @param source
17553 */
17554 public BaseSavedState(Parcel source) {
17555 super(source);
17556 }
17557
17558 /**
17559 * Constructor called by derived classes when creating their SavedState objects
17560 *
17561 * @param superState The state of the superclass of this view
17562 */
17563 public BaseSavedState(Parcelable superState) {
17564 super(superState);
17565 }
17566
17567 public static final Parcelable.Creator<BaseSavedState> CREATOR =
17568 new Parcelable.Creator<BaseSavedState>() {
17569 public BaseSavedState createFromParcel(Parcel in) {
17570 return new BaseSavedState(in);
17571 }
17572
17573 public BaseSavedState[] newArray(int size) {
17574 return new BaseSavedState[size];
17575 }
17576 };
17577 }
17578
17579 /**
17580 * A set of information given to a view when it is attached to its parent
17581 * window.
17582 */
17583 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017584 interface Callbacks {
17585 void playSoundEffect(int effectId);
17586 boolean performHapticFeedback(int effectId, boolean always);
17587 }
17588
17589 /**
17590 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
17591 * to a Handler. This class contains the target (View) to invalidate and
17592 * the coordinates of the dirty rectangle.
17593 *
17594 * For performance purposes, this class also implements a pool of up to
17595 * POOL_LIMIT objects that get reused. This reduces memory allocations
17596 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017597 */
Romain Guyd928d682009-03-31 17:52:16 -070017598 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017599 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070017600 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
17601 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070017602 public InvalidateInfo newInstance() {
17603 return new InvalidateInfo();
17604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017605
Romain Guyd928d682009-03-31 17:52:16 -070017606 public void onAcquired(InvalidateInfo element) {
17607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017608
Romain Guyd928d682009-03-31 17:52:16 -070017609 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070017610 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070017611 }
17612 }, POOL_LIMIT)
17613 );
17614
17615 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017616 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017617
17618 View target;
17619
17620 int left;
17621 int top;
17622 int right;
17623 int bottom;
17624
Romain Guyd928d682009-03-31 17:52:16 -070017625 public void setNextPoolable(InvalidateInfo element) {
17626 mNext = element;
17627 }
17628
17629 public InvalidateInfo getNextPoolable() {
17630 return mNext;
17631 }
17632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017633 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070017634 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017635 }
17636
17637 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070017638 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017639 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017640
17641 public boolean isPooled() {
17642 return mIsPooled;
17643 }
17644
17645 public void setPooled(boolean isPooled) {
17646 mIsPooled = isPooled;
17647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017648 }
17649
17650 final IWindowSession mSession;
17651
17652 final IWindow mWindow;
17653
17654 final IBinder mWindowToken;
17655
Jeff Brown98365d72012-08-19 20:30:52 -070017656 final Display mDisplay;
17657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017658 final Callbacks mRootCallbacks;
17659
Romain Guy59a12ca2011-06-09 17:48:21 -070017660 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080017661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017662 /**
17663 * The top view of the hierarchy.
17664 */
17665 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070017666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017667 IBinder mPanelParentWindowToken;
17668 Surface mSurface;
17669
Romain Guyb051e892010-09-28 19:09:36 -070017670 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080017671 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070017672 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080017673
Romain Guy7e4e5612012-03-05 14:37:29 -080017674 boolean mScreenOn;
17675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017676 /**
Romain Guy8506ab42009-06-11 17:35:47 -070017677 * Scale factor used by the compatibility mode
17678 */
17679 float mApplicationScale;
17680
17681 /**
17682 * Indicates whether the application is in compatibility mode
17683 */
17684 boolean mScalingRequired;
17685
17686 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017687 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080017688 */
17689 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080017690
Dianne Hackborn63042d62011-01-26 18:56:29 -080017691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017692 * Left position of this view's window
17693 */
17694 int mWindowLeft;
17695
17696 /**
17697 * Top position of this view's window
17698 */
17699 int mWindowTop;
17700
17701 /**
Adam Powell26153a32010-11-08 15:22:27 -080017702 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070017703 */
Adam Powell26153a32010-11-08 15:22:27 -080017704 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070017705
17706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017707 * For windows that are full-screen but using insets to layout inside
17708 * of the screen decorations, these are the current insets for the
17709 * content of the window.
17710 */
17711 final Rect mContentInsets = new Rect();
17712
17713 /**
17714 * For windows that are full-screen but using insets to layout inside
17715 * of the screen decorations, these are the current insets for the
17716 * actual visible parts of the window.
17717 */
17718 final Rect mVisibleInsets = new Rect();
17719
17720 /**
17721 * The internal insets given by this window. This value is
17722 * supplied by the client (through
17723 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
17724 * be given to the window manager when changed to be used in laying
17725 * out windows behind it.
17726 */
17727 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
17728 = new ViewTreeObserver.InternalInsetsInfo();
17729
17730 /**
17731 * All views in the window's hierarchy that serve as scroll containers,
17732 * used to determine if the window can be resized or must be panned
17733 * to adjust for a soft input area.
17734 */
17735 final ArrayList<View> mScrollContainers = new ArrayList<View>();
17736
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070017737 final KeyEvent.DispatcherState mKeyDispatchState
17738 = new KeyEvent.DispatcherState();
17739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017740 /**
17741 * Indicates whether the view's window currently has the focus.
17742 */
17743 boolean mHasWindowFocus;
17744
17745 /**
17746 * The current visibility of the window.
17747 */
17748 int mWindowVisibility;
17749
17750 /**
17751 * Indicates the time at which drawing started to occur.
17752 */
17753 long mDrawingTime;
17754
17755 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070017756 * Indicates whether or not ignoring the DIRTY_MASK flags.
17757 */
17758 boolean mIgnoreDirtyState;
17759
17760 /**
Romain Guy02ccac62011-06-24 13:20:23 -070017761 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
17762 * to avoid clearing that flag prematurely.
17763 */
17764 boolean mSetIgnoreDirtyState = false;
17765
17766 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017767 * Indicates whether the view's window is currently in touch mode.
17768 */
17769 boolean mInTouchMode;
17770
17771 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070017772 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017773 * the next time it performs a traversal
17774 */
17775 boolean mRecomputeGlobalAttributes;
17776
17777 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070017778 * Always report new attributes at next traversal.
17779 */
17780 boolean mForceReportNewAttributes;
17781
17782 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017783 * Set during a traveral if any views want to keep the screen on.
17784 */
17785 boolean mKeepScreenOn;
17786
17787 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017788 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
17789 */
17790 int mSystemUiVisibility;
17791
17792 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070017793 * Hack to force certain system UI visibility flags to be cleared.
17794 */
17795 int mDisabledSystemUiVisibility;
17796
17797 /**
Dianne Hackborncf675782012-05-10 15:07:24 -070017798 * Last global system UI visibility reported by the window manager.
17799 */
17800 int mGlobalSystemUiVisibility;
17801
17802 /**
Joe Onorato664644d2011-01-23 17:53:23 -080017803 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
17804 * attached.
17805 */
17806 boolean mHasSystemUiListeners;
17807
17808 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017809 * Set if the visibility of any views has changed.
17810 */
17811 boolean mViewVisibilityChanged;
17812
17813 /**
17814 * Set to true if a view has been scrolled.
17815 */
17816 boolean mViewScrollChanged;
17817
17818 /**
17819 * Global to the view hierarchy used as a temporary for dealing with
17820 * x/y points in the transparent region computations.
17821 */
17822 final int[] mTransparentLocation = new int[2];
17823
17824 /**
17825 * Global to the view hierarchy used as a temporary for dealing with
17826 * x/y points in the ViewGroup.invalidateChild implementation.
17827 */
17828 final int[] mInvalidateChildLocation = new int[2];
17829
Chet Haasec3aa3612010-06-17 08:50:37 -070017830
17831 /**
17832 * Global to the view hierarchy used as a temporary for dealing with
17833 * x/y location when view is transformed.
17834 */
17835 final float[] mTmpTransformLocation = new float[2];
17836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017837 /**
17838 * The view tree observer used to dispatch global events like
17839 * layout, pre-draw, touch mode change, etc.
17840 */
17841 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17842
17843 /**
17844 * A Canvas used by the view hierarchy to perform bitmap caching.
17845 */
17846 Canvas mCanvas;
17847
17848 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017849 * The view root impl.
17850 */
17851 final ViewRootImpl mViewRootImpl;
17852
17853 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017854 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017855 * handler can be used to pump events in the UI events queue.
17856 */
17857 final Handler mHandler;
17858
17859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017860 * Temporary for use in computing invalidate rectangles while
17861 * calling up the hierarchy.
17862 */
17863 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017864
17865 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017866 * Temporary for use in computing hit areas with transformed views
17867 */
17868 final RectF mTmpTransformRect = new RectF();
17869
17870 /**
Chet Haase599913d2012-07-23 16:22:05 -070017871 * Temporary for use in transforming invalidation rect
17872 */
17873 final Matrix mTmpMatrix = new Matrix();
17874
17875 /**
17876 * Temporary for use in transforming invalidation rect
17877 */
17878 final Transformation mTmpTransformation = new Transformation();
17879
17880 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017881 * Temporary list for use in collecting focusable descendents of a view.
17882 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017883 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017885 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017886 * The id of the window for accessibility purposes.
17887 */
17888 int mAccessibilityWindowId = View.NO_ID;
17889
17890 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017891 * Whether to ingore not exposed for accessibility Views when
17892 * reporting the view tree to accessibility services.
17893 */
17894 boolean mIncludeNotImportantViews;
17895
17896 /**
17897 * The drawable for highlighting accessibility focus.
17898 */
17899 Drawable mAccessibilityFocusDrawable;
17900
17901 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017902 * Show where the margins, bounds and layout bounds are for each view.
17903 */
Dianne Hackborna53de062012-05-08 18:53:51 -070017904 boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017905
17906 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017907 * Point used to compute visible regions.
17908 */
17909 final Point mPoint = new Point();
17910
17911 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017912 * Creates a new set of attachment information with the specified
17913 * events handler and thread.
17914 *
17915 * @param handler the events handler the view must use
17916 */
Jeff Brown98365d72012-08-19 20:30:52 -070017917 AttachInfo(IWindowSession session, IWindow window, Display display,
Jeff Browna175a5b2012-02-15 19:18:31 -080017918 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017919 mSession = session;
17920 mWindow = window;
17921 mWindowToken = window.asBinder();
Jeff Brown98365d72012-08-19 20:30:52 -070017922 mDisplay = display;
Jeff Browna175a5b2012-02-15 19:18:31 -080017923 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017924 mHandler = handler;
17925 mRootCallbacks = effectPlayer;
17926 }
17927 }
17928
17929 /**
17930 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17931 * is supported. This avoids keeping too many unused fields in most
17932 * instances of View.</p>
17933 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017934 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017935
Mike Cleronf116bf82009-09-27 19:14:12 -070017936 /**
17937 * Scrollbars are not visible
17938 */
17939 public static final int OFF = 0;
17940
17941 /**
17942 * Scrollbars are visible
17943 */
17944 public static final int ON = 1;
17945
17946 /**
17947 * Scrollbars are fading away
17948 */
17949 public static final int FADING = 2;
17950
17951 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017953 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017954 public int scrollBarDefaultDelayBeforeFade;
17955 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017956
17957 public int scrollBarSize;
17958 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017959 public float[] interpolatorValues;
17960 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017961
17962 public final Paint paint;
17963 public final Matrix matrix;
17964 public Shader shader;
17965
Mike Cleronf116bf82009-09-27 19:14:12 -070017966 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17967
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017968 private static final float[] OPAQUE = { 255 };
17969 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017970
Mike Cleronf116bf82009-09-27 19:14:12 -070017971 /**
17972 * When fading should start. This time moves into the future every time
17973 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17974 */
17975 public long fadeStartTime;
17976
17977
17978 /**
17979 * The current state of the scrollbars: ON, OFF, or FADING
17980 */
17981 public int state = OFF;
17982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017983 private int mLastColor;
17984
Mike Cleronf116bf82009-09-27 19:14:12 -070017985 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017986 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17987 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017988 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17989 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017990
17991 paint = new Paint();
17992 matrix = new Matrix();
17993 // use use a height of 1, and then wack the matrix each time we
17994 // actually use it.
17995 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017996 paint.setShader(shader);
17997 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Romain Guyd679b572012-08-29 21:49:00 -070017998
Mike Cleronf116bf82009-09-27 19:14:12 -070017999 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018000 }
Romain Guy8506ab42009-06-11 17:35:47 -070018001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018002 public void setFadeColor(int color) {
Romain Guyd679b572012-08-29 21:49:00 -070018003 if (color != mLastColor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018004 mLastColor = color;
Romain Guy8506ab42009-06-11 17:35:47 -070018005
Romain Guyd679b572012-08-29 21:49:00 -070018006 if (color != 0) {
18007 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
18008 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
18009 paint.setShader(shader);
18010 // Restore the default transfer mode (src_over)
18011 paint.setXfermode(null);
18012 } else {
18013 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
18014 paint.setShader(shader);
18015 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
18016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018017 }
18018 }
Joe Malin32736f02011-01-19 16:14:20 -080018019
Mike Cleronf116bf82009-09-27 19:14:12 -070018020 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070018021 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070018022 if (now >= fadeStartTime) {
18023
18024 // the animation fades the scrollbars out by changing
18025 // the opacity (alpha) from fully opaque to fully
18026 // transparent
18027 int nextFrame = (int) now;
18028 int framesCount = 0;
18029
18030 Interpolator interpolator = scrollBarInterpolator;
18031
18032 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018033 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070018034
18035 // End transparent
18036 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080018037 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070018038
18039 state = FADING;
18040
18041 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080018042 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070018043 }
18044 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070018045 }
Mike Cleronf116bf82009-09-27 19:14:12 -070018046
Svetoslav Ganova0156172011-06-26 17:55:44 -070018047 /**
18048 * Resuable callback for sending
18049 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
18050 */
18051 private class SendViewScrolledAccessibilityEvent implements Runnable {
18052 public volatile boolean mIsPending;
18053
18054 public void run() {
18055 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
18056 mIsPending = false;
18057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018058 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018059
18060 /**
18061 * <p>
18062 * This class represents a delegate that can be registered in a {@link View}
18063 * to enhance accessibility support via composition rather via inheritance.
18064 * It is specifically targeted to widget developers that extend basic View
18065 * classes i.e. classes in package android.view, that would like their
18066 * applications to be backwards compatible.
18067 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080018068 * <div class="special reference">
18069 * <h3>Developer Guides</h3>
18070 * <p>For more information about making applications accessible, read the
18071 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
18072 * developer guide.</p>
18073 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018074 * <p>
18075 * A scenario in which a developer would like to use an accessibility delegate
18076 * is overriding a method introduced in a later API version then the minimal API
18077 * version supported by the application. For example, the method
18078 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
18079 * in API version 4 when the accessibility APIs were first introduced. If a
18080 * developer would like his application to run on API version 4 devices (assuming
18081 * all other APIs used by the application are version 4 or lower) and take advantage
18082 * of this method, instead of overriding the method which would break the application's
18083 * backwards compatibility, he can override the corresponding method in this
18084 * delegate and register the delegate in the target View if the API version of
18085 * the system is high enough i.e. the API version is same or higher to the API
18086 * version that introduced
18087 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
18088 * </p>
18089 * <p>
18090 * Here is an example implementation:
18091 * </p>
18092 * <code><pre><p>
18093 * if (Build.VERSION.SDK_INT >= 14) {
18094 * // If the API version is equal of higher than the version in
18095 * // which onInitializeAccessibilityNodeInfo was introduced we
18096 * // register a delegate with a customized implementation.
18097 * View view = findViewById(R.id.view_id);
18098 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
18099 * public void onInitializeAccessibilityNodeInfo(View host,
18100 * AccessibilityNodeInfo info) {
18101 * // Let the default implementation populate the info.
18102 * super.onInitializeAccessibilityNodeInfo(host, info);
18103 * // Set some other information.
18104 * info.setEnabled(host.isEnabled());
18105 * }
18106 * });
18107 * }
18108 * </code></pre></p>
18109 * <p>
18110 * This delegate contains methods that correspond to the accessibility methods
18111 * in View. If a delegate has been specified the implementation in View hands
18112 * off handling to the corresponding method in this delegate. The default
18113 * implementation the delegate methods behaves exactly as the corresponding
18114 * method in View for the case of no accessibility delegate been set. Hence,
18115 * to customize the behavior of a View method, clients can override only the
18116 * corresponding delegate method without altering the behavior of the rest
18117 * accessibility related methods of the host view.
18118 * </p>
18119 */
18120 public static class AccessibilityDelegate {
18121
18122 /**
18123 * Sends an accessibility event of the given type. If accessibility is not
18124 * enabled this method has no effect.
18125 * <p>
18126 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
18127 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
18128 * been set.
18129 * </p>
18130 *
18131 * @param host The View hosting the delegate.
18132 * @param eventType The type of the event to send.
18133 *
18134 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
18135 */
18136 public void sendAccessibilityEvent(View host, int eventType) {
18137 host.sendAccessibilityEventInternal(eventType);
18138 }
18139
18140 /**
alanv8eeefef2012-05-07 16:57:53 -070018141 * Performs the specified accessibility action on the view. For
18142 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
18143 * <p>
18144 * The default implementation behaves as
18145 * {@link View#performAccessibilityAction(int, Bundle)
18146 * View#performAccessibilityAction(int, Bundle)} for the case of
18147 * no accessibility delegate been set.
18148 * </p>
18149 *
18150 * @param action The action to perform.
18151 * @return Whether the action was performed.
18152 *
18153 * @see View#performAccessibilityAction(int, Bundle)
18154 * View#performAccessibilityAction(int, Bundle)
18155 */
18156 public boolean performAccessibilityAction(View host, int action, Bundle args) {
18157 return host.performAccessibilityActionInternal(action, args);
18158 }
18159
18160 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018161 * Sends an accessibility event. This method behaves exactly as
18162 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
18163 * empty {@link AccessibilityEvent} and does not perform a check whether
18164 * accessibility is enabled.
18165 * <p>
18166 * The default implementation behaves as
18167 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18168 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
18169 * the case of no accessibility delegate been set.
18170 * </p>
18171 *
18172 * @param host The View hosting the delegate.
18173 * @param event The event to send.
18174 *
18175 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18176 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
18177 */
18178 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
18179 host.sendAccessibilityEventUncheckedInternal(event);
18180 }
18181
18182 /**
18183 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
18184 * to its children for adding their text content to the event.
18185 * <p>
18186 * The default implementation behaves as
18187 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18188 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
18189 * the case of no accessibility delegate been set.
18190 * </p>
18191 *
18192 * @param host The View hosting the delegate.
18193 * @param event The event.
18194 * @return True if the event population was completed.
18195 *
18196 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18197 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
18198 */
18199 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18200 return host.dispatchPopulateAccessibilityEventInternal(event);
18201 }
18202
18203 /**
18204 * Gives a chance to the host View to populate the accessibility event with its
18205 * text content.
18206 * <p>
18207 * The default implementation behaves as
18208 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
18209 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
18210 * the case of no accessibility delegate been set.
18211 * </p>
18212 *
18213 * @param host The View hosting the delegate.
18214 * @param event The accessibility event which to populate.
18215 *
18216 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
18217 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
18218 */
18219 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
18220 host.onPopulateAccessibilityEventInternal(event);
18221 }
18222
18223 /**
18224 * Initializes an {@link AccessibilityEvent} with information about the
18225 * the host View which is the event source.
18226 * <p>
18227 * The default implementation behaves as
18228 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
18229 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
18230 * the case of no accessibility delegate been set.
18231 * </p>
18232 *
18233 * @param host The View hosting the delegate.
18234 * @param event The event to initialize.
18235 *
18236 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
18237 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
18238 */
18239 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
18240 host.onInitializeAccessibilityEventInternal(event);
18241 }
18242
18243 /**
18244 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
18245 * <p>
18246 * The default implementation behaves as
18247 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18248 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
18249 * the case of no accessibility delegate been set.
18250 * </p>
18251 *
18252 * @param host The View hosting the delegate.
18253 * @param info The instance to initialize.
18254 *
18255 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18256 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
18257 */
18258 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
18259 host.onInitializeAccessibilityNodeInfoInternal(info);
18260 }
18261
18262 /**
18263 * Called when a child of the host View has requested sending an
18264 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
18265 * to augment the event.
18266 * <p>
18267 * The default implementation behaves as
18268 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18269 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
18270 * the case of no accessibility delegate been set.
18271 * </p>
18272 *
18273 * @param host The View hosting the delegate.
18274 * @param child The child which requests sending the event.
18275 * @param event The event to be sent.
18276 * @return True if the event should be sent
18277 *
18278 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18279 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
18280 */
18281 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
18282 AccessibilityEvent event) {
18283 return host.onRequestSendAccessibilityEventInternal(child, event);
18284 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070018285
18286 /**
18287 * Gets the provider for managing a virtual view hierarchy rooted at this View
18288 * and reported to {@link android.accessibilityservice.AccessibilityService}s
18289 * that explore the window content.
18290 * <p>
18291 * The default implementation behaves as
18292 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
18293 * the case of no accessibility delegate been set.
18294 * </p>
18295 *
18296 * @return The provider.
18297 *
18298 * @see AccessibilityNodeProvider
18299 */
18300 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
18301 return null;
18302 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070018303 }
Svetoslav Ganov33aef982012-09-13 12:49:03 -070018304
18305 private class MatchIdPredicate implements Predicate<View> {
18306 public int mId;
18307
18308 @Override
18309 public boolean apply(View view) {
18310 return (view.mID == mId);
18311 }
18312 }
18313
18314 private class MatchLabelForPredicate implements Predicate<View> {
18315 private int mLabeledId;
18316
18317 @Override
18318 public boolean apply(View view) {
18319 return (view.mLabelForId == mLabeledId);
18320 }
18321 }
Jeff Sharkey36901b62012-09-19 19:06:22 -070018322
18323 /**
18324 * Dump all private flags in readable format, useful for documentation and
18325 * sanity checking.
18326 */
18327 private static void dumpFlags() {
18328 final HashMap<String, String> found = Maps.newHashMap();
18329 try {
18330 for (Field field : View.class.getDeclaredFields()) {
18331 final int modifiers = field.getModifiers();
18332 if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
18333 if (field.getType().equals(int.class)) {
18334 final int value = field.getInt(null);
18335 dumpFlag(found, field.getName(), value);
18336 } else if (field.getType().equals(int[].class)) {
18337 final int[] values = (int[]) field.get(null);
18338 for (int i = 0; i < values.length; i++) {
18339 dumpFlag(found, field.getName() + "[" + i + "]", values[i]);
18340 }
18341 }
18342 }
18343 }
18344 } catch (IllegalAccessException e) {
18345 throw new RuntimeException(e);
18346 }
18347
18348 final ArrayList<String> keys = Lists.newArrayList();
18349 keys.addAll(found.keySet());
18350 Collections.sort(keys);
18351 for (String key : keys) {
18352 Log.d(VIEW_LOG_TAG, found.get(key));
18353 }
18354 }
18355
18356 private static void dumpFlag(HashMap<String, String> found, String name, int value) {
18357 // Sort flags by prefix, then by bits, always keeping unique keys
18358 final String bits = String.format("%32s", Integer.toBinaryString(value)).replace('0', ' ');
18359 final int prefix = name.indexOf('_');
18360 final String key = (prefix > 0 ? name.substring(0, prefix) : name) + bits + name;
18361 final String output = bits + " " + name;
18362 found.put(key, output);
18363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018364}