blob: 6ed4e86c18c2eea4d75631c11cfe4210c4e15268 [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;
Svetoslav Ganovaa780c12012-04-19 23:01:39 -070042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
44import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
Philip Milne10ca24a2012-04-23 15:38:27 -070049import android.os.SystemProperties;
Svetoslav Ganovea515ae2011-09-14 18:15:32 -070050import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070052import android.util.FloatProperty;
53import android.util.LocaleUtil;
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;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityEventSource;
65import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070066import android.view.accessibility.AccessibilityNodeInfo;
Svetoslav Ganov02107852011-10-03 17:06:56 -070067import android.view.accessibility.AccessibilityNodeProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
Chet Haase64a48c12012-02-13 16:33:29 -080070import android.view.animation.Transformation;
svetoslavganov75986cf2009-05-14 22:28:01 -070071import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.view.inputmethod.InputConnection;
73import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.ScrollBarDrawable;
75
Romain Guy1ef3fdb2011-09-09 15:30:30 -070076import static android.os.Build.VERSION_CODES.*;
Philip Milne6c8ea062012-04-03 17:38:43 -070077import static java.lang.Math.max;
Romain Guy1ef3fdb2011-09-09 15:30:30 -070078
Doug Feltcb3791202011-07-07 11:57:48 -070079import com.android.internal.R;
80import com.android.internal.util.Predicate;
81import com.android.internal.view.menu.MenuBuilder;
82
Christopher Tatea0374192010-10-05 13:06:41 -070083import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070084import java.lang.reflect.InvocationTargetException;
85import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.ArrayList;
87import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070088import java.util.Locale;
Adam Powell4afd62b2011-02-18 15:02:18 -080089import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * <p>
93 * This class represents the basic building block for user interface components. A View
94 * occupies a rectangular area on the screen and is responsible for drawing and
95 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070096 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
98 * are invisible containers that hold other Views (or other ViewGroups) and define
99 * their layout properties.
100 * </p>
101 *
Joe Fernandezb54e7a32011-10-03 15:09:50 -0700102 * <div class="special reference">
103 * <h3>Developer Guides</h3>
104 * <p>For information about using this class to develop your application's user interface,
105 * 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 -0800106 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700107 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * <a name="Using"></a>
109 * <h3>Using Views</h3>
110 * <p>
111 * All of the views in a window are arranged in a single tree. You can add views
112 * either from code or by specifying a tree of views in one or more XML layout
113 * files. There are many specialized subclasses of views that act as controls or
114 * are capable of displaying text, images, or other content.
115 * </p>
116 * <p>
117 * Once you have created a tree of views, there are typically a few types of
118 * common operations you may wish to perform:
119 * <ul>
120 * <li><strong>Set properties:</strong> for example setting the text of a
121 * {@link android.widget.TextView}. The available properties and the methods
122 * that set them will vary among the different subclasses of views. Note that
123 * properties that are known at build time can be set in the XML layout
124 * files.</li>
125 * <li><strong>Set focus:</strong> The framework will handled moving focus in
126 * response to user input. To force focus to a specific view, call
127 * {@link #requestFocus}.</li>
128 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
129 * that will be notified when something interesting happens to the view. For
130 * example, all views will let you set a listener to be notified when the view
131 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700132 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
Philip Milne6c8ea062012-04-03 17:38:43 -0700133 * Other view subclasses offer more specialized listeners. For example, a Button
Romain Guy5c22a8c2011-05-13 11:48:45 -0700134 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700136 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * </ul>
138 * </p>
139 * <p><em>
140 * Note: The Android framework is responsible for measuring, laying out and
141 * drawing views. You should not call methods that perform these actions on
142 * views yourself unless you are actually implementing a
143 * {@link android.view.ViewGroup}.
144 * </em></p>
145 *
146 * <a name="Lifecycle"></a>
147 * <h3>Implementing a Custom View</h3>
148 *
149 * <p>
150 * To implement a custom view, you will usually begin by providing overrides for
151 * some of the standard methods that the framework calls on all views. You do
152 * not need to override all of these methods. In fact, you can start by just
153 * overriding {@link #onDraw(android.graphics.Canvas)}.
154 * <table border="2" width="85%" align="center" cellpadding="5">
155 * <thead>
156 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
157 * </thead>
158 *
159 * <tbody>
160 * <tr>
161 * <td rowspan="2">Creation</td>
162 * <td>Constructors</td>
163 * <td>There is a form of the constructor that are called when the view
164 * is created from code and a form that is called when the view is
165 * inflated from a layout file. The second form should parse and apply
166 * any attributes defined in the layout file.
167 * </td>
168 * </tr>
169 * <tr>
170 * <td><code>{@link #onFinishInflate()}</code></td>
171 * <td>Called after a view and all of its children has been inflated
172 * from XML.</td>
173 * </tr>
174 *
175 * <tr>
176 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700177 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 * <td>Called to determine the size requirements for this view and all
179 * of its children.
180 * </td>
181 * </tr>
182 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700183 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <td>Called when this view should assign a size and position to all
185 * of its children.
186 * </td>
187 * </tr>
188 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700189 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * <td>Called when the size of this view has changed.
191 * </td>
192 * </tr>
193 *
194 * <tr>
195 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700196 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * <td>Called when the view should render its content.
198 * </td>
199 * </tr>
200 *
201 * <tr>
202 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700203 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * <td>Called when a new key event occurs.
205 * </td>
206 * </tr>
207 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700208 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 * <td>Called when a key up event occurs.
210 * </td>
211 * </tr>
212 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700213 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * <td>Called when a trackball motion event occurs.
215 * </td>
216 * </tr>
217 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700218 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 * <td>Called when a touch screen motion event occurs.
220 * </td>
221 * </tr>
222 *
223 * <tr>
224 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700225 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 * <td>Called when the view gains or loses focus.
227 * </td>
228 * </tr>
229 *
230 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700231 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * <td>Called when the window containing the view gains or loses focus.
233 * </td>
234 * </tr>
235 *
236 * <tr>
237 * <td rowspan="3">Attaching</td>
238 * <td><code>{@link #onAttachedToWindow()}</code></td>
239 * <td>Called when the view is attached to a window.
240 * </td>
241 * </tr>
242 *
243 * <tr>
244 * <td><code>{@link #onDetachedFromWindow}</code></td>
245 * <td>Called when the view is detached from its window.
246 * </td>
247 * </tr>
248 *
249 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700250 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 * <td>Called when the visibility of the window containing the view
252 * has changed.
253 * </td>
254 * </tr>
255 * </tbody>
256 *
257 * </table>
258 * </p>
259 *
260 * <a name="IDs"></a>
261 * <h3>IDs</h3>
262 * Views may have an integer id associated with them. These ids are typically
263 * assigned in the layout XML files, and are used to find specific views within
264 * the view tree. A common pattern is to:
265 * <ul>
266 * <li>Define a Button in the layout file and assign it a unique ID.
267 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700268 * &lt;Button
269 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 * android:layout_width="wrap_content"
271 * android:layout_height="wrap_content"
272 * android:text="@string/my_button_text"/&gt;
273 * </pre></li>
274 * <li>From the onCreate method of an Activity, find the Button
275 * <pre class="prettyprint">
276 * Button myButton = (Button) findViewById(R.id.my_button);
277 * </pre></li>
278 * </ul>
279 * <p>
280 * View IDs need not be unique throughout the tree, but it is good practice to
281 * ensure that they are at least unique within the part of the tree you are
282 * searching.
283 * </p>
284 *
285 * <a name="Position"></a>
286 * <h3>Position</h3>
287 * <p>
288 * The geometry of a view is that of a rectangle. A view has a location,
289 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
290 * two dimensions, expressed as a width and a height. The unit for location
291 * and dimensions is the pixel.
292 * </p>
293 *
294 * <p>
295 * It is possible to retrieve the location of a view by invoking the methods
296 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
297 * coordinate of the rectangle representing the view. The latter returns the
298 * top, or Y, coordinate of the rectangle representing the view. These methods
299 * both return the location of the view relative to its parent. For instance,
300 * when getLeft() returns 20, that means the view is located 20 pixels to the
301 * right of the left edge of its direct parent.
302 * </p>
303 *
304 * <p>
305 * In addition, several convenience methods are offered to avoid unnecessary
306 * computations, namely {@link #getRight()} and {@link #getBottom()}.
307 * These methods return the coordinates of the right and bottom edges of the
308 * rectangle representing the view. For instance, calling {@link #getRight()}
309 * is similar to the following computation: <code>getLeft() + getWidth()</code>
310 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
311 * </p>
312 *
313 * <a name="SizePaddingMargins"></a>
314 * <h3>Size, padding and margins</h3>
315 * <p>
316 * The size of a view is expressed with a width and a height. A view actually
317 * possess two pairs of width and height values.
318 * </p>
319 *
320 * <p>
321 * The first pair is known as <em>measured width</em> and
322 * <em>measured height</em>. These dimensions define how big a view wants to be
323 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
324 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
325 * and {@link #getMeasuredHeight()}.
326 * </p>
327 *
328 * <p>
329 * The second pair is simply known as <em>width</em> and <em>height</em>, or
330 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
331 * dimensions define the actual size of the view on screen, at drawing time and
332 * after layout. These values may, but do not have to, be different from the
333 * measured width and height. The width and height can be obtained by calling
334 * {@link #getWidth()} and {@link #getHeight()}.
335 * </p>
336 *
337 * <p>
338 * To measure its dimensions, a view takes into account its padding. The padding
339 * is expressed in pixels for the left, top, right and bottom parts of the view.
340 * Padding can be used to offset the content of the view by a specific amount of
341 * pixels. For instance, a left padding of 2 will push the view's content by
342 * 2 pixels to the right of the left edge. Padding can be set using the
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -0700343 * {@link #setPadding(int, int, int, int)} method and queried by calling
344 * {@link #getPaddingLeft()}, {@link #getPaddingTop()}, {@link #getPaddingRight()},
345 * {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * </p>
347 *
348 * <p>
349 * Even though a view can define a padding, it does not provide any support for
350 * margins. However, view groups provide such a support. Refer to
351 * {@link android.view.ViewGroup} and
352 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
353 * </p>
354 *
355 * <a name="Layout"></a>
356 * <h3>Layout</h3>
357 * <p>
358 * Layout is a two pass process: a measure pass and a layout pass. The measuring
359 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
360 * of the view tree. Each view pushes dimension specifications down the tree
361 * during the recursion. At the end of the measure pass, every view has stored
362 * its measurements. The second pass happens in
363 * {@link #layout(int,int,int,int)} and is also top-down. During
364 * this pass each parent is responsible for positioning all of its children
365 * using the sizes computed in the measure pass.
366 * </p>
367 *
368 * <p>
369 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
370 * {@link #getMeasuredHeight()} values must be set, along with those for all of
371 * that view's descendants. A view's measured width and measured height values
372 * must respect the constraints imposed by the view's parents. This guarantees
373 * that at the end of the measure pass, all parents accept all of their
374 * children's measurements. A parent view may call measure() more than once on
375 * its children. For example, the parent may measure each child once with
376 * unspecified dimensions to find out how big they want to be, then call
377 * measure() on them again with actual numbers if the sum of all the children's
378 * unconstrained sizes is too big or too small.
379 * </p>
380 *
381 * <p>
382 * The measure pass uses two classes to communicate dimensions. The
383 * {@link MeasureSpec} class is used by views to tell their parents how they
384 * want to be measured and positioned. The base LayoutParams class just
385 * describes how big the view wants to be for both width and height. For each
386 * dimension, it can specify one of:
387 * <ul>
388 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800389 * <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 -0800390 * (minus padding)
391 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
392 * enclose its content (plus padding).
393 * </ul>
394 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
395 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
396 * an X and Y value.
397 * </p>
398 *
399 * <p>
400 * MeasureSpecs are used to push requirements down the tree from parent to
401 * child. A MeasureSpec can be in one of three modes:
402 * <ul>
403 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
404 * of a child view. For example, a LinearLayout may call measure() on its child
405 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
406 * tall the child view wants to be given a width of 240 pixels.
407 * <li>EXACTLY: This is used by the parent to impose an exact size on the
408 * child. The child must use this size, and guarantee that all of its
409 * descendants will fit within this size.
410 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
411 * child. The child must gurantee that it and all of its descendants will fit
412 * within this size.
413 * </ul>
414 * </p>
415 *
416 * <p>
417 * To intiate a layout, call {@link #requestLayout}. This method is typically
418 * called by a view on itself when it believes that is can no longer fit within
419 * its current bounds.
420 * </p>
421 *
422 * <a name="Drawing"></a>
423 * <h3>Drawing</h3>
424 * <p>
425 * Drawing is handled by walking the tree and rendering each view that
Joe Fernandez558459f2011-10-13 16:47:36 -0700426 * intersects the invalid region. Because the tree is traversed in-order,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 * this means that parents will draw before (i.e., behind) their children, with
428 * siblings drawn in the order they appear in the tree.
429 * If you set a background drawable for a View, then the View will draw it for you
430 * before calling back to its <code>onDraw()</code> method.
431 * </p>
432 *
433 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700434 * 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 -0800435 * </p>
436 *
437 * <p>
438 * To force a view to draw, call {@link #invalidate()}.
439 * </p>
440 *
441 * <a name="EventHandlingThreading"></a>
442 * <h3>Event Handling and Threading</h3>
443 * <p>
444 * The basic cycle of a view is as follows:
445 * <ol>
446 * <li>An event comes in and is dispatched to the appropriate view. The view
447 * handles the event and notifies any listeners.</li>
448 * <li>If in the course of processing the event, the view's bounds may need
449 * to be changed, the view will call {@link #requestLayout()}.</li>
450 * <li>Similarly, if in the course of processing the event the view's appearance
451 * may need to be changed, the view will call {@link #invalidate()}.</li>
452 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
453 * the framework will take care of measuring, laying out, and drawing the tree
454 * as appropriate.</li>
455 * </ol>
456 * </p>
457 *
458 * <p><em>Note: The entire view tree is single threaded. You must always be on
459 * the UI thread when calling any method on any view.</em>
460 * If you are doing work on other threads and want to update the state of a view
461 * from that thread, you should use a {@link Handler}.
462 * </p>
463 *
464 * <a name="FocusHandling"></a>
465 * <h3>Focus Handling</h3>
466 * <p>
467 * The framework will handle routine focus movement in response to user input.
468 * This includes changing the focus as views are removed or hidden, or as new
469 * views become available. Views indicate their willingness to take focus
470 * through the {@link #isFocusable} method. To change whether a view can take
471 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
472 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
473 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
474 * </p>
475 * <p>
476 * Focus movement is based on an algorithm which finds the nearest neighbor in a
477 * given direction. In rare cases, the default algorithm may not match the
478 * intended behavior of the developer. In these situations, you can provide
479 * explicit overrides by using these XML attributes in the layout file:
480 * <pre>
481 * nextFocusDown
482 * nextFocusLeft
483 * nextFocusRight
484 * nextFocusUp
485 * </pre>
486 * </p>
487 *
488 *
489 * <p>
490 * To get a particular view to take focus, call {@link #requestFocus()}.
491 * </p>
492 *
493 * <a name="TouchMode"></a>
494 * <h3>Touch Mode</h3>
495 * <p>
496 * When a user is navigating a user interface via directional keys such as a D-pad, it is
497 * necessary to give focus to actionable items such as buttons so the user can see
498 * what will take input. If the device has touch capabilities, however, and the user
499 * begins interacting with the interface by touching it, it is no longer necessary to
500 * always highlight, or give focus to, a particular view. This motivates a mode
501 * for interaction named 'touch mode'.
502 * </p>
503 * <p>
504 * For a touch capable device, once the user touches the screen, the device
505 * will enter touch mode. From this point onward, only views for which
506 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
507 * Other views that are touchable, like buttons, will not take focus when touched; they will
508 * only fire the on click listeners.
509 * </p>
510 * <p>
511 * Any time a user hits a directional key, such as a D-pad direction, the view device will
512 * exit touch mode, and find a view to take focus, so that the user may resume interacting
513 * with the user interface without touching the screen again.
514 * </p>
515 * <p>
516 * The touch mode state is maintained across {@link android.app.Activity}s. Call
517 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
518 * </p>
519 *
520 * <a name="Scrolling"></a>
521 * <h3>Scrolling</h3>
522 * <p>
523 * The framework provides basic support for views that wish to internally
524 * scroll their content. This includes keeping track of the X and Y scroll
525 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800526 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700527 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 * </p>
529 *
530 * <a name="Tags"></a>
531 * <h3>Tags</h3>
532 * <p>
533 * Unlike IDs, tags are not used to identify views. Tags are essentially an
534 * extra piece of information that can be associated with a view. They are most
535 * often used as a convenience to store data related to views in the views
536 * themselves rather than by putting them in a separate structure.
537 * </p>
538 *
Chet Haasecb150fe2012-05-03 15:15:05 -0700539 * <a name="Properties"></a>
540 * <h3>Properties</h3>
541 * <p>
542 * The View class exposes an {@link #ALPHA} property, as well as several transform-related
543 * properties, such as {@link #TRANSLATION_X} and {@link #TRANSLATION_Y}. These properties are
544 * available both in the {@link Property} form as well as in similarly-named setter/getter
545 * methods (such as {@link #setAlpha(float)} for {@link #ALPHA}). These properties can
546 * be used to set persistent state associated with these rendering-related properties on the view.
547 * The properties and methods can also be used in conjunction with
548 * {@link android.animation.Animator Animator}-based animations, described more in the
549 * <a href="#Animation">Animation</a> section.
550 * </p>
551 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 * <a name="Animation"></a>
553 * <h3>Animation</h3>
554 * <p>
Chet Haasecb150fe2012-05-03 15:15:05 -0700555 * Starting with Android 3.0, the preferred way of animating views is to use the
556 * {@link android.animation} package APIs. These {@link android.animation.Animator Animator}-based
557 * classes change actual properties of the View object, such as {@link #setAlpha(float) alpha} and
558 * {@link #setTranslationX(float) translationX}. This behavior is contrasted to that of the pre-3.0
559 * {@link android.view.animation.Animation Animation}-based classes, which instead animate only
560 * how the view is drawn on the display. In particular, the {@link ViewPropertyAnimator} class
561 * makes animating these View properties particularly easy and efficient.
562 * </p>
563 * <p>
564 * 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 -0800565 * You can attach an {@link Animation} object to a view using
566 * {@link #setAnimation(Animation)} or
567 * {@link #startAnimation(Animation)}. The animation can alter the scale,
568 * rotation, translation and alpha of a view over time. If the animation is
569 * attached to a view that has children, the animation will affect the entire
570 * subtree rooted by that node. When an animation is started, the framework will
571 * take care of redrawing the appropriate views until the animation completes.
572 * </p>
573 *
Jeff Brown85a31762010-09-01 17:01:00 -0700574 * <a name="Security"></a>
575 * <h3>Security</h3>
576 * <p>
577 * Sometimes it is essential that an application be able to verify that an action
578 * is being performed with the full knowledge and consent of the user, such as
579 * granting a permission request, making a purchase or clicking on an advertisement.
580 * Unfortunately, a malicious application could try to spoof the user into
581 * performing these actions, unaware, by concealing the intended purpose of the view.
582 * As a remedy, the framework offers a touch filtering mechanism that can be used to
583 * improve the security of views that provide access to sensitive functionality.
584 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700585 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800586 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700587 * will discard touches that are received whenever the view's window is obscured by
588 * another visible window. As a result, the view will not receive touches whenever a
589 * toast, dialog or other window appears above the view's window.
590 * </p><p>
591 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700592 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
593 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700594 * </p>
595 *
Romain Guy171c5922011-01-06 10:04:23 -0800596 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_background
598 * @attr ref android.R.styleable#View_clickable
599 * @attr ref android.R.styleable#View_contentDescription
600 * @attr ref android.R.styleable#View_drawingCacheQuality
601 * @attr ref android.R.styleable#View_duplicateParentState
602 * @attr ref android.R.styleable#View_id
Romain Guy1ef3fdb2011-09-09 15:30:30 -0700603 * @attr ref android.R.styleable#View_requiresFadingEdge
Philip Milne6c8ea062012-04-03 17:38:43 -0700604 * @attr ref android.R.styleable#View_fadeScrollbars
Romain Guyd6a463a2009-05-21 23:10:10 -0700605 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700606 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700608 * @attr ref android.R.styleable#View_isScrollContainer
609 * @attr ref android.R.styleable#View_focusable
610 * @attr ref android.R.styleable#View_focusableInTouchMode
611 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
612 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800613 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_longClickable
615 * @attr ref android.R.styleable#View_minHeight
616 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_nextFocusDown
618 * @attr ref android.R.styleable#View_nextFocusLeft
619 * @attr ref android.R.styleable#View_nextFocusRight
620 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700621 * @attr ref android.R.styleable#View_onClick
622 * @attr ref android.R.styleable#View_padding
623 * @attr ref android.R.styleable#View_paddingBottom
624 * @attr ref android.R.styleable#View_paddingLeft
625 * @attr ref android.R.styleable#View_paddingRight
626 * @attr ref android.R.styleable#View_paddingTop
627 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800628 * @attr ref android.R.styleable#View_rotation
629 * @attr ref android.R.styleable#View_rotationX
630 * @attr ref android.R.styleable#View_rotationY
631 * @attr ref android.R.styleable#View_scaleX
632 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 * @attr ref android.R.styleable#View_scrollX
634 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_scrollbarSize
636 * @attr ref android.R.styleable#View_scrollbarStyle
637 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700638 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
639 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
641 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 * @attr ref android.R.styleable#View_scrollbarThumbVertical
643 * @attr ref android.R.styleable#View_scrollbarTrackVertical
644 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
645 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700646 * @attr ref android.R.styleable#View_soundEffectsEnabled
647 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800648 * @attr ref android.R.styleable#View_transformPivotX
649 * @attr ref android.R.styleable#View_transformPivotY
650 * @attr ref android.R.styleable#View_translationX
651 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700652 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 *
654 * @see android.view.ViewGroup
655 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700656public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
657 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 private static final boolean DBG = false;
659
660 /**
661 * The logging tag used by this class with android.util.Log.
662 */
663 protected static final String VIEW_LOG_TAG = "View";
664
665 /**
Romain Guy4b8c4f82012-04-27 15:48:35 -0700666 * When set to true, apps will draw debugging information about their layouts.
667 *
668 * @hide
669 */
670 public static final String DEBUG_LAYOUT_PROPERTY = "debug.layout";
671
672 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 * Used to mark a View that has no ID.
674 */
675 public static final int NO_ID = -1;
676
677 /**
678 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
679 * calling setFlags.
680 */
681 private static final int NOT_FOCUSABLE = 0x00000000;
682
683 /**
684 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
685 * setFlags.
686 */
687 private static final int FOCUSABLE = 0x00000001;
688
689 /**
690 * Mask for use with setFlags indicating bits used for focus.
691 */
692 private static final int FOCUSABLE_MASK = 0x00000001;
693
694 /**
695 * This view will adjust its padding to fit sytem windows (e.g. status bar)
696 */
697 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
698
699 /**
Scott Main812634c22011-07-27 13:22:35 -0700700 * This view is visible.
701 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
702 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 */
704 public static final int VISIBLE = 0x00000000;
705
706 /**
707 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700708 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
709 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 */
711 public static final int INVISIBLE = 0x00000004;
712
713 /**
714 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700715 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
716 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 */
718 public static final int GONE = 0x00000008;
719
720 /**
721 * Mask for use with setFlags indicating bits used for visibility.
722 * {@hide}
723 */
724 static final int VISIBILITY_MASK = 0x0000000C;
725
726 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
727
728 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700729 * This view is enabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 * Use with ENABLED_MASK when calling setFlags.
731 * {@hide}
732 */
733 static final int ENABLED = 0x00000000;
734
735 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -0700736 * This view is disabled. Interpretation varies by subclass.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 * Use with ENABLED_MASK when calling setFlags.
738 * {@hide}
739 */
740 static final int DISABLED = 0x00000020;
741
742 /**
743 * Mask for use with setFlags indicating bits used for indicating whether
744 * this view is enabled
745 * {@hide}
746 */
747 static final int ENABLED_MASK = 0x00000020;
748
749 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700750 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
751 * called and further optimizations will be performed. It is okay to have
752 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 * {@hide}
754 */
755 static final int WILL_NOT_DRAW = 0x00000080;
756
757 /**
758 * Mask for use with setFlags indicating bits used for indicating whether
759 * this view is will draw
760 * {@hide}
761 */
762 static final int DRAW_MASK = 0x00000080;
763
764 /**
765 * <p>This view doesn't show scrollbars.</p>
766 * {@hide}
767 */
768 static final int SCROLLBARS_NONE = 0x00000000;
769
770 /**
771 * <p>This view shows horizontal scrollbars.</p>
772 * {@hide}
773 */
774 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
775
776 /**
777 * <p>This view shows vertical scrollbars.</p>
778 * {@hide}
779 */
780 static final int SCROLLBARS_VERTICAL = 0x00000200;
781
782 /**
783 * <p>Mask for use with setFlags indicating bits used for indicating which
784 * scrollbars are enabled.</p>
785 * {@hide}
786 */
787 static final int SCROLLBARS_MASK = 0x00000300;
788
Jeff Brown85a31762010-09-01 17:01:00 -0700789 /**
790 * Indicates that the view should filter touches when its window is obscured.
791 * Refer to the class comments for more information about this security feature.
792 * {@hide}
793 */
794 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
795
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -0700796 /**
797 * Set for framework elements that use FITS_SYSTEM_WINDOWS, to indicate
798 * that they are optional and should be skipped if the window has
799 * requested system UI flags that ignore those insets for layout.
800 */
801 static final int OPTIONAL_FITS_SYSTEM_WINDOWS = 0x00000800;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802
803 /**
804 * <p>This view doesn't show fading edges.</p>
805 * {@hide}
806 */
807 static final int FADING_EDGE_NONE = 0x00000000;
808
809 /**
810 * <p>This view shows horizontal fading edges.</p>
811 * {@hide}
812 */
813 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
814
815 /**
816 * <p>This view shows vertical fading edges.</p>
817 * {@hide}
818 */
819 static final int FADING_EDGE_VERTICAL = 0x00002000;
820
821 /**
822 * <p>Mask for use with setFlags indicating bits used for indicating which
823 * fading edges are enabled.</p>
824 * {@hide}
825 */
826 static final int FADING_EDGE_MASK = 0x00003000;
827
828 /**
829 * <p>Indicates this view can be clicked. When clickable, a View reacts
830 * to clicks by notifying the OnClickListener.<p>
831 * {@hide}
832 */
833 static final int CLICKABLE = 0x00004000;
834
835 /**
836 * <p>Indicates this view is caching its drawing into a bitmap.</p>
837 * {@hide}
838 */
839 static final int DRAWING_CACHE_ENABLED = 0x00008000;
840
841 /**
842 * <p>Indicates that no icicle should be saved for this view.<p>
843 * {@hide}
844 */
845 static final int SAVE_DISABLED = 0x000010000;
846
847 /**
848 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
849 * property.</p>
850 * {@hide}
851 */
852 static final int SAVE_DISABLED_MASK = 0x000010000;
853
854 /**
855 * <p>Indicates that no drawing cache should ever be created for this view.<p>
856 * {@hide}
857 */
858 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
859
860 /**
861 * <p>Indicates this view can take / keep focus when int touch mode.</p>
862 * {@hide}
863 */
864 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
865
866 /**
867 * <p>Enables low quality mode for the drawing cache.</p>
868 */
869 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
870
871 /**
872 * <p>Enables high quality mode for the drawing cache.</p>
873 */
874 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
875
876 /**
877 * <p>Enables automatic quality mode for the drawing cache.</p>
878 */
879 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
880
881 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
882 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
883 };
884
885 /**
886 * <p>Mask for use with setFlags indicating bits used for the cache
887 * quality property.</p>
888 * {@hide}
889 */
890 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
891
892 /**
893 * <p>
894 * Indicates this view can be long clicked. When long clickable, a View
895 * reacts to long clicks by notifying the OnLongClickListener or showing a
896 * context menu.
897 * </p>
898 * {@hide}
899 */
900 static final int LONG_CLICKABLE = 0x00200000;
901
902 /**
903 * <p>Indicates that this view gets its drawable states from its direct parent
904 * and ignores its original internal states.</p>
905 *
906 * @hide
907 */
908 static final int DUPLICATE_PARENT_STATE = 0x00400000;
909
910 /**
911 * The scrollbar style to display the scrollbars inside the content area,
912 * without increasing the padding. The scrollbars will be overlaid with
913 * translucency on the view's content.
914 */
915 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
916
917 /**
918 * The scrollbar style to display the scrollbars inside the padded area,
919 * increasing the padding of the view. The scrollbars will not overlap the
920 * content area of the view.
921 */
922 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
923
924 /**
925 * The scrollbar style to display the scrollbars at the edge of the view,
926 * without increasing the padding. The scrollbars will be overlaid with
927 * translucency.
928 */
929 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
930
931 /**
932 * The scrollbar style to display the scrollbars at the edge of the view,
933 * increasing the padding of the view. The scrollbars will only overlap the
934 * background, if any.
935 */
936 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
937
938 /**
939 * Mask to check if the scrollbar style is overlay or inset.
940 * {@hide}
941 */
942 static final int SCROLLBARS_INSET_MASK = 0x01000000;
943
944 /**
945 * Mask to check if the scrollbar style is inside or outside.
946 * {@hide}
947 */
948 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
949
950 /**
951 * Mask for scrollbar style.
952 * {@hide}
953 */
954 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
955
956 /**
957 * View flag indicating that the screen should remain on while the
958 * window containing this view is visible to the user. This effectively
959 * takes care of automatically setting the WindowManager's
960 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
961 */
962 public static final int KEEP_SCREEN_ON = 0x04000000;
963
964 /**
965 * View flag indicating whether this view should have sound effects enabled
966 * for events such as clicking and touching.
967 */
968 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
969
970 /**
971 * View flag indicating whether this view should have haptic feedback
972 * enabled for events such as long presses.
973 */
974 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
975
976 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700977 * <p>Indicates that the view hierarchy should stop saving state when
978 * it reaches this view. If state saving is initiated immediately at
979 * the view, it will be allowed.
980 * {@hide}
981 */
982 static final int PARENT_SAVE_DISABLED = 0x20000000;
983
984 /**
985 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
986 * {@hide}
987 */
988 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
989
990 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700991 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
992 * should add all focusable Views regardless if they are focusable in touch mode.
993 */
994 public static final int FOCUSABLES_ALL = 0x00000000;
995
996 /**
997 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
998 * should add only Views focusable in touch mode.
999 */
1000 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1001
1002 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07001003 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1004 * should add only accessibility focusable Views.
1005 *
1006 * @hide
1007 */
1008 public static final int FOCUSABLES_ACCESSIBILITY = 0x00000002;
1009
1010 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001011 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * item.
1013 */
1014 public static final int FOCUS_BACKWARD = 0x00000001;
1015
1016 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001017 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * item.
1019 */
1020 public static final int FOCUS_FORWARD = 0x00000002;
1021
1022 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001023 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 */
1025 public static final int FOCUS_LEFT = 0x00000011;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 */
1030 public static final int FOCUS_UP = 0x00000021;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_RIGHT = 0x00000042;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_DOWN = 0x00000082;
1041
Svetoslav Ganov42138042012-03-20 11:51:39 -07001042 // Accessibility focus directions.
1043
1044 /**
1045 * The accessibility focus which is the current user position when
1046 * interacting with the accessibility framework.
1047 */
1048 public static final int FOCUS_ACCESSIBILITY = 0x00001000;
1049
1050 /**
1051 * Use with {@link #focusSearch(int)}. Move acessibility focus left.
1052 */
1053 public static final int ACCESSIBILITY_FOCUS_LEFT = FOCUS_LEFT | FOCUS_ACCESSIBILITY;
1054
1055 /**
1056 * Use with {@link #focusSearch(int)}. Move acessibility focus up.
1057 */
1058 public static final int ACCESSIBILITY_FOCUS_UP = FOCUS_UP | FOCUS_ACCESSIBILITY;
1059
1060 /**
1061 * Use with {@link #focusSearch(int)}. Move acessibility focus right.
1062 */
1063 public static final int ACCESSIBILITY_FOCUS_RIGHT = FOCUS_RIGHT | FOCUS_ACCESSIBILITY;
1064
1065 /**
1066 * Use with {@link #focusSearch(int)}. Move acessibility focus down.
1067 */
1068 public static final int ACCESSIBILITY_FOCUS_DOWN = FOCUS_DOWN | FOCUS_ACCESSIBILITY;
1069
1070 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001071 * Use with {@link #focusSearch(int)}. Move acessibility focus forward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001072 */
1073 public static final int ACCESSIBILITY_FOCUS_FORWARD = FOCUS_FORWARD | FOCUS_ACCESSIBILITY;
1074
1075 /**
Svetoslav Ganovd6e716d2012-04-20 18:36:11 -07001076 * Use with {@link #focusSearch(int)}. Move acessibility focus backward.
Svetoslav Ganov42138042012-03-20 11:51:39 -07001077 */
1078 public static final int ACCESSIBILITY_FOCUS_BACKWARD = FOCUS_BACKWARD | FOCUS_ACCESSIBILITY;
1079
1080 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001081 * Bits of {@link #getMeasuredWidthAndState()} and
1082 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1083 */
1084 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1085
1086 /**
1087 * Bits of {@link #getMeasuredWidthAndState()} and
1088 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1089 */
1090 public static final int MEASURED_STATE_MASK = 0xff000000;
1091
1092 /**
1093 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1094 * for functions that combine both width and height into a single int,
1095 * such as {@link #getMeasuredState()} and the childState argument of
1096 * {@link #resolveSizeAndState(int, int, int)}.
1097 */
1098 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1099
1100 /**
1101 * Bit of {@link #getMeasuredWidthAndState()} and
1102 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1103 * is smaller that the space the view would like to have.
1104 */
1105 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1106
1107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 * Base View state sets
1109 */
1110 // Singles
1111 /**
1112 * Indicates the view has no states set. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 /**
1121 * Indicates the view is enabled. States are used with
1122 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1123 * view depending on its state.
1124 *
1125 * @see android.graphics.drawable.Drawable
1126 * @see #getDrawableState()
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 /**
1130 * Indicates the view is focused. States are used with
1131 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1132 * view depending on its state.
1133 *
1134 * @see android.graphics.drawable.Drawable
1135 * @see #getDrawableState()
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 /**
1139 * Indicates the view is selected. States are used with
1140 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1141 * view depending on its state.
1142 *
1143 * @see android.graphics.drawable.Drawable
1144 * @see #getDrawableState()
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is pressed. States are used with
1149 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1150 * view depending on its state.
1151 *
1152 * @see android.graphics.drawable.Drawable
1153 * @see #getDrawableState()
1154 * @hide
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view's window has focus. States are used with
1159 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1160 * view depending on its state.
1161 *
1162 * @see android.graphics.drawable.Drawable
1163 * @see #getDrawableState()
1164 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001165 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 // Doubles
1167 /**
1168 * Indicates the view is enabled and has the focus.
1169 *
1170 * @see #ENABLED_STATE_SET
1171 * @see #FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is enabled and selected.
1176 *
1177 * @see #ENABLED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is enabled and that its window has focus.
1183 *
1184 * @see #ENABLED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is focused and selected.
1190 *
1191 * @see #FOCUSED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001194 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
1196 * Indicates the view has the focus and that its window has the focus.
1197 *
1198 * @see #FOCUSED_STATE_SET
1199 * @see #WINDOW_FOCUSED_STATE_SET
1200 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001201 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 /**
1203 * Indicates the view is selected and that its window has the focus.
1204 *
1205 * @see #SELECTED_STATE_SET
1206 * @see #WINDOW_FOCUSED_STATE_SET
1207 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001208 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 // Triples
1210 /**
1211 * Indicates the view is enabled, focused and selected.
1212 *
1213 * @see #ENABLED_STATE_SET
1214 * @see #FOCUSED_STATE_SET
1215 * @see #SELECTED_STATE_SET
1216 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001217 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
1219 * Indicates the view is enabled, focused and its window has the focus.
1220 *
1221 * @see #ENABLED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 * @see #WINDOW_FOCUSED_STATE_SET
1224 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001225 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 /**
1227 * Indicates the view is enabled, selected and its window has the focus.
1228 *
1229 * @see #ENABLED_STATE_SET
1230 * @see #SELECTED_STATE_SET
1231 * @see #WINDOW_FOCUSED_STATE_SET
1232 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001233 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 /**
1235 * Indicates the view is focused, selected and its window has the focus.
1236 *
1237 * @see #FOCUSED_STATE_SET
1238 * @see #SELECTED_STATE_SET
1239 * @see #WINDOW_FOCUSED_STATE_SET
1240 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001241 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 /**
1243 * Indicates the view is enabled, focused, selected and its window
1244 * has the focus.
1245 *
1246 * @see #ENABLED_STATE_SET
1247 * @see #FOCUSED_STATE_SET
1248 * @see #SELECTED_STATE_SET
1249 * @see #WINDOW_FOCUSED_STATE_SET
1250 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001251 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 /**
1253 * Indicates the view is pressed and its window has the focus.
1254 *
1255 * @see #PRESSED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed and selected.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001265 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 /**
1267 * Indicates the view is pressed, selected and its window has the focus.
1268 *
1269 * @see #PRESSED_STATE_SET
1270 * @see #SELECTED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed and focused.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #FOCUSED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, focused and its window has the focus.
1283 *
1284 * @see #PRESSED_STATE_SET
1285 * @see #FOCUSED_STATE_SET
1286 * @see #WINDOW_FOCUSED_STATE_SET
1287 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001288 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 /**
1290 * Indicates the view is pressed, focused and selected.
1291 *
1292 * @see #PRESSED_STATE_SET
1293 * @see #SELECTED_STATE_SET
1294 * @see #FOCUSED_STATE_SET
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 /**
1298 * Indicates the view is pressed, focused, selected and its window has the focus.
1299 *
1300 * @see #PRESSED_STATE_SET
1301 * @see #FOCUSED_STATE_SET
1302 * @see #SELECTED_STATE_SET
1303 * @see #WINDOW_FOCUSED_STATE_SET
1304 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 /**
1307 * Indicates the view is pressed and enabled.
1308 *
1309 * @see #PRESSED_STATE_SET
1310 * @see #ENABLED_STATE_SET
1311 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001312 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 /**
1314 * Indicates the view is pressed, enabled and its window has the focus.
1315 *
1316 * @see #PRESSED_STATE_SET
1317 * @see #ENABLED_STATE_SET
1318 * @see #WINDOW_FOCUSED_STATE_SET
1319 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001320 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 /**
1322 * Indicates the view is pressed, enabled and selected.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, selected and its window has the
1331 * focus.
1332 *
1333 * @see #PRESSED_STATE_SET
1334 * @see #ENABLED_STATE_SET
1335 * @see #SELECTED_STATE_SET
1336 * @see #WINDOW_FOCUSED_STATE_SET
1337 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 /**
1340 * Indicates the view is pressed, enabled and focused.
1341 *
1342 * @see #PRESSED_STATE_SET
1343 * @see #ENABLED_STATE_SET
1344 * @see #FOCUSED_STATE_SET
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 /**
1348 * Indicates the view is pressed, enabled, focused and its window has the
1349 * focus.
1350 *
1351 * @see #PRESSED_STATE_SET
1352 * @see #ENABLED_STATE_SET
1353 * @see #FOCUSED_STATE_SET
1354 * @see #WINDOW_FOCUSED_STATE_SET
1355 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001356 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 /**
1358 * Indicates the view is pressed, enabled, focused and selected.
1359 *
1360 * @see #PRESSED_STATE_SET
1361 * @see #ENABLED_STATE_SET
1362 * @see #SELECTED_STATE_SET
1363 * @see #FOCUSED_STATE_SET
1364 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001365 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 /**
1367 * Indicates the view is pressed, enabled, focused, selected and its window
1368 * has the focus.
1369 *
1370 * @see #PRESSED_STATE_SET
1371 * @see #ENABLED_STATE_SET
1372 * @see #SELECTED_STATE_SET
1373 * @see #FOCUSED_STATE_SET
1374 * @see #WINDOW_FOCUSED_STATE_SET
1375 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001376 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377
1378 /**
1379 * The order here is very important to {@link #getDrawableState()}
1380 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001381 private static final int[][] VIEW_STATE_SETS;
1382
Romain Guyb051e892010-09-28 19:09:36 -07001383 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1384 static final int VIEW_STATE_SELECTED = 1 << 1;
1385 static final int VIEW_STATE_FOCUSED = 1 << 2;
1386 static final int VIEW_STATE_ENABLED = 1 << 3;
1387 static final int VIEW_STATE_PRESSED = 1 << 4;
1388 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001389 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001390 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001391 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1392 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001393
1394 static final int[] VIEW_STATE_IDS = new int[] {
1395 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1396 R.attr.state_selected, VIEW_STATE_SELECTED,
1397 R.attr.state_focused, VIEW_STATE_FOCUSED,
1398 R.attr.state_enabled, VIEW_STATE_ENABLED,
1399 R.attr.state_pressed, VIEW_STATE_PRESSED,
1400 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001401 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001402 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001403 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
Svetoslav Ganov42138042012-03-20 11:51:39 -07001404 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 };
1406
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001407 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001408 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1409 throw new IllegalStateException(
1410 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1411 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001412 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001413 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001414 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001415 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001416 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001417 orderedIds[i * 2] = viewState;
1418 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001419 }
1420 }
1421 }
Romain Guyb051e892010-09-28 19:09:36 -07001422 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1423 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1424 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001425 int numBits = Integer.bitCount(i);
1426 int[] set = new int[numBits];
1427 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001428 for (int j = 0; j < orderedIds.length; j += 2) {
1429 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001430 set[pos++] = orderedIds[j];
1431 }
1432 }
1433 VIEW_STATE_SETS[i] = set;
1434 }
1435
1436 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1437 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1438 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1439 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1441 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1442 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1444 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1446 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_FOCUSED];
1449 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1450 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1452 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1454 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1456 | VIEW_STATE_ENABLED];
1457 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1459 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1461 | VIEW_STATE_ENABLED];
1462 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1464 | VIEW_STATE_ENABLED];
1465 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1467 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1468
1469 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1470 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1471 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1472 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1474 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1476 | VIEW_STATE_PRESSED];
1477 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1479 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1480 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1481 | VIEW_STATE_PRESSED];
1482 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1483 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1484 | VIEW_STATE_PRESSED];
1485 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1486 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1487 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1488 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1489 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1490 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1491 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1492 | VIEW_STATE_PRESSED];
1493 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1494 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1495 | VIEW_STATE_PRESSED];
1496 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1497 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1498 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1499 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1500 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1501 | VIEW_STATE_PRESSED];
1502 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1503 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1504 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1505 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1506 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1507 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1508 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1509 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1510 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1511 | VIEW_STATE_PRESSED];
1512 }
1513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 /**
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001515 * Accessibility event types that are dispatched for text population.
1516 */
1517 private static final int POPULATING_ACCESSIBILITY_EVENT_TYPES =
1518 AccessibilityEvent.TYPE_VIEW_CLICKED
1519 | AccessibilityEvent.TYPE_VIEW_LONG_CLICKED
1520 | AccessibilityEvent.TYPE_VIEW_SELECTED
1521 | AccessibilityEvent.TYPE_VIEW_FOCUSED
1522 | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
1523 | AccessibilityEvent.TYPE_VIEW_HOVER_ENTER
Svetoslav Ganov9920f4f2011-10-07 18:39:11 -07001524 | AccessibilityEvent.TYPE_VIEW_HOVER_EXIT
Svetoslav Ganov84dd52e2011-11-18 10:24:00 -08001525 | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED
Svetoslav Ganov42138042012-03-20 11:51:39 -07001526 | AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED
1527 | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED;
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07001528
1529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 * Temporary Rect currently for use in setBackground(). This will probably
1531 * be extended in the future to hold our own class with more than just
1532 * a Rect. :)
1533 */
1534 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001535
1536 /**
1537 * Map used to store views' tags.
1538 */
Adam Powell7db82ac2011-09-22 19:44:04 -07001539 private SparseArray<Object> mKeyedTags;
Romain Guyd90a3312009-05-06 14:54:28 -07001540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001542 * The next available accessiiblity id.
1543 */
1544 private static int sNextAccessibilityViewId;
1545
1546 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 * The animation currently associated with this view.
1548 * @hide
1549 */
1550 protected Animation mCurrentAnimation = null;
1551
1552 /**
1553 * Width as measured during measure pass.
1554 * {@hide}
1555 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001556 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001557 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558
1559 /**
1560 * Height as measured during measure pass.
1561 * {@hide}
1562 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001563 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001564 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565
1566 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001567 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1568 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1569 * its display list. This flag, used only when hw accelerated, allows us to clear the
1570 * flag while retaining this information until it's needed (at getDisplayList() time and
1571 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1572 *
1573 * {@hide}
1574 */
1575 boolean mRecreateDisplayList = false;
1576
1577 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 * The view's identifier.
1579 * {@hide}
1580 *
1581 * @see #setId(int)
1582 * @see #getId()
1583 */
1584 @ViewDebug.ExportedProperty(resolveId = true)
1585 int mID = NO_ID;
1586
1587 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07001588 * The stable ID of this view for accessibility purposes.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001589 */
1590 int mAccessibilityViewId = NO_ID;
1591
1592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 * The view's tag.
1594 * {@hide}
1595 *
1596 * @see #setTag(Object)
1597 * @see #getTag()
1598 */
1599 protected Object mTag;
1600
1601 // for mPrivateFlags:
1602 /** {@hide} */
1603 static final int WANTS_FOCUS = 0x00000001;
1604 /** {@hide} */
1605 static final int FOCUSED = 0x00000002;
1606 /** {@hide} */
1607 static final int SELECTED = 0x00000004;
1608 /** {@hide} */
1609 static final int IS_ROOT_NAMESPACE = 0x00000008;
1610 /** {@hide} */
1611 static final int HAS_BOUNDS = 0x00000010;
1612 /** {@hide} */
1613 static final int DRAWN = 0x00000020;
1614 /**
1615 * When this flag is set, this view is running an animation on behalf of its
1616 * children and should therefore not cancel invalidate requests, even if they
1617 * lie outside of this view's bounds.
1618 *
1619 * {@hide}
1620 */
1621 static final int DRAW_ANIMATION = 0x00000040;
1622 /** {@hide} */
1623 static final int SKIP_DRAW = 0x00000080;
1624 /** {@hide} */
1625 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1626 /** {@hide} */
1627 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1628 /** {@hide} */
1629 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1630 /** {@hide} */
1631 static final int MEASURED_DIMENSION_SET = 0x00000800;
1632 /** {@hide} */
1633 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001634 /** {@hide} */
1635 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 private static final int PRESSED = 0x00004000;
1638
1639 /** {@hide} */
1640 static final int DRAWING_CACHE_VALID = 0x00008000;
1641 /**
1642 * Flag used to indicate that this view should be drawn once more (and only once
1643 * more) after its animation has completed.
1644 * {@hide}
1645 */
1646 static final int ANIMATION_STARTED = 0x00010000;
1647
1648 private static final int SAVE_STATE_CALLED = 0x00020000;
1649
1650 /**
1651 * Indicates that the View returned true when onSetAlpha() was called and that
1652 * the alpha must be restored.
1653 * {@hide}
1654 */
1655 static final int ALPHA_SET = 0x00040000;
1656
1657 /**
1658 * Set by {@link #setScrollContainer(boolean)}.
1659 */
1660 static final int SCROLL_CONTAINER = 0x00080000;
1661
1662 /**
1663 * Set by {@link #setScrollContainer(boolean)}.
1664 */
1665 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1666
1667 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001668 * View flag indicating whether this view was invalidated (fully or partially.)
1669 *
1670 * @hide
1671 */
1672 static final int DIRTY = 0x00200000;
1673
1674 /**
1675 * View flag indicating whether this view was invalidated by an opaque
1676 * invalidate request.
1677 *
1678 * @hide
1679 */
1680 static final int DIRTY_OPAQUE = 0x00400000;
1681
1682 /**
1683 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1684 *
1685 * @hide
1686 */
1687 static final int DIRTY_MASK = 0x00600000;
1688
1689 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001690 * Indicates whether the background is opaque.
1691 *
1692 * @hide
1693 */
1694 static final int OPAQUE_BACKGROUND = 0x00800000;
1695
1696 /**
1697 * Indicates whether the scrollbars are opaque.
1698 *
1699 * @hide
1700 */
1701 static final int OPAQUE_SCROLLBARS = 0x01000000;
1702
1703 /**
1704 * Indicates whether the view is opaque.
1705 *
1706 * @hide
1707 */
1708 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001709
Adam Powelle14579b2009-12-16 18:39:52 -08001710 /**
1711 * Indicates a prepressed state;
1712 * the short time between ACTION_DOWN and recognizing
1713 * a 'real' press. Prepressed is used to recognize quick taps
1714 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001715 *
Adam Powelle14579b2009-12-16 18:39:52 -08001716 * @hide
1717 */
1718 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001719
Adam Powellc9fbaab2010-02-16 17:16:19 -08001720 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001721 * Indicates whether the view is temporarily detached.
1722 *
1723 * @hide
1724 */
1725 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001726
Adam Powell8568c3a2010-04-19 14:26:11 -07001727 /**
1728 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001729 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001730 * @hide
1731 */
1732 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001733
1734 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001735 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1736 * @hide
1737 */
1738 private static final int HOVERED = 0x10000000;
1739
1740 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001741 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1742 * for transform operations
1743 *
1744 * @hide
1745 */
Adam Powellf37df072010-09-17 16:22:49 -07001746 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001747
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001748 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001749 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001750
Chet Haasefd2b0022010-08-06 13:08:56 -07001751 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001752 * Indicates that this view was specifically invalidated, not just dirtied because some
1753 * child view was invalidated. The flag is used to determine when we need to recreate
1754 * a view's display list (as opposed to just returning a reference to its existing
1755 * display list).
1756 *
1757 * @hide
1758 */
1759 static final int INVALIDATED = 0x80000000;
1760
Christopher Tate3d4bf172011-03-28 16:16:46 -07001761 /* Masks for mPrivateFlags2 */
1762
1763 /**
1764 * Indicates that this view has reported that it can accept the current drag's content.
1765 * Cleared when the drag operation concludes.
1766 * @hide
1767 */
1768 static final int DRAG_CAN_ACCEPT = 0x00000001;
1769
1770 /**
1771 * Indicates that this view is currently directly under the drag location in a
1772 * drag-and-drop operation involving content that it can accept. Cleared when
1773 * the drag exits the view, or when the drag operation concludes.
1774 * @hide
1775 */
1776 static final int DRAG_HOVERED = 0x00000002;
1777
Cibu Johny86666632010-02-22 13:01:02 -08001778 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001779 * Horizontal layout direction of this view is from Left to Right.
1780 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001781 * @hide
Cibu Johny86666632010-02-22 13:01:02 -08001782 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001783 public static final int LAYOUT_DIRECTION_LTR = 0;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001784
1785 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001786 * Horizontal layout direction of this view is from Right to Left.
1787 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001788 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001789 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001790 public static final int LAYOUT_DIRECTION_RTL = 1;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001791
1792 /**
1793 * Horizontal layout direction of this view is inherited from its parent.
1794 * Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001795 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001796 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001797 public static final int LAYOUT_DIRECTION_INHERIT = 2;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001798
1799 /**
1800 * Horizontal layout direction of this view is from deduced from the default language
1801 * script for the locale. Use with {@link #setLayoutDirection}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001802 * @hide
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001803 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001804 public static final int LAYOUT_DIRECTION_LOCALE = 3;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001805
1806 /**
1807 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001808 * @hide
1809 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001810 static final int LAYOUT_DIRECTION_MASK_SHIFT = 2;
1811
1812 /**
1813 * Mask for use with private flags indicating bits used for horizontal layout direction.
1814 * @hide
1815 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001816 static final int LAYOUT_DIRECTION_MASK = 0x00000003 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001817
1818 /**
1819 * Indicates whether the view horizontal layout direction has been resolved and drawn to the
1820 * right-to-left direction.
1821 * @hide
1822 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001823 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 4 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001824
1825 /**
1826 * Indicates whether the view horizontal layout direction has been resolved.
1827 * @hide
1828 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001829 static final int LAYOUT_DIRECTION_RESOLVED = 8 << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001830
1831 /**
1832 * Mask for use with private flags indicating bits used for resolved horizontal layout direction.
1833 * @hide
1834 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001835 static final int LAYOUT_DIRECTION_RESOLVED_MASK = 0x0000000C << LAYOUT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001836
1837 /*
1838 * Array of horizontal layout direction flags for mapping attribute "layoutDirection" to correct
1839 * flag value.
1840 * @hide
1841 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001842 private static final int[] LAYOUT_DIRECTION_FLAGS = {
1843 LAYOUT_DIRECTION_LTR,
1844 LAYOUT_DIRECTION_RTL,
1845 LAYOUT_DIRECTION_INHERIT,
1846 LAYOUT_DIRECTION_LOCALE
1847 };
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001848
1849 /**
1850 * Default horizontal layout direction.
1851 * @hide
1852 */
1853 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001854
Adam Powell539ee872012-02-03 19:00:49 -08001855 /**
1856 * Indicates that the view is tracking some sort of transient state
1857 * that the app should not need to be aware of, but that the framework
1858 * should take special care to preserve.
1859 *
1860 * @hide
1861 */
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07001862 static final int HAS_TRANSIENT_STATE = 0x00000100;
Adam Powell539ee872012-02-03 19:00:49 -08001863
1864
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001865 /**
1866 * Text direction is inherited thru {@link ViewGroup}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001867 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001868 */
1869 public static final int TEXT_DIRECTION_INHERIT = 0;
1870
1871 /**
1872 * Text direction is using "first strong algorithm". The first strong directional character
1873 * determines the paragraph direction. If there is no strong directional character, the
1874 * paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001875 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001876 */
1877 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
1878
1879 /**
1880 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
1881 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
1882 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001883 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001884 */
1885 public static final int TEXT_DIRECTION_ANY_RTL = 2;
1886
1887 /**
1888 * Text direction is forced to LTR.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001889 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001890 */
1891 public static final int TEXT_DIRECTION_LTR = 3;
1892
1893 /**
1894 * Text direction is forced to RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001895 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001896 */
1897 public static final int TEXT_DIRECTION_RTL = 4;
1898
1899 /**
1900 * Text direction is coming from the system Locale.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001901 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001902 */
1903 public static final int TEXT_DIRECTION_LOCALE = 5;
1904
1905 /**
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001906 * Default text direction is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001907 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001908 */
1909 protected static int TEXT_DIRECTION_DEFAULT = TEXT_DIRECTION_INHERIT;
1910
1911 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001912 * Bit shift to get the horizontal layout direction. (bits after LAYOUT_DIRECTION_RESOLVED)
1913 * @hide
1914 */
1915 static final int TEXT_DIRECTION_MASK_SHIFT = 6;
1916
1917 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -07001918 * Mask for use with private flags indicating bits used for text direction.
1919 * @hide
1920 */
1921 static final int TEXT_DIRECTION_MASK = 0x00000007 << TEXT_DIRECTION_MASK_SHIFT;
1922
1923 /**
1924 * Array of text direction flags for mapping attribute "textDirection" to correct
1925 * flag value.
1926 * @hide
1927 */
1928 private static final int[] TEXT_DIRECTION_FLAGS = {
1929 TEXT_DIRECTION_INHERIT << TEXT_DIRECTION_MASK_SHIFT,
1930 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_MASK_SHIFT,
1931 TEXT_DIRECTION_ANY_RTL << TEXT_DIRECTION_MASK_SHIFT,
1932 TEXT_DIRECTION_LTR << TEXT_DIRECTION_MASK_SHIFT,
1933 TEXT_DIRECTION_RTL << TEXT_DIRECTION_MASK_SHIFT,
1934 TEXT_DIRECTION_LOCALE << TEXT_DIRECTION_MASK_SHIFT
1935 };
1936
1937 /**
1938 * Indicates whether the view text direction has been resolved.
1939 * @hide
1940 */
1941 static final int TEXT_DIRECTION_RESOLVED = 0x00000008 << TEXT_DIRECTION_MASK_SHIFT;
1942
1943 /**
1944 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
1945 * @hide
1946 */
1947 static final int TEXT_DIRECTION_RESOLVED_MASK_SHIFT = 10;
1948
1949 /**
1950 * Mask for use with private flags indicating bits used for resolved text direction.
1951 * @hide
1952 */
1953 static final int TEXT_DIRECTION_RESOLVED_MASK = 0x00000007 << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1954
1955 /**
1956 * Indicates whether the view text direction has been resolved to the "first strong" heuristic.
1957 * @hide
1958 */
1959 static final int TEXT_DIRECTION_RESOLVED_DEFAULT =
1960 TEXT_DIRECTION_FIRST_STRONG << TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
1961
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001962 /*
1963 * Default text alignment. The text alignment of this View is inherited from its parent.
1964 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001965 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001966 */
1967 public static final int TEXT_ALIGNMENT_INHERIT = 0;
1968
1969 /**
1970 * Default for the root view. The gravity determines the text alignment, ALIGN_NORMAL,
1971 * ALIGN_CENTER, or ALIGN_OPPOSITE, which are relative to each paragraph’s text direction.
1972 *
1973 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001974 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001975 */
1976 public static final int TEXT_ALIGNMENT_GRAVITY = 1;
1977
1978 /**
1979 * Align to the start of the paragraph, e.g. ALIGN_NORMAL.
1980 *
1981 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001982 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001983 */
1984 public static final int TEXT_ALIGNMENT_TEXT_START = 2;
1985
1986 /**
1987 * Align to the end of the paragraph, e.g. ALIGN_OPPOSITE.
1988 *
1989 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001990 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001991 */
1992 public static final int TEXT_ALIGNMENT_TEXT_END = 3;
1993
1994 /**
1995 * Center the paragraph, e.g. ALIGN_CENTER.
1996 *
1997 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07001998 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07001999 */
2000 public static final int TEXT_ALIGNMENT_CENTER = 4;
2001
2002 /**
2003 * Align to the start of the view, which is ALIGN_LEFT if the view’s resolved
2004 * layoutDirection is LTR, and ALIGN_RIGHT otherwise.
2005 *
2006 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002007 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002008 */
2009 public static final int TEXT_ALIGNMENT_VIEW_START = 5;
2010
2011 /**
2012 * Align to the end of the view, which is ALIGN_RIGHT if the view’s resolved
2013 * layoutDirection is LTR, and ALIGN_LEFT otherwise.
2014 *
2015 * Use with {@link #setTextAlignment(int)}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002016 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002017 */
2018 public static final int TEXT_ALIGNMENT_VIEW_END = 6;
2019
2020 /**
2021 * Default text alignment is inherited
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07002022 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07002023 */
2024 protected static int TEXT_ALIGNMENT_DEFAULT = TEXT_ALIGNMENT_GRAVITY;
2025
2026 /**
2027 * Bit shift to get the horizontal layout direction. (bits after DRAG_HOVERED)
2028 * @hide
2029 */
2030 static final int TEXT_ALIGNMENT_MASK_SHIFT = 13;
2031
2032 /**
2033 * Mask for use with private flags indicating bits used for text alignment.
2034 * @hide
2035 */
2036 static final int TEXT_ALIGNMENT_MASK = 0x00000007 << TEXT_ALIGNMENT_MASK_SHIFT;
2037
2038 /**
2039 * Array of text direction flags for mapping attribute "textAlignment" to correct
2040 * flag value.
2041 * @hide
2042 */
2043 private static final int[] TEXT_ALIGNMENT_FLAGS = {
2044 TEXT_ALIGNMENT_INHERIT << TEXT_ALIGNMENT_MASK_SHIFT,
2045 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_MASK_SHIFT,
2046 TEXT_ALIGNMENT_TEXT_START << TEXT_ALIGNMENT_MASK_SHIFT,
2047 TEXT_ALIGNMENT_TEXT_END << TEXT_ALIGNMENT_MASK_SHIFT,
2048 TEXT_ALIGNMENT_CENTER << TEXT_ALIGNMENT_MASK_SHIFT,
2049 TEXT_ALIGNMENT_VIEW_START << TEXT_ALIGNMENT_MASK_SHIFT,
2050 TEXT_ALIGNMENT_VIEW_END << TEXT_ALIGNMENT_MASK_SHIFT
2051 };
2052
2053 /**
2054 * Indicates whether the view text alignment has been resolved.
2055 * @hide
2056 */
2057 static final int TEXT_ALIGNMENT_RESOLVED = 0x00000008 << TEXT_ALIGNMENT_MASK_SHIFT;
2058
2059 /**
2060 * Bit shift to get the resolved text alignment.
2061 * @hide
2062 */
2063 static final int TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT = 17;
2064
2065 /**
2066 * Mask for use with private flags indicating bits used for text alignment.
2067 * @hide
2068 */
2069 static final int TEXT_ALIGNMENT_RESOLVED_MASK = 0x00000007 << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2070
2071 /**
2072 * Indicates whether if the view text alignment has been resolved to gravity
2073 */
2074 public static final int TEXT_ALIGNMENT_RESOLVED_DEFAULT =
2075 TEXT_ALIGNMENT_GRAVITY << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
2076
Svetoslav Ganov42138042012-03-20 11:51:39 -07002077 // Accessiblity constants for mPrivateFlags2
2078
2079 /**
2080 * Shift for accessibility related bits in {@link #mPrivateFlags2}.
2081 */
2082 static final int IMPORTANT_FOR_ACCESSIBILITY_SHIFT = 20;
2083
2084 /**
2085 * Automatically determine whether a view is important for accessibility.
2086 */
2087 public static final int IMPORTANT_FOR_ACCESSIBILITY_AUTO = 0x00000000;
2088
2089 /**
2090 * The view is important for accessibility.
2091 */
2092 public static final int IMPORTANT_FOR_ACCESSIBILITY_YES = 0x00000001;
2093
2094 /**
2095 * The view is not important for accessibility.
2096 */
2097 public static final int IMPORTANT_FOR_ACCESSIBILITY_NO = 0x00000002;
2098
2099 /**
2100 * The default whether the view is important for accessiblity.
2101 */
2102 static final int IMPORTANT_FOR_ACCESSIBILITY_DEFAULT = IMPORTANT_FOR_ACCESSIBILITY_AUTO;
2103
2104 /**
2105 * Mask for obtainig the bits which specify how to determine
2106 * whether a view is important for accessibility.
2107 */
2108 static final int IMPORTANT_FOR_ACCESSIBILITY_MASK = (IMPORTANT_FOR_ACCESSIBILITY_AUTO
2109 | IMPORTANT_FOR_ACCESSIBILITY_YES | IMPORTANT_FOR_ACCESSIBILITY_NO)
2110 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2111
2112 /**
2113 * Flag indicating whether a view has accessibility focus.
2114 */
2115 static final int ACCESSIBILITY_FOCUSED = 0x00000040 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
2116
2117 /**
2118 * Flag indicating whether a view state for accessibility has changed.
2119 */
2120 static final int ACCESSIBILITY_STATE_CHANGED = 0x00000080 << IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07002121
Christopher Tate3d4bf172011-03-28 16:16:46 -07002122 /* End of masks for mPrivateFlags2 */
2123
2124 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
2125
Chet Haasedaf98e92011-01-10 14:10:36 -08002126 /**
Adam Powell637d3372010-08-25 14:37:03 -07002127 * Always allow a user to over-scroll this view, provided it is a
2128 * view that can scroll.
2129 *
2130 * @see #getOverScrollMode()
2131 * @see #setOverScrollMode(int)
2132 */
2133 public static final int OVER_SCROLL_ALWAYS = 0;
2134
2135 /**
2136 * Allow a user to over-scroll this view only if the content is large
2137 * enough to meaningfully scroll, provided it is a view that can scroll.
2138 *
2139 * @see #getOverScrollMode()
2140 * @see #setOverScrollMode(int)
2141 */
2142 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
2143
2144 /**
2145 * Never allow a user to over-scroll this view.
2146 *
2147 * @see #getOverScrollMode()
2148 * @see #setOverScrollMode(int)
2149 */
2150 public static final int OVER_SCROLL_NEVER = 2;
2151
2152 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002153 * Special constant for {@link #setSystemUiVisibility(int)}: View has
2154 * requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08002155 *
Joe Malin32736f02011-01-19 16:14:20 -08002156 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002157 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002158 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08002159
2160 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002161 * Flag for {@link #setSystemUiVisibility(int)}: View has requested the
2162 * system UI to enter an unobtrusive "low profile" mode.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002163 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002164 * <p>This is for use in games, book readers, video players, or any other
Philip Milne6c8ea062012-04-03 17:38:43 -07002165 * "immersive" application where the usual system chrome is deemed too distracting.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002166 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002167 * <p>In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08002168 *
Joe Malin32736f02011-01-19 16:14:20 -08002169 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08002170 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002171 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
2172
2173 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002174 * Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
2175 * system navigation be temporarily hidden.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002176 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002177 * <p>This is an even less obtrusive state than that called for by
Daniel Sandler60ee2562011-07-22 12:34:33 -04002178 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
2179 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
2180 * those to disappear. This is useful (in conjunction with the
Philip Milne6c8ea062012-04-03 17:38:43 -07002181 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
Daniel Sandler60ee2562011-07-22 12:34:33 -04002182 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
2183 * window flags) for displaying content using every last pixel on the display.
2184 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002185 * <p>There is a limitation: because navigation controls are so important, the least user
2186 * interaction will cause them to reappear immediately. When this happens, both
2187 * this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
2188 * so that both elements reappear at the same time.
Daniel Sandler60ee2562011-07-22 12:34:33 -04002189 *
2190 * @see #setSystemUiVisibility(int)
2191 */
2192 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
2193
2194 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002195 * Flag for {@link #setSystemUiVisibility(int)}: View has requested to go
2196 * into the normal fullscreen mode so that its content can take over the screen
2197 * while still allowing the user to interact with the application.
2198 *
2199 * <p>This has the same visual effect as
2200 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN
2201 * WindowManager.LayoutParams.FLAG_FULLSCREEN},
2202 * meaning that non-critical screen decorations (such as the status bar) will be
2203 * hidden while the user is in the View's window, focusing the experience on
2204 * that content. Unlike the window flag, if you are using ActionBar in
2205 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2206 * Window.FEATURE_ACTION_BAR_OVERLAY}, then enabling this flag will also
2207 * hide the action bar.
2208 *
2209 * <p>This approach to going fullscreen is best used over the window flag when
2210 * it is a transient state -- that is, the application does this at certain
2211 * points in its user interaction where it wants to allow the user to focus
2212 * on content, but not as a continuous state. For situations where the application
2213 * would like to simply stay full screen the entire time (such as a game that
2214 * wants to take over the screen), the
2215 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN window flag}
2216 * is usually a better approach. The state set here will be removed by the system
2217 * in various situations (such as the user moving to another application) like
2218 * the other system UI states.
2219 *
2220 * <p>When using this flag, the application should provide some easy facility
2221 * for the user to go out of it. A common example would be in an e-book
2222 * reader, where tapping on the screen brings back whatever screen and UI
2223 * decorations that had been hidden while the user was immersed in reading
2224 * the book.
2225 *
2226 * @see #setSystemUiVisibility(int)
2227 */
2228 public static final int SYSTEM_UI_FLAG_FULLSCREEN = 0x00000004;
2229
2230 /**
2231 * Flag for {@link #setSystemUiVisibility(int)}: When using other layout
2232 * flags, we would like a stable view of the content insets given to
2233 * {@link #fitSystemWindows(Rect)}. This means that the insets seen there
2234 * will always represent the worst case that the application can expect
2235 * as a continue state. In practice this means with any of system bar,
2236 * nav bar, and status bar shown, but not the space that would be needed
2237 * for an input method.
2238 *
2239 * <p>If you are using ActionBar in
2240 * overlay mode with {@link Window#FEATURE_ACTION_BAR_OVERLAY
2241 * Window.FEATURE_ACTION_BAR_OVERLAY}, this flag will also impact the
2242 * insets it adds to those given to the application.
2243 */
2244 public static final int SYSTEM_UI_FLAG_LAYOUT_STABLE = 0x00000100;
2245
2246 /**
2247 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2248 * to be layed out as if it has requested
2249 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, even if it currently hasn't. This
2250 * allows it to avoid artifacts when switching in and out of that mode, at
2251 * the expense that some of its user interface may be covered by screen
2252 * decorations when they are shown. You can perform layout of your inner
2253 * UI elements to account for the navagation system UI through the
2254 * {@link #fitSystemWindows(Rect)} method.
2255 */
2256 public static final int SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 0x00000200;
2257
2258 /**
2259 * Flag for {@link #setSystemUiVisibility(int)}: View would like its window
2260 * to be layed out as if it has requested
2261 * {@link #SYSTEM_UI_FLAG_FULLSCREEN}, even if it currently hasn't. This
2262 * allows it to avoid artifacts when switching in and out of that mode, at
2263 * the expense that some of its user interface may be covered by screen
2264 * decorations when they are shown. You can perform layout of your inner
2265 * UI elements to account for non-fullscreen system UI through the
2266 * {@link #fitSystemWindows(Rect)} method.
2267 */
2268 public static final int SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 0x00000400;
2269
2270 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04002271 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
2272 */
2273 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
2274
2275 /**
2276 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
2277 */
2278 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08002279
2280 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002281 * @hide
2282 *
2283 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2284 * out of the public fields to keep the undefined bits out of the developer's way.
2285 *
2286 * Flag to make the status bar not expandable. Unless you also
2287 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
2288 */
2289 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
2290
2291 /**
2292 * @hide
2293 *
2294 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2295 * out of the public fields to keep the undefined bits out of the developer's way.
2296 *
2297 * Flag to hide notification icons and scrolling ticker text.
2298 */
2299 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
2300
2301 /**
2302 * @hide
2303 *
2304 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2305 * out of the public fields to keep the undefined bits out of the developer's way.
2306 *
2307 * Flag to disable incoming notification alerts. This will not block
2308 * icons, but it will block sound, vibrating and other visual or aural notifications.
2309 */
2310 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
2311
2312 /**
2313 * @hide
2314 *
2315 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2316 * out of the public fields to keep the undefined bits out of the developer's way.
2317 *
2318 * Flag to hide only the scrolling ticker. Note that
2319 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
2320 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
2321 */
2322 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
2323
2324 /**
2325 * @hide
2326 *
2327 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2328 * out of the public fields to keep the undefined bits out of the developer's way.
2329 *
2330 * Flag to hide the center system info area.
2331 */
2332 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
2333
2334 /**
2335 * @hide
2336 *
2337 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2338 * out of the public fields to keep the undefined bits out of the developer's way.
2339 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002340 * Flag to hide only the home button. Don't use this
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002341 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2342 */
Daniel Sandlerdba93562011-10-06 16:39:58 -04002343 public static final int STATUS_BAR_DISABLE_HOME = 0x00200000;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002344
2345 /**
2346 * @hide
2347 *
2348 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2349 * out of the public fields to keep the undefined bits out of the developer's way.
2350 *
Daniel Sandlerdba93562011-10-06 16:39:58 -04002351 * Flag to hide only the back button. Don't use this
Joe Onorato6478adc2011-01-27 21:15:01 -08002352 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2353 */
2354 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
2355
2356 /**
2357 * @hide
2358 *
2359 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2360 * out of the public fields to keep the undefined bits out of the developer's way.
2361 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002362 * Flag to hide only the clock. You might use this if your activity has
2363 * its own clock making the status bar's clock redundant.
2364 */
Joe Onorato6478adc2011-01-27 21:15:01 -08002365 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
2366
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002367 /**
2368 * @hide
Daniel Sandlerdba93562011-10-06 16:39:58 -04002369 *
2370 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
2371 * out of the public fields to keep the undefined bits out of the developer's way.
2372 *
2373 * Flag to hide only the recent apps button. Don't use this
2374 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
2375 */
2376 public static final int STATUS_BAR_DISABLE_RECENT = 0x01000000;
2377
2378 /**
2379 * @hide
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002380 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002381 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08002382
2383 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002384 * These are the system UI flags that can be cleared by events outside
2385 * of an application. Currently this is just the ability to tap on the
2386 * screen while hiding the navigation bar to have it return.
2387 * @hide
2388 */
2389 public static final int SYSTEM_UI_CLEARABLE_FLAGS =
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07002390 SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION
2391 | SYSTEM_UI_FLAG_FULLSCREEN;
2392
2393 /**
2394 * Flags that can impact the layout in relation to system UI.
2395 */
2396 public static final int SYSTEM_UI_LAYOUT_FLAGS =
2397 SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
2398 | SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Dianne Hackborn9a230e02011-10-06 11:51:27 -07002399
2400 /**
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07002401 * Find views that render the specified text.
2402 *
2403 * @see #findViewsWithText(ArrayList, CharSequence, int)
2404 */
2405 public static final int FIND_VIEWS_WITH_TEXT = 0x00000001;
2406
2407 /**
2408 * Find find views that contain the specified content description.
2409 *
2410 * @see #findViewsWithText(ArrayList, CharSequence, int)
2411 */
2412 public static final int FIND_VIEWS_WITH_CONTENT_DESCRIPTION = 0x00000002;
2413
2414 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07002415 * Find views that contain {@link AccessibilityNodeProvider}. Such
2416 * a View is a root of virtual view hierarchy and may contain the searched
2417 * text. If this flag is set Views with providers are automatically
2418 * added and it is a responsibility of the client to call the APIs of
2419 * the provider to determine whether the virtual tree rooted at this View
2420 * contains the text, i.e. getting the list of {@link AccessibilityNodeInfo}s
2421 * represeting the virtual views with this text.
2422 *
2423 * @see #findViewsWithText(ArrayList, CharSequence, int)
2424 *
2425 * @hide
2426 */
2427 public static final int FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS = 0x00000004;
2428
2429 /**
Romain Guybb9908b2012-03-08 11:14:07 -08002430 * Indicates that the screen has changed state and is now off.
2431 *
2432 * @see #onScreenStateChanged(int)
2433 */
2434 public static final int SCREEN_STATE_OFF = 0x0;
2435
2436 /**
2437 * Indicates that the screen has changed state and is now on.
2438 *
Romain Guy1e3d3132012-03-08 15:55:56 -08002439 * @see #onScreenStateChanged(int)
Romain Guybb9908b2012-03-08 11:14:07 -08002440 */
2441 public static final int SCREEN_STATE_ON = 0x1;
2442
2443 /**
Adam Powell637d3372010-08-25 14:37:03 -07002444 * Controls the over-scroll mode for this view.
2445 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
2446 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
2447 * and {@link #OVER_SCROLL_NEVER}.
2448 */
2449 private int mOverScrollMode;
2450
2451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 * The parent this view is attached to.
2453 * {@hide}
2454 *
2455 * @see #getParent()
2456 */
2457 protected ViewParent mParent;
2458
2459 /**
2460 * {@hide}
2461 */
2462 AttachInfo mAttachInfo;
2463
2464 /**
2465 * {@hide}
2466 */
Romain Guy809a7f62009-05-14 15:44:42 -07002467 @ViewDebug.ExportedProperty(flagMapping = {
2468 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
2469 name = "FORCE_LAYOUT"),
2470 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
2471 name = "LAYOUT_REQUIRED"),
2472 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07002473 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07002474 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
2475 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
2476 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
2477 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
2478 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07002480 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481
2482 /**
Joe Onorato664644d2011-01-23 17:53:23 -08002483 * This view's request for the visibility of the status bar.
2484 * @hide
2485 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04002486 @ViewDebug.ExportedProperty(flagMapping = {
2487 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
2488 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
2489 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
2490 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2491 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
2492 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
2493 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
2494 equals = SYSTEM_UI_FLAG_VISIBLE,
2495 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
2496 })
Joe Onorato664644d2011-01-23 17:53:23 -08002497 int mSystemUiVisibility;
2498
2499 /**
Chet Haase563d4f22012-04-18 16:20:08 -07002500 * Reference count for transient state.
2501 * @see #setHasTransientState(boolean)
2502 */
2503 int mTransientStateCount = 0;
2504
2505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 * Count of how many windows this view has been attached to.
2507 */
2508 int mWindowAttachCount;
2509
2510 /**
2511 * The layout parameters associated with this view and used by the parent
2512 * {@link android.view.ViewGroup} to determine how this view should be
2513 * laid out.
2514 * {@hide}
2515 */
2516 protected ViewGroup.LayoutParams mLayoutParams;
2517
2518 /**
2519 * The view flags hold various views states.
2520 * {@hide}
2521 */
2522 @ViewDebug.ExportedProperty
2523 int mViewFlags;
2524
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002525 static class TransformationInfo {
2526 /**
2527 * The transform matrix for the View. This transform is calculated internally
2528 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2529 * is used by default. Do *not* use this variable directly; instead call
2530 * getMatrix(), which will automatically recalculate the matrix if necessary
2531 * to get the correct matrix based on the latest rotation and scale properties.
2532 */
2533 private final Matrix mMatrix = new Matrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07002534
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002535 /**
2536 * The transform matrix for the View. This transform is calculated internally
2537 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2538 * is used by default. Do *not* use this variable directly; instead call
2539 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
2540 * to get the correct matrix based on the latest rotation and scale properties.
2541 */
2542 private Matrix mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07002543
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002544 /**
2545 * An internal variable that tracks whether we need to recalculate the
2546 * transform matrix, based on whether the rotation or scaleX/Y properties
2547 * have changed since the matrix was last calculated.
2548 */
2549 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002550
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002551 /**
2552 * An internal variable that tracks whether we need to recalculate the
2553 * transform matrix, based on whether the rotation or scaleX/Y properties
2554 * have changed since the matrix was last calculated.
2555 */
2556 private boolean mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002557
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002558 /**
2559 * A variable that tracks whether we need to recalculate the
2560 * transform matrix, based on whether the rotation or scaleX/Y properties
2561 * have changed since the matrix was last calculated. This variable
2562 * is only valid after a call to updateMatrix() or to a function that
2563 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
2564 */
2565 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002566
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002567 /**
2568 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2569 */
2570 private Camera mCamera = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002571
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002572 /**
2573 * This matrix is used when computing the matrix for 3D rotations.
2574 */
2575 private Matrix matrix3D = null;
Chet Haasefd2b0022010-08-06 13:08:56 -07002576
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002577 /**
2578 * These prev values are used to recalculate a centered pivot point when necessary. The
2579 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2580 * set), so thes values are only used then as well.
2581 */
2582 private int mPrevWidth = -1;
2583 private int mPrevHeight = -1;
Philip Milne6c8ea062012-04-03 17:38:43 -07002584
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002585 /**
2586 * The degrees rotation around the vertical axis through the pivot point.
2587 */
2588 @ViewDebug.ExportedProperty
2589 float mRotationY = 0f;
2590
2591 /**
2592 * The degrees rotation around the horizontal axis through the pivot point.
2593 */
2594 @ViewDebug.ExportedProperty
2595 float mRotationX = 0f;
2596
2597 /**
2598 * The degrees rotation around the pivot point.
2599 */
2600 @ViewDebug.ExportedProperty
2601 float mRotation = 0f;
2602
2603 /**
2604 * The amount of translation of the object away from its left property (post-layout).
2605 */
2606 @ViewDebug.ExportedProperty
2607 float mTranslationX = 0f;
2608
2609 /**
2610 * The amount of translation of the object away from its top property (post-layout).
2611 */
2612 @ViewDebug.ExportedProperty
2613 float mTranslationY = 0f;
2614
2615 /**
2616 * The amount of scale in the x direction around the pivot point. A
2617 * value of 1 means no scaling is applied.
2618 */
2619 @ViewDebug.ExportedProperty
2620 float mScaleX = 1f;
2621
2622 /**
2623 * The amount of scale in the y direction around the pivot point. A
2624 * value of 1 means no scaling is applied.
2625 */
2626 @ViewDebug.ExportedProperty
2627 float mScaleY = 1f;
2628
2629 /**
Chet Haasea33de552012-02-03 16:28:24 -08002630 * The x location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002631 */
2632 @ViewDebug.ExportedProperty
2633 float mPivotX = 0f;
2634
2635 /**
Chet Haasea33de552012-02-03 16:28:24 -08002636 * The y location of the point around which the view is rotated and scaled.
Dianne Hackbornddb715b2011-09-09 14:43:39 -07002637 */
2638 @ViewDebug.ExportedProperty
2639 float mPivotY = 0f;
2640
2641 /**
2642 * The opacity of the View. This is a value from 0 to 1, where 0 means
2643 * completely transparent and 1 means completely opaque.
2644 */
2645 @ViewDebug.ExportedProperty
2646 float mAlpha = 1f;
2647 }
2648
2649 TransformationInfo mTransformationInfo;
Chet Haasefd2b0022010-08-06 13:08:56 -07002650
Joe Malin32736f02011-01-19 16:14:20 -08002651 private boolean mLastIsOpaque;
2652
Chet Haasefd2b0022010-08-06 13:08:56 -07002653 /**
2654 * Convenience value to check for float values that are close enough to zero to be considered
2655 * zero.
2656 */
Romain Guy2542d192010-08-18 11:47:12 -07002657 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002658
2659 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 * The distance in pixels from the left edge of this view's parent
2661 * to the left edge of this view.
2662 * {@hide}
2663 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002664 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 protected int mLeft;
2666 /**
2667 * The distance in pixels from the left edge of this view's parent
2668 * to the right edge of this view.
2669 * {@hide}
2670 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002671 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 protected int mRight;
2673 /**
2674 * The distance in pixels from the top edge of this view's parent
2675 * to the top edge of this view.
2676 * {@hide}
2677 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002678 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 protected int mTop;
2680 /**
2681 * The distance in pixels from the top edge of this view's parent
2682 * to the bottom edge of this view.
2683 * {@hide}
2684 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002685 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 protected int mBottom;
2687
2688 /**
2689 * The offset, in pixels, by which the content of this view is scrolled
2690 * horizontally.
2691 * {@hide}
2692 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002693 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 protected int mScrollX;
2695 /**
2696 * The offset, in pixels, by which the content of this view is scrolled
2697 * vertically.
2698 * {@hide}
2699 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002700 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 protected int mScrollY;
2702
2703 /**
2704 * The left padding in pixels, that is the distance in pixels between the
2705 * left edge of this view and the left edge of its content.
2706 * {@hide}
2707 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002708 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 protected int mPaddingLeft;
2710 /**
2711 * The right padding in pixels, that is the distance in pixels between the
2712 * right edge of this view and the right edge of its content.
2713 * {@hide}
2714 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002715 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 protected int mPaddingRight;
2717 /**
2718 * The top padding in pixels, that is the distance in pixels between the
2719 * top edge of this view and the top edge of its content.
2720 * {@hide}
2721 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002722 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 protected int mPaddingTop;
2724 /**
2725 * The bottom padding in pixels, that is the distance in pixels between the
2726 * bottom edge of this view and the bottom edge of its content.
2727 * {@hide}
2728 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002729 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 protected int mPaddingBottom;
2731
2732 /**
Philip Milne1557fd72012-04-04 23:41:34 -07002733 * The layout insets in pixels, that is the distance in pixels between the
2734 * visible edges of this view its bounds.
2735 */
2736 private Insets mLayoutInsets;
2737
2738 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002739 * Briefly describes the view and is primarily used for accessibility support.
2740 */
2741 private CharSequence mContentDescription;
2742
2743 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002745 *
2746 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002748 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002749 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750
2751 /**
2752 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002753 *
2754 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002756 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002757 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002759 /**
Adam Powell20232d02010-12-08 21:08:53 -08002760 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002761 *
2762 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002763 */
2764 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002765 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002766
2767 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002768 * Cache if the user padding is relative.
2769 *
2770 */
2771 @ViewDebug.ExportedProperty(category = "padding")
2772 boolean mUserPaddingRelative;
2773
2774 /**
2775 * Cache the paddingStart set by the user to append to the scrollbar's size.
2776 *
2777 */
2778 @ViewDebug.ExportedProperty(category = "padding")
2779 int mUserPaddingStart;
2780
2781 /**
2782 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2783 *
2784 */
2785 @ViewDebug.ExportedProperty(category = "padding")
2786 int mUserPaddingEnd;
2787
2788 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002789 * @hide
2790 */
2791 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2792 /**
2793 * @hide
2794 */
2795 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
Philip Milne6c8ea062012-04-03 17:38:43 -07002797 private Drawable mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798
2799 private int mBackgroundResource;
2800 private boolean mBackgroundSizeChanged;
2801
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002802 static class ListenerInfo {
2803 /**
2804 * Listener used to dispatch focus change events.
2805 * This field should be made private, so it is hidden from the SDK.
2806 * {@hide}
2807 */
2808 protected OnFocusChangeListener mOnFocusChangeListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002810 /**
2811 * Listeners for layout change events.
2812 */
2813 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
Chet Haase21cd1382010-09-01 17:42:29 -07002814
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002815 /**
2816 * Listeners for attach events.
2817 */
2818 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
Adam Powell4afd62b2011-02-18 15:02:18 -08002819
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002820 /**
2821 * Listener used to dispatch click events.
2822 * This field should be made private, so it is hidden from the SDK.
2823 * {@hide}
2824 */
2825 public OnClickListener mOnClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002827 /**
2828 * Listener used to dispatch long click events.
2829 * This field should be made private, so it is hidden from the SDK.
2830 * {@hide}
2831 */
2832 protected OnLongClickListener mOnLongClickListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002834 /**
2835 * Listener used to build the context menu.
2836 * This field should be made private, so it is hidden from the SDK.
2837 * {@hide}
2838 */
2839 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002841 private OnKeyListener mOnKeyListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002843 private OnTouchListener mOnTouchListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002845 private OnHoverListener mOnHoverListener;
Jeff Brown10b62902011-06-20 16:40:37 -07002846
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002847 private OnGenericMotionListener mOnGenericMotionListener;
Jeff Brown33bbfd22011-02-24 20:55:35 -08002848
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002849 private OnDragListener mOnDragListener;
Chris Tate32affef2010-10-18 15:29:21 -07002850
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002851 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2852 }
2853
2854 ListenerInfo mListenerInfo;
Joe Onorato664644d2011-01-23 17:53:23 -08002855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 /**
2857 * The application environment this view lives in.
2858 * This field should be made private, so it is hidden from the SDK.
2859 * {@hide}
2860 */
2861 protected Context mContext;
2862
Dianne Hackbornab0f4852011-09-12 16:59:06 -07002863 private final Resources mResources;
2864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 private ScrollabilityCache mScrollCache;
2866
2867 private int[] mDrawableState = null;
2868
Romain Guy0211a0a2011-02-14 16:34:59 -08002869 /**
2870 * Set to true when drawing cache is enabled and cannot be created.
Philip Milne6c8ea062012-04-03 17:38:43 -07002871 *
Romain Guy0211a0a2011-02-14 16:34:59 -08002872 * @hide
2873 */
2874 public boolean mCachingFailed;
2875
Romain Guy02890fd2010-08-06 17:58:44 -07002876 private Bitmap mDrawingCache;
2877 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002878 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002879 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880
2881 /**
2882 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2883 * the user may specify which view to go to next.
2884 */
2885 private int mNextFocusLeftId = View.NO_ID;
2886
2887 /**
2888 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2889 * the user may specify which view to go to next.
2890 */
2891 private int mNextFocusRightId = View.NO_ID;
2892
2893 /**
2894 * When this view has focus and the next focus is {@link #FOCUS_UP},
2895 * the user may specify which view to go to next.
2896 */
2897 private int mNextFocusUpId = View.NO_ID;
2898
2899 /**
2900 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2901 * the user may specify which view to go to next.
2902 */
2903 private int mNextFocusDownId = View.NO_ID;
2904
Jeff Brown4e6319b2010-12-13 10:36:51 -08002905 /**
2906 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2907 * the user may specify which view to go to next.
2908 */
2909 int mNextFocusForwardId = View.NO_ID;
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002912 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002913 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002914 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 private UnsetPressedState mUnsetPressedState;
2917
2918 /**
2919 * Whether the long press's action has been invoked. The tap's action is invoked on the
2920 * up event while a long press is invoked as soon as the long press duration is reached, so
2921 * a long press could be performed before the tap is checked, in which case the tap's action
2922 * should not be invoked.
2923 */
2924 private boolean mHasPerformedLongPress;
2925
2926 /**
2927 * The minimum height of the view. We'll try our best to have the height
2928 * of this view to at least this amount.
2929 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002930 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931 private int mMinHeight;
2932
2933 /**
2934 * The minimum width of the view. We'll try our best to have the width
2935 * of this view to at least this amount.
2936 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002937 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 private int mMinWidth;
2939
2940 /**
2941 * The delegate to handle touch events that are physically in this view
2942 * but should be handled by another view.
2943 */
2944 private TouchDelegate mTouchDelegate = null;
2945
2946 /**
2947 * Solid color to use as a background when creating the drawing cache. Enables
2948 * the cache to use 16 bit bitmaps instead of 32 bit.
2949 */
2950 private int mDrawingCacheBackgroundColor = 0;
2951
2952 /**
2953 * Special tree observer used when mAttachInfo is null.
2954 */
2955 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002956
Adam Powelle14579b2009-12-16 18:39:52 -08002957 /**
2958 * Cache the touch slop from the context that created the view.
2959 */
2960 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002963 * Object that handles automatic animation of view properties.
2964 */
2965 private ViewPropertyAnimator mAnimator = null;
2966
2967 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002968 * Flag indicating that a drag can cross window boundaries. When
2969 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2970 * with this flag set, all visible applications will be able to participate
2971 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002972 *
2973 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002974 */
2975 public static final int DRAG_FLAG_GLOBAL = 1;
2976
2977 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002978 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2979 */
2980 private float mVerticalScrollFactor;
2981
2982 /**
Adam Powell20232d02010-12-08 21:08:53 -08002983 * Position of the vertical scroll bar.
2984 */
2985 private int mVerticalScrollbarPosition;
2986
2987 /**
2988 * Position the scroll bar at the default position as determined by the system.
2989 */
2990 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2991
2992 /**
2993 * Position the scroll bar along the left edge.
2994 */
2995 public static final int SCROLLBAR_POSITION_LEFT = 1;
2996
2997 /**
2998 * Position the scroll bar along the right edge.
2999 */
3000 public static final int SCROLLBAR_POSITION_RIGHT = 2;
3001
3002 /**
Romain Guy171c5922011-01-06 10:04:23 -08003003 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08003004 *
3005 * @see #getLayerType()
3006 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003007 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08003008 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003009 */
3010 public static final int LAYER_TYPE_NONE = 0;
3011
3012 /**
3013 * <p>Indicates that the view has a software layer. A software layer is backed
3014 * by a bitmap and causes the view to be rendered using Android's software
3015 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003016 *
Romain Guy171c5922011-01-06 10:04:23 -08003017 * <p>Software layers have various usages:</p>
3018 * <p>When the application is not using hardware acceleration, a software layer
3019 * is useful to apply a specific color filter and/or blending mode and/or
3020 * translucency to a view and all its children.</p>
3021 * <p>When the application is using hardware acceleration, a software layer
3022 * is useful to render drawing primitives not supported by the hardware
3023 * accelerated pipeline. It can also be used to cache a complex view tree
3024 * into a texture and reduce the complexity of drawing operations. For instance,
3025 * when animating a complex view tree with a translation, a software layer can
3026 * be used to render the view tree only once.</p>
3027 * <p>Software layers should be avoided when the affected view tree updates
3028 * often. Every update will require to re-render the software layer, which can
3029 * potentially be slow (particularly when hardware acceleration is turned on
3030 * since the layer will have to be uploaded into a hardware texture after every
3031 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08003032 *
3033 * @see #getLayerType()
3034 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08003035 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08003036 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08003037 */
3038 public static final int LAYER_TYPE_SOFTWARE = 1;
3039
3040 /**
3041 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
3042 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
3043 * OpenGL hardware) and causes the view to be rendered using Android's hardware
3044 * rendering pipeline, but only if hardware acceleration is turned on for the
3045 * view hierarchy. When hardware acceleration is turned off, hardware layers
3046 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003047 *
Romain Guy171c5922011-01-06 10:04:23 -08003048 * <p>A hardware layer is useful to apply a specific color filter and/or
3049 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08003050 * <p>A hardware layer can be used to cache a complex view tree into a
3051 * texture and reduce the complexity of drawing operations. For instance,
3052 * when animating a complex view tree with a translation, a hardware layer can
3053 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08003054 * <p>A hardware layer can also be used to increase the rendering quality when
3055 * rotation transformations are applied on a view. It can also be used to
3056 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08003057 *
3058 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08003059 * @see #setLayerType(int, android.graphics.Paint)
3060 * @see #LAYER_TYPE_NONE
3061 * @see #LAYER_TYPE_SOFTWARE
3062 */
3063 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08003064
Romain Guy3aaff3a2011-01-12 14:18:47 -08003065 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
3066 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
3067 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
3068 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
3069 })
Romain Guy171c5922011-01-06 10:04:23 -08003070 int mLayerType = LAYER_TYPE_NONE;
3071 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08003072 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08003073
3074 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07003075 * Set to true when the view is sending hover accessibility events because it
3076 * is the innermost hovered view.
3077 */
3078 private boolean mSendingHoverAccessibilityEvents;
3079
3080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 * Simple constructor to use when creating a view from code.
3082 *
3083 * @param context The Context the view is running in, through which it can
3084 * access the current theme, resources, etc.
3085 */
3086 public View(Context context) {
3087 mContext = context;
3088 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003089 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003090 // Set layout and text direction defaults
3091 mPrivateFlags2 = (LAYOUT_DIRECTION_DEFAULT << LAYOUT_DIRECTION_MASK_SHIFT) |
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003092 (TEXT_DIRECTION_DEFAULT << TEXT_DIRECTION_MASK_SHIFT) |
Svetoslav Ganov42138042012-03-20 11:51:39 -07003093 (TEXT_ALIGNMENT_DEFAULT << TEXT_ALIGNMENT_MASK_SHIFT) |
3094 (IMPORTANT_FOR_ACCESSIBILITY_DEFAULT << IMPORTANT_FOR_ACCESSIBILITY_SHIFT);
Adam Powelle14579b2009-12-16 18:39:52 -08003095 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07003096 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003097 mUserPaddingStart = -1;
3098 mUserPaddingEnd = -1;
3099 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
3101
3102 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07003103 * Delegate for injecting accessiblity functionality.
3104 */
3105 AccessibilityDelegate mAccessibilityDelegate;
3106
3107 /**
3108 * Consistency verifier for debugging purposes.
3109 * @hide
3110 */
3111 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
3112 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
3113 new InputEventConsistencyVerifier(this, 0) : null;
3114
3115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 * Constructor that is called when inflating a view from XML. This is called
3117 * when a view is being constructed from an XML file, supplying attributes
3118 * that were specified in the XML file. This version uses a default style of
3119 * 0, so the only attribute values applied are those in the Context's Theme
3120 * and the given AttributeSet.
3121 *
3122 * <p>
3123 * The method onFinishInflate() will be called after all children have been
3124 * added.
3125 *
3126 * @param context The Context the view is running in, through which it can
3127 * access the current theme, resources, etc.
3128 * @param attrs The attributes of the XML tag that is inflating the view.
3129 * @see #View(Context, AttributeSet, int)
3130 */
3131 public View(Context context, AttributeSet attrs) {
3132 this(context, attrs, 0);
3133 }
3134
3135 /**
3136 * Perform inflation from XML and apply a class-specific base style. This
3137 * constructor of View allows subclasses to use their own base style when
3138 * they are inflating. For example, a Button class's constructor would call
3139 * this version of the super class constructor and supply
3140 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
3141 * the theme's button style to modify all of the base view attributes (in
3142 * particular its background) as well as the Button class's attributes.
3143 *
3144 * @param context The Context the view is running in, through which it can
3145 * access the current theme, resources, etc.
3146 * @param attrs The attributes of the XML tag that is inflating the view.
3147 * @param defStyle The default style to apply to this view. If 0, no style
3148 * will be applied (beyond what is included in the theme). This may
3149 * either be an attribute resource, whose value will be retrieved
3150 * from the current theme, or an explicit style resource.
3151 * @see #View(Context, AttributeSet)
3152 */
3153 public View(Context context, AttributeSet attrs, int defStyle) {
3154 this(context);
3155
3156 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
3157 defStyle, 0);
3158
3159 Drawable background = null;
3160
3161 int leftPadding = -1;
3162 int topPadding = -1;
3163 int rightPadding = -1;
3164 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003165 int startPadding = -1;
3166 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167
3168 int padding = -1;
3169
3170 int viewFlagValues = 0;
3171 int viewFlagMasks = 0;
3172
3173 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07003174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 int x = 0;
3176 int y = 0;
3177
Chet Haase73066682010-11-29 15:55:32 -08003178 float tx = 0;
3179 float ty = 0;
3180 float rotation = 0;
3181 float rotationX = 0;
3182 float rotationY = 0;
3183 float sx = 1f;
3184 float sy = 1f;
3185 boolean transformSet = false;
3186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
3188
Adam Powell637d3372010-08-25 14:37:03 -07003189 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 final int N = a.getIndexCount();
3191 for (int i = 0; i < N; i++) {
3192 int attr = a.getIndex(i);
3193 switch (attr) {
3194 case com.android.internal.R.styleable.View_background:
3195 background = a.getDrawable(attr);
3196 break;
3197 case com.android.internal.R.styleable.View_padding:
3198 padding = a.getDimensionPixelSize(attr, -1);
3199 break;
3200 case com.android.internal.R.styleable.View_paddingLeft:
3201 leftPadding = a.getDimensionPixelSize(attr, -1);
3202 break;
3203 case com.android.internal.R.styleable.View_paddingTop:
3204 topPadding = a.getDimensionPixelSize(attr, -1);
3205 break;
3206 case com.android.internal.R.styleable.View_paddingRight:
3207 rightPadding = a.getDimensionPixelSize(attr, -1);
3208 break;
3209 case com.android.internal.R.styleable.View_paddingBottom:
3210 bottomPadding = a.getDimensionPixelSize(attr, -1);
3211 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003212 case com.android.internal.R.styleable.View_paddingStart:
3213 startPadding = a.getDimensionPixelSize(attr, -1);
3214 break;
3215 case com.android.internal.R.styleable.View_paddingEnd:
3216 endPadding = a.getDimensionPixelSize(attr, -1);
3217 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 case com.android.internal.R.styleable.View_scrollX:
3219 x = a.getDimensionPixelOffset(attr, 0);
3220 break;
3221 case com.android.internal.R.styleable.View_scrollY:
3222 y = a.getDimensionPixelOffset(attr, 0);
3223 break;
Chet Haase73066682010-11-29 15:55:32 -08003224 case com.android.internal.R.styleable.View_alpha:
3225 setAlpha(a.getFloat(attr, 1f));
3226 break;
3227 case com.android.internal.R.styleable.View_transformPivotX:
3228 setPivotX(a.getDimensionPixelOffset(attr, 0));
3229 break;
3230 case com.android.internal.R.styleable.View_transformPivotY:
3231 setPivotY(a.getDimensionPixelOffset(attr, 0));
3232 break;
3233 case com.android.internal.R.styleable.View_translationX:
3234 tx = a.getDimensionPixelOffset(attr, 0);
3235 transformSet = true;
3236 break;
3237 case com.android.internal.R.styleable.View_translationY:
3238 ty = a.getDimensionPixelOffset(attr, 0);
3239 transformSet = true;
3240 break;
3241 case com.android.internal.R.styleable.View_rotation:
3242 rotation = a.getFloat(attr, 0);
3243 transformSet = true;
3244 break;
3245 case com.android.internal.R.styleable.View_rotationX:
3246 rotationX = a.getFloat(attr, 0);
3247 transformSet = true;
3248 break;
3249 case com.android.internal.R.styleable.View_rotationY:
3250 rotationY = a.getFloat(attr, 0);
3251 transformSet = true;
3252 break;
3253 case com.android.internal.R.styleable.View_scaleX:
3254 sx = a.getFloat(attr, 1f);
3255 transformSet = true;
3256 break;
3257 case com.android.internal.R.styleable.View_scaleY:
3258 sy = a.getFloat(attr, 1f);
3259 transformSet = true;
3260 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 case com.android.internal.R.styleable.View_id:
3262 mID = a.getResourceId(attr, NO_ID);
3263 break;
3264 case com.android.internal.R.styleable.View_tag:
3265 mTag = a.getText(attr);
3266 break;
3267 case com.android.internal.R.styleable.View_fitsSystemWindows:
3268 if (a.getBoolean(attr, false)) {
3269 viewFlagValues |= FITS_SYSTEM_WINDOWS;
3270 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
3271 }
3272 break;
3273 case com.android.internal.R.styleable.View_focusable:
3274 if (a.getBoolean(attr, false)) {
3275 viewFlagValues |= FOCUSABLE;
3276 viewFlagMasks |= FOCUSABLE_MASK;
3277 }
3278 break;
3279 case com.android.internal.R.styleable.View_focusableInTouchMode:
3280 if (a.getBoolean(attr, false)) {
3281 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
3282 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
3283 }
3284 break;
3285 case com.android.internal.R.styleable.View_clickable:
3286 if (a.getBoolean(attr, false)) {
3287 viewFlagValues |= CLICKABLE;
3288 viewFlagMasks |= CLICKABLE;
3289 }
3290 break;
3291 case com.android.internal.R.styleable.View_longClickable:
3292 if (a.getBoolean(attr, false)) {
3293 viewFlagValues |= LONG_CLICKABLE;
3294 viewFlagMasks |= LONG_CLICKABLE;
3295 }
3296 break;
3297 case com.android.internal.R.styleable.View_saveEnabled:
3298 if (!a.getBoolean(attr, true)) {
3299 viewFlagValues |= SAVE_DISABLED;
3300 viewFlagMasks |= SAVE_DISABLED_MASK;
3301 }
3302 break;
3303 case com.android.internal.R.styleable.View_duplicateParentState:
3304 if (a.getBoolean(attr, false)) {
3305 viewFlagValues |= DUPLICATE_PARENT_STATE;
3306 viewFlagMasks |= DUPLICATE_PARENT_STATE;
3307 }
3308 break;
3309 case com.android.internal.R.styleable.View_visibility:
3310 final int visibility = a.getInt(attr, 0);
3311 if (visibility != 0) {
3312 viewFlagValues |= VISIBILITY_FLAGS[visibility];
3313 viewFlagMasks |= VISIBILITY_MASK;
3314 }
3315 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003316 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003317 // Clear any layout direction flags (included resolved bits) already set
3318 mPrivateFlags2 &= ~(LAYOUT_DIRECTION_MASK | LAYOUT_DIRECTION_RESOLVED_MASK);
3319 // Set the layout direction flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07003320 final int layoutDirection = a.getInt(attr, -1);
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07003321 final int value = (layoutDirection != -1) ?
3322 LAYOUT_DIRECTION_FLAGS[layoutDirection] : LAYOUT_DIRECTION_DEFAULT;
3323 mPrivateFlags2 |= (value << LAYOUT_DIRECTION_MASK_SHIFT);
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07003324 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 case com.android.internal.R.styleable.View_drawingCacheQuality:
3326 final int cacheQuality = a.getInt(attr, 0);
3327 if (cacheQuality != 0) {
3328 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
3329 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
3330 }
3331 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07003332 case com.android.internal.R.styleable.View_contentDescription:
3333 mContentDescription = a.getString(attr);
3334 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 case com.android.internal.R.styleable.View_soundEffectsEnabled:
3336 if (!a.getBoolean(attr, true)) {
3337 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
3338 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
3339 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003340 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
3342 if (!a.getBoolean(attr, true)) {
3343 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
3344 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
3345 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07003346 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 case R.styleable.View_scrollbars:
3348 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
3349 if (scrollbars != SCROLLBARS_NONE) {
3350 viewFlagValues |= scrollbars;
3351 viewFlagMasks |= SCROLLBARS_MASK;
3352 initializeScrollbars(a);
3353 }
3354 break;
Romain Guy2a9fa892011-09-28 16:50:02 -07003355 //noinspection deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 case R.styleable.View_fadingEdge:
Romain Guy1ef3fdb2011-09-09 15:30:30 -07003357 if (context.getApplicationInfo().targetSdkVersion >= ICE_CREAM_SANDWICH) {
3358 // Ignore the attribute starting with ICS
3359 break;
3360 }
3361 // With builds < ICS, fall through and apply fading edges
3362 case R.styleable.View_requiresFadingEdge:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
3364 if (fadingEdge != FADING_EDGE_NONE) {
3365 viewFlagValues |= fadingEdge;
3366 viewFlagMasks |= FADING_EDGE_MASK;
3367 initializeFadingEdge(a);
3368 }
3369 break;
3370 case R.styleable.View_scrollbarStyle:
3371 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
3372 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3373 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
3374 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
3375 }
3376 break;
3377 case R.styleable.View_isScrollContainer:
3378 setScrollContainer = true;
3379 if (a.getBoolean(attr, false)) {
3380 setScrollContainer(true);
3381 }
3382 break;
3383 case com.android.internal.R.styleable.View_keepScreenOn:
3384 if (a.getBoolean(attr, false)) {
3385 viewFlagValues |= KEEP_SCREEN_ON;
3386 viewFlagMasks |= KEEP_SCREEN_ON;
3387 }
3388 break;
Jeff Brown85a31762010-09-01 17:01:00 -07003389 case R.styleable.View_filterTouchesWhenObscured:
3390 if (a.getBoolean(attr, false)) {
3391 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
3392 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
3393 }
3394 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 case R.styleable.View_nextFocusLeft:
3396 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
3397 break;
3398 case R.styleable.View_nextFocusRight:
3399 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
3400 break;
3401 case R.styleable.View_nextFocusUp:
3402 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
3403 break;
3404 case R.styleable.View_nextFocusDown:
3405 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
3406 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08003407 case R.styleable.View_nextFocusForward:
3408 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
3409 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 case R.styleable.View_minWidth:
3411 mMinWidth = a.getDimensionPixelSize(attr, 0);
3412 break;
3413 case R.styleable.View_minHeight:
3414 mMinHeight = a.getDimensionPixelSize(attr, 0);
3415 break;
Romain Guy9a817362009-05-01 10:57:14 -07003416 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07003417 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08003418 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07003419 + "be used within a restricted context");
3420 }
3421
Romain Guy9a817362009-05-01 10:57:14 -07003422 final String handlerName = a.getString(attr);
3423 if (handlerName != null) {
3424 setOnClickListener(new OnClickListener() {
3425 private Method mHandler;
3426
3427 public void onClick(View v) {
3428 if (mHandler == null) {
3429 try {
3430 mHandler = getContext().getClass().getMethod(handlerName,
3431 View.class);
3432 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08003433 int id = getId();
3434 String idText = id == NO_ID ? "" : " with id '"
3435 + getContext().getResources().getResourceEntryName(
3436 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07003437 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08003438 handlerName + "(View) in the activity "
3439 + getContext().getClass() + " for onClick handler"
3440 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07003441 }
3442 }
3443
3444 try {
3445 mHandler.invoke(getContext(), View.this);
3446 } catch (IllegalAccessException e) {
3447 throw new IllegalStateException("Could not execute non "
3448 + "public method of the activity", e);
3449 } catch (InvocationTargetException e) {
3450 throw new IllegalStateException("Could not execute "
3451 + "method of the activity", e);
3452 }
3453 }
3454 });
3455 }
3456 break;
Adam Powell637d3372010-08-25 14:37:03 -07003457 case R.styleable.View_overScrollMode:
3458 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3459 break;
Adam Powell20232d02010-12-08 21:08:53 -08003460 case R.styleable.View_verticalScrollbarPosition:
3461 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3462 break;
Romain Guy171c5922011-01-06 10:04:23 -08003463 case R.styleable.View_layerType:
3464 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3465 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003466 case R.styleable.View_textDirection:
Fabrice Di Megliob934db72012-03-20 14:33:01 -07003467 // Clear any text direction flag already set
3468 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
3469 // Set the text direction flags depending on the value of the attribute
3470 final int textDirection = a.getInt(attr, -1);
3471 if (textDirection != -1) {
3472 mPrivateFlags2 |= TEXT_DIRECTION_FLAGS[textDirection];
3473 }
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003474 break;
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -07003475 case R.styleable.View_textAlignment:
3476 // Clear any text alignment flag already set
3477 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
3478 // Set the text alignment flag depending on the value of the attribute
3479 final int textAlignment = a.getInt(attr, TEXT_ALIGNMENT_DEFAULT);
3480 mPrivateFlags2 |= TEXT_ALIGNMENT_FLAGS[textAlignment];
3481 break;
Svetoslav Ganov42138042012-03-20 11:51:39 -07003482 case R.styleable.View_importantForAccessibility:
3483 setImportantForAccessibility(a.getInt(attr,
3484 IMPORTANT_FOR_ACCESSIBILITY_DEFAULT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 }
3486 }
3487
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003488 a.recycle();
3489
Adam Powell637d3372010-08-25 14:37:03 -07003490 setOverScrollMode(overScrollMode);
3491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 if (background != null) {
Philip Milne6c8ea062012-04-03 17:38:43 -07003493 setBackground(background);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003496 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3497 // layout direction). Those cached values will be used later during padding resolution.
3498 mUserPaddingStart = startPadding;
3499 mUserPaddingEnd = endPadding;
3500
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003501 updateUserPaddingRelative();
3502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 if (padding >= 0) {
3504 leftPadding = padding;
3505 topPadding = padding;
3506 rightPadding = padding;
3507 bottomPadding = padding;
3508 }
3509
3510 // If the user specified the padding (either with android:padding or
3511 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3512 // use the default padding or the padding from the background drawable
3513 // (stored at this point in mPadding*)
3514 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3515 topPadding >= 0 ? topPadding : mPaddingTop,
3516 rightPadding >= 0 ? rightPadding : mPaddingRight,
3517 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3518
3519 if (viewFlagMasks != 0) {
3520 setFlags(viewFlagValues, viewFlagMasks);
3521 }
3522
3523 // Needs to be called after mViewFlags is set
3524 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3525 recomputePadding();
3526 }
3527
3528 if (x != 0 || y != 0) {
3529 scrollTo(x, y);
3530 }
3531
Chet Haase73066682010-11-29 15:55:32 -08003532 if (transformSet) {
3533 setTranslationX(tx);
3534 setTranslationY(ty);
3535 setRotation(rotation);
3536 setRotationX(rotationX);
3537 setRotationY(rotationY);
3538 setScaleX(sx);
3539 setScaleY(sy);
3540 }
3541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3543 setScrollContainer(true);
3544 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003545
3546 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
3548
Fabrice Di Meglio509708d2012-03-06 15:41:11 -08003549 private void updateUserPaddingRelative() {
3550 mUserPaddingRelative = (mUserPaddingStart >= 0 || mUserPaddingEnd >= 0);
3551 }
3552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 /**
3554 * Non-public constructor for use in testing
3555 */
3556 View() {
Dianne Hackbornab0f4852011-09-12 16:59:06 -07003557 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 }
3559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 /**
3561 * <p>
3562 * Initializes the fading edges from a given set of styled attributes. This
3563 * method should be called by subclasses that need fading edges and when an
3564 * instance of these subclasses is created programmatically rather than
3565 * being inflated from XML. This method is automatically called when the XML
3566 * is inflated.
3567 * </p>
3568 *
3569 * @param a the styled attributes set to initialize the fading edges from
3570 */
3571 protected void initializeFadingEdge(TypedArray a) {
3572 initScrollCache();
3573
3574 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3575 R.styleable.View_fadingEdgeLength,
3576 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3577 }
3578
3579 /**
3580 * Returns the size of the vertical faded edges used to indicate that more
3581 * content in this view is visible.
3582 *
3583 * @return The size in pixels of the vertical faded edge or 0 if vertical
3584 * faded edges are not enabled for this view.
3585 * @attr ref android.R.styleable#View_fadingEdgeLength
3586 */
3587 public int getVerticalFadingEdgeLength() {
3588 if (isVerticalFadingEdgeEnabled()) {
3589 ScrollabilityCache cache = mScrollCache;
3590 if (cache != null) {
3591 return cache.fadingEdgeLength;
3592 }
3593 }
3594 return 0;
3595 }
3596
3597 /**
3598 * Set the size of the faded edge used to indicate that more content in this
3599 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003600 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3601 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3602 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 *
3604 * @param length The size in pixels of the faded edge used to indicate that more
3605 * content in this view is visible.
3606 */
3607 public void setFadingEdgeLength(int length) {
3608 initScrollCache();
3609 mScrollCache.fadingEdgeLength = length;
3610 }
3611
3612 /**
3613 * Returns the size of the horizontal faded edges used to indicate that more
3614 * content in this view is visible.
3615 *
3616 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3617 * faded edges are not enabled for this view.
3618 * @attr ref android.R.styleable#View_fadingEdgeLength
3619 */
3620 public int getHorizontalFadingEdgeLength() {
3621 if (isHorizontalFadingEdgeEnabled()) {
3622 ScrollabilityCache cache = mScrollCache;
3623 if (cache != null) {
3624 return cache.fadingEdgeLength;
3625 }
3626 }
3627 return 0;
3628 }
3629
3630 /**
3631 * Returns the width of the vertical scrollbar.
3632 *
3633 * @return The width in pixels of the vertical scrollbar or 0 if there
3634 * is no vertical scrollbar.
3635 */
3636 public int getVerticalScrollbarWidth() {
3637 ScrollabilityCache cache = mScrollCache;
3638 if (cache != null) {
3639 ScrollBarDrawable scrollBar = cache.scrollBar;
3640 if (scrollBar != null) {
3641 int size = scrollBar.getSize(true);
3642 if (size <= 0) {
3643 size = cache.scrollBarSize;
3644 }
3645 return size;
3646 }
3647 return 0;
3648 }
3649 return 0;
3650 }
3651
3652 /**
3653 * Returns the height of the horizontal scrollbar.
3654 *
3655 * @return The height in pixels of the horizontal scrollbar or 0 if
3656 * there is no horizontal scrollbar.
3657 */
3658 protected int getHorizontalScrollbarHeight() {
3659 ScrollabilityCache cache = mScrollCache;
3660 if (cache != null) {
3661 ScrollBarDrawable scrollBar = cache.scrollBar;
3662 if (scrollBar != null) {
3663 int size = scrollBar.getSize(false);
3664 if (size <= 0) {
3665 size = cache.scrollBarSize;
3666 }
3667 return size;
3668 }
3669 return 0;
3670 }
3671 return 0;
3672 }
3673
3674 /**
3675 * <p>
3676 * Initializes the scrollbars from a given set of styled attributes. This
3677 * method should be called by subclasses that need scrollbars and when an
3678 * instance of these subclasses is created programmatically rather than
3679 * being inflated from XML. This method is automatically called when the XML
3680 * is inflated.
3681 * </p>
3682 *
3683 * @param a the styled attributes set to initialize the scrollbars from
3684 */
3685 protected void initializeScrollbars(TypedArray a) {
3686 initScrollCache();
3687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003689
Mike Cleronf116bf82009-09-27 19:14:12 -07003690 if (scrollabilityCache.scrollBar == null) {
3691 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3692 }
Joe Malin32736f02011-01-19 16:14:20 -08003693
Romain Guy8bda2482010-03-02 11:42:11 -08003694 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695
Mike Cleronf116bf82009-09-27 19:14:12 -07003696 if (!fadeScrollbars) {
3697 scrollabilityCache.state = ScrollabilityCache.ON;
3698 }
3699 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003700
3701
Mike Cleronf116bf82009-09-27 19:14:12 -07003702 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3703 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3704 .getScrollBarFadeDuration());
3705 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3706 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3707 ViewConfiguration.getScrollDefaultDelay());
3708
Joe Malin32736f02011-01-19 16:14:20 -08003709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3711 com.android.internal.R.styleable.View_scrollbarSize,
3712 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3713
3714 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3715 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3716
3717 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3718 if (thumb != null) {
3719 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3720 }
3721
3722 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3723 false);
3724 if (alwaysDraw) {
3725 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3726 }
3727
3728 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3729 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3730
3731 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3732 if (thumb != null) {
3733 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3734 }
3735
3736 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3737 false);
3738 if (alwaysDraw) {
3739 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3740 }
3741
3742 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003743 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003744 }
3745
3746 /**
3747 * <p>
3748 * Initalizes the scrollability cache if necessary.
3749 * </p>
3750 */
3751 private void initScrollCache() {
3752 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003753 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 }
3755 }
3756
Philip Milne6c8ea062012-04-03 17:38:43 -07003757 private ScrollabilityCache getScrollCache() {
3758 initScrollCache();
3759 return mScrollCache;
3760 }
3761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 /**
Adam Powell20232d02010-12-08 21:08:53 -08003763 * Set the position of the vertical scroll bar. Should be one of
3764 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3765 * {@link #SCROLLBAR_POSITION_RIGHT}.
3766 *
3767 * @param position Where the vertical scroll bar should be positioned.
3768 */
3769 public void setVerticalScrollbarPosition(int position) {
3770 if (mVerticalScrollbarPosition != position) {
3771 mVerticalScrollbarPosition = position;
3772 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003773 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003774 }
3775 }
3776
3777 /**
3778 * @return The position where the vertical scroll bar will show, if applicable.
3779 * @see #setVerticalScrollbarPosition(int)
3780 */
3781 public int getVerticalScrollbarPosition() {
3782 return mVerticalScrollbarPosition;
3783 }
3784
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003785 ListenerInfo getListenerInfo() {
3786 if (mListenerInfo != null) {
3787 return mListenerInfo;
3788 }
3789 mListenerInfo = new ListenerInfo();
3790 return mListenerInfo;
3791 }
3792
Adam Powell20232d02010-12-08 21:08:53 -08003793 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 * Register a callback to be invoked when focus of this view changed.
3795 *
3796 * @param l The callback that will run.
3797 */
3798 public void setOnFocusChangeListener(OnFocusChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003799 getListenerInfo().mOnFocusChangeListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003800 }
3801
3802 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003803 * Add a listener that will be called when the bounds of the view change due to
3804 * layout processing.
3805 *
3806 * @param listener The listener that will be called when layout bounds change.
3807 */
3808 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003809 ListenerInfo li = getListenerInfo();
3810 if (li.mOnLayoutChangeListeners == null) {
3811 li.mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
Chet Haase21cd1382010-09-01 17:42:29 -07003812 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003813 if (!li.mOnLayoutChangeListeners.contains(listener)) {
3814 li.mOnLayoutChangeListeners.add(listener);
Chet Haase1a76dcd2011-10-06 11:16:40 -07003815 }
Chet Haase21cd1382010-09-01 17:42:29 -07003816 }
3817
3818 /**
3819 * Remove a listener for layout changes.
3820 *
3821 * @param listener The listener for layout bounds change.
3822 */
3823 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003824 ListenerInfo li = mListenerInfo;
3825 if (li == null || li.mOnLayoutChangeListeners == null) {
Chet Haase21cd1382010-09-01 17:42:29 -07003826 return;
3827 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003828 li.mOnLayoutChangeListeners.remove(listener);
Chet Haase21cd1382010-09-01 17:42:29 -07003829 }
3830
3831 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003832 * Add a listener for attach state changes.
3833 *
3834 * This listener will be called whenever this view is attached or detached
3835 * from a window. Remove the listener using
3836 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3837 *
3838 * @param listener Listener to attach
3839 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3840 */
3841 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003842 ListenerInfo li = getListenerInfo();
3843 if (li.mOnAttachStateChangeListeners == null) {
3844 li.mOnAttachStateChangeListeners
3845 = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
Adam Powell4afd62b2011-02-18 15:02:18 -08003846 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003847 li.mOnAttachStateChangeListeners.add(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003848 }
3849
3850 /**
3851 * Remove a listener for attach state changes. The listener will receive no further
3852 * notification of window attach/detach events.
3853 *
3854 * @param listener Listener to remove
3855 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3856 */
3857 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003858 ListenerInfo li = mListenerInfo;
3859 if (li == null || li.mOnAttachStateChangeListeners == null) {
Adam Powell4afd62b2011-02-18 15:02:18 -08003860 return;
3861 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003862 li.mOnAttachStateChangeListeners.remove(listener);
Adam Powell4afd62b2011-02-18 15:02:18 -08003863 }
3864
3865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 * Returns the focus-change callback registered for this view.
3867 *
3868 * @return The callback, or null if one is not registered.
3869 */
3870 public OnFocusChangeListener getOnFocusChangeListener() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003871 ListenerInfo li = mListenerInfo;
3872 return li != null ? li.mOnFocusChangeListener : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 }
3874
3875 /**
3876 * Register a callback to be invoked when this view is clicked. If this view is not
3877 * clickable, it becomes clickable.
3878 *
3879 * @param l The callback that will run
3880 *
3881 * @see #setClickable(boolean)
3882 */
3883 public void setOnClickListener(OnClickListener l) {
3884 if (!isClickable()) {
3885 setClickable(true);
3886 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003887 getListenerInfo().mOnClickListener = l;
3888 }
3889
3890 /**
3891 * Return whether this view has an attached OnClickListener. Returns
3892 * true if there is a listener, false if there is none.
3893 */
3894 public boolean hasOnClickListeners() {
3895 ListenerInfo li = mListenerInfo;
3896 return (li != null && li.mOnClickListener != null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 }
3898
3899 /**
3900 * Register a callback to be invoked when this view is clicked and held. If this view is not
3901 * long clickable, it becomes long clickable.
3902 *
3903 * @param l The callback that will run
3904 *
3905 * @see #setLongClickable(boolean)
3906 */
3907 public void setOnLongClickListener(OnLongClickListener l) {
3908 if (!isLongClickable()) {
3909 setLongClickable(true);
3910 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003911 getListenerInfo().mOnLongClickListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 }
3913
3914 /**
3915 * Register a callback to be invoked when the context menu for this view is
3916 * being built. If this view is not long clickable, it becomes long clickable.
3917 *
3918 * @param l The callback that will run
3919 *
3920 */
3921 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3922 if (!isLongClickable()) {
3923 setLongClickable(true);
3924 }
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003925 getListenerInfo().mOnCreateContextMenuListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 }
3927
3928 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003929 * Call this view's OnClickListener, if it is defined. Performs all normal
3930 * actions associated with clicking: reporting accessibility event, playing
3931 * a sound, etc.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003932 *
3933 * @return True there was an assigned OnClickListener that was called, false
3934 * otherwise is returned.
3935 */
3936 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003937 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3938
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003939 ListenerInfo li = mListenerInfo;
3940 if (li != null && li.mOnClickListener != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 playSoundEffect(SoundEffectConstants.CLICK);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003942 li.mOnClickListener.onClick(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 return true;
3944 }
3945
3946 return false;
3947 }
3948
3949 /**
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003950 * Directly call any attached OnClickListener. Unlike {@link #performClick()},
3951 * this only calls the listener, and does not do any associated clicking
3952 * actions like reporting an accessibility event.
3953 *
3954 * @return True there was an assigned OnClickListener that was called, false
3955 * otherwise is returned.
3956 */
3957 public boolean callOnClick() {
3958 ListenerInfo li = mListenerInfo;
3959 if (li != null && li.mOnClickListener != null) {
3960 li.mOnClickListener.onClick(this);
3961 return true;
3962 }
3963 return false;
3964 }
3965
3966 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003967 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3968 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003970 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 */
3972 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003973 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 boolean handled = false;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003976 ListenerInfo li = mListenerInfo;
3977 if (li != null && li.mOnLongClickListener != null) {
3978 handled = li.mOnLongClickListener.onLongClick(View.this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 }
3980 if (!handled) {
3981 handled = showContextMenu();
3982 }
3983 if (handled) {
3984 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3985 }
3986 return handled;
3987 }
3988
3989 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003990 * Performs button-related actions during a touch down event.
3991 *
3992 * @param event The event.
3993 * @return True if the down was consumed.
3994 *
3995 * @hide
3996 */
3997 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3998 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3999 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
4000 return true;
4001 }
4002 }
4003 return false;
4004 }
4005
4006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 * Bring up the context menu for this view.
4008 *
4009 * @return Whether a context menu was displayed.
4010 */
4011 public boolean showContextMenu() {
4012 return getParent().showContextMenuForChild(this);
4013 }
4014
4015 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004016 * Bring up the context menu for this view, referring to the item under the specified point.
4017 *
4018 * @param x The referenced x coordinate.
4019 * @param y The referenced y coordinate.
4020 * @param metaState The keyboard modifiers that were pressed.
4021 * @return Whether a context menu was displayed.
4022 *
4023 * @hide
4024 */
4025 public boolean showContextMenu(float x, float y, int metaState) {
4026 return showContextMenu();
4027 }
4028
4029 /**
Adam Powell6e346362010-07-23 10:18:23 -07004030 * Start an action mode.
4031 *
4032 * @param callback Callback that will control the lifecycle of the action mode
4033 * @return The new action mode if it is started, null otherwise
4034 *
4035 * @see ActionMode
4036 */
4037 public ActionMode startActionMode(ActionMode.Callback callback) {
John Reck5160e2a2012-02-22 09:35:12 -08004038 ViewParent parent = getParent();
4039 if (parent == null) return null;
4040 return parent.startActionModeForChild(this, callback);
Adam Powell6e346362010-07-23 10:18:23 -07004041 }
4042
4043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 * Register a callback to be invoked when a key is pressed in this view.
4045 * @param l the key listener to attach to this view
4046 */
4047 public void setOnKeyListener(OnKeyListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004048 getListenerInfo().mOnKeyListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 }
4050
4051 /**
4052 * Register a callback to be invoked when a touch event is sent to this view.
4053 * @param l the touch listener to attach to this view
4054 */
4055 public void setOnTouchListener(OnTouchListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004056 getListenerInfo().mOnTouchListener = l;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 }
4058
4059 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004060 * Register a callback to be invoked when a generic motion event is sent to this view.
4061 * @param l the generic motion listener to attach to this view
4062 */
4063 public void setOnGenericMotionListener(OnGenericMotionListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004064 getListenerInfo().mOnGenericMotionListener = l;
Jeff Brown33bbfd22011-02-24 20:55:35 -08004065 }
4066
4067 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07004068 * Register a callback to be invoked when a hover event is sent to this view.
4069 * @param l the hover listener to attach to this view
4070 */
4071 public void setOnHoverListener(OnHoverListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004072 getListenerInfo().mOnHoverListener = l;
Jeff Brown53ca3f12011-06-27 18:36:00 -07004073 }
4074
4075 /**
Joe Malin32736f02011-01-19 16:14:20 -08004076 * Register a drag event listener callback object for this View. The parameter is
4077 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
4078 * View, the system calls the
4079 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
4080 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07004081 */
4082 public void setOnDragListener(OnDragListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004083 getListenerInfo().mOnDragListener = l;
Chris Tate32affef2010-10-18 15:29:21 -07004084 }
4085
4086 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07004087 * Give this view focus. This will cause
4088 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 *
4090 * Note: this does not check whether this {@link View} should get focus, it just
4091 * gives it focus no matter what. It should only be called internally by framework
4092 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
4093 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004094 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
4095 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 * focus moved when requestFocus() is called. It may not always
4097 * apply, in which case use the default View.FOCUS_DOWN.
4098 * @param previouslyFocusedRect The rectangle of the view that had focus
4099 * prior in this View's coordinate system.
4100 */
4101 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
4102 if (DBG) {
4103 System.out.println(this + " requestFocus()");
4104 }
4105
4106 if ((mPrivateFlags & FOCUSED) == 0) {
4107 mPrivateFlags |= FOCUSED;
4108
4109 if (mParent != null) {
4110 mParent.requestChildFocus(this, this);
4111 }
4112
4113 onFocusChanged(true, direction, previouslyFocusedRect);
4114 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004115
4116 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4117 notifyAccessibilityStateChanged();
4118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 }
4120 }
4121
4122 /**
4123 * Request that a rectangle of this view be visible on the screen,
4124 * scrolling if necessary just enough.
4125 *
4126 * <p>A View should call this if it maintains some notion of which part
4127 * of its content is interesting. For example, a text editing view
4128 * should call this when its cursor moves.
4129 *
4130 * @param rectangle The rectangle.
4131 * @return Whether any parent scrolled.
4132 */
4133 public boolean requestRectangleOnScreen(Rect rectangle) {
4134 return requestRectangleOnScreen(rectangle, false);
4135 }
4136
4137 /**
4138 * Request that a rectangle of this view be visible on the screen,
4139 * scrolling if necessary just enough.
4140 *
4141 * <p>A View should call this if it maintains some notion of which part
4142 * of its content is interesting. For example, a text editing view
4143 * should call this when its cursor moves.
4144 *
4145 * <p>When <code>immediate</code> is set to true, scrolling will not be
4146 * animated.
4147 *
4148 * @param rectangle The rectangle.
4149 * @param immediate True to forbid animated scrolling, false otherwise
4150 * @return Whether any parent scrolled.
4151 */
4152 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
4153 View child = this;
4154 ViewParent parent = mParent;
4155 boolean scrolled = false;
4156 while (parent != null) {
4157 scrolled |= parent.requestChildRectangleOnScreen(child,
4158 rectangle, immediate);
4159
4160 // offset rect so next call has the rectangle in the
4161 // coordinate system of its direct child.
4162 rectangle.offset(child.getLeft(), child.getTop());
4163 rectangle.offset(-child.getScrollX(), -child.getScrollY());
4164
4165 if (!(parent instanceof View)) {
4166 break;
4167 }
Romain Guy8506ab42009-06-11 17:35:47 -07004168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 child = (View) parent;
4170 parent = child.getParent();
4171 }
4172 return scrolled;
4173 }
4174
4175 /**
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004176 * Called when this view wants to give up focus. If focus is cleared
4177 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} is called.
4178 * <p>
4179 * <strong>Note:</strong> When a View clears focus the framework is trying
4180 * to give focus to the first focusable View from the top. Hence, if this
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004181 * View is the first from the top that can take focus, then all callbacks
4182 * related to clearing focus will be invoked after wich the framework will
4183 * give focus to this view.
Svetoslav Ganov13fd5612012-02-01 17:01:12 -08004184 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 */
4186 public void clearFocus() {
4187 if (DBG) {
4188 System.out.println(this + " clearFocus()");
4189 }
4190
4191 if ((mPrivateFlags & FOCUSED) != 0) {
4192 mPrivateFlags &= ~FOCUSED;
4193
4194 if (mParent != null) {
4195 mParent.clearChildFocus(this);
4196 }
4197
4198 onFocusChanged(false, 0, null);
Svetoslav Ganov42138042012-03-20 11:51:39 -07004199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 refreshDrawableState();
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004201
4202 ensureInputFocusOnFirstFocusable();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004203
4204 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4205 notifyAccessibilityStateChanged();
4206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
4208 }
4209
Svetoslav Ganov57cadf22012-04-04 16:44:39 -07004210 void ensureInputFocusOnFirstFocusable() {
4211 View root = getRootView();
4212 if (root != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004213 root.requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 }
4215 }
4216
4217 /**
4218 * Called internally by the view system when a new view is getting focus.
4219 * This is what clears the old focus.
4220 */
4221 void unFocus() {
4222 if (DBG) {
4223 System.out.println(this + " unFocus()");
4224 }
4225
4226 if ((mPrivateFlags & FOCUSED) != 0) {
4227 mPrivateFlags &= ~FOCUSED;
4228
4229 onFocusChanged(false, 0, null);
4230 refreshDrawableState();
Svetoslav Ganov42138042012-03-20 11:51:39 -07004231
4232 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4233 notifyAccessibilityStateChanged();
4234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 }
4236 }
4237
4238 /**
4239 * Returns true if this view has focus iteself, or is the ancestor of the
4240 * view that has focus.
4241 *
4242 * @return True if this view has or contains focus, false otherwise.
4243 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004244 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 public boolean hasFocus() {
4246 return (mPrivateFlags & FOCUSED) != 0;
4247 }
4248
4249 /**
4250 * Returns true if this view is focusable or if it contains a reachable View
4251 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
4252 * is a View whose parents do not block descendants focus.
4253 *
4254 * Only {@link #VISIBLE} views are considered focusable.
4255 *
4256 * @return True if the view is focusable or if the view contains a focusable
4257 * View, false otherwise.
4258 *
4259 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
4260 */
4261 public boolean hasFocusable() {
4262 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
4263 }
4264
4265 /**
4266 * Called by the view system when the focus state of this view changes.
4267 * When the focus change event is caused by directional navigation, direction
4268 * and previouslyFocusedRect provide insight into where the focus is coming from.
4269 * When overriding, be sure to call up through to the super class so that
4270 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07004271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272 * @param gainFocus True if the View has focus; false otherwise.
4273 * @param direction The direction focus has moved when requestFocus()
4274 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08004275 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
4276 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
4277 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 * @param previouslyFocusedRect The rectangle, in this view's coordinate
4279 * system, of the previously focused view. If applicable, this will be
4280 * passed in as finer grained information about where the focus is coming
4281 * from (in addition to direction). Will be <code>null</code> otherwise.
4282 */
4283 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004284 if (gainFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004285 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4286 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
4287 requestAccessibilityFocus();
4288 }
svetoslavganov75986cf2009-05-14 22:28:01 -07004289 }
4290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 InputMethodManager imm = InputMethodManager.peekInstance();
4292 if (!gainFocus) {
4293 if (isPressed()) {
4294 setPressed(false);
4295 }
4296 if (imm != null && mAttachInfo != null
4297 && mAttachInfo.mHasWindowFocus) {
4298 imm.focusOut(this);
4299 }
Romain Guya2431d02009-04-30 16:30:00 -07004300 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 } else if (imm != null && mAttachInfo != null
4302 && mAttachInfo.mHasWindowFocus) {
4303 imm.focusIn(this);
4304 }
Romain Guy8506ab42009-06-11 17:35:47 -07004305
Romain Guy0fd89bf2011-01-26 15:41:30 -08004306 invalidate(true);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004307 ListenerInfo li = mListenerInfo;
4308 if (li != null && li.mOnFocusChangeListener != null) {
4309 li.mOnFocusChangeListener.onFocusChange(this, gainFocus);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 }
Joe Malin32736f02011-01-19 16:14:20 -08004311
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004312 if (mAttachInfo != null) {
4313 mAttachInfo.mKeyDispatchState.reset(this);
4314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 }
4316
4317 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004318 * Sends an accessibility event of the given type. If accessiiblity is
4319 * not enabled this method has no effect. The default implementation calls
4320 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
4321 * to populate information about the event source (this View), then calls
4322 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
4323 * populate the text content of the event source including its descendants,
4324 * and last calls
4325 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
4326 * on its parent to resuest sending of the event to interested parties.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004327 * <p>
4328 * If an {@link AccessibilityDelegate} has been specified via calling
4329 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4330 * {@link AccessibilityDelegate#sendAccessibilityEvent(View, int)} is
4331 * responsible for handling this call.
4332 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004333 *
Scott Mainb303d832011-10-12 16:45:18 -07004334 * @param eventType The type of the event to send, as defined by several types from
4335 * {@link android.view.accessibility.AccessibilityEvent}, such as
4336 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_CLICKED} or
4337 * {@link android.view.accessibility.AccessibilityEvent#TYPE_VIEW_HOVER_ENTER}.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004338 *
4339 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4340 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4341 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004342 * @see AccessibilityDelegate
svetoslavganov75986cf2009-05-14 22:28:01 -07004343 */
4344 public void sendAccessibilityEvent(int eventType) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004345 if (mAccessibilityDelegate != null) {
4346 mAccessibilityDelegate.sendAccessibilityEvent(this, eventType);
4347 } else {
4348 sendAccessibilityEventInternal(eventType);
4349 }
4350 }
4351
4352 /**
Svetoslav Ganov51ab90c2012-03-09 10:54:49 -08004353 * Convenience method for sending a {@link AccessibilityEvent#TYPE_ANNOUNCEMENT}
4354 * {@link AccessibilityEvent} to make an announcement which is related to some
4355 * sort of a context change for which none of the events representing UI transitions
4356 * is a good fit. For example, announcing a new page in a book. If accessibility
4357 * is not enabled this method does nothing.
4358 *
4359 * @param text The announcement text.
4360 */
4361 public void announceForAccessibility(CharSequence text) {
4362 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4363 AccessibilityEvent event = AccessibilityEvent.obtain(
4364 AccessibilityEvent.TYPE_ANNOUNCEMENT);
4365 event.getText().add(text);
4366 sendAccessibilityEventUnchecked(event);
4367 }
4368 }
4369
4370 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004371 * @see #sendAccessibilityEvent(int)
4372 *
4373 * Note: Called from the default {@link AccessibilityDelegate}.
4374 */
4375 void sendAccessibilityEventInternal(int eventType) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004376 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
4377 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
4378 }
4379 }
4380
4381 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004382 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
4383 * takes as an argument an empty {@link AccessibilityEvent} and does not
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004384 * perform a check whether accessibility is enabled.
4385 * <p>
4386 * If an {@link AccessibilityDelegate} has been specified via calling
4387 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4388 * {@link AccessibilityDelegate#sendAccessibilityEventUnchecked(View, AccessibilityEvent)}
4389 * is responsible for handling this call.
4390 * </p>
Svetoslav Ganov30401322011-05-12 18:53:45 -07004391 *
4392 * @param event The event to send.
4393 *
4394 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07004395 */
4396 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004397 if (mAccessibilityDelegate != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004398 mAccessibilityDelegate.sendAccessibilityEventUnchecked(this, event);
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004399 } else {
4400 sendAccessibilityEventUncheckedInternal(event);
4401 }
4402 }
4403
4404 /**
4405 * @see #sendAccessibilityEventUnchecked(AccessibilityEvent)
4406 *
4407 * Note: Called from the default {@link AccessibilityDelegate}.
4408 */
4409 void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08004410 if (!isShown()) {
4411 return;
4412 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07004413 onInitializeAccessibilityEvent(event);
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004414 // Only a subset of accessibility events populates text content.
4415 if ((event.getEventType() & POPULATING_ACCESSIBILITY_EVENT_TYPES) != 0) {
4416 dispatchPopulateAccessibilityEvent(event);
4417 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07004418 // Intercept accessibility focus events fired by virtual nodes to keep
4419 // track of accessibility focus position in such nodes.
4420 final int eventType = event.getEventType();
4421 switch (eventType) {
4422 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: {
4423 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4424 event.getSourceNodeId());
4425 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4426 ViewRootImpl viewRootImpl = getViewRootImpl();
4427 if (viewRootImpl != null) {
4428 viewRootImpl.setAccessibilityFocusedHost(this);
4429 }
4430 }
4431 } break;
4432 case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED: {
4433 final long virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
4434 event.getSourceNodeId());
4435 if (virtualNodeId != AccessibilityNodeInfo.UNDEFINED) {
4436 ViewRootImpl viewRootImpl = getViewRootImpl();
4437 if (viewRootImpl != null) {
4438 viewRootImpl.setAccessibilityFocusedHost(null);
4439 }
4440 }
4441 } break;
4442 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004443 // In the beginning we called #isShown(), so we know that getParent() is not null.
4444 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004445 }
4446
4447 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07004448 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
4449 * to its children for adding their text content to the event. Note that the
4450 * event text is populated in a separate dispatch path since we add to the
4451 * event not only the text of the source but also the text of all its descendants.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004452 * A typical implementation will call
4453 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
4454 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
4455 * on each child. Override this method if custom population of the event text
4456 * content is required.
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004457 * <p>
4458 * If an {@link AccessibilityDelegate} has been specified via calling
4459 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4460 * {@link AccessibilityDelegate#dispatchPopulateAccessibilityEvent(View, AccessibilityEvent)}
4461 * is responsible for handling this call.
4462 * </p>
Svetoslav Ganov82e236d2011-09-29 19:31:06 -07004463 * <p>
4464 * <em>Note:</em> Accessibility events of certain types are not dispatched for
4465 * populating the event text via this method. For details refer to {@link AccessibilityEvent}.
4466 * </p>
svetoslavganov75986cf2009-05-14 22:28:01 -07004467 *
4468 * @param event The event.
4469 *
4470 * @return True if the event population was completed.
4471 */
4472 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004473 if (mAccessibilityDelegate != null) {
4474 return mAccessibilityDelegate.dispatchPopulateAccessibilityEvent(this, event);
4475 } else {
4476 return dispatchPopulateAccessibilityEventInternal(event);
4477 }
4478 }
4479
4480 /**
4481 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4482 *
4483 * Note: Called from the default {@link AccessibilityDelegate}.
4484 */
4485 boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004486 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07004487 return false;
4488 }
4489
4490 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004491 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07004492 * giving a chance to this View to populate the accessibility event with its
Scott Mainb303d832011-10-12 16:45:18 -07004493 * text content. While this method is free to modify event
4494 * attributes other than text content, doing so should normally be performed in
Svetoslav Ganov30401322011-05-12 18:53:45 -07004495 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
4496 * <p>
4497 * Example: Adding formatted date string to an accessibility event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004498 * to the text added by the super implementation:
4499 * <pre> public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov30401322011-05-12 18:53:45 -07004500 * super.onPopulateAccessibilityEvent(event);
4501 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
4502 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
4503 * mCurrentDate.getTimeInMillis(), flags);
4504 * event.getText().add(selectedDateUtterance);
Scott Mainb303d832011-10-12 16:45:18 -07004505 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004506 * <p>
4507 * If an {@link AccessibilityDelegate} has been specified via calling
4508 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4509 * {@link AccessibilityDelegate#onPopulateAccessibilityEvent(View, AccessibilityEvent)}
4510 * is responsible for handling this call.
4511 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004512 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4513 * information to the event, in case the default implementation has basic information to add.
4514 * </p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004515 *
4516 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004517 *
4518 * @see #sendAccessibilityEvent(int)
4519 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004520 */
4521 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004522 if (mAccessibilityDelegate != null) {
4523 mAccessibilityDelegate.onPopulateAccessibilityEvent(this, event);
4524 } else {
4525 onPopulateAccessibilityEventInternal(event);
4526 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07004527 }
4528
4529 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004530 * @see #onPopulateAccessibilityEvent(AccessibilityEvent)
4531 *
4532 * Note: Called from the default {@link AccessibilityDelegate}.
4533 */
4534 void onPopulateAccessibilityEventInternal(AccessibilityEvent event) {
4535
4536 }
4537
4538 /**
4539 * Initializes an {@link AccessibilityEvent} with information about
4540 * this View which is the event source. In other words, the source of
4541 * an accessibility event is the view whose state change triggered firing
4542 * the event.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004543 * <p>
4544 * Example: Setting the password property of an event in addition
Scott Mainb303d832011-10-12 16:45:18 -07004545 * to properties set by the super implementation:
4546 * <pre> public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
4547 * super.onInitializeAccessibilityEvent(event);
4548 * event.setPassword(true);
4549 * }</pre>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004550 * <p>
4551 * If an {@link AccessibilityDelegate} has been specified via calling
4552 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4553 * {@link AccessibilityDelegate#onInitializeAccessibilityEvent(View, AccessibilityEvent)}
4554 * is responsible for handling this call.
4555 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07004556 * <p class="note"><strong>Note:</strong> Always call the super implementation before adding
4557 * information to the event, in case the default implementation has basic information to add.
4558 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004559 * @param event The event to initialize.
Svetoslav Ganov30401322011-05-12 18:53:45 -07004560 *
4561 * @see #sendAccessibilityEvent(int)
4562 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
4563 */
4564 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004565 if (mAccessibilityDelegate != null) {
4566 mAccessibilityDelegate.onInitializeAccessibilityEvent(this, event);
4567 } else {
4568 onInitializeAccessibilityEventInternal(event);
4569 }
4570 }
4571
4572 /**
4573 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
4574 *
4575 * Note: Called from the default {@link AccessibilityDelegate}.
4576 */
4577 void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004578 event.setSource(this);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004579 event.setClassName(View.class.getName());
Svetoslav Ganov30401322011-05-12 18:53:45 -07004580 event.setPackageName(getContext().getPackageName());
4581 event.setEnabled(isEnabled());
4582 event.setContentDescription(mContentDescription);
4583
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004584 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004585 ArrayList<View> focusablesTempList = mAttachInfo.mTempArrayList;
Svetoslav Ganovd9ee72f2011-10-05 22:26:05 -07004586 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
4587 FOCUSABLES_ALL);
4588 event.setItemCount(focusablesTempList.size());
4589 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
4590 focusablesTempList.clear();
Svetoslav Ganov30401322011-05-12 18:53:45 -07004591 }
4592 }
4593
4594 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004595 * Returns an {@link AccessibilityNodeInfo} representing this view from the
4596 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
4597 * This method is responsible for obtaining an accessibility node info from a
4598 * pool of reusable instances and calling
4599 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
4600 * initialize the former.
4601 * <p>
4602 * Note: The client is responsible for recycling the obtained instance by calling
4603 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
4604 * </p>
Svetoslav Ganov02107852011-10-03 17:06:56 -07004605 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004606 * @return A populated {@link AccessibilityNodeInfo}.
4607 *
4608 * @see AccessibilityNodeInfo
4609 */
4610 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
Svetoslav Ganov02107852011-10-03 17:06:56 -07004611 AccessibilityNodeProvider provider = getAccessibilityNodeProvider();
4612 if (provider != null) {
4613 return provider.createAccessibilityNodeInfo(View.NO_ID);
4614 } else {
4615 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
4616 onInitializeAccessibilityNodeInfo(info);
4617 return info;
4618 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004619 }
4620
4621 /**
4622 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
4623 * The base implementation sets:
4624 * <ul>
4625 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004626 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
4627 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004628 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
4629 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
4630 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
4631 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
4632 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
4633 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
4634 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
4635 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
4636 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
4637 * </ul>
4638 * <p>
4639 * Subclasses should override this method, call the super implementation,
4640 * and set additional attributes.
4641 * </p>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004642 * <p>
4643 * If an {@link AccessibilityDelegate} has been specified via calling
4644 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4645 * {@link AccessibilityDelegate#onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)}
4646 * is responsible for handling this call.
4647 * </p>
4648 *
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004649 * @param info The instance to initialize.
4650 */
4651 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004652 if (mAccessibilityDelegate != null) {
4653 mAccessibilityDelegate.onInitializeAccessibilityNodeInfo(this, info);
4654 } else {
4655 onInitializeAccessibilityNodeInfoInternal(info);
4656 }
4657 }
4658
4659 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004660 * Gets the location of this view in screen coordintates.
4661 *
4662 * @param outRect The output location
4663 */
4664 private void getBoundsOnScreen(Rect outRect) {
4665 if (mAttachInfo == null) {
4666 return;
4667 }
4668
4669 RectF position = mAttachInfo.mTmpTransformRect;
4670 position.setEmpty();
4671
4672 if (!hasIdentityMatrix()) {
4673 getMatrix().mapRect(position);
4674 }
4675
4676 position.offset(mLeft, mRight);
4677
4678 ViewParent parent = mParent;
4679 while (parent instanceof View) {
4680 View parentView = (View) parent;
4681
4682 position.offset(-parentView.mScrollX, -parentView.mScrollY);
4683
4684 if (!parentView.hasIdentityMatrix()) {
4685 parentView.getMatrix().mapRect(position);
4686 }
4687
4688 position.offset(parentView.mLeft, parentView.mTop);
4689
4690 parent = parentView.mParent;
4691 }
4692
4693 if (parent instanceof ViewRootImpl) {
4694 ViewRootImpl viewRootImpl = (ViewRootImpl) parent;
4695 position.offset(0, -viewRootImpl.mCurScrollY);
4696 }
4697
4698 position.offset(mAttachInfo.mWindowLeft, mAttachInfo.mWindowTop);
4699
4700 outRect.set((int) (position.left + 0.5f), (int) (position.top + 0.5f),
4701 (int) (position.right + 0.5f), (int) (position.bottom + 0.5f));
4702 }
4703
4704 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004705 * @see #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
4706 *
4707 * Note: Called from the default {@link AccessibilityDelegate}.
4708 */
4709 void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004710 Rect bounds = mAttachInfo.mTmpInvalRect;
4711 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004712 info.setBoundsInParent(bounds);
4713
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004714 getBoundsOnScreen(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07004715 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004716
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004717 if ((mPrivateFlags & IS_ROOT_NAMESPACE) == 0) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004718 ViewParent parent = getParentForAccessibility();
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004719 if (parent instanceof View) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07004720 info.setParent((View) parent);
Svetoslav Ganov57f3b566db2011-10-31 17:59:14 -07004721 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004722 }
4723
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004724 info.setVisibleToUser(isVisibleToUser());
4725
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004726 info.setPackageName(mContext.getPackageName());
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08004727 info.setClassName(View.class.getName());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004728 info.setContentDescription(getContentDescription());
4729
4730 info.setEnabled(isEnabled());
4731 info.setClickable(isClickable());
4732 info.setFocusable(isFocusable());
4733 info.setFocused(isFocused());
Svetoslav Ganov42138042012-03-20 11:51:39 -07004734 info.setAccessibilityFocused(isAccessibilityFocused());
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004735 info.setSelected(isSelected());
4736 info.setLongClickable(isLongClickable());
4737
4738 // TODO: These make sense only if we are in an AdapterView but all
4739 // views can be selected. Maybe from accessiiblity perspective
4740 // we should report as selectable view in an AdapterView.
4741 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
4742 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
4743
4744 if (isFocusable()) {
4745 if (isFocused()) {
4746 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
4747 } else {
4748 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
4749 }
4750 }
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004751
4752 info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
4753 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
4754
4755 if (isClickable()) {
4756 info.addAction(AccessibilityNodeInfo.ACTION_CLICK);
4757 }
4758
4759 if (isLongClickable()) {
4760 info.addAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
4761 }
4762
4763 if (getContentDescription() != null) {
Svetoslav Ganov2b435aa2012-05-04 17:16:37 -07004764 info.addAction(AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
4765 info.addAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
4766 info.setMovementGranularities(AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER
4767 | AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
Svetoslav Ganovcfcff982012-04-28 15:31:09 -07004768 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004769 }
4770
4771 /**
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004772 * Computes whether this view is visible to the user. Such a view is
4773 * attached, visible, all its predecessors are visible, it is not clipped
4774 * entirely by its predecessors, and has an alpha greater than zero.
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004775 *
4776 * @return Whether the view is visible on the screen.
4777 */
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07004778 private boolean isVisibleToUser() {
Svetoslav Ganov749e7962012-04-19 17:13:46 -07004779 // The first two checks are made also made by isShown() which
4780 // however traverses the tree up to the parent to catch that.
4781 // Therefore, we do some fail fast check to minimize the up
4782 // tree traversal.
4783 return (mAttachInfo != null
4784 && mAttachInfo.mWindowVisibility == View.VISIBLE
4785 && getAlpha() > 0
4786 && isShown()
4787 && getGlobalVisibleRect(mAttachInfo.mTmpInvalRect));
4788 }
4789
4790 /**
Svetoslav Ganov031d9c12011-09-09 16:41:13 -07004791 * Sets a delegate for implementing accessibility support via compositon as
4792 * opposed to inheritance. The delegate's primary use is for implementing
4793 * backwards compatible widgets. For more details see {@link AccessibilityDelegate}.
4794 *
4795 * @param delegate The delegate instance.
4796 *
4797 * @see AccessibilityDelegate
4798 */
4799 public void setAccessibilityDelegate(AccessibilityDelegate delegate) {
4800 mAccessibilityDelegate = delegate;
4801 }
4802
4803 /**
Svetoslav Ganov02107852011-10-03 17:06:56 -07004804 * Gets the provider for managing a virtual view hierarchy rooted at this View
4805 * and reported to {@link android.accessibilityservice.AccessibilityService}s
4806 * that explore the window content.
4807 * <p>
4808 * If this method returns an instance, this instance is responsible for managing
4809 * {@link AccessibilityNodeInfo}s describing the virtual sub-tree rooted at this
4810 * View including the one representing the View itself. Similarly the returned
4811 * instance is responsible for performing accessibility actions on any virtual
4812 * view or the root view itself.
4813 * </p>
4814 * <p>
4815 * If an {@link AccessibilityDelegate} has been specified via calling
4816 * {@link #setAccessibilityDelegate(AccessibilityDelegate)} its
4817 * {@link AccessibilityDelegate#getAccessibilityNodeProvider(View)}
4818 * is responsible for handling this call.
4819 * </p>
4820 *
4821 * @return The provider.
4822 *
4823 * @see AccessibilityNodeProvider
4824 */
4825 public AccessibilityNodeProvider getAccessibilityNodeProvider() {
4826 if (mAccessibilityDelegate != null) {
4827 return mAccessibilityDelegate.getAccessibilityNodeProvider(this);
4828 } else {
4829 return null;
4830 }
4831 }
4832
4833 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004834 * Gets the unique identifier of this view on the screen for accessibility purposes.
4835 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
4836 *
4837 * @return The view accessibility id.
4838 *
4839 * @hide
4840 */
4841 public int getAccessibilityViewId() {
4842 if (mAccessibilityViewId == NO_ID) {
4843 mAccessibilityViewId = sNextAccessibilityViewId++;
4844 }
4845 return mAccessibilityViewId;
4846 }
4847
4848 /**
4849 * Gets the unique identifier of the window in which this View reseides.
4850 *
4851 * @return The window accessibility id.
4852 *
4853 * @hide
4854 */
4855 public int getAccessibilityWindowId() {
4856 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4857 }
4858
4859 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004860 * Gets the {@link View} description. It briefly describes the view and is
4861 * primarily used for accessibility support. Set this property to enable
4862 * better accessibility support for your application. This is especially
4863 * true for views that do not have textual representation (For example,
4864 * ImageButton).
4865 *
Svetoslav Ganov42138042012-03-20 11:51:39 -07004866 * @return The content description.
svetoslavganov75986cf2009-05-14 22:28:01 -07004867 *
4868 * @attr ref android.R.styleable#View_contentDescription
4869 */
Svetoslav Ganov42138042012-03-20 11:51:39 -07004870 @ViewDebug.ExportedProperty(category = "accessibility")
svetoslavganov75986cf2009-05-14 22:28:01 -07004871 public CharSequence getContentDescription() {
4872 return mContentDescription;
4873 }
4874
4875 /**
4876 * Sets the {@link View} description. It briefly describes the view and is
4877 * primarily used for accessibility support. Set this property to enable
4878 * better accessibility support for your application. This is especially
4879 * true for views that do not have textual representation (For example,
4880 * ImageButton).
4881 *
4882 * @param contentDescription The content description.
4883 *
4884 * @attr ref android.R.styleable#View_contentDescription
4885 */
Svetoslav Ganove261e282011-10-18 17:47:04 -07004886 @RemotableViewMethod
svetoslavganov75986cf2009-05-14 22:28:01 -07004887 public void setContentDescription(CharSequence contentDescription) {
4888 mContentDescription = contentDescription;
4889 }
4890
4891 /**
Romain Guya2431d02009-04-30 16:30:00 -07004892 * Invoked whenever this view loses focus, either by losing window focus or by losing
4893 * focus within its window. This method can be used to clear any state tied to the
4894 * focus. For instance, if a button is held pressed with the trackball and the window
4895 * loses focus, this method can be used to cancel the press.
4896 *
4897 * Subclasses of View overriding this method should always call super.onFocusLost().
4898 *
4899 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004900 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004901 *
4902 * @hide pending API council approval
4903 */
4904 protected void onFocusLost() {
4905 resetPressedState();
4906 }
4907
4908 private void resetPressedState() {
4909 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4910 return;
4911 }
4912
4913 if (isPressed()) {
4914 setPressed(false);
4915
4916 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004917 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004918 }
4919 }
4920 }
4921
4922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 * Returns true if this view has focus
4924 *
4925 * @return True if this view has focus, false otherwise.
4926 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004927 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 public boolean isFocused() {
4929 return (mPrivateFlags & FOCUSED) != 0;
4930 }
4931
4932 /**
4933 * Find the view in the hierarchy rooted at this view that currently has
4934 * focus.
4935 *
4936 * @return The view that currently has focus, or null if no focused view can
4937 * be found.
4938 */
4939 public View findFocus() {
4940 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4941 }
4942
4943 /**
Philip Milne6c8ea062012-04-03 17:38:43 -07004944 * Indicates whether this view is one of the set of scrollable containers in
4945 * its window.
4946 *
4947 * @return whether this view is one of the set of scrollable containers in
4948 * its window
4949 *
4950 * @attr ref android.R.styleable#View_isScrollContainer
4951 */
4952 public boolean isScrollContainer() {
4953 return (mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0;
4954 }
4955
4956 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 * Change whether this view is one of the set of scrollable containers in
4958 * its window. This will be used to determine whether the window can
4959 * resize or must pan when a soft input area is open -- scrollable
4960 * containers allow the window to use resize mode since the container
4961 * will appropriately shrink.
Philip Milne6c8ea062012-04-03 17:38:43 -07004962 *
4963 * @attr ref android.R.styleable#View_isScrollContainer
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 */
4965 public void setScrollContainer(boolean isScrollContainer) {
4966 if (isScrollContainer) {
4967 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4968 mAttachInfo.mScrollContainers.add(this);
4969 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4970 }
4971 mPrivateFlags |= SCROLL_CONTAINER;
4972 } else {
4973 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4974 mAttachInfo.mScrollContainers.remove(this);
4975 }
4976 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4977 }
4978 }
4979
4980 /**
4981 * Returns the quality of the drawing cache.
4982 *
4983 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4984 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4985 *
4986 * @see #setDrawingCacheQuality(int)
4987 * @see #setDrawingCacheEnabled(boolean)
4988 * @see #isDrawingCacheEnabled()
4989 *
4990 * @attr ref android.R.styleable#View_drawingCacheQuality
4991 */
4992 public int getDrawingCacheQuality() {
4993 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4994 }
4995
4996 /**
4997 * Set the drawing cache quality of this view. This value is used only when the
4998 * drawing cache is enabled
4999 *
5000 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
5001 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
5002 *
5003 * @see #getDrawingCacheQuality()
5004 * @see #setDrawingCacheEnabled(boolean)
5005 * @see #isDrawingCacheEnabled()
5006 *
5007 * @attr ref android.R.styleable#View_drawingCacheQuality
5008 */
5009 public void setDrawingCacheQuality(int quality) {
5010 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
5011 }
5012
5013 /**
5014 * Returns whether the screen should remain on, corresponding to the current
5015 * value of {@link #KEEP_SCREEN_ON}.
5016 *
5017 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
5018 *
5019 * @see #setKeepScreenOn(boolean)
5020 *
5021 * @attr ref android.R.styleable#View_keepScreenOn
5022 */
5023 public boolean getKeepScreenOn() {
5024 return (mViewFlags & KEEP_SCREEN_ON) != 0;
5025 }
5026
5027 /**
5028 * Controls whether the screen should remain on, modifying the
5029 * value of {@link #KEEP_SCREEN_ON}.
5030 *
5031 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
5032 *
5033 * @see #getKeepScreenOn()
5034 *
5035 * @attr ref android.R.styleable#View_keepScreenOn
5036 */
5037 public void setKeepScreenOn(boolean keepScreenOn) {
5038 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
5039 }
5040
5041 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005042 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5043 * @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 -08005044 *
5045 * @attr ref android.R.styleable#View_nextFocusLeft
5046 */
5047 public int getNextFocusLeftId() {
5048 return mNextFocusLeftId;
5049 }
5050
5051 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005052 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
5053 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
5054 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 *
5056 * @attr ref android.R.styleable#View_nextFocusLeft
5057 */
5058 public void setNextFocusLeftId(int nextFocusLeftId) {
5059 mNextFocusLeftId = nextFocusLeftId;
5060 }
5061
5062 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005063 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5064 * @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 -08005065 *
5066 * @attr ref android.R.styleable#View_nextFocusRight
5067 */
5068 public int getNextFocusRightId() {
5069 return mNextFocusRightId;
5070 }
5071
5072 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005073 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
5074 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
5075 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 *
5077 * @attr ref android.R.styleable#View_nextFocusRight
5078 */
5079 public void setNextFocusRightId(int nextFocusRightId) {
5080 mNextFocusRightId = nextFocusRightId;
5081 }
5082
5083 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005084 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5085 * @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 -08005086 *
5087 * @attr ref android.R.styleable#View_nextFocusUp
5088 */
5089 public int getNextFocusUpId() {
5090 return mNextFocusUpId;
5091 }
5092
5093 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005094 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
5095 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
5096 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 *
5098 * @attr ref android.R.styleable#View_nextFocusUp
5099 */
5100 public void setNextFocusUpId(int nextFocusUpId) {
5101 mNextFocusUpId = nextFocusUpId;
5102 }
5103
5104 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005105 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5106 * @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 -08005107 *
5108 * @attr ref android.R.styleable#View_nextFocusDown
5109 */
5110 public int getNextFocusDownId() {
5111 return mNextFocusDownId;
5112 }
5113
5114 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005115 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
5116 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
5117 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 *
5119 * @attr ref android.R.styleable#View_nextFocusDown
5120 */
5121 public void setNextFocusDownId(int nextFocusDownId) {
5122 mNextFocusDownId = nextFocusDownId;
5123 }
5124
5125 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08005126 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5127 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
5128 *
5129 * @attr ref android.R.styleable#View_nextFocusForward
5130 */
5131 public int getNextFocusForwardId() {
5132 return mNextFocusForwardId;
5133 }
5134
5135 /**
5136 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
5137 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
5138 * decide automatically.
5139 *
5140 * @attr ref android.R.styleable#View_nextFocusForward
5141 */
5142 public void setNextFocusForwardId(int nextFocusForwardId) {
5143 mNextFocusForwardId = nextFocusForwardId;
5144 }
5145
5146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 * Returns the visibility of this view and all of its ancestors
5148 *
5149 * @return True if this view and all of its ancestors are {@link #VISIBLE}
5150 */
5151 public boolean isShown() {
5152 View current = this;
5153 //noinspection ConstantConditions
5154 do {
5155 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5156 return false;
5157 }
5158 ViewParent parent = current.mParent;
5159 if (parent == null) {
5160 return false; // We are not attached to the view root
5161 }
5162 if (!(parent instanceof View)) {
5163 return true;
5164 }
5165 current = (View) parent;
5166 } while (current != null);
5167
5168 return false;
5169 }
5170
5171 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005172 * Called by the view hierarchy when the content insets for a window have
5173 * changed, to allow it to adjust its content to fit within those windows.
5174 * The content insets tell you the space that the status bar, input method,
5175 * and other system windows infringe on the application's window.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005177 * <p>You do not normally need to deal with this function, since the default
5178 * window decoration given to applications takes care of applying it to the
5179 * content of the window. If you use {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}
5180 * or {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION} this will not be the case,
5181 * and your content can be placed under those system elements. You can then
5182 * use this method within your view hierarchy if you have parts of your UI
5183 * which you would like to ensure are not being covered.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 *
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005185 * <p>The default implementation of this method simply applies the content
5186 * inset's to the view's padding. This can be enabled through
5187 * {@link #setFitsSystemWindows(boolean)}. Alternatively, you can override
5188 * the method and handle the insets however you would like. Note that the
5189 * insets provided by the framework are always relative to the far edges
5190 * of the window, not accounting for the location of the called view within
5191 * that window. (In fact when this method is called you do not yet know
5192 * where the layout will place the view, as it is done before layout happens.)
5193 *
5194 * <p>Note: unlike many View methods, there is no dispatch phase to this
5195 * call. If you are overriding it in a ViewGroup and want to allow the
5196 * call to continue to your children, you must be sure to call the super
5197 * implementation.
5198 *
5199 * @param insets Current content insets of the window. Prior to
5200 * {@link android.os.Build.VERSION_CODES#JELLY_BEAN} you must not modify
5201 * the insets or else you and Android will be unhappy.
5202 *
5203 * @return Return true if this view applied the insets and it should not
5204 * continue propagating further down the hierarchy, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 */
5206 protected boolean fitSystemWindows(Rect insets) {
5207 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005208 mUserPaddingStart = -1;
5209 mUserPaddingEnd = -1;
5210 mUserPaddingRelative = false;
5211 if ((mViewFlags & OPTIONAL_FITS_SYSTEM_WINDOWS) == 0
5212 || mAttachInfo == null
5213 || (mAttachInfo.mSystemUiVisibility & SYSTEM_UI_LAYOUT_FLAGS) == 0) {
5214 internalSetPadding(insets.left, insets.top, insets.right, insets.bottom);
5215 return true;
5216 } else {
5217 internalSetPadding(0, 0, 0, 0);
5218 return false;
5219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 }
5221 return false;
5222 }
5223
5224 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07005225 * Set whether or not this view should account for system screen decorations
5226 * such as the status bar and inset its content. This allows this view to be
5227 * positioned in absolute screen coordinates and remain visible to the user.
5228 *
5229 * <p>This should only be used by top-level window decor views.
5230 *
5231 * @param fitSystemWindows true to inset content for system screen decorations, false for
5232 * default behavior.
5233 *
5234 * @attr ref android.R.styleable#View_fitsSystemWindows
5235 */
5236 public void setFitsSystemWindows(boolean fitSystemWindows) {
5237 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
5238 }
5239
5240 /**
5241 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
5242 * will account for system screen decorations such as the status bar and inset its
5243 * content. This allows the view to be positioned in absolute screen coordinates
5244 * and remain visible to the user.
5245 *
5246 * @return true if this view will adjust its content bounds for system screen decorations.
5247 *
5248 * @attr ref android.R.styleable#View_fitsSystemWindows
5249 */
5250 public boolean fitsSystemWindows() {
5251 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
5252 }
5253
5254 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07005255 * Ask that a new dispatch of {@link #fitSystemWindows(Rect)} be performed.
5256 */
5257 public void requestFitSystemWindows() {
5258 if (mParent != null) {
5259 mParent.requestFitSystemWindows();
5260 }
5261 }
5262
5263 /**
5264 * For use by PhoneWindow to make its own system window fitting optional.
5265 * @hide
5266 */
5267 public void makeOptionalFitsSystemWindows() {
5268 setFlags(OPTIONAL_FITS_SYSTEM_WINDOWS, OPTIONAL_FITS_SYSTEM_WINDOWS);
5269 }
5270
5271 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 * Returns the visibility status for this view.
5273 *
5274 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5275 * @attr ref android.R.styleable#View_visibility
5276 */
5277 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07005278 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
5279 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
5280 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 })
5282 public int getVisibility() {
5283 return mViewFlags & VISIBILITY_MASK;
5284 }
5285
5286 /**
5287 * Set the enabled state of this view.
5288 *
5289 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
5290 * @attr ref android.R.styleable#View_visibility
5291 */
5292 @RemotableViewMethod
5293 public void setVisibility(int visibility) {
5294 setFlags(visibility, VISIBILITY_MASK);
Philip Milne6c8ea062012-04-03 17:38:43 -07005295 if (mBackground != null) mBackground.setVisible(visibility == VISIBLE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 }
5297
5298 /**
5299 * Returns the enabled status for this view. The interpretation of the
5300 * enabled state varies by subclass.
5301 *
5302 * @return True if this view is enabled, false otherwise.
5303 */
5304 @ViewDebug.ExportedProperty
5305 public boolean isEnabled() {
5306 return (mViewFlags & ENABLED_MASK) == ENABLED;
5307 }
5308
5309 /**
5310 * Set the enabled state of this view. The interpretation of the enabled
5311 * state varies by subclass.
5312 *
5313 * @param enabled True if this view is enabled, false otherwise.
5314 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08005315 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07005317 if (enabled == isEnabled()) return;
5318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
5320
5321 /*
5322 * The View most likely has to change its appearance, so refresh
5323 * the drawable state.
5324 */
5325 refreshDrawableState();
5326
5327 // Invalidate too, since the default behavior for views is to be
5328 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08005329 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331
5332 /**
5333 * Set whether this view can receive the focus.
5334 *
5335 * Setting this to false will also ensure that this view is not focusable
5336 * in touch mode.
5337 *
5338 * @param focusable If true, this view can receive the focus.
5339 *
5340 * @see #setFocusableInTouchMode(boolean)
5341 * @attr ref android.R.styleable#View_focusable
5342 */
5343 public void setFocusable(boolean focusable) {
5344 if (!focusable) {
5345 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
5346 }
5347 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
5348 }
5349
5350 /**
5351 * Set whether this view can receive focus while in touch mode.
5352 *
5353 * Setting this to true will also ensure that this view is focusable.
5354 *
5355 * @param focusableInTouchMode If true, this view can receive the focus while
5356 * in touch mode.
5357 *
5358 * @see #setFocusable(boolean)
5359 * @attr ref android.R.styleable#View_focusableInTouchMode
5360 */
5361 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
5362 // Focusable in touch mode should always be set before the focusable flag
5363 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
5364 // which, in touch mode, will not successfully request focus on this view
5365 // because the focusable in touch mode flag is not set
5366 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
5367 if (focusableInTouchMode) {
5368 setFlags(FOCUSABLE, FOCUSABLE_MASK);
5369 }
5370 }
5371
5372 /**
5373 * Set whether this view should have sound effects enabled for events such as
5374 * clicking and touching.
5375 *
5376 * <p>You may wish to disable sound effects for a view if you already play sounds,
5377 * for instance, a dial key that plays dtmf tones.
5378 *
5379 * @param soundEffectsEnabled whether sound effects are enabled for this view.
5380 * @see #isSoundEffectsEnabled()
5381 * @see #playSoundEffect(int)
5382 * @attr ref android.R.styleable#View_soundEffectsEnabled
5383 */
5384 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
5385 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
5386 }
5387
5388 /**
5389 * @return whether this view should have sound effects enabled for events such as
5390 * clicking and touching.
5391 *
5392 * @see #setSoundEffectsEnabled(boolean)
5393 * @see #playSoundEffect(int)
5394 * @attr ref android.R.styleable#View_soundEffectsEnabled
5395 */
5396 @ViewDebug.ExportedProperty
5397 public boolean isSoundEffectsEnabled() {
5398 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
5399 }
5400
5401 /**
5402 * Set whether this view should have haptic feedback for events such as
5403 * long presses.
5404 *
5405 * <p>You may wish to disable haptic feedback if your view already controls
5406 * its own haptic feedback.
5407 *
5408 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
5409 * @see #isHapticFeedbackEnabled()
5410 * @see #performHapticFeedback(int)
5411 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5412 */
5413 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
5414 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
5415 }
5416
5417 /**
5418 * @return whether this view should have haptic feedback enabled for events
5419 * long presses.
5420 *
5421 * @see #setHapticFeedbackEnabled(boolean)
5422 * @see #performHapticFeedback(int)
5423 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
5424 */
5425 @ViewDebug.ExportedProperty
5426 public boolean isHapticFeedbackEnabled() {
5427 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
5428 }
5429
5430 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005431 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005432 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005433 * @return One of {@link #LAYOUT_DIRECTION_LTR},
5434 * {@link #LAYOUT_DIRECTION_RTL},
5435 * {@link #LAYOUT_DIRECTION_INHERIT} or
5436 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005437 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005438 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005439 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005440 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07005441 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005442 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
5443 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
5444 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
5445 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08005446 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005447 public int getLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005448 return (mPrivateFlags2 & LAYOUT_DIRECTION_MASK) >> LAYOUT_DIRECTION_MASK_SHIFT;
Cibu Johny7632cb92010-02-22 13:01:02 -08005449 }
5450
5451 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005452 * Set the layout direction for this view. This will propagate a reset of layout direction
5453 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08005454 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005455 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
5456 * {@link #LAYOUT_DIRECTION_RTL},
5457 * {@link #LAYOUT_DIRECTION_INHERIT} or
5458 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005459 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005460 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005461 * @hide
Cibu Johny7632cb92010-02-22 13:01:02 -08005462 */
5463 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07005464 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005465 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -07005466 // Reset the current layout direction and the resolved one
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005467 mPrivateFlags2 &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07005468 resetResolvedLayoutDirection();
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005469 // Set the new layout direction (filtered) and ask for a layout pass
5470 mPrivateFlags2 |=
5471 ((layoutDirection << LAYOUT_DIRECTION_MASK_SHIFT) & LAYOUT_DIRECTION_MASK);
5472 requestLayout();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07005473 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005474 }
5475
5476 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005477 * Returns the resolved layout direction for this view.
5478 *
5479 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005480 * {@link #LAYOUT_DIRECTION_LTR} if the layout direction is not RTL.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005481 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005482 */
5483 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005484 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
5485 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005486 })
5487 public int getResolvedLayoutDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -07005488 // The layout diretion will be resolved only if needed
5489 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) != LAYOUT_DIRECTION_RESOLVED) {
5490 resolveLayoutDirection();
5491 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07005492 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005493 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
5494 }
5495
5496 /**
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -07005497 * Indicates whether or not this view's layout is right-to-left. This is resolved from
5498 * layout attribute and/or the inherited value from the parent
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005499 *
5500 * @return true if the layout is right-to-left.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -07005501 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07005502 */
5503 @ViewDebug.ExportedProperty(category = "layout")
5504 public boolean isLayoutRtl() {
5505 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
5506 }
5507
5508 /**
Adam Powell539ee872012-02-03 19:00:49 -08005509 * Indicates whether the view is currently tracking transient state that the
5510 * app should not need to concern itself with saving and restoring, but that
5511 * the framework should take special note to preserve when possible.
5512 *
Adam Powell785c4472012-05-02 21:25:39 -07005513 * <p>A view with transient state cannot be trivially rebound from an external
5514 * data source, such as an adapter binding item views in a list. This may be
5515 * because the view is performing an animation, tracking user selection
5516 * of content, or similar.</p>
5517 *
Adam Powell539ee872012-02-03 19:00:49 -08005518 * @return true if the view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005519 */
5520 @ViewDebug.ExportedProperty(category = "layout")
5521 public boolean hasTransientState() {
5522 return (mPrivateFlags2 & HAS_TRANSIENT_STATE) == HAS_TRANSIENT_STATE;
5523 }
5524
5525 /**
5526 * Set whether this view is currently tracking transient state that the
Chet Haase563d4f22012-04-18 16:20:08 -07005527 * framework should attempt to preserve when possible. This flag is reference counted,
5528 * so every call to setHasTransientState(true) should be paired with a later call
5529 * to setHasTransientState(false).
Adam Powell539ee872012-02-03 19:00:49 -08005530 *
Adam Powell785c4472012-05-02 21:25:39 -07005531 * <p>A view with transient state cannot be trivially rebound from an external
5532 * data source, such as an adapter binding item views in a list. This may be
5533 * because the view is performing an animation, tracking user selection
5534 * of content, or similar.</p>
5535 *
Adam Powell539ee872012-02-03 19:00:49 -08005536 * @param hasTransientState true if this view has transient state
Adam Powell539ee872012-02-03 19:00:49 -08005537 */
5538 public void setHasTransientState(boolean hasTransientState) {
Chet Haase563d4f22012-04-18 16:20:08 -07005539 mTransientStateCount = hasTransientState ? mTransientStateCount + 1 :
5540 mTransientStateCount - 1;
5541 if (mTransientStateCount < 0) {
5542 mTransientStateCount = 0;
5543 Log.e(VIEW_LOG_TAG, "hasTransientState decremented below 0: " +
5544 "unmatched pair of setHasTransientState calls");
5545 }
5546 if ((hasTransientState && mTransientStateCount == 1) ||
5547 (hasTransientState && mTransientStateCount == 0)) {
5548 // update flag if we've just incremented up from 0 or decremented down to 0
5549 mPrivateFlags2 = (mPrivateFlags2 & ~HAS_TRANSIENT_STATE) |
5550 (hasTransientState ? HAS_TRANSIENT_STATE : 0);
5551 if (mParent != null) {
5552 try {
5553 mParent.childHasTransientStateChanged(this, hasTransientState);
5554 } catch (AbstractMethodError e) {
5555 Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() +
5556 " does not fully implement ViewParent", e);
5557 }
Adam Powell539ee872012-02-03 19:00:49 -08005558 }
5559 }
5560 }
5561
5562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 * If this view doesn't do any drawing on its own, set this flag to
5564 * allow further optimizations. By default, this flag is not set on
5565 * View, but could be set on some View subclasses such as ViewGroup.
5566 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005567 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
5568 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 *
5570 * @param willNotDraw whether or not this View draw on its own
5571 */
5572 public void setWillNotDraw(boolean willNotDraw) {
5573 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
5574 }
5575
5576 /**
5577 * Returns whether or not this View draws on its own.
5578 *
5579 * @return true if this view has nothing to draw, false otherwise
5580 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005581 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 public boolean willNotDraw() {
5583 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
5584 }
5585
5586 /**
5587 * When a View's drawing cache is enabled, drawing is redirected to an
5588 * offscreen bitmap. Some views, like an ImageView, must be able to
5589 * bypass this mechanism if they already draw a single bitmap, to avoid
5590 * unnecessary usage of the memory.
5591 *
5592 * @param willNotCacheDrawing true if this view does not cache its
5593 * drawing, false otherwise
5594 */
5595 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
5596 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
5597 }
5598
5599 /**
5600 * Returns whether or not this View can cache its drawing or not.
5601 *
5602 * @return true if this view does not cache its drawing, false otherwise
5603 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005604 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005605 public boolean willNotCacheDrawing() {
5606 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
5607 }
5608
5609 /**
5610 * Indicates whether this view reacts to click events or not.
5611 *
5612 * @return true if the view is clickable, false otherwise
5613 *
5614 * @see #setClickable(boolean)
5615 * @attr ref android.R.styleable#View_clickable
5616 */
5617 @ViewDebug.ExportedProperty
5618 public boolean isClickable() {
5619 return (mViewFlags & CLICKABLE) == CLICKABLE;
5620 }
5621
5622 /**
5623 * Enables or disables click events for this view. When a view
5624 * is clickable it will change its state to "pressed" on every click.
5625 * Subclasses should set the view clickable to visually react to
5626 * user's clicks.
5627 *
5628 * @param clickable true to make the view clickable, false otherwise
5629 *
5630 * @see #isClickable()
5631 * @attr ref android.R.styleable#View_clickable
5632 */
5633 public void setClickable(boolean clickable) {
5634 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
5635 }
5636
5637 /**
5638 * Indicates whether this view reacts to long click events or not.
5639 *
5640 * @return true if the view is long clickable, false otherwise
5641 *
5642 * @see #setLongClickable(boolean)
5643 * @attr ref android.R.styleable#View_longClickable
5644 */
5645 public boolean isLongClickable() {
5646 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
5647 }
5648
5649 /**
5650 * Enables or disables long click events for this view. When a view is long
5651 * clickable it reacts to the user holding down the button for a longer
5652 * duration than a tap. This event can either launch the listener or a
5653 * context menu.
5654 *
5655 * @param longClickable true to make the view long clickable, false otherwise
5656 * @see #isLongClickable()
5657 * @attr ref android.R.styleable#View_longClickable
5658 */
5659 public void setLongClickable(boolean longClickable) {
5660 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
5661 }
5662
5663 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07005664 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 *
5666 * @see #isClickable()
5667 * @see #setClickable(boolean)
5668 *
5669 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
5670 * the View's internal state from a previously set "pressed" state.
5671 */
5672 public void setPressed(boolean pressed) {
Adam Powell035a1fc2012-02-27 15:23:50 -08005673 final boolean needsRefresh = pressed != ((mPrivateFlags & PRESSED) == PRESSED);
Adam Powell4d6f0662012-02-21 15:11:11 -08005674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 if (pressed) {
5676 mPrivateFlags |= PRESSED;
5677 } else {
5678 mPrivateFlags &= ~PRESSED;
5679 }
Adam Powell035a1fc2012-02-27 15:23:50 -08005680
5681 if (needsRefresh) {
5682 refreshDrawableState();
5683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 dispatchSetPressed(pressed);
5685 }
5686
5687 /**
5688 * Dispatch setPressed to all of this View's children.
5689 *
5690 * @see #setPressed(boolean)
5691 *
5692 * @param pressed The new pressed state
5693 */
5694 protected void dispatchSetPressed(boolean pressed) {
5695 }
5696
5697 /**
5698 * Indicates whether the view is currently in pressed state. Unless
5699 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
5700 * the pressed state.
5701 *
Philip Milne6c8ea062012-04-03 17:38:43 -07005702 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 * @see #isClickable()
5704 * @see #setClickable(boolean)
5705 *
5706 * @return true if the view is currently pressed, false otherwise
5707 */
5708 public boolean isPressed() {
5709 return (mPrivateFlags & PRESSED) == PRESSED;
5710 }
5711
5712 /**
5713 * Indicates whether this view will save its state (that is,
5714 * whether its {@link #onSaveInstanceState} method will be called).
5715 *
5716 * @return Returns true if the view state saving is enabled, else false.
5717 *
5718 * @see #setSaveEnabled(boolean)
5719 * @attr ref android.R.styleable#View_saveEnabled
5720 */
5721 public boolean isSaveEnabled() {
5722 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
5723 }
5724
5725 /**
5726 * Controls whether the saving of this view's state is
5727 * enabled (that is, whether its {@link #onSaveInstanceState} method
5728 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07005729 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 * for its state to be saved. This flag can only disable the
5731 * saving of this view; any child views may still have their state saved.
5732 *
5733 * @param enabled Set to false to <em>disable</em> state saving, or true
5734 * (the default) to allow it.
5735 *
5736 * @see #isSaveEnabled()
5737 * @see #setId(int)
5738 * @see #onSaveInstanceState()
5739 * @attr ref android.R.styleable#View_saveEnabled
5740 */
5741 public void setSaveEnabled(boolean enabled) {
5742 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
5743 }
5744
Jeff Brown85a31762010-09-01 17:01:00 -07005745 /**
5746 * Gets whether the framework should discard touches when the view's
5747 * window is obscured by another visible window.
5748 * Refer to the {@link View} security documentation for more details.
5749 *
5750 * @return True if touch filtering is enabled.
5751 *
5752 * @see #setFilterTouchesWhenObscured(boolean)
5753 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5754 */
5755 @ViewDebug.ExportedProperty
5756 public boolean getFilterTouchesWhenObscured() {
5757 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
5758 }
5759
5760 /**
5761 * Sets whether the framework should discard touches when the view's
5762 * window is obscured by another visible window.
5763 * Refer to the {@link View} security documentation for more details.
5764 *
5765 * @param enabled True if touch filtering should be enabled.
5766 *
5767 * @see #getFilterTouchesWhenObscured
5768 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
5769 */
5770 public void setFilterTouchesWhenObscured(boolean enabled) {
5771 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
5772 FILTER_TOUCHES_WHEN_OBSCURED);
5773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774
5775 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07005776 * Indicates whether the entire hierarchy under this view will save its
5777 * state when a state saving traversal occurs from its parent. The default
5778 * is true; if false, these views will not be saved unless
5779 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5780 *
5781 * @return Returns true if the view state saving from parent is enabled, else false.
5782 *
5783 * @see #setSaveFromParentEnabled(boolean)
5784 */
5785 public boolean isSaveFromParentEnabled() {
5786 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
5787 }
5788
5789 /**
5790 * Controls whether the entire hierarchy under this view will save its
5791 * state when a state saving traversal occurs from its parent. The default
5792 * is true; if false, these views will not be saved unless
5793 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
5794 *
5795 * @param enabled Set to false to <em>disable</em> state saving, or true
5796 * (the default) to allow it.
5797 *
5798 * @see #isSaveFromParentEnabled()
5799 * @see #setId(int)
5800 * @see #onSaveInstanceState()
5801 */
5802 public void setSaveFromParentEnabled(boolean enabled) {
5803 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
5804 }
5805
5806
5807 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 * Returns whether this View is able to take focus.
5809 *
5810 * @return True if this view can take focus, or false otherwise.
5811 * @attr ref android.R.styleable#View_focusable
5812 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005813 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 public final boolean isFocusable() {
5815 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
5816 }
5817
5818 /**
5819 * When a view is focusable, it may not want to take focus when in touch mode.
5820 * For example, a button would like focus when the user is navigating via a D-pad
5821 * so that the user can click on it, but once the user starts touching the screen,
5822 * the button shouldn't take focus
5823 * @return Whether the view is focusable in touch mode.
5824 * @attr ref android.R.styleable#View_focusableInTouchMode
5825 */
5826 @ViewDebug.ExportedProperty
5827 public final boolean isFocusableInTouchMode() {
5828 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
5829 }
5830
5831 /**
5832 * Find the nearest view in the specified direction that can take focus.
5833 * This does not actually give focus to that view.
5834 *
5835 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5836 *
5837 * @return The nearest focusable in the specified direction, or null if none
5838 * can be found.
5839 */
5840 public View focusSearch(int direction) {
5841 if (mParent != null) {
5842 return mParent.focusSearch(this, direction);
5843 } else {
5844 return null;
5845 }
5846 }
5847
5848 /**
5849 * This method is the last chance for the focused view and its ancestors to
5850 * respond to an arrow key. This is called when the focused view did not
5851 * consume the key internally, nor could the view system find a new view in
5852 * the requested direction to give focus to.
5853 *
5854 * @param focused The currently focused view.
5855 * @param direction The direction focus wants to move. One of FOCUS_UP,
5856 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
5857 * @return True if the this view consumed this unhandled move.
5858 */
5859 public boolean dispatchUnhandledMove(View focused, int direction) {
5860 return false;
5861 }
5862
5863 /**
5864 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08005865 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08005867 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
5868 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 * @return The user specified next view, or null if there is none.
5870 */
5871 View findUserSetNextFocus(View root, int direction) {
5872 switch (direction) {
5873 case FOCUS_LEFT:
5874 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005875 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 case FOCUS_RIGHT:
5877 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005878 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 case FOCUS_UP:
5880 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005881 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 case FOCUS_DOWN:
5883 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005884 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005885 case FOCUS_FORWARD:
5886 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005887 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08005888 case FOCUS_BACKWARD: {
John Reck1ecebbb2012-03-06 16:08:54 -08005889 if (mID == View.NO_ID) return null;
Jeff Brown4e6319b2010-12-13 10:36:51 -08005890 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07005891 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08005892 @Override
5893 public boolean apply(View t) {
5894 return t.mNextFocusForwardId == id;
5895 }
5896 });
5897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 }
5899 return null;
5900 }
5901
Jeff Brown4dfbec22011-08-15 14:55:37 -07005902 private View findViewInsideOutShouldExist(View root, final int childViewId) {
5903 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
5904 @Override
5905 public boolean apply(View t) {
5906 return t.mID == childViewId;
5907 }
5908 });
5909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 if (result == null) {
5911 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
5912 + "by user for id " + childViewId);
5913 }
5914 return result;
5915 }
5916
5917 /**
5918 * Find and return all focusable views that are descendants of this view,
5919 * possibly including this view if it is focusable itself.
5920 *
5921 * @param direction The direction of the focus
5922 * @return A list of focusable views
5923 */
5924 public ArrayList<View> getFocusables(int direction) {
5925 ArrayList<View> result = new ArrayList<View>(24);
5926 addFocusables(result, direction);
5927 return result;
5928 }
5929
5930 /**
5931 * Add any focusable views that are descendants of this view (possibly
5932 * including this view if it is focusable itself) to views. If we are in touch mode,
5933 * only add views that are also focusable in touch mode.
5934 *
5935 * @param views Focusable views found so far
5936 * @param direction The direction of the focus
5937 */
5938 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005939 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
5940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941
svetoslavganov75986cf2009-05-14 22:28:01 -07005942 /**
5943 * Adds any focusable views that are descendants of this view (possibly
5944 * including this view if it is focusable itself) to views. This method
5945 * adds all focusable views regardless if we are in touch mode or
Svetoslav Ganov42138042012-03-20 11:51:39 -07005946 * only views focusable in touch mode if we are in touch mode or
5947 * only views that can take accessibility focus if accessibility is enabeld
5948 * depending on the focusable mode paramater.
svetoslavganov75986cf2009-05-14 22:28:01 -07005949 *
5950 * @param views Focusable views found so far or null if all we are interested is
5951 * the number of focusables.
5952 * @param direction The direction of the focus.
5953 * @param focusableMode The type of focusables to be added.
5954 *
5955 * @see #FOCUSABLES_ALL
5956 * @see #FOCUSABLES_TOUCH_MODE
5957 */
5958 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07005959 if (views == null) {
svetoslavganov75986cf2009-05-14 22:28:01 -07005960 return;
5961 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07005962 if ((focusableMode & FOCUSABLES_ACCESSIBILITY) == FOCUSABLES_ACCESSIBILITY) {
5963 if (AccessibilityManager.getInstance(mContext).isEnabled()
5964 && includeForAccessibility()) {
5965 views.add(this);
5966 return;
5967 }
5968 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005969 if (!isFocusable()) {
5970 return;
svetoslavganov75986cf2009-05-14 22:28:01 -07005971 }
Svetoslav Ganov3cb889c2012-04-16 19:10:30 -07005972 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE
5973 && isInTouchMode() && !isFocusableInTouchMode()) {
5974 return;
5975 }
5976 views.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 }
5978
5979 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005980 * Finds the Views that contain given text. The containment is case insensitive.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005981 * The search is performed by either the text that the View renders or the content
5982 * description that describes the view for accessibility purposes and the view does
5983 * not render or both. Clients can specify how the search is to be performed via
5984 * passing the {@link #FIND_VIEWS_WITH_TEXT} and
5985 * {@link #FIND_VIEWS_WITH_CONTENT_DESCRIPTION} flags.
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005986 *
5987 * @param outViews The output list of matching Views.
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005988 * @param searched The text to match against.
Svetoslav Ganov02107852011-10-03 17:06:56 -07005989 *
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005990 * @see #FIND_VIEWS_WITH_TEXT
5991 * @see #FIND_VIEWS_WITH_CONTENT_DESCRIPTION
5992 * @see #setContentDescription(CharSequence)
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005993 */
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07005994 public void findViewsWithText(ArrayList<View> outViews, CharSequence searched, int flags) {
Svetoslav Ganov02107852011-10-03 17:06:56 -07005995 if (getAccessibilityNodeProvider() != null) {
5996 if ((flags & FIND_VIEWS_WITH_ACCESSIBILITY_NODE_PROVIDERS) != 0) {
5997 outViews.add(this);
5998 }
5999 } else if ((flags & FIND_VIEWS_WITH_CONTENT_DESCRIPTION) != 0
6000 && !TextUtils.isEmpty(searched) && !TextUtils.isEmpty(mContentDescription)) {
Svetoslav Ganovea515ae2011-09-14 18:15:32 -07006001 String searchedLowerCase = searched.toString().toLowerCase();
6002 String contentDescriptionLowerCase = mContentDescription.toString().toLowerCase();
6003 if (contentDescriptionLowerCase.contains(searchedLowerCase)) {
6004 outViews.add(this);
6005 }
6006 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006007 }
6008
6009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006010 * Find and return all touchable views that are descendants of this view,
6011 * possibly including this view if it is touchable itself.
6012 *
6013 * @return A list of touchable views
6014 */
6015 public ArrayList<View> getTouchables() {
6016 ArrayList<View> result = new ArrayList<View>();
6017 addTouchables(result);
6018 return result;
6019 }
6020
6021 /**
6022 * Add any touchable views that are descendants of this view (possibly
6023 * including this view if it is touchable itself) to views.
6024 *
6025 * @param views Touchable views found so far
6026 */
6027 public void addTouchables(ArrayList<View> views) {
6028 final int viewFlags = mViewFlags;
6029
6030 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
6031 && (viewFlags & ENABLED_MASK) == ENABLED) {
6032 views.add(this);
6033 }
6034 }
6035
6036 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006037 * Returns whether this View is accessibility focused.
6038 *
6039 * @return True if this View is accessibility focused.
6040 */
6041 boolean isAccessibilityFocused() {
6042 return (mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0;
6043 }
6044
6045 /**
6046 * Call this to try to give accessibility focus to this view.
6047 *
6048 * A view will not actually take focus if {@link AccessibilityManager#isEnabled()}
6049 * returns false or the view is no visible or the view already has accessibility
6050 * focus.
6051 *
6052 * See also {@link #focusSearch(int)}, which is what you call to say that you
6053 * have focus, and you want your parent to look for the next one.
6054 *
6055 * @return Whether this view actually took accessibility focus.
6056 *
6057 * @hide
6058 */
6059 public boolean requestAccessibilityFocus() {
Svetoslav Ganov07b726c2012-04-30 12:24:57 -07006060 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
6061 if (!manager.isEnabled() || !manager.isTouchExplorationEnabled()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006062 return false;
6063 }
6064 if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6065 return false;
6066 }
6067 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) == 0) {
6068 mPrivateFlags2 |= ACCESSIBILITY_FOCUSED;
6069 ViewRootImpl viewRootImpl = getViewRootImpl();
6070 if (viewRootImpl != null) {
6071 viewRootImpl.setAccessibilityFocusedHost(this);
6072 }
6073 invalidate();
6074 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
6075 notifyAccessibilityStateChanged();
6076 // Try to give input focus to this view - not a descendant.
6077 requestFocusNoSearch(View.FOCUS_DOWN, null);
6078 return true;
6079 }
6080 return false;
6081 }
6082
6083 /**
6084 * Call this to try to clear accessibility focus of this view.
6085 *
6086 * See also {@link #focusSearch(int)}, which is what you call to say that you
6087 * have focus, and you want your parent to look for the next one.
6088 *
6089 * @hide
6090 */
6091 public void clearAccessibilityFocus() {
6092 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6093 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6094 ViewRootImpl viewRootImpl = getViewRootImpl();
6095 if (viewRootImpl != null) {
6096 viewRootImpl.setAccessibilityFocusedHost(null);
6097 }
6098 invalidate();
6099 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);
6100 notifyAccessibilityStateChanged();
6101 // Try to move accessibility focus to the input focus.
6102 View rootView = getRootView();
6103 if (rootView != null) {
6104 View inputFocus = rootView.findFocus();
6105 if (inputFocus != null) {
6106 inputFocus.requestAccessibilityFocus();
6107 }
6108 }
6109 }
6110 }
6111
6112 /**
6113 * Find the best view to take accessibility focus from a hover.
6114 * This function finds the deepest actionable view and if that
6115 * fails ask the parent to take accessibility focus from hover.
6116 *
6117 * @param x The X hovered location in this view coorditantes.
6118 * @param y The Y hovered location in this view coorditantes.
6119 * @return Whether the request was handled.
6120 *
6121 * @hide
6122 */
6123 public boolean requestAccessibilityFocusFromHover(float x, float y) {
6124 if (onRequestAccessibilityFocusFromHover(x, y)) {
6125 return true;
6126 }
6127 ViewParent parent = mParent;
6128 if (parent instanceof View) {
6129 View parentView = (View) parent;
6130
6131 float[] position = mAttachInfo.mTmpTransformLocation;
6132 position[0] = x;
6133 position[1] = y;
6134
6135 // Compensate for the transformation of the current matrix.
6136 if (!hasIdentityMatrix()) {
6137 getMatrix().mapPoints(position);
6138 }
6139
6140 // Compensate for the parent scroll and the offset
6141 // of this view stop from the parent top.
6142 position[0] += mLeft - parentView.mScrollX;
6143 position[1] += mTop - parentView.mScrollY;
6144
6145 return parentView.requestAccessibilityFocusFromHover(position[0], position[1]);
6146 }
6147 return false;
6148 }
6149
6150 /**
6151 * Requests to give this View focus from hover.
6152 *
6153 * @param x The X hovered location in this view coorditantes.
6154 * @param y The Y hovered location in this view coorditantes.
6155 * @return Whether the request was handled.
6156 *
6157 * @hide
6158 */
6159 public boolean onRequestAccessibilityFocusFromHover(float x, float y) {
6160 if (includeForAccessibility()
6161 && (isActionableForAccessibility() || hasListenersForAccessibility())) {
6162 return requestAccessibilityFocus();
6163 }
6164 return false;
6165 }
6166
6167 /**
6168 * Clears accessibility focus without calling any callback methods
6169 * normally invoked in {@link #clearAccessibilityFocus()}. This method
6170 * is used for clearing accessibility focus when giving this focus to
6171 * another view.
6172 */
6173 void clearAccessibilityFocusNoCallbacks() {
6174 if ((mPrivateFlags2 & ACCESSIBILITY_FOCUSED) != 0) {
6175 mPrivateFlags2 &= ~ACCESSIBILITY_FOCUSED;
6176 invalidate();
6177 }
6178 }
6179
6180 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 * Call this to try to give focus to a specific view or to one of its
6182 * descendants.
6183 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006184 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6185 * false), or if it is focusable and it is not focusable in touch mode
6186 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006188 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189 * have focus, and you want your parent to look for the next one.
6190 *
6191 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
6192 * {@link #FOCUS_DOWN} and <code>null</code>.
6193 *
6194 * @return Whether this view or one of its descendants actually took focus.
6195 */
6196 public final boolean requestFocus() {
6197 return requestFocus(View.FOCUS_DOWN);
6198 }
6199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 /**
6201 * Call this to try to give focus to a specific view or to one of its
6202 * descendants and give it a hint about what direction focus is heading.
6203 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006204 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6205 * false), or if it is focusable and it is not focusable in touch mode
6206 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006208 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 * have focus, and you want your parent to look for the next one.
6210 *
6211 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
6212 * <code>null</code> set for the previously focused rectangle.
6213 *
6214 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6215 * @return Whether this view or one of its descendants actually took focus.
6216 */
6217 public final boolean requestFocus(int direction) {
6218 return requestFocus(direction, null);
6219 }
6220
6221 /**
6222 * Call this to try to give focus to a specific view or to one of its descendants
6223 * and give it hints about the direction and a specific rectangle that the focus
6224 * is coming from. The rectangle can help give larger views a finer grained hint
6225 * about where focus is coming from, and therefore, where to show selection, or
6226 * forward focus change internally.
6227 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006228 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
6229 * false), or if it is focusable and it is not focusable in touch mode
6230 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 *
6232 * A View will not take focus if it is not visible.
6233 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006234 * A View will not take focus if one of its parents has
6235 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
6236 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07006238 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 * have focus, and you want your parent to look for the next one.
6240 *
6241 * You may wish to override this method if your custom {@link View} has an internal
6242 * {@link View} that it wishes to forward the request to.
6243 *
6244 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
6245 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
6246 * to give a finer grained hint about where focus is coming from. May be null
6247 * if there is no hint.
6248 * @return Whether this view or one of its descendants actually took focus.
6249 */
6250 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006251 return requestFocusNoSearch(direction, previouslyFocusedRect);
6252 }
6253
6254 private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006255 // need to be focusable
6256 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
6257 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
6258 return false;
6259 }
6260
6261 // need to be focusable in touch mode if in touch mode
6262 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07006263 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
6264 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 }
6266
6267 // need to not have any parents blocking us
6268 if (hasAncestorThatBlocksDescendantFocus()) {
6269 return false;
6270 }
6271
6272 handleFocusGainInternal(direction, previouslyFocusedRect);
6273 return true;
6274 }
6275
6276 /**
6277 * Call this to try to give focus to a specific view or to one of its descendants. This is a
6278 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
6279 * touch mode to request focus when they are touched.
6280 *
6281 * @return Whether this view or one of its descendants actually took focus.
6282 *
6283 * @see #isInTouchMode()
6284 *
6285 */
6286 public final boolean requestFocusFromTouch() {
6287 // Leave touch mode if we need to
6288 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006289 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07006290 if (viewRoot != null) {
6291 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 }
6293 }
6294 return requestFocus(View.FOCUS_DOWN);
6295 }
6296
6297 /**
6298 * @return Whether any ancestor of this view blocks descendant focus.
6299 */
6300 private boolean hasAncestorThatBlocksDescendantFocus() {
6301 ViewParent ancestor = mParent;
6302 while (ancestor instanceof ViewGroup) {
6303 final ViewGroup vgAncestor = (ViewGroup) ancestor;
6304 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
6305 return true;
6306 } else {
6307 ancestor = vgAncestor.getParent();
6308 }
6309 }
6310 return false;
6311 }
6312
6313 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -07006314 * Gets the mode for determining whether this View is important for accessibility
6315 * which is if it fires accessibility events and if it is reported to
6316 * accessibility services that query the screen.
6317 *
6318 * @return The mode for determining whether a View is important for accessibility.
6319 *
6320 * @attr ref android.R.styleable#View_importantForAccessibility
6321 *
6322 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6323 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6324 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6325 */
6326 @ViewDebug.ExportedProperty(category = "accessibility", mapping = {
6327 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_AUTO,
6328 to = "IMPORTANT_FOR_ACCESSIBILITY_AUTO"),
6329 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_YES,
6330 to = "IMPORTANT_FOR_ACCESSIBILITY_YES"),
6331 @ViewDebug.IntToString(from = IMPORTANT_FOR_ACCESSIBILITY_NO,
6332 to = "IMPORTANT_FOR_ACCESSIBILITY_NO")
6333 })
6334 public int getImportantForAccessibility() {
6335 return (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6336 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6337 }
6338
6339 /**
6340 * Sets how to determine whether this view is important for accessibility
6341 * which is if it fires accessibility events and if it is reported to
6342 * accessibility services that query the screen.
6343 *
6344 * @param mode How to determine whether this view is important for accessibility.
6345 *
6346 * @attr ref android.R.styleable#View_importantForAccessibility
6347 *
6348 * @see #IMPORTANT_FOR_ACCESSIBILITY_YES
6349 * @see #IMPORTANT_FOR_ACCESSIBILITY_NO
6350 * @see #IMPORTANT_FOR_ACCESSIBILITY_AUTO
6351 */
6352 public void setImportantForAccessibility(int mode) {
6353 if (mode != getImportantForAccessibility()) {
6354 mPrivateFlags2 &= ~IMPORTANT_FOR_ACCESSIBILITY_MASK;
6355 mPrivateFlags2 |= (mode << IMPORTANT_FOR_ACCESSIBILITY_SHIFT)
6356 & IMPORTANT_FOR_ACCESSIBILITY_MASK;
6357 notifyAccessibilityStateChanged();
6358 }
6359 }
6360
6361 /**
6362 * Gets whether this view should be exposed for accessibility.
6363 *
6364 * @return Whether the view is exposed for accessibility.
6365 *
6366 * @hide
6367 */
6368 public boolean isImportantForAccessibility() {
6369 final int mode = (mPrivateFlags2 & IMPORTANT_FOR_ACCESSIBILITY_MASK)
6370 >> IMPORTANT_FOR_ACCESSIBILITY_SHIFT;
6371 switch (mode) {
6372 case IMPORTANT_FOR_ACCESSIBILITY_YES:
6373 return true;
6374 case IMPORTANT_FOR_ACCESSIBILITY_NO:
6375 return false;
6376 case IMPORTANT_FOR_ACCESSIBILITY_AUTO:
6377 return isActionableForAccessibility() || hasListenersForAccessibility();
6378 default:
6379 throw new IllegalArgumentException("Unknow important for accessibility mode: "
6380 + mode);
6381 }
6382 }
6383
6384 /**
6385 * Gets the parent for accessibility purposes. Note that the parent for
6386 * accessibility is not necessary the immediate parent. It is the first
6387 * predecessor that is important for accessibility.
6388 *
6389 * @return The parent for accessibility purposes.
6390 */
6391 public ViewParent getParentForAccessibility() {
6392 if (mParent instanceof View) {
6393 View parentView = (View) mParent;
6394 if (parentView.includeForAccessibility()) {
6395 return mParent;
6396 } else {
6397 return mParent.getParentForAccessibility();
6398 }
6399 }
6400 return null;
6401 }
6402
6403 /**
6404 * Adds the children of a given View for accessibility. Since some Views are
6405 * not important for accessibility the children for accessibility are not
6406 * necessarily direct children of the riew, rather they are the first level of
6407 * descendants important for accessibility.
6408 *
6409 * @param children The list of children for accessibility.
6410 */
6411 public void addChildrenForAccessibility(ArrayList<View> children) {
6412 if (includeForAccessibility()) {
6413 children.add(this);
6414 }
6415 }
6416
6417 /**
6418 * Whether to regard this view for accessibility. A view is regarded for
6419 * accessibility if it is important for accessibility or the querying
6420 * accessibility service has explicitly requested that view not
6421 * important for accessibility are regarded.
6422 *
6423 * @return Whether to regard the view for accessibility.
6424 */
6425 boolean includeForAccessibility() {
6426 if (mAttachInfo != null) {
6427 if (!mAttachInfo.mIncludeNotImportantViews) {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006428 return isImportantForAccessibility();
Svetoslav Ganov42138042012-03-20 11:51:39 -07006429 } else {
Svetoslav Ganov0a1bb6d2012-05-07 11:54:39 -07006430 return true;
Svetoslav Ganov42138042012-03-20 11:51:39 -07006431 }
6432 }
6433 return false;
6434 }
6435
6436 /**
6437 * Returns whether the View is considered actionable from
6438 * accessibility perspective. Such view are important for
6439 * accessiiblity.
6440 *
6441 * @return True if the view is actionable for accessibility.
6442 */
6443 private boolean isActionableForAccessibility() {
6444 return (isClickable() || isLongClickable() || isFocusable());
6445 }
6446
6447 /**
6448 * Returns whether the View has registered callbacks wich makes it
6449 * important for accessiiblity.
6450 *
6451 * @return True if the view is actionable for accessibility.
6452 */
6453 private boolean hasListenersForAccessibility() {
6454 ListenerInfo info = getListenerInfo();
6455 return mTouchDelegate != null || info.mOnKeyListener != null
6456 || info.mOnTouchListener != null || info.mOnGenericMotionListener != null
6457 || info.mOnHoverListener != null || info.mOnDragListener != null;
6458 }
6459
6460 /**
6461 * Notifies accessibility services that some view's important for
6462 * accessibility state has changed. Note that such notifications
6463 * are made at most once every
6464 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}
6465 * to avoid unnecessary load to the system. Also once a view has
6466 * made a notifucation this method is a NOP until the notification has
6467 * been sent to clients.
6468 *
6469 * @hide
6470 *
6471 * TODO: Makse sure this method is called for any view state change
6472 * that is interesting for accessilility purposes.
6473 */
6474 public void notifyAccessibilityStateChanged() {
6475 if ((mPrivateFlags2 & ACCESSIBILITY_STATE_CHANGED) == 0) {
6476 mPrivateFlags2 |= ACCESSIBILITY_STATE_CHANGED;
6477 if (mParent != null) {
6478 mParent.childAccessibilityStateChanged(this);
6479 }
6480 }
6481 }
6482
6483 /**
6484 * Reset the state indicating the this view has requested clients
6485 * interested in its accessiblity state to be notified.
6486 *
6487 * @hide
6488 */
6489 public void resetAccessibilityStateChanged() {
6490 mPrivateFlags2 &= ~ACCESSIBILITY_STATE_CHANGED;
6491 }
6492
6493 /**
6494 * Performs the specified accessibility action on the view. For
6495 * possible accessibility actions look at {@link AccessibilityNodeInfo}.
6496 *
6497 * @param action The action to perform.
6498 * @return Whether the action was performed.
6499 */
Svetoslav Ganovaa780c12012-04-19 23:01:39 -07006500 public boolean performAccessibilityAction(int action, Bundle args) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07006501 switch (action) {
6502 case AccessibilityNodeInfo.ACTION_CLICK: {
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006503 if (isClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006504 return performClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006505 }
6506 } break;
6507 case AccessibilityNodeInfo.ACTION_LONG_CLICK: {
6508 if (isLongClickable()) {
Svetoslav Ganov773f2622012-05-05 19:59:42 -07006509 return performLongClick();
Svetoslav Ganov005b83b2012-04-16 18:17:17 -07006510 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07006511 } break;
6512 case AccessibilityNodeInfo.ACTION_FOCUS: {
6513 if (!hasFocus()) {
6514 // Get out of touch mode since accessibility
6515 // wants to move focus around.
6516 getViewRootImpl().ensureTouchMode(false);
6517 return requestFocus();
6518 }
6519 } break;
6520 case AccessibilityNodeInfo.ACTION_CLEAR_FOCUS: {
6521 if (hasFocus()) {
6522 clearFocus();
6523 return !isFocused();
6524 }
6525 } break;
6526 case AccessibilityNodeInfo.ACTION_SELECT: {
6527 if (!isSelected()) {
6528 setSelected(true);
6529 return isSelected();
6530 }
6531 } break;
6532 case AccessibilityNodeInfo.ACTION_CLEAR_SELECTION: {
6533 if (isSelected()) {
6534 setSelected(false);
6535 return !isSelected();
6536 }
6537 } break;
6538 case AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS: {
6539 if (!isAccessibilityFocused()) {
6540 return requestAccessibilityFocus();
6541 }
6542 } break;
6543 case AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS: {
6544 if (isAccessibilityFocused()) {
6545 clearAccessibilityFocus();
6546 return true;
6547 }
6548 } break;
6549 }
6550 return false;
6551 }
6552
6553 /**
Romain Guya440b002010-02-24 15:57:54 -08006554 * @hide
6555 */
6556 public void dispatchStartTemporaryDetach() {
6557 onStartTemporaryDetach();
6558 }
6559
6560 /**
6561 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
6563 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08006564 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006565 */
6566 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08006567 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08006568 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006569 }
6570
6571 /**
6572 * @hide
6573 */
6574 public void dispatchFinishTemporaryDetach() {
6575 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 }
Romain Guy8506ab42009-06-11 17:35:47 -07006577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 /**
6579 * Called after {@link #onStartTemporaryDetach} when the container is done
6580 * changing the view.
6581 */
6582 public void onFinishTemporaryDetach() {
6583 }
Romain Guy8506ab42009-06-11 17:35:47 -07006584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006586 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
6587 * for this view's window. Returns null if the view is not currently attached
6588 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07006589 * just use the standard high-level event callbacks like
6590 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006591 */
6592 public KeyEvent.DispatcherState getKeyDispatcherState() {
6593 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
6594 }
Joe Malin32736f02011-01-19 16:14:20 -08006595
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07006596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006597 * Dispatch a key event before it is processed by any input method
6598 * associated with the view hierarchy. This can be used to intercept
6599 * key events in special situations before the IME consumes them; a
6600 * typical example would be handling the BACK key to update the application's
6601 * UI instead of allowing the IME to see it and close itself.
6602 *
6603 * @param event The key event to be dispatched.
6604 * @return True if the event was handled, false otherwise.
6605 */
6606 public boolean dispatchKeyEventPreIme(KeyEvent event) {
6607 return onKeyPreIme(event.getKeyCode(), event);
6608 }
6609
6610 /**
6611 * Dispatch a key event to the next view on the focus path. This path runs
6612 * from the top of the view tree down to the currently focused view. If this
6613 * view has focus, it will dispatch to itself. Otherwise it will dispatch
6614 * the next node down the focus path. This method also fires any key
6615 * listeners.
6616 *
6617 * @param event The key event to be dispatched.
6618 * @return True if the event was handled, false otherwise.
6619 */
6620 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006621 if (mInputEventConsistencyVerifier != null) {
6622 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
6623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624
Jeff Brown21bc5c92011-02-28 18:27:14 -08006625 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07006626 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006627 ListenerInfo li = mListenerInfo;
6628 if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6629 && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 return true;
6631 }
6632
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006633 if (event.dispatch(this, mAttachInfo != null
6634 ? mAttachInfo.mKeyDispatchState : null, this)) {
6635 return true;
6636 }
6637
6638 if (mInputEventConsistencyVerifier != null) {
6639 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6640 }
6641 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 }
6643
6644 /**
6645 * Dispatches a key shortcut event.
6646 *
6647 * @param event The key event to be dispatched.
6648 * @return True if the event was handled by the view, false otherwise.
6649 */
6650 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
6651 return onKeyShortcut(event.getKeyCode(), event);
6652 }
6653
6654 /**
6655 * Pass the touch screen motion event down to the target view, or this
6656 * view if it is the target.
6657 *
6658 * @param event The motion event to be dispatched.
6659 * @return True if the event was handled by the view, false otherwise.
6660 */
6661 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006662 if (mInputEventConsistencyVerifier != null) {
6663 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
6664 }
6665
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006666 if (onFilterTouchEventForSecurity(event)) {
6667 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006668 ListenerInfo li = mListenerInfo;
6669 if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
6670 && li.mOnTouchListener.onTouch(this, event)) {
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006671 return true;
6672 }
6673
6674 if (onTouchEvent(event)) {
6675 return true;
6676 }
Jeff Brown85a31762010-09-01 17:01:00 -07006677 }
6678
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006679 if (mInputEventConsistencyVerifier != null) {
6680 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006681 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006682 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 }
6684
6685 /**
Jeff Brown85a31762010-09-01 17:01:00 -07006686 * Filter the touch event to apply security policies.
6687 *
6688 * @param event The motion event to be filtered.
6689 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08006690 *
Jeff Brown85a31762010-09-01 17:01:00 -07006691 * @see #getFilterTouchesWhenObscured
6692 */
6693 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07006694 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07006695 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
6696 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
6697 // Window is obscured, drop this touch.
6698 return false;
6699 }
6700 return true;
6701 }
6702
6703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 * Pass a trackball motion event down to the focused view.
6705 *
6706 * @param event The motion event to be dispatched.
6707 * @return True if the event was handled by the view, false otherwise.
6708 */
6709 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006710 if (mInputEventConsistencyVerifier != null) {
6711 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
6712 }
6713
Romain Guy02ccac62011-06-24 13:20:23 -07006714 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 }
6716
6717 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006718 * Dispatch a generic motion event.
6719 * <p>
6720 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
6721 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08006722 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07006723 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08006724 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08006725 *
6726 * @param event The motion event to be dispatched.
6727 * @return True if the event was handled by the view, false otherwise.
6728 */
6729 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08006730 if (mInputEventConsistencyVerifier != null) {
6731 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
6732 }
6733
Jeff Browna032cc02011-03-07 16:56:21 -08006734 final int source = event.getSource();
6735 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
6736 final int action = event.getAction();
6737 if (action == MotionEvent.ACTION_HOVER_ENTER
6738 || action == MotionEvent.ACTION_HOVER_MOVE
6739 || action == MotionEvent.ACTION_HOVER_EXIT) {
6740 if (dispatchHoverEvent(event)) {
6741 return true;
6742 }
6743 } else if (dispatchGenericPointerEvent(event)) {
6744 return true;
6745 }
6746 } else if (dispatchGenericFocusedEvent(event)) {
6747 return true;
6748 }
6749
Jeff Brown10b62902011-06-20 16:40:37 -07006750 if (dispatchGenericMotionEventInternal(event)) {
6751 return true;
6752 }
6753
6754 if (mInputEventConsistencyVerifier != null) {
6755 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6756 }
6757 return false;
6758 }
6759
6760 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07006761 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006762 ListenerInfo li = mListenerInfo;
6763 if (li != null && li.mOnGenericMotionListener != null
6764 && (mViewFlags & ENABLED_MASK) == ENABLED
6765 && li.mOnGenericMotionListener.onGenericMotion(this, event)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08006766 return true;
6767 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07006768
6769 if (onGenericMotionEvent(event)) {
6770 return true;
6771 }
6772
6773 if (mInputEventConsistencyVerifier != null) {
6774 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
6775 }
6776 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006777 }
6778
6779 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006780 * Dispatch a hover event.
6781 * <p>
Philip Milne6c8ea062012-04-03 17:38:43 -07006782 * Do not call this method directly.
Romain Guy5c22a8c2011-05-13 11:48:45 -07006783 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006784 * </p>
6785 *
6786 * @param event The motion event to be dispatched.
6787 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006788 */
6789 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07006790 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07006791 ListenerInfo li = mListenerInfo;
6792 if (li != null && li.mOnHoverListener != null
6793 && (mViewFlags & ENABLED_MASK) == ENABLED
6794 && li.mOnHoverListener.onHover(this, event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07006795 return true;
6796 }
6797
Jeff Browna032cc02011-03-07 16:56:21 -08006798 return onHoverEvent(event);
6799 }
6800
6801 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006802 * Returns true if the view has a child to which it has recently sent
6803 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
6804 * it does not have a hovered child, then it must be the innermost hovered view.
6805 * @hide
6806 */
6807 protected boolean hasHoveredChild() {
6808 return false;
6809 }
6810
6811 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006812 * Dispatch a generic motion event to the view under the first pointer.
6813 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006814 * Do not call this method directly.
6815 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006816 * </p>
6817 *
6818 * @param event The motion event to be dispatched.
6819 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006820 */
6821 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
6822 return false;
6823 }
6824
6825 /**
6826 * Dispatch a generic motion event to the currently focused view.
6827 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006828 * Do not call this method directly.
6829 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08006830 * </p>
6831 *
6832 * @param event The motion event to be dispatched.
6833 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08006834 */
6835 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
6836 return false;
6837 }
6838
6839 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08006840 * Dispatch a pointer event.
6841 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07006842 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
6843 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
6844 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08006845 * and should not be expected to handle other pointing device features.
6846 * </p>
6847 *
6848 * @param event The motion event to be dispatched.
6849 * @return True if the event was handled by the view, false otherwise.
6850 * @hide
6851 */
6852 public final boolean dispatchPointerEvent(MotionEvent event) {
6853 if (event.isTouchEvent()) {
6854 return dispatchTouchEvent(event);
6855 } else {
6856 return dispatchGenericMotionEvent(event);
6857 }
6858 }
6859
6860 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 * Called when the window containing this view gains or loses window focus.
6862 * ViewGroups should override to route to their children.
6863 *
6864 * @param hasFocus True if the window containing this view now has focus,
6865 * false otherwise.
6866 */
6867 public void dispatchWindowFocusChanged(boolean hasFocus) {
6868 onWindowFocusChanged(hasFocus);
6869 }
6870
6871 /**
6872 * Called when the window containing this view gains or loses focus. Note
6873 * that this is separate from view focus: to receive key events, both
6874 * your view and its window must have focus. If a window is displayed
6875 * on top of yours that takes input focus, then your own window will lose
6876 * focus but the view focus will remain unchanged.
6877 *
6878 * @param hasWindowFocus True if the window containing this view now has
6879 * focus, false otherwise.
6880 */
6881 public void onWindowFocusChanged(boolean hasWindowFocus) {
6882 InputMethodManager imm = InputMethodManager.peekInstance();
6883 if (!hasWindowFocus) {
6884 if (isPressed()) {
6885 setPressed(false);
6886 }
6887 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6888 imm.focusOut(this);
6889 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006890 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08006891 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07006892 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
6894 imm.focusIn(this);
6895 }
6896 refreshDrawableState();
6897 }
6898
6899 /**
6900 * Returns true if this view is in a window that currently has window focus.
6901 * Note that this is not the same as the view itself having focus.
6902 *
6903 * @return True if this view is in a window that currently has window focus.
6904 */
6905 public boolean hasWindowFocus() {
6906 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
6907 }
6908
6909 /**
Adam Powell326d8082009-12-09 15:10:07 -08006910 * Dispatch a view visibility change down the view hierarchy.
6911 * ViewGroups should override to route to their children.
6912 * @param changedView The view whose visibility changed. Could be 'this' or
6913 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006914 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6915 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006916 */
6917 protected void dispatchVisibilityChanged(View changedView, int visibility) {
6918 onVisibilityChanged(changedView, visibility);
6919 }
6920
6921 /**
6922 * Called when the visibility of the view or an ancestor of the view is changed.
6923 * @param changedView The view whose visibility changed. Could be 'this' or
6924 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08006925 * @param visibility The new visibility of changedView: {@link #VISIBLE},
6926 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08006927 */
6928 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006929 if (visibility == VISIBLE) {
6930 if (mAttachInfo != null) {
6931 initialAwakenScrollBars();
6932 } else {
6933 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
6934 }
6935 }
Adam Powell326d8082009-12-09 15:10:07 -08006936 }
6937
6938 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08006939 * Dispatch a hint about whether this view is displayed. For instance, when
6940 * a View moves out of the screen, it might receives a display hint indicating
6941 * the view is not displayed. Applications should not <em>rely</em> on this hint
6942 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006943 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006944 * @param hint A hint about whether or not this view is displayed:
6945 * {@link #VISIBLE} or {@link #INVISIBLE}.
6946 */
6947 public void dispatchDisplayHint(int hint) {
6948 onDisplayHint(hint);
6949 }
6950
6951 /**
6952 * Gives this view a hint about whether is displayed or not. For instance, when
6953 * a View moves out of the screen, it might receives a display hint indicating
6954 * the view is not displayed. Applications should not <em>rely</em> on this hint
6955 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08006956 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08006957 * @param hint A hint about whether or not this view is displayed:
6958 * {@link #VISIBLE} or {@link #INVISIBLE}.
6959 */
6960 protected void onDisplayHint(int hint) {
6961 }
6962
6963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006964 * Dispatch a window visibility change down the view hierarchy.
6965 * ViewGroups should override to route to their children.
6966 *
6967 * @param visibility The new visibility of the window.
6968 *
Philip Milne6c8ea062012-04-03 17:38:43 -07006969 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 */
6971 public void dispatchWindowVisibilityChanged(int visibility) {
6972 onWindowVisibilityChanged(visibility);
6973 }
6974
6975 /**
6976 * Called when the window containing has change its visibility
6977 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
6978 * that this tells you whether or not your window is being made visible
6979 * to the window manager; this does <em>not</em> tell you whether or not
6980 * your window is obscured by other windows on the screen, even if it
6981 * is itself visible.
6982 *
6983 * @param visibility The new visibility of the window.
6984 */
6985 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07006986 if (visibility == VISIBLE) {
6987 initialAwakenScrollBars();
6988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 }
6990
6991 /**
6992 * Returns the current visibility of the window this view is attached to
6993 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
6994 *
6995 * @return Returns the current visibility of the view's window.
6996 */
6997 public int getWindowVisibility() {
6998 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
6999 }
7000
7001 /**
7002 * Retrieve the overall visible display size in which the window this view is
7003 * attached to has been positioned in. This takes into account screen
7004 * decorations above the window, for both cases where the window itself
7005 * is being position inside of them or the window is being placed under
7006 * then and covered insets are used for the window to position its content
7007 * inside. In effect, this tells you the available area where content can
7008 * be placed and remain visible to users.
7009 *
7010 * <p>This function requires an IPC back to the window manager to retrieve
7011 * the requested information, so should not be used in performance critical
7012 * code like drawing.
7013 *
7014 * @param outRect Filled in with the visible display frame. If the view
7015 * is not attached to a window, this is simply the raw display size.
7016 */
7017 public void getWindowVisibleDisplayFrame(Rect outRect) {
7018 if (mAttachInfo != null) {
7019 try {
7020 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
7021 } catch (RemoteException e) {
7022 return;
7023 }
7024 // XXX This is really broken, and probably all needs to be done
7025 // in the window manager, and we need to know more about whether
7026 // we want the area behind or in front of the IME.
7027 final Rect insets = mAttachInfo.mVisibleInsets;
7028 outRect.left += insets.left;
7029 outRect.top += insets.top;
7030 outRect.right -= insets.right;
7031 outRect.bottom -= insets.bottom;
7032 return;
7033 }
7034 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07007035 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 }
7037
7038 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007039 * Dispatch a notification about a resource configuration change down
7040 * the view hierarchy.
7041 * ViewGroups should override to route to their children.
7042 *
7043 * @param newConfig The new resource configuration.
7044 *
Philip Milne6c8ea062012-04-03 17:38:43 -07007045 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08007046 */
7047 public void dispatchConfigurationChanged(Configuration newConfig) {
7048 onConfigurationChanged(newConfig);
7049 }
7050
7051 /**
7052 * Called when the current configuration of the resources being used
7053 * by the application have changed. You can use this to decide when
7054 * to reload resources that can changed based on orientation and other
7055 * configuration characterstics. You only need to use this if you are
7056 * not relying on the normal {@link android.app.Activity} mechanism of
7057 * recreating the activity instance upon a configuration change.
7058 *
7059 * @param newConfig The new resource configuration.
7060 */
7061 protected void onConfigurationChanged(Configuration newConfig) {
7062 }
7063
7064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 * Private function to aggregate all per-view attributes in to the view
7066 * root.
7067 */
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007068 void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7069 performCollectViewAttributes(attachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 }
7071
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007072 void performCollectViewAttributes(AttachInfo attachInfo, int visibility) {
7073 if ((visibility & VISIBILITY_MASK) == VISIBLE) {
Joe Onorato664644d2011-01-23 17:53:23 -08007074 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007075 attachInfo.mKeepScreenOn = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007076 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007077 attachInfo.mSystemUiVisibility |= mSystemUiVisibility;
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007078 ListenerInfo li = mListenerInfo;
7079 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -07007080 attachInfo.mHasSystemUiListeners = true;
Joe Onorato664644d2011-01-23 17:53:23 -08007081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 }
7083 }
7084
7085 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08007086 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007087 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08007088 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
7089 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 ai.mRecomputeGlobalAttributes = true;
7091 }
7092 }
7093 }
7094
7095 /**
7096 * Returns whether the device is currently in touch mode. Touch mode is entered
7097 * once the user begins interacting with the device by touch, and affects various
7098 * things like whether focus is always visible to the user.
7099 *
7100 * @return Whether the device is in touch mode.
7101 */
7102 @ViewDebug.ExportedProperty
7103 public boolean isInTouchMode() {
7104 if (mAttachInfo != null) {
7105 return mAttachInfo.mInTouchMode;
7106 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07007107 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 }
7109 }
7110
7111 /**
7112 * Returns the context the view is running in, through which it can
7113 * access the current theme, resources, etc.
7114 *
7115 * @return The view's Context.
7116 */
7117 @ViewDebug.CapturedViewProperty
7118 public final Context getContext() {
7119 return mContext;
7120 }
7121
7122 /**
7123 * Handle a key event before it is processed by any input method
7124 * associated with the view hierarchy. This can be used to intercept
7125 * key events in special situations before the IME consumes them; a
7126 * typical example would be handling the BACK key to update the application's
7127 * UI instead of allowing the IME to see it and close itself.
7128 *
7129 * @param keyCode The value in event.getKeyCode().
7130 * @param event Description of the key event.
7131 * @return If you handled the event, return true. If you want to allow the
7132 * event to be handled by the next receiver, return false.
7133 */
7134 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
7135 return false;
7136 }
7137
7138 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007139 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
7140 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007141 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
7142 * is released, if the view is enabled and clickable.
7143 *
7144 * @param keyCode A key code that represents the button pressed, from
7145 * {@link android.view.KeyEvent}.
7146 * @param event The KeyEvent object that defines the button action.
7147 */
7148 public boolean onKeyDown(int keyCode, KeyEvent event) {
7149 boolean result = false;
7150
7151 switch (keyCode) {
7152 case KeyEvent.KEYCODE_DPAD_CENTER:
7153 case KeyEvent.KEYCODE_ENTER: {
7154 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7155 return true;
7156 }
7157 // Long clickable items don't necessarily have to be clickable
7158 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
7159 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
7160 (event.getRepeatCount() == 0)) {
7161 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007162 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163 return true;
7164 }
7165 break;
7166 }
7167 }
7168 return result;
7169 }
7170
7171 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07007172 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
7173 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
7174 * the event).
7175 */
7176 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
7177 return false;
7178 }
7179
7180 /**
Jeff Brown995e7742010-12-22 16:59:36 -08007181 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
7182 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
7184 * {@link KeyEvent#KEYCODE_ENTER} is released.
7185 *
7186 * @param keyCode A key code that represents the button pressed, from
7187 * {@link android.view.KeyEvent}.
7188 * @param event The KeyEvent object that defines the button action.
7189 */
7190 public boolean onKeyUp(int keyCode, KeyEvent event) {
7191 boolean result = false;
7192
7193 switch (keyCode) {
7194 case KeyEvent.KEYCODE_DPAD_CENTER:
7195 case KeyEvent.KEYCODE_ENTER: {
7196 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
7197 return true;
7198 }
7199 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
7200 setPressed(false);
7201
7202 if (!mHasPerformedLongPress) {
7203 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007204 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205
7206 result = performClick();
7207 }
7208 }
7209 break;
7210 }
7211 }
7212 return result;
7213 }
7214
7215 /**
7216 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
7217 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
7218 * the event).
7219 *
7220 * @param keyCode A key code that represents the button pressed, from
7221 * {@link android.view.KeyEvent}.
7222 * @param repeatCount The number of times the action was made.
7223 * @param event The KeyEvent object that defines the button action.
7224 */
7225 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
7226 return false;
7227 }
7228
7229 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08007230 * Called on the focused view when a key shortcut event is not handled.
7231 * Override this method to implement local key shortcuts for the View.
7232 * Key shortcuts can also be implemented by setting the
7233 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007234 *
7235 * @param keyCode The value in event.getKeyCode().
7236 * @param event Description of the key event.
7237 * @return If you handled the event, return true. If you want to allow the
7238 * event to be handled by the next receiver, return false.
7239 */
7240 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
7241 return false;
7242 }
7243
7244 /**
7245 * Check whether the called view is a text editor, in which case it
7246 * would make sense to automatically display a soft input window for
7247 * it. Subclasses should override this if they implement
7248 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007249 * a call on that method would return a non-null InputConnection, and
7250 * they are really a first-class editor that the user would normally
7251 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07007252 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007253 * <p>The default implementation always returns false. This does
7254 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
7255 * will not be called or the user can not otherwise perform edits on your
7256 * view; it is just a hint to the system that this is not the primary
7257 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07007258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 * @return Returns true if this view is a text editor, else false.
7260 */
7261 public boolean onCheckIsTextEditor() {
7262 return false;
7263 }
Romain Guy8506ab42009-06-11 17:35:47 -07007264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 /**
7266 * Create a new InputConnection for an InputMethod to interact
7267 * with the view. The default implementation returns null, since it doesn't
7268 * support input methods. You can override this to implement such support.
7269 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07007270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 * <p>When implementing this, you probably also want to implement
7272 * {@link #onCheckIsTextEditor()} to indicate you will return a
7273 * non-null InputConnection.
7274 *
7275 * @param outAttrs Fill in with attribute information about the connection.
7276 */
7277 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
7278 return null;
7279 }
7280
7281 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007282 * Called by the {@link android.view.inputmethod.InputMethodManager}
7283 * when a view who is not the current
7284 * input connection target is trying to make a call on the manager. The
7285 * default implementation returns false; you can override this to return
7286 * true for certain views if you are performing InputConnection proxying
7287 * to them.
7288 * @param view The View that is making the InputMethodManager call.
7289 * @return Return true to allow the call, false to reject.
7290 */
7291 public boolean checkInputConnectionProxy(View view) {
7292 return false;
7293 }
Romain Guy8506ab42009-06-11 17:35:47 -07007294
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 * Show the context menu for this view. It is not safe to hold on to the
7297 * menu after returning from this method.
7298 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07007299 * You should normally not overload this method. Overload
7300 * {@link #onCreateContextMenu(ContextMenu)} or define an
7301 * {@link OnCreateContextMenuListener} to add items to the context menu.
7302 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 * @param menu The context menu to populate
7304 */
7305 public void createContextMenu(ContextMenu menu) {
7306 ContextMenuInfo menuInfo = getContextMenuInfo();
7307
7308 // Sets the current menu info so all items added to menu will have
7309 // my extra info set.
7310 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
7311
7312 onCreateContextMenu(menu);
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07007313 ListenerInfo li = mListenerInfo;
7314 if (li != null && li.mOnCreateContextMenuListener != null) {
7315 li.mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 }
7317
7318 // Clear the extra information so subsequent items that aren't mine don't
7319 // have my extra info.
7320 ((MenuBuilder)menu).setCurrentMenuInfo(null);
7321
7322 if (mParent != null) {
7323 mParent.createContextMenu(menu);
7324 }
7325 }
7326
7327 /**
7328 * Views should implement this if they have extra information to associate
7329 * with the context menu. The return result is supplied as a parameter to
7330 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
7331 * callback.
7332 *
7333 * @return Extra information about the item for which the context menu
7334 * should be shown. This information will vary across different
7335 * subclasses of View.
7336 */
7337 protected ContextMenuInfo getContextMenuInfo() {
7338 return null;
7339 }
7340
7341 /**
7342 * Views should implement this if the view itself is going to add items to
7343 * the context menu.
7344 *
7345 * @param menu the context menu to populate
7346 */
7347 protected void onCreateContextMenu(ContextMenu menu) {
7348 }
7349
7350 /**
7351 * Implement this method to handle trackball motion events. The
7352 * <em>relative</em> movement of the trackball since the last event
7353 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
7354 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
7355 * that a movement of 1 corresponds to the user pressing one DPAD key (so
7356 * they will often be fractional values, representing the more fine-grained
7357 * movement information available from a trackball).
7358 *
7359 * @param event The motion event.
7360 * @return True if the event was handled, false otherwise.
7361 */
7362 public boolean onTrackballEvent(MotionEvent event) {
7363 return false;
7364 }
7365
7366 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08007367 * Implement this method to handle generic motion events.
7368 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08007369 * Generic motion events describe joystick movements, mouse hovers, track pad
7370 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08007371 * {@link MotionEvent#getSource() source} of the motion event specifies
7372 * the class of input that was received. Implementations of this method
7373 * must examine the bits in the source before processing the event.
7374 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08007375 * </p><p>
7376 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
7377 * are delivered to the view under the pointer. All other generic motion events are
7378 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08007379 * </p>
Scott Mainb303d832011-10-12 16:45:18 -07007380 * <pre> public boolean onGenericMotionEvent(MotionEvent event) {
Jeff Browncb1404e2011-01-15 18:14:15 -08007381 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08007382 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
7383 * // process the joystick movement...
7384 * return true;
7385 * }
7386 * }
7387 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
7388 * switch (event.getAction()) {
7389 * case MotionEvent.ACTION_HOVER_MOVE:
7390 * // process the mouse hover movement...
7391 * return true;
7392 * case MotionEvent.ACTION_SCROLL:
7393 * // process the scroll wheel movement...
7394 * return true;
7395 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08007396 * }
7397 * return super.onGenericMotionEvent(event);
Scott Mainb303d832011-10-12 16:45:18 -07007398 * }</pre>
Jeff Browncb1404e2011-01-15 18:14:15 -08007399 *
7400 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08007401 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08007402 */
7403 public boolean onGenericMotionEvent(MotionEvent event) {
7404 return false;
7405 }
7406
7407 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007408 * Implement this method to handle hover events.
7409 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07007410 * This method is called whenever a pointer is hovering into, over, or out of the
7411 * bounds of a view and the view is not currently being touched.
7412 * Hover events are represented as pointer events with action
7413 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
7414 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
7415 * </p>
7416 * <ul>
7417 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
7418 * when the pointer enters the bounds of the view.</li>
7419 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
7420 * when the pointer has already entered the bounds of the view and has moved.</li>
7421 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
7422 * when the pointer has exited the bounds of the view or when the pointer is
7423 * about to go down due to a button click, tap, or similar user action that
7424 * causes the view to be touched.</li>
7425 * </ul>
7426 * <p>
7427 * The view should implement this method to return true to indicate that it is
7428 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08007429 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07007430 * The default implementation calls {@link #setHovered} to update the hovered state
7431 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07007432 * is enabled and is clickable. The default implementation also sends hover
7433 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08007434 * </p>
7435 *
7436 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07007437 * @return True if the view handled the hover event.
7438 *
7439 * @see #isHovered
7440 * @see #setHovered
7441 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007442 */
7443 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007444 // The root view may receive hover (or touch) events that are outside the bounds of
7445 // the window. This code ensures that we only send accessibility events for
7446 // hovers that are actually within the bounds of the root view.
Svetoslav Ganov42138042012-03-20 11:51:39 -07007447 final int action = event.getActionMasked();
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007448 if (!mSendingHoverAccessibilityEvents) {
7449 if ((action == MotionEvent.ACTION_HOVER_ENTER
7450 || action == MotionEvent.ACTION_HOVER_MOVE)
7451 && !hasHoveredChild()
7452 && pointInView(event.getX(), event.getY())) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007453 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007454 mSendingHoverAccessibilityEvents = true;
7455 requestAccessibilityFocusFromHover((int) event.getX(), (int) event.getY());
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007456 }
7457 } else {
7458 if (action == MotionEvent.ACTION_HOVER_EXIT
Svetoslav Ganov42138042012-03-20 11:51:39 -07007459 || (action == MotionEvent.ACTION_MOVE
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007460 && !pointInView(event.getX(), event.getY()))) {
7461 mSendingHoverAccessibilityEvents = false;
7462 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Svetoslav Ganov42138042012-03-20 11:51:39 -07007463 // If the window does not have input focus we take away accessibility
7464 // focus as soon as the user stop hovering over the view.
Jeff Brown59a422e2012-04-19 15:19:19 -07007465 if (mAttachInfo != null && !mAttachInfo.mHasWindowFocus) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007466 getViewRootImpl().setAccessibilityFocusedHost(null);
7467 }
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007468 }
Jeff Browna1b24182011-07-28 13:38:24 -07007469 }
7470
Jeff Brown87b7f802011-06-21 18:35:45 -07007471 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07007472 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07007473 case MotionEvent.ACTION_HOVER_ENTER:
7474 setHovered(true);
7475 break;
7476 case MotionEvent.ACTION_HOVER_EXIT:
7477 setHovered(false);
7478 break;
7479 }
Jeff Browna1b24182011-07-28 13:38:24 -07007480
7481 // Dispatch the event to onGenericMotionEvent before returning true.
7482 // This is to provide compatibility with existing applications that
7483 // handled HOVER_MOVE events in onGenericMotionEvent and that would
7484 // break because of the new default handling for hoverable views
7485 // in onHoverEvent.
7486 // Note that onGenericMotionEvent will be called by default when
7487 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
7488 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07007489 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08007490 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007491
Svetoslav Ganov736c2752011-04-22 18:30:36 -07007492 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08007493 }
7494
7495 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07007496 * Returns true if the view should handle {@link #onHoverEvent}
7497 * by calling {@link #setHovered} to change its hovered state.
7498 *
7499 * @return True if the view is hoverable.
7500 */
7501 private boolean isHoverable() {
7502 final int viewFlags = mViewFlags;
7503 if ((viewFlags & ENABLED_MASK) == DISABLED) {
7504 return false;
7505 }
7506
7507 return (viewFlags & CLICKABLE) == CLICKABLE
7508 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
7509 }
7510
7511 /**
Jeff Browna032cc02011-03-07 16:56:21 -08007512 * Returns true if the view is currently hovered.
7513 *
7514 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007515 *
7516 * @see #setHovered
7517 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007518 */
Jeff Brown10b62902011-06-20 16:40:37 -07007519 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08007520 public boolean isHovered() {
7521 return (mPrivateFlags & HOVERED) != 0;
7522 }
7523
7524 /**
7525 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007526 * <p>
7527 * Calling this method also changes the drawable state of the view. This
7528 * enables the view to react to hover by using different drawable resources
7529 * to change its appearance.
7530 * </p><p>
7531 * The {@link #onHoverChanged} method is called when the hovered state changes.
7532 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08007533 *
7534 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07007535 *
7536 * @see #isHovered
7537 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08007538 */
7539 public void setHovered(boolean hovered) {
7540 if (hovered) {
7541 if ((mPrivateFlags & HOVERED) == 0) {
7542 mPrivateFlags |= HOVERED;
7543 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007544 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08007545 }
7546 } else {
7547 if ((mPrivateFlags & HOVERED) != 0) {
7548 mPrivateFlags &= ~HOVERED;
7549 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07007550 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08007551 }
7552 }
7553 }
7554
7555 /**
Jeff Brown10b62902011-06-20 16:40:37 -07007556 * Implement this method to handle hover state changes.
7557 * <p>
7558 * This method is called whenever the hover state changes as a result of a
7559 * call to {@link #setHovered}.
7560 * </p>
7561 *
7562 * @param hovered The current hover state, as returned by {@link #isHovered}.
7563 *
7564 * @see #isHovered
7565 * @see #setHovered
7566 */
7567 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07007568 }
7569
7570 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 * Implement this method to handle touch screen motion events.
7572 *
7573 * @param event The motion event.
7574 * @return True if the event was handled, false otherwise.
7575 */
7576 public boolean onTouchEvent(MotionEvent event) {
7577 final int viewFlags = mViewFlags;
7578
7579 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007580 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
Adam Powell4d6f0662012-02-21 15:11:11 -08007581 setPressed(false);
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07007582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 // A disabled view that is clickable still consumes the touch
7584 // events, it just doesn't respond to them.
7585 return (((viewFlags & CLICKABLE) == CLICKABLE ||
7586 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
7587 }
7588
7589 if (mTouchDelegate != null) {
7590 if (mTouchDelegate.onTouchEvent(event)) {
7591 return true;
7592 }
7593 }
7594
7595 if (((viewFlags & CLICKABLE) == CLICKABLE ||
7596 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
7597 switch (event.getAction()) {
7598 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08007599 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
7600 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 // take focus if we don't have it already and we should in
7602 // touch mode.
7603 boolean focusTaken = false;
7604 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
7605 focusTaken = requestFocus();
7606 }
7607
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007608 if (prepressed) {
7609 // The button is being released before we actually
7610 // showed it as pressed. Make it show the pressed
7611 // state now (before scheduling the click) to ensure
7612 // the user sees it.
Adam Powell4d6f0662012-02-21 15:11:11 -08007613 setPressed(true);
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08007614 }
Joe Malin32736f02011-01-19 16:14:20 -08007615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 if (!mHasPerformedLongPress) {
7617 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05007618 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007619
7620 // Only perform take click actions if we were in the pressed state
7621 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08007622 // Use a Runnable and post this rather than calling
7623 // performClick directly. This lets other visual state
7624 // of the view update before click actions start.
7625 if (mPerformClick == null) {
7626 mPerformClick = new PerformClick();
7627 }
7628 if (!post(mPerformClick)) {
7629 performClick();
7630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 }
7632 }
7633
7634 if (mUnsetPressedState == null) {
7635 mUnsetPressedState = new UnsetPressedState();
7636 }
7637
Adam Powelle14579b2009-12-16 18:39:52 -08007638 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08007639 postDelayed(mUnsetPressedState,
7640 ViewConfiguration.getPressedStateDuration());
7641 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007642 // If the post failed, unpress right now
7643 mUnsetPressedState.run();
7644 }
Adam Powelle14579b2009-12-16 18:39:52 -08007645 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 }
7647 break;
7648
7649 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08007650 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007651
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07007652 if (performButtonActionOnTouchDown(event)) {
7653 break;
7654 }
7655
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007656 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07007657 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007658
7659 // For views inside a scrolling container, delay the pressed feedback for
7660 // a short period in case this is a scroll.
7661 if (isInScrollingContainer) {
7662 mPrivateFlags |= PREPRESSED;
7663 if (mPendingCheckForTap == null) {
7664 mPendingCheckForTap = new CheckForTap();
7665 }
7666 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
7667 } else {
7668 // Not inside a scrolling container, so show the feedback right away
Adam Powell4d6f0662012-02-21 15:11:11 -08007669 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07007670 checkForLongClick(0);
7671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 break;
7673
7674 case MotionEvent.ACTION_CANCEL:
Adam Powell4d6f0662012-02-21 15:11:11 -08007675 setPressed(false);
Adam Powelle14579b2009-12-16 18:39:52 -08007676 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 break;
7678
7679 case MotionEvent.ACTION_MOVE:
7680 final int x = (int) event.getX();
7681 final int y = (int) event.getY();
7682
7683 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07007684 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08007686 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007687 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08007688 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05007689 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690
Adam Powell4d6f0662012-02-21 15:11:11 -08007691 setPressed(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 }
7694 break;
7695 }
7696 return true;
7697 }
7698
7699 return false;
7700 }
7701
7702 /**
Adam Powell10298662011-08-14 18:26:30 -07007703 * @hide
7704 */
7705 public boolean isInScrollingContainer() {
7706 ViewParent p = getParent();
7707 while (p != null && p instanceof ViewGroup) {
7708 if (((ViewGroup) p).shouldDelayChildPressedState()) {
7709 return true;
7710 }
7711 p = p.getParent();
7712 }
7713 return false;
7714 }
7715
7716 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05007717 * Remove the longpress detection timer.
7718 */
7719 private void removeLongPressCallback() {
7720 if (mPendingCheckForLongPress != null) {
7721 removeCallbacks(mPendingCheckForLongPress);
7722 }
7723 }
Adam Powell3cb8b632011-01-21 15:34:14 -08007724
7725 /**
7726 * Remove the pending click action
7727 */
7728 private void removePerformClickCallback() {
7729 if (mPerformClick != null) {
7730 removeCallbacks(mPerformClick);
7731 }
7732 }
7733
Adam Powelle14579b2009-12-16 18:39:52 -08007734 /**
Romain Guya440b002010-02-24 15:57:54 -08007735 * Remove the prepress detection timer.
7736 */
7737 private void removeUnsetPressCallback() {
7738 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
7739 setPressed(false);
7740 removeCallbacks(mUnsetPressedState);
7741 }
7742 }
7743
7744 /**
Adam Powelle14579b2009-12-16 18:39:52 -08007745 * Remove the tap detection timer.
7746 */
7747 private void removeTapCallback() {
7748 if (mPendingCheckForTap != null) {
7749 mPrivateFlags &= ~PREPRESSED;
7750 removeCallbacks(mPendingCheckForTap);
7751 }
7752 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05007753
7754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 * Cancels a pending long press. Your subclass can use this if you
7756 * want the context menu to come up if the user presses and holds
7757 * at the same place, but you don't want it to come up if they press
7758 * and then move around enough to cause scrolling.
7759 */
7760 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05007761 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08007762
7763 /*
7764 * The prepressed state handled by the tap callback is a display
7765 * construct, but the tap callback will post a long press callback
7766 * less its own timeout. Remove it here.
7767 */
7768 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 }
7770
7771 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07007772 * Remove the pending callback for sending a
7773 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
7774 */
7775 private void removeSendViewScrolledAccessibilityEventCallback() {
7776 if (mSendViewScrolledAccessibilityEvent != null) {
7777 removeCallbacks(mSendViewScrolledAccessibilityEvent);
7778 }
7779 }
7780
7781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 * Sets the TouchDelegate for this View.
7783 */
7784 public void setTouchDelegate(TouchDelegate delegate) {
7785 mTouchDelegate = delegate;
7786 }
7787
7788 /**
7789 * Gets the TouchDelegate for this View.
7790 */
7791 public TouchDelegate getTouchDelegate() {
7792 return mTouchDelegate;
7793 }
7794
7795 /**
7796 * Set flags controlling behavior of this view.
7797 *
7798 * @param flags Constant indicating the value which should be set
7799 * @param mask Constant indicating the bit range that should be changed
7800 */
7801 void setFlags(int flags, int mask) {
7802 int old = mViewFlags;
7803 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
7804
7805 int changed = mViewFlags ^ old;
7806 if (changed == 0) {
7807 return;
7808 }
7809 int privateFlags = mPrivateFlags;
7810
7811 /* Check if the FOCUSABLE bit has changed */
7812 if (((changed & FOCUSABLE_MASK) != 0) &&
7813 ((privateFlags & HAS_BOUNDS) !=0)) {
7814 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
7815 && ((privateFlags & FOCUSED) != 0)) {
7816 /* Give up focus if we are no longer focusable */
7817 clearFocus();
7818 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
7819 && ((privateFlags & FOCUSED) == 0)) {
7820 /*
7821 * Tell the view system that we are now available to take focus
7822 * if no one else already has it.
7823 */
7824 if (mParent != null) mParent.focusableViewAvailable(this);
7825 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007826 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7827 notifyAccessibilityStateChanged();
7828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 }
7830
7831 if ((flags & VISIBILITY_MASK) == VISIBLE) {
7832 if ((changed & VISIBILITY_MASK) != 0) {
7833 /*
Chet Haase4324ead2011-08-24 21:31:03 -07007834 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07007835 * it was not visible. Marking it drawn ensures that the invalidation will
7836 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 */
Chet Haaseaceafe62011-08-26 15:44:33 -07007838 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07007839 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840
7841 needGlobalAttributesUpdate(true);
7842
7843 // a view becoming visible is worth notifying the parent
7844 // about in case nothing has focus. even if this specific view
7845 // isn't focusable, it may contain something that is, so let
7846 // the root view try to give this focus if nothing else does.
7847 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
7848 mParent.focusableViewAvailable(this);
7849 }
7850 }
7851 }
7852
7853 /* Check if the GONE bit has changed */
7854 if ((changed & GONE) != 0) {
7855 needGlobalAttributesUpdate(false);
7856 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857
Romain Guyecd80ee2009-12-03 17:13:02 -08007858 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
7859 if (hasFocus()) clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007860 clearAccessibilityFocus();
Romain Guyecd80ee2009-12-03 17:13:02 -08007861 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07007862 if (mParent instanceof View) {
7863 // GONE views noop invalidation, so invalidate the parent
7864 ((View) mParent).invalidate(true);
7865 }
7866 // Mark the view drawn to ensure that it gets invalidated properly the next
7867 // time it is visible and gets invalidated
7868 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 }
7870 if (mAttachInfo != null) {
7871 mAttachInfo.mViewVisibilityChanged = true;
7872 }
7873 }
7874
7875 /* Check if the VISIBLE bit has changed */
7876 if ((changed & INVISIBLE) != 0) {
7877 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07007878 /*
7879 * If this view is becoming invisible, set the DRAWN flag so that
7880 * the next invalidate() will not be skipped.
7881 */
7882 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007883
7884 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07007885 // root view becoming invisible shouldn't clear focus and accessibility focus
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007886 if (getRootView() != this) {
7887 clearFocus();
Svetoslav Ganov42138042012-03-20 11:51:39 -07007888 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 }
7890 }
7891 if (mAttachInfo != null) {
7892 mAttachInfo.mViewVisibilityChanged = true;
7893 }
7894 }
7895
Adam Powell326d8082009-12-09 15:10:07 -08007896 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07007897 if (mParent instanceof ViewGroup) {
Romain Guyfe455af2012-02-15 16:40:20 -08007898 ((ViewGroup) mParent).onChildVisibilityChanged(this,
7899 (changed & VISIBILITY_MASK), (flags & VISIBILITY_MASK));
Romain Guy0fd89bf2011-01-26 15:41:30 -08007900 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07007901 } else if (mParent != null) {
7902 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07007903 }
Adam Powell326d8082009-12-09 15:10:07 -08007904 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
7905 }
7906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
7908 destroyDrawingCache();
7909 }
7910
7911 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
7912 destroyDrawingCache();
7913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007914 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007915 }
7916
7917 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
7918 destroyDrawingCache();
7919 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7920 }
7921
7922 if ((changed & DRAW_MASK) != 0) {
7923 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
Philip Milne6c8ea062012-04-03 17:38:43 -07007924 if (mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 mPrivateFlags &= ~SKIP_DRAW;
7926 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7927 } else {
7928 mPrivateFlags |= SKIP_DRAW;
7929 }
7930 } else {
7931 mPrivateFlags &= ~SKIP_DRAW;
7932 }
7933 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08007934 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 }
7936
7937 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08007938 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 mParent.recomputeViewAttributes(this);
7940 }
7941 }
Svetoslav Ganov42138042012-03-20 11:51:39 -07007942
7943 if (AccessibilityManager.getInstance(mContext).isEnabled()
7944 && ((changed & FOCUSABLE) != 0 || (changed & CLICKABLE) != 0
7945 || (changed & LONG_CLICKABLE) != 0 || (changed & ENABLED) != 0)) {
7946 notifyAccessibilityStateChanged();
7947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007948 }
7949
7950 /**
7951 * Change the view's z order in the tree, so it's on top of other sibling
7952 * views
7953 */
7954 public void bringToFront() {
7955 if (mParent != null) {
7956 mParent.bringChildToFront(this);
7957 }
7958 }
7959
7960 /**
7961 * This is called in response to an internal scroll in this view (i.e., the
7962 * view scrolled its own contents). This is typically as a result of
7963 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
7964 * called.
7965 *
7966 * @param l Current horizontal scroll origin.
7967 * @param t Current vertical scroll origin.
7968 * @param oldl Previous horizontal scroll origin.
7969 * @param oldt Previous vertical scroll origin.
7970 */
7971 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07007972 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
7973 postSendViewScrolledAccessibilityEventCallback();
7974 }
7975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 mBackgroundSizeChanged = true;
7977
7978 final AttachInfo ai = mAttachInfo;
7979 if (ai != null) {
7980 ai.mViewScrollChanged = true;
7981 }
7982 }
7983
7984 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007985 * Interface definition for a callback to be invoked when the layout bounds of a view
7986 * changes due to layout processing.
7987 */
7988 public interface OnLayoutChangeListener {
7989 /**
7990 * Called when the focus state of a view has changed.
7991 *
7992 * @param v The view whose state has changed.
7993 * @param left The new value of the view's left property.
7994 * @param top The new value of the view's top property.
7995 * @param right The new value of the view's right property.
7996 * @param bottom The new value of the view's bottom property.
7997 * @param oldLeft The previous value of the view's left property.
7998 * @param oldTop The previous value of the view's top property.
7999 * @param oldRight The previous value of the view's right property.
8000 * @param oldBottom The previous value of the view's bottom property.
8001 */
8002 void onLayoutChange(View v, int left, int top, int right, int bottom,
8003 int oldLeft, int oldTop, int oldRight, int oldBottom);
8004 }
8005
8006 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 * This is called during layout when the size of this view has changed. If
8008 * you were just added to the view hierarchy, you're called with the old
8009 * values of 0.
8010 *
8011 * @param w Current width of this view.
8012 * @param h Current height of this view.
8013 * @param oldw Old width of this view.
8014 * @param oldh Old height of this view.
8015 */
8016 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
8017 }
8018
8019 /**
8020 * Called by draw to draw the child views. This may be overridden
8021 * by derived classes to gain control just before its children are drawn
8022 * (but after its own view has been drawn).
8023 * @param canvas the canvas on which to draw the view
8024 */
8025 protected void dispatchDraw(Canvas canvas) {
Svetoslav Ganov42138042012-03-20 11:51:39 -07008026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008027 }
8028
8029 /**
8030 * Gets the parent of this view. Note that the parent is a
8031 * ViewParent and not necessarily a View.
8032 *
8033 * @return Parent of this view.
8034 */
8035 public final ViewParent getParent() {
8036 return mParent;
8037 }
8038
8039 /**
Chet Haasecca2c982011-05-20 14:34:18 -07008040 * Set the horizontal scrolled position of your view. This will cause a call to
8041 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8042 * invalidated.
8043 * @param value the x position to scroll to
8044 */
8045 public void setScrollX(int value) {
8046 scrollTo(value, mScrollY);
8047 }
8048
8049 /**
8050 * Set the vertical scrolled position of your view. This will cause a call to
8051 * {@link #onScrollChanged(int, int, int, int)} and the view will be
8052 * invalidated.
8053 * @param value the y position to scroll to
8054 */
8055 public void setScrollY(int value) {
8056 scrollTo(mScrollX, value);
8057 }
8058
8059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 * Return the scrolled left position of this view. This is the left edge of
8061 * the displayed part of your view. You do not need to draw any pixels
8062 * farther left, since those are outside of the frame of your view on
8063 * screen.
8064 *
8065 * @return The left edge of the displayed part of your view, in pixels.
8066 */
8067 public final int getScrollX() {
8068 return mScrollX;
8069 }
8070
8071 /**
8072 * Return the scrolled top position of this view. This is the top edge of
8073 * the displayed part of your view. You do not need to draw any pixels above
8074 * it, since those are outside of the frame of your view on screen.
8075 *
8076 * @return The top edge of the displayed part of your view, in pixels.
8077 */
8078 public final int getScrollY() {
8079 return mScrollY;
8080 }
8081
8082 /**
8083 * Return the width of the your view.
8084 *
8085 * @return The width of your view, in pixels.
8086 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008087 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088 public final int getWidth() {
8089 return mRight - mLeft;
8090 }
8091
8092 /**
8093 * Return the height of your view.
8094 *
8095 * @return The height of your view, in pixels.
8096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008097 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008098 public final int getHeight() {
8099 return mBottom - mTop;
8100 }
8101
8102 /**
8103 * Return the visible drawing bounds of your view. Fills in the output
8104 * rectangle with the values from getScrollX(), getScrollY(),
8105 * getWidth(), and getHeight().
8106 *
8107 * @param outRect The (scrolled) drawing bounds of the view.
8108 */
8109 public void getDrawingRect(Rect outRect) {
8110 outRect.left = mScrollX;
8111 outRect.top = mScrollY;
8112 outRect.right = mScrollX + (mRight - mLeft);
8113 outRect.bottom = mScrollY + (mBottom - mTop);
8114 }
8115
8116 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008117 * Like {@link #getMeasuredWidthAndState()}, but only returns the
8118 * raw width component (that is the result is masked by
8119 * {@link #MEASURED_SIZE_MASK}).
8120 *
8121 * @return The raw measured width of this view.
8122 */
8123 public final int getMeasuredWidth() {
8124 return mMeasuredWidth & MEASURED_SIZE_MASK;
8125 }
8126
8127 /**
8128 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008129 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008130 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 * This should be used during measurement and layout calculations only. Use
8132 * {@link #getWidth()} to see how wide a view is after layout.
8133 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008134 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08008136 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 return mMeasuredWidth;
8138 }
8139
8140 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008141 * Like {@link #getMeasuredHeightAndState()}, but only returns the
8142 * raw width component (that is the result is masked by
8143 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008144 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08008145 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008146 */
8147 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08008148 return mMeasuredHeight & MEASURED_SIZE_MASK;
8149 }
8150
8151 /**
8152 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07008153 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08008154 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
8155 * This should be used during measurement and layout calculations only. Use
8156 * {@link #getHeight()} to see how wide a view is after layout.
8157 *
8158 * @return The measured width of this view as a bit mask.
8159 */
8160 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 return mMeasuredHeight;
8162 }
8163
8164 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08008165 * Return only the state bits of {@link #getMeasuredWidthAndState()}
8166 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
8167 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
8168 * and the height component is at the shifted bits
8169 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
8170 */
8171 public final int getMeasuredState() {
8172 return (mMeasuredWidth&MEASURED_STATE_MASK)
8173 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
8174 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
8175 }
8176
8177 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008178 * The transform matrix of this view, which is calculated based on the current
8179 * roation, scale, and pivot properties.
8180 *
8181 * @see #getRotation()
8182 * @see #getScaleX()
8183 * @see #getScaleY()
8184 * @see #getPivotX()
8185 * @see #getPivotY()
8186 * @return The current transform matrix for the view
8187 */
8188 public Matrix getMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008189 if (mTransformationInfo != null) {
8190 updateMatrix();
8191 return mTransformationInfo.mMatrix;
8192 }
8193 return Matrix.IDENTITY_MATRIX;
Romain Guy33e72ae2010-07-17 12:40:29 -07008194 }
8195
8196 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008197 * Utility function to determine if the value is far enough away from zero to be
8198 * considered non-zero.
8199 * @param value A floating point value to check for zero-ness
8200 * @return whether the passed-in value is far enough away from zero to be considered non-zero
8201 */
8202 private static boolean nonzero(float value) {
8203 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
8204 }
8205
8206 /**
Jeff Brown86671742010-09-30 20:00:15 -07008207 * Returns true if the transform matrix is the identity matrix.
8208 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08008209 *
Romain Guy33e72ae2010-07-17 12:40:29 -07008210 * @return True if the transform matrix is the identity matrix, false otherwise.
8211 */
Jeff Brown86671742010-09-30 20:00:15 -07008212 final boolean hasIdentityMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008213 if (mTransformationInfo != null) {
8214 updateMatrix();
8215 return mTransformationInfo.mMatrixIsIdentity;
8216 }
8217 return true;
8218 }
8219
8220 void ensureTransformationInfo() {
8221 if (mTransformationInfo == null) {
8222 mTransformationInfo = new TransformationInfo();
8223 }
Jeff Brown86671742010-09-30 20:00:15 -07008224 }
8225
8226 /**
8227 * Recomputes the transform matrix if necessary.
8228 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008229 private void updateMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008230 final TransformationInfo info = mTransformationInfo;
8231 if (info == null) {
8232 return;
8233 }
8234 if (info.mMatrixDirty) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008235 // transform-related properties have changed since the last time someone
8236 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07008237
8238 // Figure out if we need to update the pivot point
8239 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008240 if ((mRight - mLeft) != info.mPrevWidth || (mBottom - mTop) != info.mPrevHeight) {
8241 info.mPrevWidth = mRight - mLeft;
8242 info.mPrevHeight = mBottom - mTop;
8243 info.mPivotX = info.mPrevWidth / 2f;
8244 info.mPivotY = info.mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07008245 }
8246 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008247 info.mMatrix.reset();
8248 if (!nonzero(info.mRotationX) && !nonzero(info.mRotationY)) {
8249 info.mMatrix.setTranslate(info.mTranslationX, info.mTranslationY);
8250 info.mMatrix.preRotate(info.mRotation, info.mPivotX, info.mPivotY);
8251 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07008252 } else {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008253 if (info.mCamera == null) {
8254 info.mCamera = new Camera();
8255 info.matrix3D = new Matrix();
Chet Haasefd2b0022010-08-06 13:08:56 -07008256 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008257 info.mCamera.save();
8258 info.mMatrix.preScale(info.mScaleX, info.mScaleY, info.mPivotX, info.mPivotY);
8259 info.mCamera.rotate(info.mRotationX, info.mRotationY, -info.mRotation);
8260 info.mCamera.getMatrix(info.matrix3D);
8261 info.matrix3D.preTranslate(-info.mPivotX, -info.mPivotY);
8262 info.matrix3D.postTranslate(info.mPivotX + info.mTranslationX,
8263 info.mPivotY + info.mTranslationY);
8264 info.mMatrix.postConcat(info.matrix3D);
8265 info.mCamera.restore();
Chet Haasefd2b0022010-08-06 13:08:56 -07008266 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008267 info.mMatrixDirty = false;
8268 info.mMatrixIsIdentity = info.mMatrix.isIdentity();
8269 info.mInverseMatrixDirty = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07008270 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008271 }
8272
8273 /**
8274 * Utility method to retrieve the inverse of the current mMatrix property.
8275 * We cache the matrix to avoid recalculating it when transform properties
8276 * have not changed.
8277 *
8278 * @return The inverse of the current matrix of this view.
8279 */
Jeff Brown86671742010-09-30 20:00:15 -07008280 final Matrix getInverseMatrix() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008281 final TransformationInfo info = mTransformationInfo;
8282 if (info != null) {
8283 updateMatrix();
8284 if (info.mInverseMatrixDirty) {
8285 if (info.mInverseMatrix == null) {
8286 info.mInverseMatrix = new Matrix();
8287 }
8288 info.mMatrix.invert(info.mInverseMatrix);
8289 info.mInverseMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07008290 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008291 return info.mInverseMatrix;
Chet Haasec3aa3612010-06-17 08:50:37 -07008292 }
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008293 return Matrix.IDENTITY_MATRIX;
Chet Haasec3aa3612010-06-17 08:50:37 -07008294 }
8295
8296 /**
Chet Haasea1cff502012-02-21 13:43:44 -08008297 * Gets the distance along the Z axis from the camera to this view.
8298 *
8299 * @see #setCameraDistance(float)
8300 *
8301 * @return The distance along the Z axis.
8302 */
8303 public float getCameraDistance() {
8304 ensureTransformationInfo();
8305 final float dpi = mResources.getDisplayMetrics().densityDpi;
8306 final TransformationInfo info = mTransformationInfo;
8307 if (info.mCamera == null) {
8308 info.mCamera = new Camera();
8309 info.matrix3D = new Matrix();
8310 }
8311 return -(info.mCamera.getLocationZ() * dpi);
8312 }
8313
8314 /**
Romain Guya5364ee2011-02-24 14:46:04 -08008315 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
8316 * views are drawn) from the camera to this view. The camera's distance
8317 * affects 3D transformations, for instance rotations around the X and Y
8318 * axis. If the rotationX or rotationY properties are changed and this view is
Philip Milne6c8ea062012-04-03 17:38:43 -07008319 * large (more than half the size of the screen), it is recommended to always
Romain Guya5364ee2011-02-24 14:46:04 -08008320 * use a camera distance that's greater than the height (X axis rotation) or
8321 * the width (Y axis rotation) of this view.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008322 *
Romain Guya5364ee2011-02-24 14:46:04 -08008323 * <p>The distance of the camera from the view plane can have an affect on the
8324 * perspective distortion of the view when it is rotated around the x or y axis.
8325 * For example, a large distance will result in a large viewing angle, and there
8326 * will not be much perspective distortion of the view as it rotates. A short
Philip Milne6c8ea062012-04-03 17:38:43 -07008327 * distance may cause much more perspective distortion upon rotation, and can
Romain Guya5364ee2011-02-24 14:46:04 -08008328 * also result in some drawing artifacts if the rotated view ends up partially
8329 * behind the camera (which is why the recommendation is to use a distance at
8330 * least as far as the size of the view, if the view is to be rotated.)</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008331 *
Romain Guya5364ee2011-02-24 14:46:04 -08008332 * <p>The distance is expressed in "depth pixels." The default distance depends
8333 * on the screen density. For instance, on a medium density display, the
8334 * default distance is 1280. On a high density display, the default distance
8335 * is 1920.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008336 *
Romain Guya5364ee2011-02-24 14:46:04 -08008337 * <p>If you want to specify a distance that leads to visually consistent
8338 * results across various densities, use the following formula:</p>
8339 * <pre>
8340 * float scale = context.getResources().getDisplayMetrics().density;
8341 * view.setCameraDistance(distance * scale);
8342 * </pre>
Philip Milne6c8ea062012-04-03 17:38:43 -07008343 *
Romain Guya5364ee2011-02-24 14:46:04 -08008344 * <p>The density scale factor of a high density display is 1.5,
8345 * and 1920 = 1280 * 1.5.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07008346 *
Romain Guya5364ee2011-02-24 14:46:04 -08008347 * @param distance The distance in "depth pixels", if negative the opposite
8348 * value is used
Philip Milne6c8ea062012-04-03 17:38:43 -07008349 *
8350 * @see #setRotationX(float)
8351 * @see #setRotationY(float)
Romain Guya5364ee2011-02-24 14:46:04 -08008352 */
8353 public void setCameraDistance(float distance) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008354 invalidateViewProperty(true, false);
Romain Guya5364ee2011-02-24 14:46:04 -08008355
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008356 ensureTransformationInfo();
Romain Guya5364ee2011-02-24 14:46:04 -08008357 final float dpi = mResources.getDisplayMetrics().densityDpi;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008358 final TransformationInfo info = mTransformationInfo;
8359 if (info.mCamera == null) {
8360 info.mCamera = new Camera();
8361 info.matrix3D = new Matrix();
Romain Guya5364ee2011-02-24 14:46:04 -08008362 }
8363
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008364 info.mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
8365 info.mMatrixDirty = true;
Romain Guya5364ee2011-02-24 14:46:04 -08008366
Chet Haase9d1992d2012-03-13 11:03:25 -07008367 invalidateViewProperty(false, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008368 if (mDisplayList != null) {
Chet Haaseb85967b2012-03-26 14:37:51 -07008369 mDisplayList.setCameraDistance(-Math.abs(distance) / dpi);
Chet Haasea1cff502012-02-21 13:43:44 -08008370 }
Romain Guya5364ee2011-02-24 14:46:04 -08008371 }
8372
8373 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008374 * The degrees that the view is rotated around the pivot point.
8375 *
Philip Milne6c8ea062012-04-03 17:38:43 -07008376 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07008377 * @see #getPivotX()
8378 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008379 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008380 * @return The degrees of rotation.
8381 */
Chet Haasea5531132012-02-02 13:41:44 -08008382 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008383 public float getRotation() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008384 return mTransformationInfo != null ? mTransformationInfo.mRotation : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008385 }
8386
8387 /**
Chet Haase897247b2010-09-09 14:54:47 -07008388 * Sets the degrees that the view is rotated around the pivot point. Increasing values
8389 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07008390 *
8391 * @param rotation The degrees of rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008392 *
8393 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07008394 * @see #getPivotX()
8395 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008396 * @see #setRotationX(float)
8397 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08008398 *
8399 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07008400 */
8401 public void setRotation(float rotation) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008402 ensureTransformationInfo();
8403 final TransformationInfo info = mTransformationInfo;
8404 if (info.mRotation != rotation) {
Chet Haasec3aa3612010-06-17 08:50:37 -07008405 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07008406 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008407 info.mRotation = rotation;
8408 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008409 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008410 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008411 mDisplayList.setRotation(rotation);
8412 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008413 }
8414 }
8415
8416 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07008417 * The degrees that the view is rotated around the vertical axis through the pivot point.
8418 *
8419 * @see #getPivotX()
8420 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008421 * @see #setRotationY(float)
8422 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008423 * @return The degrees of Y rotation.
8424 */
Chet Haasea5531132012-02-02 13:41:44 -08008425 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008426 public float getRotationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008427 return mTransformationInfo != null ? mTransformationInfo.mRotationY : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008428 }
8429
8430 /**
Chet Haase897247b2010-09-09 14:54:47 -07008431 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
8432 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
8433 * down the y axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008434 *
Romain Guya5364ee2011-02-24 14:46:04 -08008435 * When rotating large views, it is recommended to adjust the camera distance
8436 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008437 *
8438 * @param rotationY The degrees of Y rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008439 *
8440 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07008441 * @see #getPivotX()
8442 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008443 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008444 * @see #setRotationX(float)
8445 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008446 *
8447 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07008448 */
8449 public void setRotationY(float rotationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008450 ensureTransformationInfo();
8451 final TransformationInfo info = mTransformationInfo;
8452 if (info.mRotationY != rotationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008453 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008454 info.mRotationY = rotationY;
8455 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008456 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008457 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008458 mDisplayList.setRotationY(rotationY);
8459 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008460 }
8461 }
8462
8463 /**
8464 * The degrees that the view is rotated around the horizontal axis through the pivot point.
8465 *
8466 * @see #getPivotX()
8467 * @see #getPivotY()
Philip Milne6c8ea062012-04-03 17:38:43 -07008468 * @see #setRotationX(float)
8469 *
Chet Haasefd2b0022010-08-06 13:08:56 -07008470 * @return The degrees of X rotation.
8471 */
Chet Haasea5531132012-02-02 13:41:44 -08008472 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasefd2b0022010-08-06 13:08:56 -07008473 public float getRotationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008474 return mTransformationInfo != null ? mTransformationInfo.mRotationX : 0;
Chet Haasefd2b0022010-08-06 13:08:56 -07008475 }
8476
8477 /**
Chet Haase897247b2010-09-09 14:54:47 -07008478 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
8479 * Increasing values result in clockwise rotation from the viewpoint of looking down the
8480 * x axis.
Philip Milne6c8ea062012-04-03 17:38:43 -07008481 *
Romain Guya5364ee2011-02-24 14:46:04 -08008482 * When rotating large views, it is recommended to adjust the camera distance
8483 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07008484 *
8485 * @param rotationX The degrees of X rotation.
Philip Milne6c8ea062012-04-03 17:38:43 -07008486 *
8487 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07008488 * @see #getPivotX()
8489 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08008490 * @see #setRotation(float)
Philip Milne6c8ea062012-04-03 17:38:43 -07008491 * @see #setRotationY(float)
8492 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08008493 *
8494 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07008495 */
8496 public void setRotationX(float rotationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008497 ensureTransformationInfo();
8498 final TransformationInfo info = mTransformationInfo;
8499 if (info.mRotationX != rotationX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008500 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008501 info.mRotationX = rotationX;
8502 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008503 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008504 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008505 mDisplayList.setRotationX(rotationX);
8506 }
Chet Haasefd2b0022010-08-06 13:08:56 -07008507 }
8508 }
8509
8510 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07008511 * The amount that the view is scaled in x around the pivot point, as a proportion of
8512 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
8513 *
Joe Onorato93162322010-09-16 15:42:01 -04008514 * <p>By default, this is 1.0f.
8515 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008516 * @see #getPivotX()
8517 * @see #getPivotY()
8518 * @return The scaling factor.
8519 */
Chet Haasea5531132012-02-02 13:41:44 -08008520 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008521 public float getScaleX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008522 return mTransformationInfo != null ? mTransformationInfo.mScaleX : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008523 }
8524
8525 /**
8526 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
8527 * the view's unscaled width. A value of 1 means that no scaling is applied.
8528 *
8529 * @param scaleX The scaling factor.
8530 * @see #getPivotX()
8531 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008532 *
8533 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07008534 */
8535 public void setScaleX(float scaleX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008536 ensureTransformationInfo();
8537 final TransformationInfo info = mTransformationInfo;
8538 if (info.mScaleX != scaleX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008539 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008540 info.mScaleX = scaleX;
8541 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008542 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008543 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008544 mDisplayList.setScaleX(scaleX);
8545 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008546 }
8547 }
8548
8549 /**
8550 * The amount that the view is scaled in y around the pivot point, as a proportion of
8551 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
8552 *
Joe Onorato93162322010-09-16 15:42:01 -04008553 * <p>By default, this is 1.0f.
8554 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008555 * @see #getPivotX()
8556 * @see #getPivotY()
8557 * @return The scaling factor.
8558 */
Chet Haasea5531132012-02-02 13:41:44 -08008559 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008560 public float getScaleY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008561 return mTransformationInfo != null ? mTransformationInfo.mScaleY : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008562 }
8563
8564 /**
8565 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
8566 * the view's unscaled width. A value of 1 means that no scaling is applied.
8567 *
8568 * @param scaleY The scaling factor.
8569 * @see #getPivotX()
8570 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008571 *
8572 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07008573 */
8574 public void setScaleY(float scaleY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008575 ensureTransformationInfo();
8576 final TransformationInfo info = mTransformationInfo;
8577 if (info.mScaleY != scaleY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008578 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008579 info.mScaleY = scaleY;
8580 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008581 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008582 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008583 mDisplayList.setScaleY(scaleY);
8584 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008585 }
8586 }
8587
8588 /**
8589 * The x location of the point around which the view is {@link #setRotation(float) rotated}
8590 * and {@link #setScaleX(float) scaled}.
8591 *
8592 * @see #getRotation()
8593 * @see #getScaleX()
8594 * @see #getScaleY()
8595 * @see #getPivotY()
8596 * @return The x location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008597 *
8598 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008599 */
Chet Haasea5531132012-02-02 13:41:44 -08008600 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008601 public float getPivotX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008602 return mTransformationInfo != null ? mTransformationInfo.mPivotX : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008603 }
8604
8605 /**
8606 * Sets the x location of the point around which the view is
8607 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07008608 * By default, the pivot point is centered on the object.
8609 * Setting this property disables this behavior and causes the view to use only the
8610 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008611 *
8612 * @param pivotX The x location of the pivot point.
8613 * @see #getRotation()
8614 * @see #getScaleX()
8615 * @see #getScaleY()
8616 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008617 *
8618 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07008619 */
8620 public void setPivotX(float pivotX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008621 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008622 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008623 final TransformationInfo info = mTransformationInfo;
8624 if (info.mPivotX != pivotX) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008625 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008626 info.mPivotX = pivotX;
8627 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008628 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008629 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008630 mDisplayList.setPivotX(pivotX);
8631 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008632 }
8633 }
8634
8635 /**
8636 * The y location of the point around which the view is {@link #setRotation(float) rotated}
8637 * and {@link #setScaleY(float) scaled}.
8638 *
8639 * @see #getRotation()
8640 * @see #getScaleX()
8641 * @see #getScaleY()
8642 * @see #getPivotY()
8643 * @return The y location of the pivot point.
Philip Milne6c8ea062012-04-03 17:38:43 -07008644 *
8645 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008646 */
Chet Haasea5531132012-02-02 13:41:44 -08008647 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008648 public float getPivotY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008649 return mTransformationInfo != null ? mTransformationInfo.mPivotY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07008650 }
8651
8652 /**
8653 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07008654 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
8655 * Setting this property disables this behavior and causes the view to use only the
8656 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07008657 *
8658 * @param pivotY The y location of the pivot point.
8659 * @see #getRotation()
8660 * @see #getScaleX()
8661 * @see #getScaleY()
8662 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08008663 *
8664 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07008665 */
8666 public void setPivotY(float pivotY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008667 ensureTransformationInfo();
Chet Haasefd2b0022010-08-06 13:08:56 -07008668 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008669 final TransformationInfo info = mTransformationInfo;
8670 if (info.mPivotY != pivotY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07008671 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008672 info.mPivotY = pivotY;
8673 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07008674 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07008675 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008676 mDisplayList.setPivotY(pivotY);
8677 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008678 }
8679 }
8680
8681 /**
8682 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
8683 * completely transparent and 1 means the view is completely opaque.
8684 *
Joe Onorato93162322010-09-16 15:42:01 -04008685 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07008686 * @return The opacity of the view.
8687 */
Chet Haasea5531132012-02-02 13:41:44 -08008688 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasec3aa3612010-06-17 08:50:37 -07008689 public float getAlpha() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008690 return mTransformationInfo != null ? mTransformationInfo.mAlpha : 1;
Chet Haasec3aa3612010-06-17 08:50:37 -07008691 }
8692
8693 /**
Chet Haasedb8c9a62012-03-21 18:54:18 -07008694 * Returns whether this View has content which overlaps. This function, intended to be
8695 * overridden by specific View types, is an optimization when alpha is set on a view. If
8696 * rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
8697 * and then composited it into place, which can be expensive. If the view has no overlapping
8698 * rendering, the view can draw each primitive with the appropriate alpha value directly.
8699 * An example of overlapping rendering is a TextView with a background image, such as a
8700 * Button. An example of non-overlapping rendering is a TextView with no background, or
8701 * an ImageView with only the foreground image. The default implementation returns true;
8702 * subclasses should override if they have cases which can be optimized.
8703 *
8704 * @return true if the content in this view might overlap, false otherwise.
8705 */
8706 public boolean hasOverlappingRendering() {
8707 return true;
8708 }
8709
8710 /**
Romain Guy171c5922011-01-06 10:04:23 -08008711 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
8712 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008713 *
Romain Guy171c5922011-01-06 10:04:23 -08008714 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
8715 * responsible for applying the opacity itself. Otherwise, calling this method is
8716 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08008717 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07008718 *
Chet Haasea5531132012-02-02 13:41:44 -08008719 * <p>Note that setting alpha to a translucent value (0 < alpha < 1) may have
8720 * performance implications. It is generally best to use the alpha property sparingly and
8721 * transiently, as in the case of fading animations.</p>
8722 *
Chet Haasec3aa3612010-06-17 08:50:37 -07008723 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08008724 *
Joe Malin32736f02011-01-19 16:14:20 -08008725 * @see #setLayerType(int, android.graphics.Paint)
8726 *
Chet Haase73066682010-11-29 15:55:32 -08008727 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07008728 */
8729 public void setAlpha(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008730 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008731 if (mTransformationInfo.mAlpha != alpha) {
8732 mTransformationInfo.mAlpha = alpha;
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008733 if (onSetAlpha((int) (alpha * 255))) {
8734 mPrivateFlags |= ALPHA_SET;
8735 // subclass is handling alpha - don't optimize rendering cache invalidation
Chet Haase9d1992d2012-03-13 11:03:25 -07008736 invalidateParentCaches();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008737 invalidate(true);
8738 } else {
8739 mPrivateFlags &= ~ALPHA_SET;
Chet Haase9d1992d2012-03-13 11:03:25 -07008740 invalidateViewProperty(true, false);
Chet Haase1271e2c2012-04-20 09:54:27 -07008741 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008742 mDisplayList.setAlpha(alpha);
8743 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008744 }
Chet Haaseed032702010-10-01 14:05:54 -07008745 }
Chet Haasec3aa3612010-06-17 08:50:37 -07008746 }
8747
8748 /**
Chet Haasea00f3862011-02-22 06:34:40 -08008749 * Faster version of setAlpha() which performs the same steps except there are
8750 * no calls to invalidate(). The caller of this function should perform proper invalidation
8751 * on the parent and this object. The return value indicates whether the subclass handles
8752 * alpha (the return value for onSetAlpha()).
8753 *
8754 * @param alpha The new value for the alpha property
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008755 * @return true if the View subclass handles alpha (the return value for onSetAlpha()) and
8756 * the new value for the alpha property is different from the old value
Chet Haasea00f3862011-02-22 06:34:40 -08008757 */
8758 boolean setAlphaNoInvalidation(float alpha) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008759 ensureTransformationInfo();
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008760 if (mTransformationInfo.mAlpha != alpha) {
8761 mTransformationInfo.mAlpha = alpha;
8762 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
8763 if (subclassHandlesAlpha) {
8764 mPrivateFlags |= ALPHA_SET;
8765 return true;
8766 } else {
8767 mPrivateFlags &= ~ALPHA_SET;
Chet Haase1271e2c2012-04-20 09:54:27 -07008768 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008769 mDisplayList.setAlpha(alpha);
8770 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008771 }
Chet Haasea00f3862011-02-22 06:34:40 -08008772 }
Michael Jurkaa7a7eed2012-01-17 06:06:17 -08008773 return false;
Chet Haasea00f3862011-02-22 06:34:40 -08008774 }
8775
8776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 * Top position of this view relative to its parent.
8778 *
8779 * @return The top of this view, in pixels.
8780 */
8781 @ViewDebug.CapturedViewProperty
8782 public final int getTop() {
8783 return mTop;
8784 }
8785
8786 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008787 * Sets the top position of this view relative to its parent. This method is meant to be called
8788 * by the layout system and should not generally be called otherwise, because the property
8789 * may be changed at any time by the layout.
8790 *
8791 * @param top The top of this view, in pixels.
8792 */
8793 public final void setTop(int top) {
8794 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07008795 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008796 final boolean matrixIsIdentity = mTransformationInfo == null
8797 || mTransformationInfo.mMatrixIsIdentity;
8798 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008799 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008800 int minTop;
8801 int yLoc;
8802 if (top < mTop) {
8803 minTop = top;
8804 yLoc = top - mTop;
8805 } else {
8806 minTop = mTop;
8807 yLoc = 0;
8808 }
Chet Haasee9140a72011-02-16 16:23:29 -08008809 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008810 }
8811 } else {
8812 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008813 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008814 }
8815
Chet Haaseed032702010-10-01 14:05:54 -07008816 int width = mRight - mLeft;
8817 int oldHeight = mBottom - mTop;
8818
Chet Haase21cd1382010-09-01 17:42:29 -07008819 mTop = top;
Chet Haase1271e2c2012-04-20 09:54:27 -07008820 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008821 mDisplayList.setTop(mTop);
8822 }
Chet Haase21cd1382010-09-01 17:42:29 -07008823
Chet Haaseed032702010-10-01 14:05:54 -07008824 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8825
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008826 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008827 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8828 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008829 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008830 }
Chet Haase21cd1382010-09-01 17:42:29 -07008831 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008832 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008833 }
Chet Haase55dbb652010-12-21 20:15:08 -08008834 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008835 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008836 }
8837 }
8838
8839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 * Bottom position of this view relative to its parent.
8841 *
8842 * @return The bottom of this view, in pixels.
8843 */
8844 @ViewDebug.CapturedViewProperty
8845 public final int getBottom() {
8846 return mBottom;
8847 }
8848
8849 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08008850 * True if this view has changed since the last time being drawn.
8851 *
8852 * @return The dirty state of this view.
8853 */
8854 public boolean isDirty() {
8855 return (mPrivateFlags & DIRTY_MASK) != 0;
8856 }
8857
8858 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008859 * Sets the bottom position of this view relative to its parent. This method is meant to be
8860 * called by the layout system and should not generally be called otherwise, because the
8861 * property may be changed at any time by the layout.
8862 *
8863 * @param bottom The bottom of this view, in pixels.
8864 */
8865 public final void setBottom(int bottom) {
8866 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07008867 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008868 final boolean matrixIsIdentity = mTransformationInfo == null
8869 || mTransformationInfo.mMatrixIsIdentity;
8870 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008871 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008872 int maxBottom;
8873 if (bottom < mBottom) {
8874 maxBottom = mBottom;
8875 } else {
8876 maxBottom = bottom;
8877 }
Chet Haasee9140a72011-02-16 16:23:29 -08008878 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008879 }
8880 } else {
8881 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008882 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008883 }
8884
Chet Haaseed032702010-10-01 14:05:54 -07008885 int width = mRight - mLeft;
8886 int oldHeight = mBottom - mTop;
8887
Chet Haase21cd1382010-09-01 17:42:29 -07008888 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -07008889 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008890 mDisplayList.setBottom(mBottom);
8891 }
Chet Haase21cd1382010-09-01 17:42:29 -07008892
Chet Haaseed032702010-10-01 14:05:54 -07008893 onSizeChanged(width, mBottom - mTop, width, oldHeight);
8894
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008895 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008896 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8897 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008898 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008899 }
Chet Haase21cd1382010-09-01 17:42:29 -07008900 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008901 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008902 }
Chet Haase55dbb652010-12-21 20:15:08 -08008903 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008904 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008905 }
8906 }
8907
8908 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909 * Left position of this view relative to its parent.
8910 *
8911 * @return The left edge of this view, in pixels.
8912 */
8913 @ViewDebug.CapturedViewProperty
8914 public final int getLeft() {
8915 return mLeft;
8916 }
8917
8918 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008919 * Sets the left position of this view relative to its parent. This method is meant to be called
8920 * by the layout system and should not generally be called otherwise, because the property
8921 * may be changed at any time by the layout.
8922 *
8923 * @param left The bottom of this view, in pixels.
8924 */
8925 public final void setLeft(int left) {
8926 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07008927 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008928 final boolean matrixIsIdentity = mTransformationInfo == null
8929 || mTransformationInfo.mMatrixIsIdentity;
8930 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008931 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008932 int minLeft;
8933 int xLoc;
8934 if (left < mLeft) {
8935 minLeft = left;
8936 xLoc = left - mLeft;
8937 } else {
8938 minLeft = mLeft;
8939 xLoc = 0;
8940 }
Chet Haasee9140a72011-02-16 16:23:29 -08008941 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07008942 }
8943 } else {
8944 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08008945 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008946 }
8947
Chet Haaseed032702010-10-01 14:05:54 -07008948 int oldWidth = mRight - mLeft;
8949 int height = mBottom - mTop;
8950
Chet Haase21cd1382010-09-01 17:42:29 -07008951 mLeft = left;
Chet Haase1271e2c2012-04-20 09:54:27 -07008952 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08008953 mDisplayList.setLeft(left);
8954 }
Chet Haase21cd1382010-09-01 17:42:29 -07008955
Chet Haaseed032702010-10-01 14:05:54 -07008956 onSizeChanged(mRight - mLeft, height, oldWidth, height);
8957
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008958 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008959 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
8960 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008961 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08008962 }
Chet Haase21cd1382010-09-01 17:42:29 -07008963 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08008964 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07008965 }
Chet Haase55dbb652010-12-21 20:15:08 -08008966 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08008967 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07008968 }
8969 }
8970
8971 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008972 * Right position of this view relative to its parent.
8973 *
8974 * @return The right edge of this view, in pixels.
8975 */
8976 @ViewDebug.CapturedViewProperty
8977 public final int getRight() {
8978 return mRight;
8979 }
8980
8981 /**
Chet Haase21cd1382010-09-01 17:42:29 -07008982 * Sets the right position of this view relative to its parent. This method is meant to be called
8983 * by the layout system and should not generally be called otherwise, because the property
8984 * may be changed at any time by the layout.
8985 *
8986 * @param right The bottom of this view, in pixels.
8987 */
8988 public final void setRight(int right) {
8989 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07008990 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07008991 final boolean matrixIsIdentity = mTransformationInfo == null
8992 || mTransformationInfo.mMatrixIsIdentity;
8993 if (matrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08008994 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07008995 int maxRight;
8996 if (right < mRight) {
8997 maxRight = mRight;
8998 } else {
8999 maxRight = right;
9000 }
Chet Haasee9140a72011-02-16 16:23:29 -08009001 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07009002 }
9003 } else {
9004 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08009005 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009006 }
9007
Chet Haaseed032702010-10-01 14:05:54 -07009008 int oldWidth = mRight - mLeft;
9009 int height = mBottom - mTop;
9010
Chet Haase21cd1382010-09-01 17:42:29 -07009011 mRight = right;
Chet Haase1271e2c2012-04-20 09:54:27 -07009012 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009013 mDisplayList.setRight(mRight);
9014 }
Chet Haase21cd1382010-09-01 17:42:29 -07009015
Chet Haaseed032702010-10-01 14:05:54 -07009016 onSizeChanged(mRight - mLeft, height, oldWidth, height);
9017
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009018 if (!matrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009019 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9020 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009021 mTransformationInfo.mMatrixDirty = true;
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009022 }
Chet Haase21cd1382010-09-01 17:42:29 -07009023 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08009024 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07009025 }
Chet Haase55dbb652010-12-21 20:15:08 -08009026 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08009027 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07009028 }
9029 }
9030
9031 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009032 * The visual x position of this view, in pixels. This is equivalent to the
9033 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08009034 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07009035 *
Chet Haasedf030d22010-07-30 17:22:38 -07009036 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009037 */
Chet Haasea5531132012-02-02 13:41:44 -08009038 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009039 public float getX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009040 return mLeft + (mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009041 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009042
Chet Haasedf030d22010-07-30 17:22:38 -07009043 /**
9044 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
9045 * {@link #setTranslationX(float) translationX} property to be the difference between
9046 * the x value passed in and the current {@link #getLeft() left} property.
9047 *
9048 * @param x The visual x position of this view, in pixels.
9049 */
9050 public void setX(float x) {
9051 setTranslationX(x - mLeft);
9052 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009053
Chet Haasedf030d22010-07-30 17:22:38 -07009054 /**
9055 * The visual y position of this view, in pixels. This is equivalent to the
9056 * {@link #setTranslationY(float) translationY} property plus the current
9057 * {@link #getTop() top} property.
9058 *
9059 * @return The visual y position of this view, in pixels.
9060 */
Chet Haasea5531132012-02-02 13:41:44 -08009061 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009062 public float getY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009063 return mTop + (mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0);
Chet Haasedf030d22010-07-30 17:22:38 -07009064 }
9065
9066 /**
9067 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
9068 * {@link #setTranslationY(float) translationY} property to be the difference between
9069 * the y value passed in and the current {@link #getTop() top} property.
9070 *
9071 * @param y The visual y position of this view, in pixels.
9072 */
9073 public void setY(float y) {
9074 setTranslationY(y - mTop);
9075 }
9076
9077
9078 /**
9079 * The horizontal location of this view relative to its {@link #getLeft() left} position.
9080 * This position is post-layout, in addition to wherever the object's
9081 * layout placed it.
9082 *
9083 * @return The horizontal position of this view relative to its left position, in pixels.
9084 */
Chet Haasea5531132012-02-02 13:41:44 -08009085 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009086 public float getTranslationX() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009087 return mTransformationInfo != null ? mTransformationInfo.mTranslationX : 0;
Chet Haasedf030d22010-07-30 17:22:38 -07009088 }
9089
9090 /**
9091 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
9092 * This effectively positions the object post-layout, in addition to wherever the object's
9093 * layout placed it.
9094 *
9095 * @param translationX The horizontal position of this view relative to its left position,
9096 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009097 *
9098 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07009099 */
9100 public void setTranslationX(float translationX) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009101 ensureTransformationInfo();
9102 final TransformationInfo info = mTransformationInfo;
9103 if (info.mTranslationX != translationX) {
Chet Haasedf030d22010-07-30 17:22:38 -07009104 // Double-invalidation is necessary to capture view's old and new areas
Chet Haase9d1992d2012-03-13 11:03:25 -07009105 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009106 info.mTranslationX = translationX;
9107 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009108 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009109 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009110 mDisplayList.setTranslationX(translationX);
9111 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009112 }
9113 }
9114
9115 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009116 * The horizontal location of this view relative to its {@link #getTop() top} position.
9117 * This position is post-layout, in addition to wherever the object's
9118 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009119 *
Chet Haasedf030d22010-07-30 17:22:38 -07009120 * @return The vertical position of this view relative to its top position,
9121 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07009122 */
Chet Haasea5531132012-02-02 13:41:44 -08009123 @ViewDebug.ExportedProperty(category = "drawing")
Chet Haasedf030d22010-07-30 17:22:38 -07009124 public float getTranslationY() {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009125 return mTransformationInfo != null ? mTransformationInfo.mTranslationY : 0;
Chet Haasec3aa3612010-06-17 08:50:37 -07009126 }
9127
9128 /**
Chet Haasedf030d22010-07-30 17:22:38 -07009129 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
9130 * This effectively positions the object post-layout, in addition to wherever the object's
9131 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07009132 *
Chet Haasedf030d22010-07-30 17:22:38 -07009133 * @param translationY The vertical position of this view relative to its top position,
9134 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08009135 *
9136 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07009137 */
Chet Haasedf030d22010-07-30 17:22:38 -07009138 public void setTranslationY(float translationY) {
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009139 ensureTransformationInfo();
9140 final TransformationInfo info = mTransformationInfo;
9141 if (info.mTranslationY != translationY) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009142 invalidateViewProperty(true, false);
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009143 info.mTranslationY = translationY;
9144 info.mMatrixDirty = true;
Chet Haase9d1992d2012-03-13 11:03:25 -07009145 invalidateViewProperty(false, true);
Chet Haase1271e2c2012-04-20 09:54:27 -07009146 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009147 mDisplayList.setTranslationY(translationY);
9148 }
Chet Haasedf030d22010-07-30 17:22:38 -07009149 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009150 }
9151
9152 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 * Hit rectangle in parent's coordinates
9154 *
9155 * @param outRect The hit rectangle of the view.
9156 */
9157 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07009158 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009159 final TransformationInfo info = mTransformationInfo;
9160 if (info == null || info.mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009161 outRect.set(mLeft, mTop, mRight, mBottom);
9162 } else {
9163 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009164 tmpRect.set(-info.mPivotX, -info.mPivotY,
9165 getWidth() - info.mPivotX, getHeight() - info.mPivotY);
9166 info.mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07009167 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
9168 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07009169 }
9170 }
9171
9172 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07009173 * Determines whether the given point, in local coordinates is inside the view.
9174 */
9175 /*package*/ final boolean pointInView(float localX, float localY) {
9176 return localX >= 0 && localX < (mRight - mLeft)
9177 && localY >= 0 && localY < (mBottom - mTop);
9178 }
9179
9180 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009181 * Utility method to determine whether the given point, in local coordinates,
9182 * is inside the view, where the area of the view is expanded by the slop factor.
9183 * This method is called while processing touch-move events to determine if the event
9184 * is still within the view.
9185 */
9186 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07009187 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07009188 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009189 }
9190
9191 /**
9192 * When a view has focus and the user navigates away from it, the next view is searched for
9193 * starting from the rectangle filled in by this method.
9194 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009195 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
9196 * of the view. However, if your view maintains some idea of internal selection,
9197 * such as a cursor, or a selected row or column, you should override this method and
9198 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 *
9200 * @param r The rectangle to fill in, in this view's coordinates.
9201 */
9202 public void getFocusedRect(Rect r) {
9203 getDrawingRect(r);
9204 }
9205
9206 /**
9207 * If some part of this view is not clipped by any of its parents, then
9208 * return that area in r in global (root) coordinates. To convert r to local
Gilles Debunnecea45132011-11-24 02:19:27 +01009209 * coordinates (without taking possible View rotations into account), offset
9210 * it by -globalOffset (e.g. r.offset(-globalOffset.x, -globalOffset.y)).
9211 * If the view is completely clipped or translated out, return false.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 *
9213 * @param r If true is returned, r holds the global coordinates of the
9214 * visible portion of this view.
9215 * @param globalOffset If true is returned, globalOffset holds the dx,dy
9216 * between this view and its root. globalOffet may be null.
9217 * @return true if r is non-empty (i.e. part of the view is visible at the
9218 * root level.
9219 */
9220 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
9221 int width = mRight - mLeft;
9222 int height = mBottom - mTop;
9223 if (width > 0 && height > 0) {
9224 r.set(0, 0, width, height);
9225 if (globalOffset != null) {
9226 globalOffset.set(-mScrollX, -mScrollY);
9227 }
9228 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
9229 }
9230 return false;
9231 }
9232
9233 public final boolean getGlobalVisibleRect(Rect r) {
9234 return getGlobalVisibleRect(r, null);
9235 }
9236
9237 public final boolean getLocalVisibleRect(Rect r) {
Romain Guyab4c4f4f2012-05-06 13:11:24 -07009238 final Point offset = mAttachInfo != null ? mAttachInfo.mPoint : new Point();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 if (getGlobalVisibleRect(r, offset)) {
9240 r.offset(-offset.x, -offset.y); // make r local
9241 return true;
9242 }
9243 return false;
9244 }
9245
9246 /**
9247 * Offset this view's vertical location by the specified number of pixels.
9248 *
9249 * @param offset the number of pixels to offset the view by
9250 */
9251 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009252 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009253 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009254 final boolean matrixIsIdentity = mTransformationInfo == null
9255 || mTransformationInfo.mMatrixIsIdentity;
9256 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009257 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009258 invalidateViewProperty(false, false);
9259 } else {
9260 final ViewParent p = mParent;
9261 if (p != null && mAttachInfo != null) {
9262 final Rect r = mAttachInfo.mTmpInvalRect;
9263 int minTop;
9264 int maxBottom;
9265 int yLoc;
9266 if (offset < 0) {
9267 minTop = mTop + offset;
9268 maxBottom = mBottom;
9269 yLoc = offset;
9270 } else {
9271 minTop = mTop;
9272 maxBottom = mBottom + offset;
9273 yLoc = 0;
9274 }
9275 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
9276 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009277 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009278 }
9279 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009280 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009281 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009282
Chet Haasec3aa3612010-06-17 08:50:37 -07009283 mTop += offset;
9284 mBottom += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009285 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009286 mDisplayList.offsetTopBottom(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009287 invalidateViewProperty(false, false);
9288 } else {
9289 if (!matrixIsIdentity) {
9290 invalidateViewProperty(false, true);
9291 }
9292 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009293 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 }
9296
9297 /**
9298 * Offset this view's horizontal location by the specified amount of pixels.
9299 *
9300 * @param offset the numer of pixels to offset the view by
9301 */
9302 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07009303 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07009304 updateMatrix();
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009305 final boolean matrixIsIdentity = mTransformationInfo == null
9306 || mTransformationInfo.mMatrixIsIdentity;
9307 if (matrixIsIdentity) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009308 if (mDisplayList != null) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009309 invalidateViewProperty(false, false);
9310 } else {
9311 final ViewParent p = mParent;
9312 if (p != null && mAttachInfo != null) {
9313 final Rect r = mAttachInfo.mTmpInvalRect;
9314 int minLeft;
9315 int maxRight;
9316 if (offset < 0) {
9317 minLeft = mLeft + offset;
9318 maxRight = mRight;
9319 } else {
9320 minLeft = mLeft;
9321 maxRight = mRight + offset;
9322 }
9323 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
9324 p.invalidateChild(this, r);
Chet Haase8fbf8d22010-07-30 15:01:32 -07009325 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009326 }
9327 } else {
Chet Haase9d1992d2012-03-13 11:03:25 -07009328 invalidateViewProperty(false, false);
Chet Haasec3aa3612010-06-17 08:50:37 -07009329 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009330
Chet Haasec3aa3612010-06-17 08:50:37 -07009331 mLeft += offset;
9332 mRight += offset;
Chet Haase1271e2c2012-04-20 09:54:27 -07009333 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -08009334 mDisplayList.offsetLeftRight(offset);
Chet Haase9d1992d2012-03-13 11:03:25 -07009335 invalidateViewProperty(false, false);
9336 } else {
9337 if (!matrixIsIdentity) {
9338 invalidateViewProperty(false, true);
9339 }
9340 invalidateParentIfNeeded();
Chet Haasea1cff502012-02-21 13:43:44 -08009341 }
Chet Haasec3aa3612010-06-17 08:50:37 -07009342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009343 }
9344
9345 /**
9346 * Get the LayoutParams associated with this view. All views should have
9347 * layout parameters. These supply parameters to the <i>parent</i> of this
9348 * view specifying how it should be arranged. There are many subclasses of
9349 * ViewGroup.LayoutParams, and these correspond to the different subclasses
9350 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08009351 *
9352 * This method may return null if this View is not attached to a parent
9353 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
9354 * was not invoked successfully. When a View is attached to a parent
9355 * ViewGroup, this method must not return null.
9356 *
9357 * @return The LayoutParams associated with this view, or null if no
9358 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07009360 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 public ViewGroup.LayoutParams getLayoutParams() {
9362 return mLayoutParams;
9363 }
9364
9365 /**
9366 * Set the layout parameters associated with this view. These supply
9367 * parameters to the <i>parent</i> of this view specifying how it should be
9368 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
9369 * correspond to the different subclasses of ViewGroup that are responsible
9370 * for arranging their children.
9371 *
Romain Guy01c174b2011-02-22 11:51:06 -08009372 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009373 */
9374 public void setLayoutParams(ViewGroup.LayoutParams params) {
9375 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08009376 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009377 }
9378 mLayoutParams = params;
Philip Milned7dd8902012-01-26 16:55:30 -08009379 if (mParent instanceof ViewGroup) {
9380 ((ViewGroup) mParent).onSetLayoutParams(this, params);
9381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 requestLayout();
9383 }
9384
9385 /**
9386 * Set the scrolled position of your view. This will cause a call to
9387 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9388 * invalidated.
9389 * @param x the x position to scroll to
9390 * @param y the y position to scroll to
9391 */
9392 public void scrollTo(int x, int y) {
9393 if (mScrollX != x || mScrollY != y) {
9394 int oldX = mScrollX;
9395 int oldY = mScrollY;
9396 mScrollX = x;
9397 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009398 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009399 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07009400 if (!awakenScrollBars()) {
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009401 postInvalidateOnAnimation();
Mike Cleronf116bf82009-09-27 19:14:12 -07009402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009403 }
9404 }
9405
9406 /**
9407 * Move the scrolled position of your view. This will cause a call to
9408 * {@link #onScrollChanged(int, int, int, int)} and the view will be
9409 * invalidated.
9410 * @param x the amount of pixels to scroll by horizontally
9411 * @param y the amount of pixels to scroll by vertically
9412 */
9413 public void scrollBy(int x, int y) {
9414 scrollTo(mScrollX + x, mScrollY + y);
9415 }
9416
9417 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009418 * <p>Trigger the scrollbars to draw. When invoked this method starts an
9419 * animation to fade the scrollbars out after a default delay. If a subclass
9420 * provides animated scrolling, the start delay should equal the duration
9421 * of the scrolling animation.</p>
9422 *
9423 * <p>The animation starts only if at least one of the scrollbars is
9424 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
9425 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9426 * this method returns true, and false otherwise. If the animation is
9427 * started, this method calls {@link #invalidate()}; in that case the
9428 * caller should not call {@link #invalidate()}.</p>
9429 *
9430 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07009431 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07009432 *
9433 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
9434 * and {@link #scrollTo(int, int)}.</p>
9435 *
9436 * @return true if the animation is played, false otherwise
9437 *
9438 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07009439 * @see #scrollBy(int, int)
9440 * @see #scrollTo(int, int)
9441 * @see #isHorizontalScrollBarEnabled()
9442 * @see #isVerticalScrollBarEnabled()
9443 * @see #setHorizontalScrollBarEnabled(boolean)
9444 * @see #setVerticalScrollBarEnabled(boolean)
9445 */
9446 protected boolean awakenScrollBars() {
9447 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07009448 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07009449 }
9450
9451 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07009452 * Trigger the scrollbars to draw.
9453 * This method differs from awakenScrollBars() only in its default duration.
9454 * initialAwakenScrollBars() will show the scroll bars for longer than
9455 * usual to give the user more of a chance to notice them.
9456 *
9457 * @return true if the animation is played, false otherwise.
9458 */
9459 private boolean initialAwakenScrollBars() {
9460 return mScrollCache != null &&
9461 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
9462 }
9463
9464 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07009465 * <p>
9466 * Trigger the scrollbars to draw. When invoked this method starts an
9467 * animation to fade the scrollbars out after a fixed delay. If a subclass
9468 * provides animated scrolling, the start delay should equal the duration of
9469 * the scrolling animation.
9470 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009471 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009472 * <p>
9473 * The animation starts only if at least one of the scrollbars is enabled,
9474 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9475 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9476 * this method returns true, and false otherwise. If the animation is
9477 * started, this method calls {@link #invalidate()}; in that case the caller
9478 * should not call {@link #invalidate()}.
9479 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009480 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009481 * <p>
9482 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07009483 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07009484 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009485 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009486 * @param startDelay the delay, in milliseconds, after which the animation
9487 * should start; when the delay is 0, the animation starts
9488 * immediately
9489 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009490 *
Mike Cleronf116bf82009-09-27 19:14:12 -07009491 * @see #scrollBy(int, int)
9492 * @see #scrollTo(int, int)
9493 * @see #isHorizontalScrollBarEnabled()
9494 * @see #isVerticalScrollBarEnabled()
9495 * @see #setHorizontalScrollBarEnabled(boolean)
9496 * @see #setVerticalScrollBarEnabled(boolean)
9497 */
9498 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07009499 return awakenScrollBars(startDelay, true);
9500 }
Joe Malin32736f02011-01-19 16:14:20 -08009501
Mike Cleron290947b2009-09-29 18:34:32 -07009502 /**
9503 * <p>
9504 * Trigger the scrollbars to draw. When invoked this method starts an
9505 * animation to fade the scrollbars out after a fixed delay. If a subclass
9506 * provides animated scrolling, the start delay should equal the duration of
9507 * the scrolling animation.
9508 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009509 *
Mike Cleron290947b2009-09-29 18:34:32 -07009510 * <p>
9511 * The animation starts only if at least one of the scrollbars is enabled,
9512 * as specified by {@link #isHorizontalScrollBarEnabled()} and
9513 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
9514 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08009515 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07009516 * is set to true; in that case the caller
9517 * should not call {@link #invalidate()}.
9518 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009519 *
Mike Cleron290947b2009-09-29 18:34:32 -07009520 * <p>
9521 * This method should be invoked everytime a subclass directly updates the
9522 * scroll parameters.
9523 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08009524 *
Mike Cleron290947b2009-09-29 18:34:32 -07009525 * @param startDelay the delay, in milliseconds, after which the animation
9526 * should start; when the delay is 0, the animation starts
9527 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08009528 *
Mike Cleron290947b2009-09-29 18:34:32 -07009529 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08009530 *
Mike Cleron290947b2009-09-29 18:34:32 -07009531 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08009532 *
Mike Cleron290947b2009-09-29 18:34:32 -07009533 * @see #scrollBy(int, int)
9534 * @see #scrollTo(int, int)
9535 * @see #isHorizontalScrollBarEnabled()
9536 * @see #isVerticalScrollBarEnabled()
9537 * @see #setHorizontalScrollBarEnabled(boolean)
9538 * @see #setVerticalScrollBarEnabled(boolean)
9539 */
9540 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07009541 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08009542
Mike Cleronf116bf82009-09-27 19:14:12 -07009543 if (scrollCache == null || !scrollCache.fadeScrollBars) {
9544 return false;
9545 }
9546
9547 if (scrollCache.scrollBar == null) {
9548 scrollCache.scrollBar = new ScrollBarDrawable();
9549 }
9550
9551 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
9552
Mike Cleron290947b2009-09-29 18:34:32 -07009553 if (invalidate) {
9554 // Invalidate to show the scrollbars
Adam Powelldf3ae4f2012-04-10 18:55:22 -07009555 postInvalidateOnAnimation();
Mike Cleron290947b2009-09-29 18:34:32 -07009556 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009557
9558 if (scrollCache.state == ScrollabilityCache.OFF) {
9559 // FIXME: this is copied from WindowManagerService.
9560 // We should get this value from the system when it
9561 // is possible to do so.
9562 final int KEY_REPEAT_FIRST_DELAY = 750;
9563 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
9564 }
9565
9566 // Tell mScrollCache when we should start fading. This may
9567 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009568 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07009569 scrollCache.fadeStartTime = fadeStartTime;
9570 scrollCache.state = ScrollabilityCache.ON;
9571
9572 // Schedule our fader to run, unscheduling any old ones first
9573 if (mAttachInfo != null) {
9574 mAttachInfo.mHandler.removeCallbacks(scrollCache);
9575 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
9576 }
9577
9578 return true;
9579 }
9580
9581 return false;
9582 }
9583
9584 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07009585 * Do not invalidate views which are not visible and which are not running an animation. They
9586 * will not get drawn and they should not set dirty flags as if they will be drawn
9587 */
9588 private boolean skipInvalidate() {
9589 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
9590 (!(mParent instanceof ViewGroup) ||
9591 !((ViewGroup) mParent).isViewTransitioning(this));
9592 }
9593 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009594 * Mark the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009595 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
9596 * in the future. This must be called from a UI thread. To call from a non-UI
9597 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 *
9599 * WARNING: This method is destructive to dirty.
9600 * @param dirty the rectangle representing the bounds of the dirty region
9601 */
9602 public void invalidate(Rect dirty) {
9603 if (ViewDebug.TRACE_HIERARCHY) {
9604 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9605 }
9606
Chet Haaseaceafe62011-08-26 15:44:33 -07009607 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009608 return;
9609 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009610 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009611 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9612 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009614 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009615 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009616 final ViewParent p = mParent;
9617 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009618 //noinspection PointlessBooleanExpression,ConstantConditions
9619 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9620 if (p != null && ai != null && ai.mHardwareAccelerated) {
9621 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009622 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009623 p.invalidateChild(this, null);
9624 return;
9625 }
Romain Guyaf636eb2010-12-09 17:47:21 -08009626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 if (p != null && ai != null) {
9628 final int scrollX = mScrollX;
9629 final int scrollY = mScrollY;
9630 final Rect r = ai.mTmpInvalRect;
9631 r.set(dirty.left - scrollX, dirty.top - scrollY,
9632 dirty.right - scrollX, dirty.bottom - scrollY);
9633 mParent.invalidateChild(this, r);
9634 }
9635 }
9636 }
9637
9638 /**
Joe Fernandez558459f2011-10-13 16:47:36 -07009639 * 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 -08009640 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009641 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
9642 * will be called at some point in the future. This must be called from
9643 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 * @param l the left position of the dirty region
9645 * @param t the top position of the dirty region
9646 * @param r the right position of the dirty region
9647 * @param b the bottom position of the dirty region
9648 */
9649 public void invalidate(int l, int t, int r, int b) {
9650 if (ViewDebug.TRACE_HIERARCHY) {
9651 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9652 }
9653
Chet Haaseaceafe62011-08-26 15:44:33 -07009654 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009655 return;
9656 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009657 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08009658 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
9659 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009660 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08009661 mPrivateFlags |= INVALIDATED;
Chet Haasef186f302011-09-11 11:06:06 -07009662 mPrivateFlags |= DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009663 final ViewParent p = mParent;
9664 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08009665 //noinspection PointlessBooleanExpression,ConstantConditions
9666 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9667 if (p != null && ai != null && ai.mHardwareAccelerated) {
9668 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009669 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009670 p.invalidateChild(this, null);
9671 return;
9672 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 if (p != null && ai != null && l < r && t < b) {
9675 final int scrollX = mScrollX;
9676 final int scrollY = mScrollY;
9677 final Rect tmpr = ai.mTmpInvalRect;
9678 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
9679 p.invalidateChild(this, tmpr);
9680 }
9681 }
9682 }
9683
9684 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009685 * Invalidate the whole view. If the view is visible,
9686 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
9687 * the future. This must be called from a UI thread. To call from a non-UI thread,
9688 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 */
9690 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07009691 invalidate(true);
9692 }
Joe Malin32736f02011-01-19 16:14:20 -08009693
Chet Haaseed032702010-10-01 14:05:54 -07009694 /**
9695 * This is where the invalidate() work actually happens. A full invalidate()
9696 * causes the drawing cache to be invalidated, but this function can be called with
9697 * invalidateCache set to false to skip that invalidation step for cases that do not
9698 * need it (for example, a component that remains at the same dimensions with the same
9699 * content).
9700 *
9701 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
9702 * well. This is usually true for a full invalidate, but may be set to false if the
9703 * View's contents or dimensions have not changed.
9704 */
Romain Guy849d0a32011-02-01 17:20:48 -08009705 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 if (ViewDebug.TRACE_HIERARCHY) {
9707 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
9708 }
9709
Chet Haaseaceafe62011-08-26 15:44:33 -07009710 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07009711 return;
9712 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009713 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08009714 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08009715 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
9716 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07009717 mPrivateFlags &= ~DRAWN;
Chet Haasef186f302011-09-11 11:06:06 -07009718 mPrivateFlags |= DIRTY;
Chet Haaseed032702010-10-01 14:05:54 -07009719 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009720 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07009721 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07009724 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08009725 //noinspection PointlessBooleanExpression,ConstantConditions
9726 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
9727 if (p != null && ai != null && ai.mHardwareAccelerated) {
9728 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07009729 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08009730 p.invalidateChild(this, null);
9731 return;
9732 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08009733 }
Michael Jurkaebefea42010-11-15 16:04:01 -08009734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009735 if (p != null && ai != null) {
9736 final Rect r = ai.mTmpInvalRect;
9737 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9738 // Don't call invalidate -- we don't want to internally scroll
9739 // our own bounds
9740 p.invalidateChild(this, r);
9741 }
9742 }
9743 }
9744
9745 /**
Chet Haase9d1992d2012-03-13 11:03:25 -07009746 * Quick invalidation for View property changes (alpha, translationXY, etc.). We don't want to
9747 * set any flags or handle all of the cases handled by the default invalidation methods.
9748 * Instead, we just want to schedule a traversal in ViewRootImpl with the appropriate
9749 * dirty rect. This method calls into fast invalidation methods in ViewGroup that
9750 * walk up the hierarchy, transforming the dirty rect as necessary.
9751 *
9752 * The method also handles normal invalidation logic if display list properties are not
9753 * being used in this view. The invalidateParent and forceRedraw flags are used by that
9754 * backup approach, to handle these cases used in the various property-setting methods.
9755 *
9756 * @param invalidateParent Force a call to invalidateParentCaches() if display list properties
9757 * are not being used in this view
9758 * @param forceRedraw Mark the view as DRAWN to force the invalidation to propagate, if display
9759 * list properties are not being used in this view
9760 */
9761 void invalidateViewProperty(boolean invalidateParent, boolean forceRedraw) {
Chet Haase1271e2c2012-04-20 09:54:27 -07009762 if (mDisplayList == null || (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
Chet Haase9d1992d2012-03-13 11:03:25 -07009763 if (invalidateParent) {
9764 invalidateParentCaches();
9765 }
9766 if (forceRedraw) {
9767 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
9768 }
9769 invalidate(false);
9770 } else {
9771 final AttachInfo ai = mAttachInfo;
9772 final ViewParent p = mParent;
9773 if (p != null && ai != null) {
9774 final Rect r = ai.mTmpInvalRect;
9775 r.set(0, 0, mRight - mLeft, mBottom - mTop);
9776 if (mParent instanceof ViewGroup) {
9777 ((ViewGroup) mParent).invalidateChildFast(this, r);
9778 } else {
9779 mParent.invalidateChild(this, r);
9780 }
9781 }
9782 }
9783 }
9784
9785 /**
9786 * Utility method to transform a given Rect by the current matrix of this view.
9787 */
9788 void transformRect(final Rect rect) {
9789 if (!getMatrix().isIdentity()) {
9790 RectF boundingRect = mAttachInfo.mTmpTransformRect;
9791 boundingRect.set(rect);
9792 getMatrix().mapRect(boundingRect);
9793 rect.set((int) (boundingRect.left - 0.5f),
9794 (int) (boundingRect.top - 0.5f),
9795 (int) (boundingRect.right + 0.5f),
9796 (int) (boundingRect.bottom + 0.5f));
9797 }
9798 }
9799
9800 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08009801 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08009802 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9803 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08009804 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
9805 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08009806 *
9807 * @hide
9808 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08009809 protected void invalidateParentCaches() {
9810 if (mParent instanceof View) {
9811 ((View) mParent).mPrivateFlags |= INVALIDATED;
9812 }
9813 }
Joe Malin32736f02011-01-19 16:14:20 -08009814
Romain Guy0fd89bf2011-01-26 15:41:30 -08009815 /**
9816 * Used to indicate that the parent of this view should be invalidated. This functionality
9817 * is used to force the parent to rebuild its display list (when hardware-accelerated),
9818 * which is necessary when various parent-managed properties of the view change, such as
9819 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
9820 * an invalidation event to the parent.
9821 *
9822 * @hide
9823 */
9824 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08009825 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08009826 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08009827 }
9828 }
9829
9830 /**
Romain Guy24443ea2009-05-11 11:56:30 -07009831 * Indicates whether this View is opaque. An opaque View guarantees that it will
9832 * draw all the pixels overlapping its bounds using a fully opaque color.
9833 *
9834 * Subclasses of View should override this method whenever possible to indicate
9835 * whether an instance is opaque. Opaque Views are treated in a special way by
9836 * the View hierarchy, possibly allowing it to perform optimizations during
9837 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07009838 *
Romain Guy24443ea2009-05-11 11:56:30 -07009839 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07009840 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009841 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07009842 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07009843 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
Dianne Hackbornddb715b2011-09-09 14:43:39 -07009844 ((mTransformationInfo != null ? mTransformationInfo.mAlpha : 1)
9845 >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07009846 }
9847
Adam Powell20232d02010-12-08 21:08:53 -08009848 /**
9849 * @hide
9850 */
9851 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07009852 // Opaque if:
9853 // - Has a background
9854 // - Background is opaque
9855 // - Doesn't have scrollbars or scrollbars are inside overlay
9856
Philip Milne6c8ea062012-04-03 17:38:43 -07009857 if (mBackground != null && mBackground.getOpacity() == PixelFormat.OPAQUE) {
Romain Guy8f1344f52009-05-15 16:03:59 -07009858 mPrivateFlags |= OPAQUE_BACKGROUND;
9859 } else {
9860 mPrivateFlags &= ~OPAQUE_BACKGROUND;
9861 }
9862
9863 final int flags = mViewFlags;
9864 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
9865 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
9866 mPrivateFlags |= OPAQUE_SCROLLBARS;
9867 } else {
9868 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
9869 }
9870 }
9871
9872 /**
9873 * @hide
9874 */
9875 protected boolean hasOpaqueScrollbars() {
9876 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07009877 }
9878
9879 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 * @return A handler associated with the thread running the View. This
9881 * handler can be used to pump events in the UI events queue.
9882 */
9883 public Handler getHandler() {
9884 if (mAttachInfo != null) {
9885 return mAttachInfo.mHandler;
9886 }
9887 return null;
9888 }
9889
9890 /**
Jeff Browna175a5b2012-02-15 19:18:31 -08009891 * Gets the view root associated with the View.
9892 * @return The view root, or null if none.
9893 * @hide
9894 */
9895 public ViewRootImpl getViewRootImpl() {
9896 if (mAttachInfo != null) {
9897 return mAttachInfo.mViewRootImpl;
9898 }
9899 return null;
9900 }
9901
9902 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009903 * <p>Causes the Runnable to be added to the message queue.
9904 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009905 *
Romain Guye63a4f32011-08-11 11:33:31 -07009906 * <p>This method can be invoked from outside of the UI thread
9907 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 *
9909 * @param action The Runnable that will be executed.
9910 *
9911 * @return Returns true if the Runnable was successfully placed in to the
9912 * message queue. Returns false on failure, usually because the
9913 * looper processing the message queue is exiting.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009914 *
9915 * @see #postDelayed
9916 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 */
9918 public boolean post(Runnable action) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009919 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009920 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009921 return attachInfo.mHandler.post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009923 // Assume that post will succeed later
9924 ViewRootImpl.getRunQueue().post(action);
9925 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 }
9927
9928 /**
Romain Guye63a4f32011-08-11 11:33:31 -07009929 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07009931 * The runnable will be run on the user interface thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -07009932 *
Romain Guye63a4f32011-08-11 11:33:31 -07009933 * <p>This method can be invoked from outside of the UI thread
9934 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 *
9936 * @param action The Runnable that will be executed.
9937 * @param delayMillis The delay (in milliseconds) until the Runnable
9938 * will be executed.
9939 *
9940 * @return true if the Runnable was successfully placed in to the
9941 * message queue. Returns false on failure, usually because the
9942 * looper processing the message queue is exiting. Note that a
9943 * result of true does not mean the Runnable will be processed --
9944 * if the looper is quit before the delivery time of the message
9945 * occurs then the message will be dropped.
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009946 *
9947 * @see #post
9948 * @see #removeCallbacks
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 */
9950 public boolean postDelayed(Runnable action, long delayMillis) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009951 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -07009952 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -08009953 return attachInfo.mHandler.postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 }
Jeff Browna175a5b2012-02-15 19:18:31 -08009955 // Assume that post will succeed later
9956 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
9957 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 }
9959
9960 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009961 * <p>Causes the Runnable to execute on the next animation time step.
9962 * The runnable will be run on the user interface thread.</p>
9963 *
9964 * <p>This method can be invoked from outside of the UI thread
9965 * only when this View is attached to a window.</p>
9966 *
9967 * @param action The Runnable that will be executed.
9968 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009969 * @see #postOnAnimationDelayed
9970 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009971 */
9972 public void postOnAnimation(Runnable action) {
9973 final AttachInfo attachInfo = mAttachInfo;
9974 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -07009975 attachInfo.mViewRootImpl.mChoreographer.postCallback(
9976 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009977 } else {
9978 // Assume that post will succeed later
9979 ViewRootImpl.getRunQueue().post(action);
9980 }
9981 }
9982
9983 /**
9984 * <p>Causes the Runnable to execute on the next animation time step,
9985 * after the specified amount of time elapses.
9986 * The runnable will be run on the user interface thread.</p>
9987 *
9988 * <p>This method can be invoked from outside of the UI thread
9989 * only when this View is attached to a window.</p>
9990 *
9991 * @param action The Runnable that will be executed.
9992 * @param delayMillis The delay (in milliseconds) until the Runnable
9993 * will be executed.
9994 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -07009995 * @see #postOnAnimation
9996 * @see #removeCallbacks
Jeff Brown7ae9d5f2012-03-05 19:33:49 -08009997 */
9998 public void postOnAnimationDelayed(Runnable action, long delayMillis) {
9999 final AttachInfo attachInfo = mAttachInfo;
10000 if (attachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010001 attachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
10002 Choreographer.CALLBACK_ANIMATION, action, null, delayMillis);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080010003 } else {
10004 // Assume that post will succeed later
10005 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
10006 }
10007 }
10008
10009 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010010 * <p>Removes the specified Runnable from the message queue.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010011 *
Romain Guye63a4f32011-08-11 11:33:31 -070010012 * <p>This method can be invoked from outside of the UI thread
10013 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 *
10015 * @param action The Runnable to remove from the message handling queue
10016 *
10017 * @return true if this view could ask the Handler to remove the Runnable,
10018 * false otherwise. When the returned value is true, the Runnable
10019 * may or may not have been actually removed from the message queue
10020 * (for instance, if the Runnable was not in the queue already.)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010021 *
10022 * @see #post
10023 * @see #postDelayed
10024 * @see #postOnAnimation
10025 * @see #postOnAnimationDelayed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 */
10027 public boolean removeCallbacks(Runnable action) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080010028 if (action != null) {
10029 final AttachInfo attachInfo = mAttachInfo;
10030 if (attachInfo != null) {
10031 attachInfo.mHandler.removeCallbacks(action);
Jeff Brownebb2d8d2012-03-23 17:14:34 -070010032 attachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
10033 Choreographer.CALLBACK_ANIMATION, action, null);
Jeff Brown43ea54b2012-03-09 14:37:48 -080010034 } else {
10035 // Assume that post will succeed later
10036 ViewRootImpl.getRunQueue().removeCallbacks(action);
10037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 return true;
10040 }
10041
10042 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010043 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
10044 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 *
Romain Guye63a4f32011-08-11 11:33:31 -070010046 * <p>This method can be invoked from outside of the UI thread
10047 * only when this View is attached to a window.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049 * @see #invalidate()
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010050 * @see #postInvalidateDelayed(long)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 */
10052 public void postInvalidate() {
10053 postInvalidateDelayed(0);
10054 }
10055
10056 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010057 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10058 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010059 *
Romain Guye63a4f32011-08-11 11:33:31 -070010060 * <p>This method can be invoked from outside of the UI thread
10061 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010062 *
10063 * @param left The left coordinate of the rectangle to invalidate.
10064 * @param top The top coordinate of the rectangle to invalidate.
10065 * @param right The right coordinate of the rectangle to invalidate.
10066 * @param bottom The bottom coordinate of the rectangle to invalidate.
10067 *
10068 * @see #invalidate(int, int, int, int)
10069 * @see #invalidate(Rect)
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010070 * @see #postInvalidateDelayed(long, int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010071 */
10072 public void postInvalidate(int left, int top, int right, int bottom) {
10073 postInvalidateDelayed(0, left, top, right, bottom);
10074 }
10075
10076 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010077 * <p>Cause an invalidate to happen on a subsequent cycle through the event
10078 * loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010079 *
Romain Guye63a4f32011-08-11 11:33:31 -070010080 * <p>This method can be invoked from outside of the UI thread
10081 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 *
10083 * @param delayMilliseconds the duration in milliseconds to delay the
10084 * invalidation by
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010085 *
10086 * @see #invalidate()
10087 * @see #postInvalidate()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 */
10089 public void postInvalidateDelayed(long delayMilliseconds) {
10090 // We try only with the AttachInfo because there's no point in invalidating
10091 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010092 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010093 if (attachInfo != null) {
Jeff Browna175a5b2012-02-15 19:18:31 -080010094 attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 }
10096 }
10097
10098 /**
Romain Guye63a4f32011-08-11 11:33:31 -070010099 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
10100 * through the event loop. Waits for the specified amount of time.</p>
Philip Milne6c8ea062012-04-03 17:38:43 -070010101 *
Romain Guye63a4f32011-08-11 11:33:31 -070010102 * <p>This method can be invoked from outside of the UI thread
10103 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 *
10105 * @param delayMilliseconds the duration in milliseconds to delay the
10106 * invalidation by
10107 * @param left The left coordinate of the rectangle to invalidate.
10108 * @param top The top coordinate of the rectangle to invalidate.
10109 * @param right The right coordinate of the rectangle to invalidate.
10110 * @param bottom The bottom coordinate of the rectangle to invalidate.
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010111 *
10112 * @see #invalidate(int, int, int, int)
10113 * @see #invalidate(Rect)
10114 * @see #postInvalidate(int, int, int, int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 */
10116 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
10117 int right, int bottom) {
10118
10119 // We try only with the AttachInfo because there's no point in invalidating
10120 // if we are not attached to our window
Jeff Browna175a5b2012-02-15 19:18:31 -080010121 final AttachInfo attachInfo = mAttachInfo;
Romain Guyc5a43a22011-03-24 13:28:56 -070010122 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010123 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10124 info.target = this;
10125 info.left = left;
10126 info.top = top;
10127 info.right = right;
10128 info.bottom = bottom;
10129
Jeff Browna175a5b2012-02-15 19:18:31 -080010130 attachInfo.mViewRootImpl.dispatchInvalidateRectDelayed(info, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 }
10132 }
10133
10134 /**
Jeff Brown6cb7b462012-03-05 13:21:17 -080010135 * <p>Cause an invalidate to happen on the next animation time step, typically the
10136 * next display frame.</p>
10137 *
10138 * <p>This method can be invoked from outside of the UI thread
10139 * only when this View is attached to a window.</p>
10140 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010141 * @see #invalidate()
Jeff Brown6cb7b462012-03-05 13:21:17 -080010142 */
10143 public void postInvalidateOnAnimation() {
10144 // We try only with the AttachInfo because there's no point in invalidating
10145 // if we are not attached to our window
10146 final AttachInfo attachInfo = mAttachInfo;
10147 if (attachInfo != null) {
10148 attachInfo.mViewRootImpl.dispatchInvalidateOnAnimation(this);
10149 }
10150 }
10151
10152 /**
10153 * <p>Cause an invalidate of the specified area to happen on the next animation
10154 * time step, typically the next display frame.</p>
10155 *
10156 * <p>This method can be invoked from outside of the UI thread
10157 * only when this View is attached to a window.</p>
10158 *
10159 * @param left The left coordinate of the rectangle to invalidate.
10160 * @param top The top coordinate of the rectangle to invalidate.
10161 * @param right The right coordinate of the rectangle to invalidate.
10162 * @param bottom The bottom coordinate of the rectangle to invalidate.
10163 *
Jeff Brown4d6a82d2012-04-11 14:23:51 -070010164 * @see #invalidate(int, int, int, int)
10165 * @see #invalidate(Rect)
Jeff Brown6cb7b462012-03-05 13:21:17 -080010166 */
10167 public void postInvalidateOnAnimation(int left, int top, int right, int bottom) {
10168 // We try only with the AttachInfo because there's no point in invalidating
10169 // if we are not attached to our window
10170 final AttachInfo attachInfo = mAttachInfo;
10171 if (attachInfo != null) {
10172 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
10173 info.target = this;
10174 info.left = left;
10175 info.top = top;
10176 info.right = right;
10177 info.bottom = bottom;
10178
10179 attachInfo.mViewRootImpl.dispatchInvalidateRectOnAnimation(info);
10180 }
10181 }
10182
10183 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -070010184 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
10185 * This event is sent at most once every
10186 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
10187 */
10188 private void postSendViewScrolledAccessibilityEventCallback() {
10189 if (mSendViewScrolledAccessibilityEvent == null) {
10190 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
10191 }
10192 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
10193 mSendViewScrolledAccessibilityEvent.mIsPending = true;
10194 postDelayed(mSendViewScrolledAccessibilityEvent,
10195 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
10196 }
10197 }
10198
10199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010200 * Called by a parent to request that a child update its values for mScrollX
10201 * and mScrollY if necessary. This will typically be done if the child is
10202 * animating a scroll using a {@link android.widget.Scroller Scroller}
10203 * object.
10204 */
10205 public void computeScroll() {
10206 }
10207
10208 /**
10209 * <p>Indicate whether the horizontal edges are faded when the view is
10210 * scrolled horizontally.</p>
10211 *
10212 * @return true if the horizontal edges should are faded on scroll, false
10213 * otherwise
10214 *
10215 * @see #setHorizontalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010216 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010217 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010218 */
10219 public boolean isHorizontalFadingEdgeEnabled() {
10220 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
10221 }
10222
10223 /**
10224 * <p>Define whether the horizontal edges should be faded when this view
10225 * is scrolled horizontally.</p>
10226 *
10227 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
10228 * be faded when the view is scrolled
10229 * horizontally
10230 *
10231 * @see #isHorizontalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010232 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010233 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010234 */
10235 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
10236 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
10237 if (horizontalFadingEdgeEnabled) {
10238 initScrollCache();
10239 }
10240
10241 mViewFlags ^= FADING_EDGE_HORIZONTAL;
10242 }
10243 }
10244
10245 /**
10246 * <p>Indicate whether the vertical edges are faded when the view is
10247 * scrolled horizontally.</p>
10248 *
10249 * @return true if the vertical edges should are faded on scroll, false
10250 * otherwise
10251 *
10252 * @see #setVerticalFadingEdgeEnabled(boolean)
Philip Milne6c8ea062012-04-03 17:38:43 -070010253 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010254 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010255 */
10256 public boolean isVerticalFadingEdgeEnabled() {
10257 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
10258 }
10259
10260 /**
10261 * <p>Define whether the vertical edges should be faded when this view
10262 * is scrolled vertically.</p>
10263 *
10264 * @param verticalFadingEdgeEnabled true if the vertical edges should
10265 * be faded when the view is scrolled
10266 * vertically
10267 *
10268 * @see #isVerticalFadingEdgeEnabled()
Philip Milne6c8ea062012-04-03 17:38:43 -070010269 *
Romain Guy1ef3fdb2011-09-09 15:30:30 -070010270 * @attr ref android.R.styleable#View_requiresFadingEdge
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 */
10272 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
10273 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
10274 if (verticalFadingEdgeEnabled) {
10275 initScrollCache();
10276 }
10277
10278 mViewFlags ^= FADING_EDGE_VERTICAL;
10279 }
10280 }
10281
10282 /**
10283 * Returns the strength, or intensity, of the top faded edge. The strength is
10284 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10285 * returns 0.0 or 1.0 but no value in between.
10286 *
10287 * Subclasses should override this method to provide a smoother fade transition
10288 * when scrolling occurs.
10289 *
10290 * @return the intensity of the top fade as a float between 0.0f and 1.0f
10291 */
10292 protected float getTopFadingEdgeStrength() {
10293 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
10294 }
10295
10296 /**
10297 * Returns the strength, or intensity, of the bottom faded edge. The strength is
10298 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10299 * returns 0.0 or 1.0 but no value in between.
10300 *
10301 * Subclasses should override this method to provide a smoother fade transition
10302 * when scrolling occurs.
10303 *
10304 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
10305 */
10306 protected float getBottomFadingEdgeStrength() {
10307 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
10308 computeVerticalScrollRange() ? 1.0f : 0.0f;
10309 }
10310
10311 /**
10312 * Returns the strength, or intensity, of the left faded edge. The strength is
10313 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10314 * returns 0.0 or 1.0 but no value in between.
10315 *
10316 * Subclasses should override this method to provide a smoother fade transition
10317 * when scrolling occurs.
10318 *
10319 * @return the intensity of the left fade as a float between 0.0f and 1.0f
10320 */
10321 protected float getLeftFadingEdgeStrength() {
10322 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
10323 }
10324
10325 /**
10326 * Returns the strength, or intensity, of the right faded edge. The strength is
10327 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
10328 * returns 0.0 or 1.0 but no value in between.
10329 *
10330 * Subclasses should override this method to provide a smoother fade transition
10331 * when scrolling occurs.
10332 *
10333 * @return the intensity of the right fade as a float between 0.0f and 1.0f
10334 */
10335 protected float getRightFadingEdgeStrength() {
10336 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
10337 computeHorizontalScrollRange() ? 1.0f : 0.0f;
10338 }
10339
10340 /**
10341 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
10342 * scrollbar is not drawn by default.</p>
10343 *
10344 * @return true if the horizontal scrollbar should be painted, false
10345 * otherwise
10346 *
10347 * @see #setHorizontalScrollBarEnabled(boolean)
10348 */
10349 public boolean isHorizontalScrollBarEnabled() {
10350 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10351 }
10352
10353 /**
10354 * <p>Define whether the horizontal scrollbar should be drawn or not. The
10355 * scrollbar is not drawn by default.</p>
10356 *
10357 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
10358 * be painted
10359 *
10360 * @see #isHorizontalScrollBarEnabled()
10361 */
10362 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
10363 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
10364 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010365 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010366 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 }
10368 }
10369
10370 /**
10371 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
10372 * scrollbar is not drawn by default.</p>
10373 *
10374 * @return true if the vertical scrollbar should be painted, false
10375 * otherwise
10376 *
10377 * @see #setVerticalScrollBarEnabled(boolean)
10378 */
10379 public boolean isVerticalScrollBarEnabled() {
10380 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
10381 }
10382
10383 /**
10384 * <p>Define whether the vertical scrollbar should be drawn or not. The
10385 * scrollbar is not drawn by default.</p>
10386 *
10387 * @param verticalScrollBarEnabled true if the vertical scrollbar should
10388 * be painted
10389 *
10390 * @see #isVerticalScrollBarEnabled()
10391 */
10392 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
10393 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
10394 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -070010395 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010396 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010397 }
10398 }
10399
Adam Powell20232d02010-12-08 21:08:53 -080010400 /**
10401 * @hide
10402 */
10403 protected void recomputePadding() {
10404 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 }
Joe Malin32736f02011-01-19 16:14:20 -080010406
Mike Cleronfe81d382009-09-28 14:22:16 -070010407 /**
10408 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -080010409 *
Mike Cleronfe81d382009-09-28 14:22:16 -070010410 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -080010411 *
Philip Milne6c8ea062012-04-03 17:38:43 -070010412 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleronfe81d382009-09-28 14:22:16 -070010413 */
10414 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
10415 initScrollCache();
10416 final ScrollabilityCache scrollabilityCache = mScrollCache;
10417 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010418 if (fadeScrollbars) {
10419 scrollabilityCache.state = ScrollabilityCache.OFF;
10420 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -070010421 scrollabilityCache.state = ScrollabilityCache.ON;
10422 }
10423 }
Joe Malin32736f02011-01-19 16:14:20 -080010424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 /**
Joe Malin32736f02011-01-19 16:14:20 -080010426 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010427 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -080010428 *
Mike Cleron52f0a642009-09-28 18:21:37 -070010429 * @return true if scrollbar fading is enabled
Philip Milne6c8ea062012-04-03 17:38:43 -070010430 *
10431 * @attr ref android.R.styleable#View_fadeScrollbars
Mike Cleron52f0a642009-09-28 18:21:37 -070010432 */
10433 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -080010434 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -070010435 }
Joe Malin32736f02011-01-19 16:14:20 -080010436
Mike Cleron52f0a642009-09-28 18:21:37 -070010437 /**
Philip Milne6c8ea062012-04-03 17:38:43 -070010438 *
10439 * Returns the delay before scrollbars fade.
10440 *
10441 * @return the delay before scrollbars fade
10442 *
10443 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10444 */
10445 public int getScrollBarDefaultDelayBeforeFade() {
10446 return mScrollCache == null ? ViewConfiguration.getScrollDefaultDelay() :
10447 mScrollCache.scrollBarDefaultDelayBeforeFade;
10448 }
10449
10450 /**
10451 * Define the delay before scrollbars fade.
10452 *
10453 * @param scrollBarDefaultDelayBeforeFade - the delay before scrollbars fade
10454 *
10455 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
10456 */
10457 public void setScrollBarDefaultDelayBeforeFade(int scrollBarDefaultDelayBeforeFade) {
10458 getScrollCache().scrollBarDefaultDelayBeforeFade = scrollBarDefaultDelayBeforeFade;
10459 }
10460
10461 /**
10462 *
10463 * Returns the scrollbar fade duration.
10464 *
10465 * @return the scrollbar fade duration
10466 *
10467 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10468 */
10469 public int getScrollBarFadeDuration() {
10470 return mScrollCache == null ? ViewConfiguration.getScrollBarFadeDuration() :
10471 mScrollCache.scrollBarFadeDuration;
10472 }
10473
10474 /**
10475 * Define the scrollbar fade duration.
10476 *
10477 * @param scrollBarFadeDuration - the scrollbar fade duration
10478 *
10479 * @attr ref android.R.styleable#View_scrollbarFadeDuration
10480 */
10481 public void setScrollBarFadeDuration(int scrollBarFadeDuration) {
10482 getScrollCache().scrollBarFadeDuration = scrollBarFadeDuration;
10483 }
10484
10485 /**
10486 *
10487 * Returns the scrollbar size.
10488 *
10489 * @return the scrollbar size
10490 *
10491 * @attr ref android.R.styleable#View_scrollbarSize
10492 */
10493 public int getScrollBarSize() {
Romain Guyeb378892012-04-12 11:33:14 -070010494 return mScrollCache == null ? ViewConfiguration.get(mContext).getScaledScrollBarSize() :
Philip Milne6c8ea062012-04-03 17:38:43 -070010495 mScrollCache.scrollBarSize;
10496 }
10497
10498 /**
10499 * Define the scrollbar size.
10500 *
10501 * @param scrollBarSize - the scrollbar size
10502 *
10503 * @attr ref android.R.styleable#View_scrollbarSize
10504 */
10505 public void setScrollBarSize(int scrollBarSize) {
10506 getScrollCache().scrollBarSize = scrollBarSize;
10507 }
10508
10509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010510 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
10511 * inset. When inset, they add to the padding of the view. And the scrollbars
10512 * can be drawn inside the padding area or on the edge of the view. For example,
10513 * if a view has a background drawable and you want to draw the scrollbars
10514 * inside the padding specified by the drawable, you can use
10515 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
10516 * appear at the edge of the view, ignoring the padding, then you can use
10517 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
10518 * @param style the style of the scrollbars. Should be one of
10519 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
10520 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
10521 * @see #SCROLLBARS_INSIDE_OVERLAY
10522 * @see #SCROLLBARS_INSIDE_INSET
10523 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10524 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010525 *
10526 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010527 */
10528 public void setScrollBarStyle(int style) {
10529 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
10530 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -070010531 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010532 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 }
10534 }
10535
10536 /**
10537 * <p>Returns the current scrollbar style.</p>
10538 * @return the current scrollbar style
10539 * @see #SCROLLBARS_INSIDE_OVERLAY
10540 * @see #SCROLLBARS_INSIDE_INSET
10541 * @see #SCROLLBARS_OUTSIDE_OVERLAY
10542 * @see #SCROLLBARS_OUTSIDE_INSET
Philip Milne6c8ea062012-04-03 17:38:43 -070010543 *
10544 * @attr ref android.R.styleable#View_scrollbarStyle
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -070010546 @ViewDebug.ExportedProperty(mapping = {
10547 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
10548 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
10549 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
10550 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
10551 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010552 public int getScrollBarStyle() {
10553 return mViewFlags & SCROLLBARS_STYLE_MASK;
10554 }
10555
10556 /**
10557 * <p>Compute the horizontal range that the horizontal scrollbar
10558 * represents.</p>
10559 *
10560 * <p>The range is expressed in arbitrary units that must be the same as the
10561 * units used by {@link #computeHorizontalScrollExtent()} and
10562 * {@link #computeHorizontalScrollOffset()}.</p>
10563 *
10564 * <p>The default range is the drawing width of this view.</p>
10565 *
10566 * @return the total horizontal range represented by the horizontal
10567 * scrollbar
10568 *
10569 * @see #computeHorizontalScrollExtent()
10570 * @see #computeHorizontalScrollOffset()
10571 * @see android.widget.ScrollBarDrawable
10572 */
10573 protected int computeHorizontalScrollRange() {
10574 return getWidth();
10575 }
10576
10577 /**
10578 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
10579 * within the horizontal range. This value is used to compute the position
10580 * of the thumb within the scrollbar's track.</p>
10581 *
10582 * <p>The range is expressed in arbitrary units that must be the same as the
10583 * units used by {@link #computeHorizontalScrollRange()} and
10584 * {@link #computeHorizontalScrollExtent()}.</p>
10585 *
10586 * <p>The default offset is the scroll offset of this view.</p>
10587 *
10588 * @return the horizontal offset of the scrollbar's thumb
10589 *
10590 * @see #computeHorizontalScrollRange()
10591 * @see #computeHorizontalScrollExtent()
10592 * @see android.widget.ScrollBarDrawable
10593 */
10594 protected int computeHorizontalScrollOffset() {
10595 return mScrollX;
10596 }
10597
10598 /**
10599 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
10600 * within the horizontal range. This value is used to compute the length
10601 * of the thumb within the scrollbar's track.</p>
10602 *
10603 * <p>The range is expressed in arbitrary units that must be the same as the
10604 * units used by {@link #computeHorizontalScrollRange()} and
10605 * {@link #computeHorizontalScrollOffset()}.</p>
10606 *
10607 * <p>The default extent is the drawing width of this view.</p>
10608 *
10609 * @return the horizontal extent of the scrollbar's thumb
10610 *
10611 * @see #computeHorizontalScrollRange()
10612 * @see #computeHorizontalScrollOffset()
10613 * @see android.widget.ScrollBarDrawable
10614 */
10615 protected int computeHorizontalScrollExtent() {
10616 return getWidth();
10617 }
10618
10619 /**
10620 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
10621 *
10622 * <p>The range is expressed in arbitrary units that must be the same as the
10623 * units used by {@link #computeVerticalScrollExtent()} and
10624 * {@link #computeVerticalScrollOffset()}.</p>
10625 *
10626 * @return the total vertical range represented by the vertical scrollbar
10627 *
10628 * <p>The default range is the drawing height of this view.</p>
10629 *
10630 * @see #computeVerticalScrollExtent()
10631 * @see #computeVerticalScrollOffset()
10632 * @see android.widget.ScrollBarDrawable
10633 */
10634 protected int computeVerticalScrollRange() {
10635 return getHeight();
10636 }
10637
10638 /**
10639 * <p>Compute the vertical offset of the vertical scrollbar's thumb
10640 * within the horizontal range. This value is used to compute the position
10641 * of the thumb within the scrollbar's track.</p>
10642 *
10643 * <p>The range is expressed in arbitrary units that must be the same as the
10644 * units used by {@link #computeVerticalScrollRange()} and
10645 * {@link #computeVerticalScrollExtent()}.</p>
10646 *
10647 * <p>The default offset is the scroll offset of this view.</p>
10648 *
10649 * @return the vertical offset of the scrollbar's thumb
10650 *
10651 * @see #computeVerticalScrollRange()
10652 * @see #computeVerticalScrollExtent()
10653 * @see android.widget.ScrollBarDrawable
10654 */
10655 protected int computeVerticalScrollOffset() {
10656 return mScrollY;
10657 }
10658
10659 /**
10660 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
10661 * within the vertical range. This value is used to compute the length
10662 * of the thumb within the scrollbar's track.</p>
10663 *
10664 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -080010665 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010666 * {@link #computeVerticalScrollOffset()}.</p>
10667 *
10668 * <p>The default extent is the drawing height of this view.</p>
10669 *
10670 * @return the vertical extent of the scrollbar's thumb
10671 *
10672 * @see #computeVerticalScrollRange()
10673 * @see #computeVerticalScrollOffset()
10674 * @see android.widget.ScrollBarDrawable
10675 */
10676 protected int computeVerticalScrollExtent() {
10677 return getHeight();
10678 }
10679
10680 /**
Adam Powell69159442011-06-13 17:53:06 -070010681 * Check if this view can be scrolled horizontally in a certain direction.
10682 *
10683 * @param direction Negative to check scrolling left, positive to check scrolling right.
10684 * @return true if this view can be scrolled in the specified direction, false otherwise.
10685 */
10686 public boolean canScrollHorizontally(int direction) {
10687 final int offset = computeHorizontalScrollOffset();
10688 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
10689 if (range == 0) return false;
10690 if (direction < 0) {
10691 return offset > 0;
10692 } else {
10693 return offset < range - 1;
10694 }
10695 }
10696
10697 /**
10698 * Check if this view can be scrolled vertically in a certain direction.
10699 *
10700 * @param direction Negative to check scrolling up, positive to check scrolling down.
10701 * @return true if this view can be scrolled in the specified direction, false otherwise.
10702 */
10703 public boolean canScrollVertically(int direction) {
10704 final int offset = computeVerticalScrollOffset();
10705 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
10706 if (range == 0) return false;
10707 if (direction < 0) {
10708 return offset > 0;
10709 } else {
10710 return offset < range - 1;
10711 }
10712 }
10713
10714 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
10716 * scrollbars are painted only if they have been awakened first.</p>
10717 *
10718 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -080010719 *
Mike Cleronf116bf82009-09-27 19:14:12 -070010720 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 */
Romain Guy1d5b3a62009-11-05 18:44:12 -080010722 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 // scrollbars are drawn only when the animation is running
10724 final ScrollabilityCache cache = mScrollCache;
10725 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -080010726
Mike Cleronf116bf82009-09-27 19:14:12 -070010727 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -080010728
Mike Cleronf116bf82009-09-27 19:14:12 -070010729 if (state == ScrollabilityCache.OFF) {
10730 return;
10731 }
Joe Malin32736f02011-01-19 16:14:20 -080010732
Mike Cleronf116bf82009-09-27 19:14:12 -070010733 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -080010734
Mike Cleronf116bf82009-09-27 19:14:12 -070010735 if (state == ScrollabilityCache.FADING) {
10736 // We're fading -- get our fade interpolation
10737 if (cache.interpolatorValues == null) {
10738 cache.interpolatorValues = new float[1];
10739 }
Joe Malin32736f02011-01-19 16:14:20 -080010740
Mike Cleronf116bf82009-09-27 19:14:12 -070010741 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -080010742
Mike Cleronf116bf82009-09-27 19:14:12 -070010743 // Stops the animation if we're done
10744 if (cache.scrollBarInterpolator.timeToValues(values) ==
10745 Interpolator.Result.FREEZE_END) {
10746 cache.state = ScrollabilityCache.OFF;
10747 } else {
10748 cache.scrollBar.setAlpha(Math.round(values[0]));
10749 }
Joe Malin32736f02011-01-19 16:14:20 -080010750
10751 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -070010752 // drawing. We only want this when we're fading so that
10753 // we prevent excessive redraws
10754 invalidate = true;
10755 } else {
10756 // We're just on -- but we may have been fading before so
10757 // reset alpha
10758 cache.scrollBar.setAlpha(255);
10759 }
10760
Joe Malin32736f02011-01-19 16:14:20 -080010761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 final int viewFlags = mViewFlags;
10763
10764 final boolean drawHorizontalScrollBar =
10765 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
10766 final boolean drawVerticalScrollBar =
10767 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
10768 && !isVerticalScrollBarHidden();
10769
10770 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
10771 final int width = mRight - mLeft;
10772 final int height = mBottom - mTop;
10773
10774 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775
Mike Reede8853fc2009-09-04 14:01:48 -040010776 final int scrollX = mScrollX;
10777 final int scrollY = mScrollY;
10778 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
10779
Mike Cleronf116bf82009-09-27 19:14:12 -070010780 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -080010781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010783 int size = scrollBar.getSize(false);
10784 if (size <= 0) {
10785 size = cache.scrollBarSize;
10786 }
10787
Mike Cleronf116bf82009-09-27 19:14:12 -070010788 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -040010789 computeHorizontalScrollOffset(),
10790 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -040010791 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -070010792 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -080010793 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -070010794 left = scrollX + (mPaddingLeft & inside);
10795 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
10796 bottom = top + size;
10797 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
10798 if (invalidate) {
10799 invalidate(left, top, right, bottom);
10800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 }
10802
10803 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -080010804 int size = scrollBar.getSize(true);
10805 if (size <= 0) {
10806 size = cache.scrollBarSize;
10807 }
10808
Mike Reede8853fc2009-09-04 14:01:48 -040010809 scrollBar.setParameters(computeVerticalScrollRange(),
10810 computeVerticalScrollOffset(),
10811 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -080010812 switch (mVerticalScrollbarPosition) {
10813 default:
10814 case SCROLLBAR_POSITION_DEFAULT:
10815 case SCROLLBAR_POSITION_RIGHT:
10816 left = scrollX + width - size - (mUserPaddingRight & inside);
10817 break;
10818 case SCROLLBAR_POSITION_LEFT:
10819 left = scrollX + (mUserPaddingLeft & inside);
10820 break;
10821 }
Mike Cleronf116bf82009-09-27 19:14:12 -070010822 top = scrollY + (mPaddingTop & inside);
10823 right = left + size;
10824 bottom = scrollY + height - (mUserPaddingBottom & inside);
10825 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
10826 if (invalidate) {
10827 invalidate(left, top, right, bottom);
10828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 }
10830 }
10831 }
10832 }
Romain Guy8506ab42009-06-11 17:35:47 -070010833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010834 /**
Romain Guy8506ab42009-06-11 17:35:47 -070010835 * 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 -080010836 * FastScroller is visible.
10837 * @return whether to temporarily hide the vertical scrollbar
10838 * @hide
10839 */
10840 protected boolean isVerticalScrollBarHidden() {
10841 return false;
10842 }
10843
10844 /**
10845 * <p>Draw the horizontal scrollbar if
10846 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
10847 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010848 * @param canvas the canvas on which to draw the scrollbar
10849 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 *
10851 * @see #isHorizontalScrollBarEnabled()
10852 * @see #computeHorizontalScrollRange()
10853 * @see #computeHorizontalScrollExtent()
10854 * @see #computeHorizontalScrollOffset()
10855 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -070010856 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010857 */
Romain Guy8fb95422010-08-17 18:38:51 -070010858 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
10859 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010860 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010861 scrollBar.draw(canvas);
10862 }
Mike Reede8853fc2009-09-04 14:01:48 -040010863
Mike Reed4d6fe5f2009-09-03 13:29:05 -040010864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010865 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
10866 * returns true.</p>
10867 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010868 * @param canvas the canvas on which to draw the scrollbar
10869 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 *
10871 * @see #isVerticalScrollBarEnabled()
10872 * @see #computeVerticalScrollRange()
10873 * @see #computeVerticalScrollExtent()
10874 * @see #computeVerticalScrollOffset()
10875 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -040010876 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010877 */
Romain Guy8fb95422010-08-17 18:38:51 -070010878 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
10879 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -040010880 scrollBar.setBounds(l, t, r, b);
10881 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010882 }
10883
10884 /**
10885 * Implement this to do your drawing.
10886 *
10887 * @param canvas the canvas on which the background will be drawn
10888 */
10889 protected void onDraw(Canvas canvas) {
10890 }
10891
10892 /*
10893 * Caller is responsible for calling requestLayout if necessary.
10894 * (This allows addViewInLayout to not request a new layout.)
10895 */
10896 void assignParent(ViewParent parent) {
10897 if (mParent == null) {
10898 mParent = parent;
10899 } else if (parent == null) {
10900 mParent = null;
10901 } else {
10902 throw new RuntimeException("view " + this + " being added, but"
10903 + " it already has a parent");
10904 }
10905 }
10906
10907 /**
10908 * This is called when the view is attached to a window. At this point it
10909 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010910 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
10911 * however it may be called any time before the first onDraw -- including
10912 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010913 *
10914 * @see #onDetachedFromWindow()
10915 */
10916 protected void onAttachedToWindow() {
10917 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
10918 mParent.requestTransparentRegion(this);
10919 }
Adam Powell8568c3a2010-04-19 14:26:11 -070010920 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
10921 initialAwakenScrollBars();
10922 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
10923 }
Chet Haasea9b61ac2010-12-20 07:40:25 -080010924 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -070010925 // Order is important here: LayoutDirection MUST be resolved before Padding
10926 // and TextDirection
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010927 resolveLayoutDirection();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010928 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070010929 resolveTextDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070010930 resolveTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070010931 clearAccessibilityFocus();
Amith Yamasani4503c8d2011-06-17 12:36:14 -070010932 if (isFocused()) {
10933 InputMethodManager imm = InputMethodManager.peekInstance();
10934 imm.focusIn(this);
10935 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010936 }
Cibu Johny86666632010-02-22 13:01:02 -080010937
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010938 /**
Romain Guybb9908b2012-03-08 11:14:07 -080010939 * @see #onScreenStateChanged(int)
10940 */
10941 void dispatchScreenStateChanged(int screenState) {
10942 onScreenStateChanged(screenState);
10943 }
10944
10945 /**
10946 * This method is called whenever the state of the screen this view is
10947 * attached to changes. A state change will usually occurs when the screen
10948 * turns on or off (whether it happens automatically or the user does it
10949 * manually.)
10950 *
10951 * @param screenState The new state of the screen. Can be either
10952 * {@link #SCREEN_STATE_ON} or {@link #SCREEN_STATE_OFF}
10953 */
10954 public void onScreenStateChanged(int screenState) {
10955 }
10956
10957 /**
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010958 * Return true if the application tag in the AndroidManifest has set "supportRtl" to true
10959 */
10960 private boolean hasRtlSupport() {
10961 return mContext.getApplicationInfo().hasRtlSupport();
10962 }
10963
10964 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010965 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
10966 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010967 * Will call {@link View#onResolvedLayoutDirectionChanged} when resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070010968 * @hide
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -070010969 */
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010970 public void resolveLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010971 // Clear any previous layout direction resolution
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070010972 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070010973
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010974 if (hasRtlSupport()) {
10975 // Set resolved depending on layout direction
10976 switch (getLayoutDirection()) {
10977 case LAYOUT_DIRECTION_INHERIT:
10978 // If this is root view, no need to look at parent's layout dir.
10979 if (canResolveLayoutDirection()) {
10980 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010981
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010982 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
10983 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10984 }
10985 } else {
10986 // Nothing to do, LTR by default
10987 }
10988 break;
10989 case LAYOUT_DIRECTION_RTL:
10990 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10991 break;
10992 case LAYOUT_DIRECTION_LOCALE:
10993 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070010994 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
10995 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010996 break;
10997 default:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070010998 // Nothing to do, LTR by default
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070010999 }
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011000 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011001
11002 // Set to resolved
11003 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011004 onResolvedLayoutDirectionChanged();
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011005 // Resolve padding
11006 resolvePadding();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011007 }
11008
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011009 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011010 * Called when layout direction has been resolved.
11011 *
11012 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011013 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011014 */
11015 public void onResolvedLayoutDirectionChanged() {
11016 }
11017
11018 /**
11019 * Resolve padding depending on layout direction.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011020 * @hide
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -070011021 */
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011022 public void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011023 // If the user specified the absolute padding (either with android:padding or
11024 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
11025 // use the default padding or the padding from the background drawable
11026 // (stored at this point in mPadding*)
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011027 int resolvedLayoutDirection = getResolvedLayoutDirection();
11028 switch (resolvedLayoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011029 case LAYOUT_DIRECTION_RTL:
11030 // Start user padding override Right user padding. Otherwise, if Right user
11031 // padding is not defined, use the default Right padding. If Right user padding
11032 // is defined, just use it.
11033 if (mUserPaddingStart >= 0) {
11034 mUserPaddingRight = mUserPaddingStart;
11035 } else if (mUserPaddingRight < 0) {
11036 mUserPaddingRight = mPaddingRight;
11037 }
11038 if (mUserPaddingEnd >= 0) {
11039 mUserPaddingLeft = mUserPaddingEnd;
11040 } else if (mUserPaddingLeft < 0) {
11041 mUserPaddingLeft = mPaddingLeft;
11042 }
11043 break;
11044 case LAYOUT_DIRECTION_LTR:
11045 default:
11046 // Start user padding override Left user padding. Otherwise, if Left user
11047 // padding is not defined, use the default left padding. If Left user padding
11048 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011049 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011050 mUserPaddingLeft = mUserPaddingStart;
11051 } else if (mUserPaddingLeft < 0) {
11052 mUserPaddingLeft = mPaddingLeft;
11053 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -070011054 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011055 mUserPaddingRight = mUserPaddingEnd;
11056 } else if (mUserPaddingRight < 0) {
11057 mUserPaddingRight = mPaddingRight;
11058 }
11059 }
11060
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011061 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
11062
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080011063 if(isPaddingRelative()) {
11064 setPaddingRelative(mUserPaddingStart, mPaddingTop, mUserPaddingEnd, mUserPaddingBottom);
11065 } else {
11066 recomputePadding();
11067 }
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011068 onPaddingChanged(resolvedLayoutDirection);
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011069 }
11070
11071 /**
11072 * Resolve padding depending on the layout direction. Subclasses that care about
11073 * padding resolution should override this method. The default implementation does
11074 * nothing.
11075 *
11076 * @param layoutDirection the direction of the layout
11077 *
Fabrice Di Meglioe8dc07d2012-03-09 17:10:19 -080011078 * @see {@link #LAYOUT_DIRECTION_LTR}
11079 * @see {@link #LAYOUT_DIRECTION_RTL}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011080 * @hide
Fabrice Di Meglioccb15622012-02-15 15:52:19 -080011081 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011082 public void onPaddingChanged(int layoutDirection) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011083 }
11084
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011085 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011086 * Check if layout direction resolution can be done.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011087 *
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011088 * @return true if layout direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011089 * @hide
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070011090 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011091 public boolean canResolveLayoutDirection() {
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011092 switch (getLayoutDirection()) {
11093 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070011094 return (mParent != null) && (mParent instanceof ViewGroup);
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -070011095 default:
11096 return true;
11097 }
11098 }
11099
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011100 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011101 * Reset the resolved layout direction. Will call {@link View#onResolvedLayoutDirectionReset}
11102 * when reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011103 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011104 */
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011105 public void resetResolvedLayoutDirection() {
Fabrice Di Meglioedc1e592012-03-15 17:28:47 -070011106 // Reset the current resolved bits
11107 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_MASK;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011108 onResolvedLayoutDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080011109 // Reset also the text direction
11110 resetResolvedTextDirection();
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011111 }
11112
11113 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011114 * Called during reset of resolved layout direction.
11115 *
11116 * Subclasses need to override this method to clear cached information that depends on the
11117 * resolved layout direction, or to inform child views that inherit their layout direction.
11118 *
11119 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011120 * @hide
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080011121 */
11122 public void onResolvedLayoutDirectionReset() {
11123 }
11124
11125 /**
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011126 * Check if a Locale uses an RTL script.
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011127 *
11128 * @param locale Locale to check
Fabrice Di Meglio98aec1c2012-02-13 16:54:05 -080011129 * @return true if the Locale uses an RTL script.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070011130 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070011131 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -070011132 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglio3fb824b2012-02-28 17:58:31 -080011133 return (LAYOUT_DIRECTION_RTL == LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 }
11135
11136 /**
11137 * This is called when the view is detached from a window. At this point it
11138 * no longer has a surface for drawing.
11139 *
11140 * @see #onAttachedToWindow()
11141 */
11142 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -080011143 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -080011144
Romain Guya440b002010-02-24 15:57:54 -080011145 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -050011146 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -080011147 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -070011148 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -080011149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011151
Romain Guya998dff2012-03-23 18:58:36 -070011152 destroyLayer(false);
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011153
11154 if (mAttachInfo != null) {
Romain Guy51e4d4d2012-03-15 18:30:47 -070011155 if (mDisplayList != null) {
11156 mAttachInfo.mViewRootImpl.invalidateDisplayList(mDisplayList);
11157 }
Jeff Browna175a5b2012-02-15 19:18:31 -080011158 mAttachInfo.mViewRootImpl.cancelInvalidate(this);
Romain Guy51e4d4d2012-03-15 18:30:47 -070011159 } else {
11160 if (mDisplayList != null) {
11161 // Should never happen
11162 mDisplayList.invalidate();
11163 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -080011164 }
11165
Patrick Dubroyec84c3a2011-01-13 17:55:37 -080011166 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -070011167
11168 resetResolvedLayoutDirection();
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070011169 resetResolvedTextAlignment();
Svetoslav Ganov42138042012-03-20 11:51:39 -070011170 resetAccessibilityStateChanged();
11171 clearAccessibilityFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011172 }
11173
11174 /**
11175 * @return The number of times this view has been attached to a window
11176 */
11177 protected int getWindowAttachCount() {
11178 return mWindowAttachCount;
11179 }
11180
11181 /**
11182 * Retrieve a unique token identifying the window this view is attached to.
11183 * @return Return the window's token for use in
11184 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
11185 */
11186 public IBinder getWindowToken() {
11187 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
11188 }
11189
11190 /**
11191 * Retrieve a unique token identifying the top-level "real" window of
11192 * the window that this view is attached to. That is, this is like
11193 * {@link #getWindowToken}, except if the window this view in is a panel
11194 * window (attached to another containing window), then the token of
11195 * the containing window is returned instead.
11196 *
11197 * @return Returns the associated window token, either
11198 * {@link #getWindowToken()} or the containing window's token.
11199 */
11200 public IBinder getApplicationWindowToken() {
11201 AttachInfo ai = mAttachInfo;
11202 if (ai != null) {
11203 IBinder appWindowToken = ai.mPanelParentWindowToken;
11204 if (appWindowToken == null) {
11205 appWindowToken = ai.mWindowToken;
11206 }
11207 return appWindowToken;
11208 }
11209 return null;
11210 }
11211
11212 /**
11213 * Retrieve private session object this view hierarchy is using to
11214 * communicate with the window manager.
11215 * @return the session object to communicate with the window manager
11216 */
11217 /*package*/ IWindowSession getWindowSession() {
11218 return mAttachInfo != null ? mAttachInfo.mSession : null;
11219 }
11220
11221 /**
11222 * @param info the {@link android.view.View.AttachInfo} to associated with
11223 * this view
11224 */
11225 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
11226 //System.out.println("Attached! " + this);
11227 mAttachInfo = info;
11228 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011229 // We will need to evaluate the drawable state at least once.
11230 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 if (mFloatingTreeObserver != null) {
11232 info.mTreeObserver.merge(mFloatingTreeObserver);
11233 mFloatingTreeObserver = null;
11234 }
11235 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
11236 mAttachInfo.mScrollContainers.add(this);
11237 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
11238 }
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070011239 performCollectViewAttributes(mAttachInfo, visibility);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011240 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -080011241
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011242 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011243 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011244 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011245 if (listeners != null && listeners.size() > 0) {
11246 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11247 // perform the dispatching. The iterator is a safe guard against listeners that
11248 // could mutate the list by calling the various add/remove methods. This prevents
11249 // the array from being modified while we iterate it.
11250 for (OnAttachStateChangeListener listener : listeners) {
11251 listener.onViewAttachedToWindow(this);
11252 }
11253 }
11254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 int vis = info.mWindowVisibility;
11256 if (vis != GONE) {
11257 onWindowVisibilityChanged(vis);
11258 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011259 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
11260 // If nobody has evaluated the drawable state yet, then do it now.
11261 refreshDrawableState();
11262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011263 }
11264
11265 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011266 AttachInfo info = mAttachInfo;
11267 if (info != null) {
11268 int vis = info.mWindowVisibility;
11269 if (vis != GONE) {
11270 onWindowVisibilityChanged(GONE);
11271 }
11272 }
11273
11274 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -080011275
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011276 ListenerInfo li = mListenerInfo;
Adam Powell4afd62b2011-02-18 15:02:18 -080011277 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070011278 li != null ? li.mOnAttachStateChangeListeners : null;
Adam Powell4afd62b2011-02-18 15:02:18 -080011279 if (listeners != null && listeners.size() > 0) {
11280 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
11281 // perform the dispatching. The iterator is a safe guard against listeners that
11282 // could mutate the list by calling the various add/remove methods. This prevents
11283 // the array from being modified while we iterate it.
11284 for (OnAttachStateChangeListener listener : listeners) {
11285 listener.onViewDetachedFromWindow(this);
11286 }
11287 }
11288
Romain Guy01d5edc2011-01-28 11:28:53 -080011289 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011290 mAttachInfo.mScrollContainers.remove(this);
11291 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
11292 }
Romain Guy01d5edc2011-01-28 11:28:53 -080011293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011294 mAttachInfo = null;
11295 }
11296
11297 /**
11298 * Store this view hierarchy's frozen state into the given container.
11299 *
11300 * @param container The SparseArray in which to save the view's state.
11301 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011302 * @see #restoreHierarchyState(android.util.SparseArray)
11303 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11304 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 */
11306 public void saveHierarchyState(SparseArray<Parcelable> container) {
11307 dispatchSaveInstanceState(container);
11308 }
11309
11310 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011311 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
11312 * this view and its children. May be overridden to modify how freezing happens to a
11313 * 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 -080011314 *
11315 * @param container The SparseArray in which to save the view's state.
11316 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011317 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11318 * @see #saveHierarchyState(android.util.SparseArray)
11319 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 */
11321 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
11322 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
11323 mPrivateFlags &= ~SAVE_STATE_CALLED;
11324 Parcelable state = onSaveInstanceState();
11325 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11326 throw new IllegalStateException(
11327 "Derived class did not call super.onSaveInstanceState()");
11328 }
11329 if (state != null) {
11330 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
11331 // + ": " + state);
11332 container.put(mID, state);
11333 }
11334 }
11335 }
11336
11337 /**
11338 * Hook allowing a view to generate a representation of its internal state
11339 * that can later be used to create a new instance with that same state.
11340 * This state should only contain information that is not persistent or can
11341 * not be reconstructed later. For example, you will never store your
11342 * current position on screen because that will be computed again when a
11343 * new instance of the view is placed in its view hierarchy.
11344 * <p>
11345 * Some examples of things you may store here: the current cursor position
11346 * in a text view (but usually not the text itself since that is stored in a
11347 * content provider or other persistent storage), the currently selected
11348 * item in a list view.
11349 *
11350 * @return Returns a Parcelable object containing the view's current dynamic
11351 * state, or null if there is nothing interesting to save. The
11352 * default implementation returns null.
Philip Milne6c8ea062012-04-03 17:38:43 -070011353 * @see #onRestoreInstanceState(android.os.Parcelable)
11354 * @see #saveHierarchyState(android.util.SparseArray)
11355 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011356 * @see #setSaveEnabled(boolean)
11357 */
11358 protected Parcelable onSaveInstanceState() {
11359 mPrivateFlags |= SAVE_STATE_CALLED;
11360 return BaseSavedState.EMPTY_STATE;
11361 }
11362
11363 /**
11364 * Restore this view hierarchy's frozen state from the given container.
11365 *
11366 * @param container The SparseArray which holds previously frozen states.
11367 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011368 * @see #saveHierarchyState(android.util.SparseArray)
11369 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
11370 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011371 */
11372 public void restoreHierarchyState(SparseArray<Parcelable> container) {
11373 dispatchRestoreInstanceState(container);
11374 }
11375
11376 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -070011377 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
11378 * state for this view and its children. May be overridden to modify how restoring
11379 * happens to a view's children; for example, some views may want to not store state
11380 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 *
11382 * @param container The SparseArray which holds previously saved state.
11383 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011384 * @see #dispatchSaveInstanceState(android.util.SparseArray)
11385 * @see #restoreHierarchyState(android.util.SparseArray)
11386 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 */
11388 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
11389 if (mID != NO_ID) {
11390 Parcelable state = container.get(mID);
11391 if (state != null) {
11392 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
11393 // + ": " + state);
11394 mPrivateFlags &= ~SAVE_STATE_CALLED;
11395 onRestoreInstanceState(state);
11396 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
11397 throw new IllegalStateException(
11398 "Derived class did not call super.onRestoreInstanceState()");
11399 }
11400 }
11401 }
11402 }
11403
11404 /**
11405 * Hook allowing a view to re-apply a representation of its internal state that had previously
11406 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
11407 * null state.
11408 *
11409 * @param state The frozen state that had previously been returned by
11410 * {@link #onSaveInstanceState}.
11411 *
Philip Milne6c8ea062012-04-03 17:38:43 -070011412 * @see #onSaveInstanceState()
11413 * @see #restoreHierarchyState(android.util.SparseArray)
11414 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011415 */
11416 protected void onRestoreInstanceState(Parcelable state) {
11417 mPrivateFlags |= SAVE_STATE_CALLED;
11418 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -080011419 throw new IllegalArgumentException("Wrong state class, expecting View State but "
11420 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -080011421 + "when two views of different type have the same id in the same hierarchy. "
11422 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -080011423 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011424 }
11425 }
11426
11427 /**
11428 * <p>Return the time at which the drawing of the view hierarchy started.</p>
11429 *
11430 * @return the drawing start time in milliseconds
11431 */
11432 public long getDrawingTime() {
11433 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
11434 }
11435
11436 /**
11437 * <p>Enables or disables the duplication of the parent's state into this view. When
11438 * duplication is enabled, this view gets its drawable state from its parent rather
11439 * than from its own internal properties.</p>
11440 *
11441 * <p>Note: in the current implementation, setting this property to true after the
11442 * view was added to a ViewGroup might have no effect at all. This property should
11443 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
11444 *
11445 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
11446 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011447 *
Gilles Debunnefb817032011-01-13 13:52:49 -080011448 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
11449 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 *
11451 * @param enabled True to enable duplication of the parent's drawable state, false
11452 * to disable it.
11453 *
11454 * @see #getDrawableState()
11455 * @see #isDuplicateParentStateEnabled()
11456 */
11457 public void setDuplicateParentStateEnabled(boolean enabled) {
11458 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
11459 }
11460
11461 /**
11462 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
11463 *
11464 * @return True if this view's drawable state is duplicated from the parent,
11465 * false otherwise
11466 *
11467 * @see #getDrawableState()
11468 * @see #setDuplicateParentStateEnabled(boolean)
11469 */
11470 public boolean isDuplicateParentStateEnabled() {
11471 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
11472 }
11473
11474 /**
Romain Guy171c5922011-01-06 10:04:23 -080011475 * <p>Specifies the type of layer backing this view. The layer can be
11476 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
11477 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011478 *
Romain Guy171c5922011-01-06 10:04:23 -080011479 * <p>A layer is associated with an optional {@link android.graphics.Paint}
11480 * instance that controls how the layer is composed on screen. The following
11481 * properties of the paint are taken into account when composing the layer:</p>
11482 * <ul>
11483 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
11484 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
11485 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
11486 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -080011487 *
Romain Guy171c5922011-01-06 10:04:23 -080011488 * <p>If this view has an alpha value set to < 1.0 by calling
11489 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
11490 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
11491 * equivalent to setting a hardware layer on this view and providing a paint with
11492 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -080011493 *
Romain Guy171c5922011-01-06 10:04:23 -080011494 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
11495 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
11496 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011497 *
Romain Guy171c5922011-01-06 10:04:23 -080011498 * @param layerType The ype of layer to use with this view, must be one of
11499 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11500 * {@link #LAYER_TYPE_HARDWARE}
11501 * @param paint The paint used to compose the layer. This argument is optional
11502 * and can be null. It is ignored when the layer type is
11503 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -080011504 *
11505 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -080011506 * @see #LAYER_TYPE_NONE
11507 * @see #LAYER_TYPE_SOFTWARE
11508 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -080011509 * @see #setAlpha(float)
11510 *
Romain Guy171c5922011-01-06 10:04:23 -080011511 * @attr ref android.R.styleable#View_layerType
11512 */
11513 public void setLayerType(int layerType, Paint paint) {
11514 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -080011515 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -080011516 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
11517 }
Chet Haasedaf98e92011-01-10 14:10:36 -080011518
Romain Guyd6cd5722011-01-17 14:42:41 -080011519 if (layerType == mLayerType) {
11520 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
11521 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011522 invalidateParentCaches();
11523 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -080011524 }
11525 return;
11526 }
Romain Guy171c5922011-01-06 10:04:23 -080011527
11528 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -080011529 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -070011530 case LAYER_TYPE_HARDWARE:
Romain Guya998dff2012-03-23 18:58:36 -070011531 destroyLayer(false);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011532 // fall through - non-accelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -080011533 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -070011534 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -080011535 break;
Romain Guy6c319ca2011-01-11 14:29:25 -080011536 default:
11537 break;
Romain Guy171c5922011-01-06 10:04:23 -080011538 }
11539
11540 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -080011541 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
11542 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
11543 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -080011544
Romain Guy0fd89bf2011-01-26 15:41:30 -080011545 invalidateParentCaches();
11546 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -080011547 }
11548
11549 /**
Romain Guy59c7f802011-09-29 17:21:45 -070011550 * Indicates whether this view has a static layer. A view with layer type
11551 * {@link #LAYER_TYPE_NONE} is a static layer. Other types of layers are
11552 * dynamic.
11553 */
11554 boolean hasStaticLayer() {
Romain Guy2bf68f02012-03-02 13:37:47 -080011555 return true;
Romain Guy59c7f802011-09-29 17:21:45 -070011556 }
11557
11558 /**
Romain Guy171c5922011-01-06 10:04:23 -080011559 * Indicates what type of layer is currently associated with this view. By default
11560 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
11561 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
11562 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -080011563 *
Romain Guy171c5922011-01-06 10:04:23 -080011564 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
11565 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -080011566 *
11567 * @see #setLayerType(int, android.graphics.Paint)
Philip Milne6c8ea062012-04-03 17:38:43 -070011568 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -080011569 * @see #LAYER_TYPE_NONE
11570 * @see #LAYER_TYPE_SOFTWARE
11571 * @see #LAYER_TYPE_HARDWARE
11572 */
11573 public int getLayerType() {
11574 return mLayerType;
11575 }
Joe Malin32736f02011-01-19 16:14:20 -080011576
Romain Guy6c319ca2011-01-11 14:29:25 -080011577 /**
Romain Guyf1ae1062011-03-02 18:16:04 -080011578 * Forces this view's layer to be created and this view to be rendered
11579 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
11580 * invoking this method will have no effect.
Philip Milne6c8ea062012-04-03 17:38:43 -070011581 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011582 * This method can for instance be used to render a view into its layer before
11583 * starting an animation. If this view is complex, rendering into the layer
11584 * before starting the animation will avoid skipping frames.
Philip Milne6c8ea062012-04-03 17:38:43 -070011585 *
Romain Guyf1ae1062011-03-02 18:16:04 -080011586 * @throws IllegalStateException If this view is not attached to a window
Philip Milne6c8ea062012-04-03 17:38:43 -070011587 *
11588 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -080011589 */
11590 public void buildLayer() {
11591 if (mLayerType == LAYER_TYPE_NONE) return;
11592
11593 if (mAttachInfo == null) {
11594 throw new IllegalStateException("This view must be attached to a window first");
11595 }
11596
11597 switch (mLayerType) {
11598 case LAYER_TYPE_HARDWARE:
Romain Guyd0609e42011-11-21 17:21:15 -080011599 if (mAttachInfo.mHardwareRenderer != null &&
11600 mAttachInfo.mHardwareRenderer.isEnabled() &&
11601 mAttachInfo.mHardwareRenderer.validate()) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080011602 getHardwareLayer();
Romain Guyd0609e42011-11-21 17:21:15 -080011603 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011604 break;
11605 case LAYER_TYPE_SOFTWARE:
11606 buildDrawingCache(true);
11607 break;
11608 }
11609 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011610
Romain Guy9c4b79a2011-11-10 19:23:58 -080011611 // Make sure the HardwareRenderer.validate() was invoked before calling this method
11612 void flushLayer() {
11613 if (mLayerType == LAYER_TYPE_HARDWARE && mHardwareLayer != null) {
11614 mHardwareLayer.flush();
11615 }
11616 }
Romain Guyf1ae1062011-03-02 18:16:04 -080011617
11618 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080011619 * <p>Returns a hardware layer that can be used to draw this view again
11620 * without executing its draw method.</p>
11621 *
11622 * @return A HardwareLayer ready to render, or null if an error occurred.
11623 */
Michael Jurka7e52caf2012-03-06 15:57:06 -080011624 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -070011625 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
11626 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -080011627 return null;
11628 }
Philip Milne6c8ea062012-04-03 17:38:43 -070011629
Romain Guy9c4b79a2011-11-10 19:23:58 -080011630 if (!mAttachInfo.mHardwareRenderer.validate()) return null;
Romain Guy6c319ca2011-01-11 14:29:25 -080011631
11632 final int width = mRight - mLeft;
11633 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -080011634
Romain Guy6c319ca2011-01-11 14:29:25 -080011635 if (width == 0 || height == 0) {
11636 return null;
11637 }
11638
11639 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
11640 if (mHardwareLayer == null) {
11641 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
11642 width, height, isOpaque());
Michael Jurka952e02b2012-03-13 18:34:35 -070011643 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011644 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
11645 mHardwareLayer.resize(width, height);
Michael Jurka952e02b2012-03-13 18:34:35 -070011646 mLocalDirtyRect.set(0, 0, width, height);
Romain Guy6c319ca2011-01-11 14:29:25 -080011647 }
11648
Romain Guy5cd5c3f2011-10-17 17:10:02 -070011649 // The layer is not valid if the underlying GPU resources cannot be allocated
11650 if (!mHardwareLayer.isValid()) {
11651 return null;
11652 }
11653
Chet Haasea1cff502012-02-21 13:43:44 -080011654 mHardwareLayer.redraw(getHardwareLayerDisplayList(mHardwareLayer), mLocalDirtyRect);
Michael Jurka7e52caf2012-03-06 15:57:06 -080011655 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -080011656 }
11657
11658 return mHardwareLayer;
11659 }
Romain Guy171c5922011-01-06 10:04:23 -080011660
Romain Guy589b0bb2011-10-10 13:57:47 -070011661 /**
11662 * Destroys this View's hardware layer if possible.
Philip Milne6c8ea062012-04-03 17:38:43 -070011663 *
Romain Guy589b0bb2011-10-10 13:57:47 -070011664 * @return True if the layer was destroyed, false otherwise.
Philip Milne6c8ea062012-04-03 17:38:43 -070011665 *
11666 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy589b0bb2011-10-10 13:57:47 -070011667 * @see #LAYER_TYPE_HARDWARE
11668 */
Romain Guya998dff2012-03-23 18:58:36 -070011669 boolean destroyLayer(boolean valid) {
Romain Guy6d7475d2011-07-27 16:28:21 -070011670 if (mHardwareLayer != null) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011671 AttachInfo info = mAttachInfo;
11672 if (info != null && info.mHardwareRenderer != null &&
Romain Guya998dff2012-03-23 18:58:36 -070011673 info.mHardwareRenderer.isEnabled() &&
11674 (valid || info.mHardwareRenderer.validate())) {
Romain Guy9c4b79a2011-11-10 19:23:58 -080011675 mHardwareLayer.destroy();
11676 mHardwareLayer = null;
Romain Guy31f2c2e2011-11-21 10:55:41 -080011677
Romain Guy9c4b79a2011-11-10 19:23:58 -080011678 invalidate(true);
11679 invalidateParentCaches();
11680 }
Romain Guy65b345f2011-07-27 18:51:50 -070011681 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -070011682 }
Romain Guy65b345f2011-07-27 18:51:50 -070011683 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -070011684 }
11685
Romain Guy171c5922011-01-06 10:04:23 -080011686 /**
Romain Guy31f2c2e2011-11-21 10:55:41 -080011687 * Destroys all hardware rendering resources. This method is invoked
11688 * when the system needs to reclaim resources. Upon execution of this
11689 * method, you should free any OpenGL resources created by the view.
Philip Milne6c8ea062012-04-03 17:38:43 -070011690 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011691 * Note: you <strong>must</strong> call
11692 * <code>super.destroyHardwareResources()</code> when overriding
11693 * this method.
Philip Milne6c8ea062012-04-03 17:38:43 -070011694 *
Romain Guy31f2c2e2011-11-21 10:55:41 -080011695 * @hide
11696 */
11697 protected void destroyHardwareResources() {
Romain Guya998dff2012-03-23 18:58:36 -070011698 destroyLayer(true);
Romain Guy31f2c2e2011-11-21 10:55:41 -080011699 }
11700
11701 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011702 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
11703 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
11704 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
11705 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
11706 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
11707 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011708 *
Romain Guy171c5922011-01-06 10:04:23 -080011709 * <p>Enabling the drawing cache is similar to
11710 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -080011711 * acceleration is turned off. When hardware acceleration is turned on, enabling the
11712 * drawing cache has no effect on rendering because the system uses a different mechanism
11713 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
11714 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
11715 * for information on how to enable software and hardware layers.</p>
11716 *
11717 * <p>This API can be used to manually generate
11718 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
11719 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011720 *
11721 * @param enabled true to enable the drawing cache, false otherwise
11722 *
11723 * @see #isDrawingCacheEnabled()
11724 * @see #getDrawingCache()
11725 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -080011726 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011727 */
11728 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -080011729 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011730 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
11731 }
11732
11733 /**
11734 * <p>Indicates whether the drawing cache is enabled for this view.</p>
11735 *
11736 * @return true if the drawing cache is enabled
11737 *
11738 * @see #setDrawingCacheEnabled(boolean)
11739 * @see #getDrawingCache()
11740 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011741 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011742 public boolean isDrawingCacheEnabled() {
11743 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
11744 }
11745
11746 /**
Chet Haasedaf98e92011-01-10 14:10:36 -080011747 * Debugging utility which recursively outputs the dirty state of a view and its
11748 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -080011749 *
Chet Haasedaf98e92011-01-10 14:10:36 -080011750 * @hide
11751 */
Romain Guy676b1732011-02-14 14:45:33 -080011752 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -080011753 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
11754 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
11755 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
11756 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
11757 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
11758 if (clear) {
11759 mPrivateFlags &= clearMask;
11760 }
11761 if (this instanceof ViewGroup) {
11762 ViewGroup parent = (ViewGroup) this;
11763 final int count = parent.getChildCount();
11764 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -080011765 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -080011766 child.outputDirtyFlags(indent + " ", clear, clearMask);
11767 }
11768 }
11769 }
11770
11771 /**
11772 * This method is used by ViewGroup to cause its children to restore or recreate their
11773 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
11774 * to recreate its own display list, which would happen if it went through the normal
11775 * draw/dispatchDraw mechanisms.
11776 *
11777 * @hide
11778 */
11779 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -080011780
11781 /**
11782 * A view that is not attached or hardware accelerated cannot create a display list.
11783 * This method checks these conditions and returns the appropriate result.
11784 *
11785 * @return true if view has the ability to create a display list, false otherwise.
11786 *
11787 * @hide
11788 */
11789 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -080011790 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -080011791 }
Joe Malin32736f02011-01-19 16:14:20 -080011792
Chet Haasedaf98e92011-01-10 14:10:36 -080011793 /**
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080011794 * @return The HardwareRenderer associated with that view or null if hardware rendering
11795 * is not supported or this this has not been attached to a window.
11796 *
11797 * @hide
11798 */
11799 public HardwareRenderer getHardwareRenderer() {
11800 if (mAttachInfo != null) {
11801 return mAttachInfo.mHardwareRenderer;
11802 }
11803 return null;
11804 }
11805
11806 /**
Chet Haasea1cff502012-02-21 13:43:44 -080011807 * Returns a DisplayList. If the incoming displayList is null, one will be created.
11808 * Otherwise, the same display list will be returned (after having been rendered into
11809 * along the way, depending on the invalidation state of the view).
11810 *
11811 * @param displayList The previous version of this displayList, could be null.
11812 * @param isLayer Whether the requester of the display list is a layer. If so,
11813 * the view will avoid creating a layer inside the resulting display list.
11814 * @return A new or reused DisplayList object.
11815 */
11816 private DisplayList getDisplayList(DisplayList displayList, boolean isLayer) {
11817 if (!canHaveDisplayList()) {
11818 return null;
11819 }
11820
11821 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
11822 displayList == null || !displayList.isValid() ||
11823 (!isLayer && mRecreateDisplayList))) {
11824 // Don't need to recreate the display list, just need to tell our
11825 // children to restore/recreate theirs
11826 if (displayList != null && displayList.isValid() &&
11827 !isLayer && !mRecreateDisplayList) {
11828 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11829 mPrivateFlags &= ~DIRTY_MASK;
11830 dispatchGetDisplayList();
11831
11832 return displayList;
11833 }
11834
11835 if (!isLayer) {
11836 // If we got here, we're recreating it. Mark it as such to ensure that
11837 // we copy in child display lists into ours in drawChild()
11838 mRecreateDisplayList = true;
11839 }
11840 if (displayList == null) {
11841 final String name = getClass().getSimpleName();
11842 displayList = mAttachInfo.mHardwareRenderer.createDisplayList(name);
11843 // If we're creating a new display list, make sure our parent gets invalidated
11844 // since they will need to recreate their display list to account for this
11845 // new child display list.
11846 invalidateParentCaches();
11847 }
11848
11849 boolean caching = false;
11850 final HardwareCanvas canvas = displayList.start();
11851 int restoreCount = 0;
11852 int width = mRight - mLeft;
11853 int height = mBottom - mTop;
11854
11855 try {
11856 canvas.setViewport(width, height);
11857 // The dirty rect should always be null for a display list
11858 canvas.onPreDraw(null);
11859 int layerType = (
11860 !(mParent instanceof ViewGroup) || ((ViewGroup)mParent).mDrawLayers) ?
11861 getLayerType() : LAYER_TYPE_NONE;
Chet Haase1271e2c2012-04-20 09:54:27 -070011862 if (!isLayer && layerType != LAYER_TYPE_NONE) {
Chet Haaseb85967b2012-03-26 14:37:51 -070011863 if (layerType == LAYER_TYPE_HARDWARE) {
11864 final HardwareLayer layer = getHardwareLayer();
11865 if (layer != null && layer.isValid()) {
11866 canvas.drawHardwareLayer(layer, 0, 0, mLayerPaint);
11867 } else {
11868 canvas.saveLayer(0, 0, mRight - mLeft, mBottom - mTop, mLayerPaint,
11869 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
11870 Canvas.CLIP_TO_LAYER_SAVE_FLAG);
11871 }
11872 caching = true;
Chet Haasea1cff502012-02-21 13:43:44 -080011873 } else {
Chet Haaseb85967b2012-03-26 14:37:51 -070011874 buildDrawingCache(true);
11875 Bitmap cache = getDrawingCache(true);
11876 if (cache != null) {
11877 canvas.drawBitmap(cache, 0, 0, mLayerPaint);
11878 caching = true;
11879 }
Chet Haasea1cff502012-02-21 13:43:44 -080011880 }
Chet Haasea1cff502012-02-21 13:43:44 -080011881 } else {
11882
11883 computeScroll();
11884
Chet Haasea1cff502012-02-21 13:43:44 -080011885 canvas.translate(-mScrollX, -mScrollY);
11886 if (!isLayer) {
11887 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11888 mPrivateFlags &= ~DIRTY_MASK;
11889 }
11890
11891 // Fast path for layouts with no backgrounds
11892 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
11893 dispatchDraw(canvas);
11894 } else {
11895 draw(canvas);
11896 }
11897 }
11898 } finally {
Chet Haasea1cff502012-02-21 13:43:44 -080011899 canvas.onPostDraw();
11900
11901 displayList.end();
Chet Haase1271e2c2012-04-20 09:54:27 -070011902 displayList.setCaching(caching);
11903 if (isLayer) {
Chet Haasea1cff502012-02-21 13:43:44 -080011904 displayList.setLeftTopRightBottom(0, 0, width, height);
11905 } else {
11906 setDisplayListProperties(displayList);
11907 }
11908 }
11909 } else if (!isLayer) {
11910 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
11911 mPrivateFlags &= ~DIRTY_MASK;
11912 }
11913
11914 return displayList;
11915 }
11916
11917 /**
11918 * Get the DisplayList for the HardwareLayer
11919 *
11920 * @param layer The HardwareLayer whose DisplayList we want
11921 * @return A DisplayList fopr the specified HardwareLayer
11922 */
11923 private DisplayList getHardwareLayerDisplayList(HardwareLayer layer) {
11924 DisplayList displayList = getDisplayList(layer.getDisplayList(), true);
11925 layer.setDisplayList(displayList);
11926 return displayList;
11927 }
11928
11929
11930 /**
Romain Guyb051e892010-09-28 19:09:36 -070011931 * <p>Returns a display list that can be used to draw this view again
11932 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011933 *
Romain Guyb051e892010-09-28 19:09:36 -070011934 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -080011935 *
11936 * @hide
Romain Guyb051e892010-09-28 19:09:36 -070011937 */
Chet Haasedaf98e92011-01-10 14:10:36 -080011938 public DisplayList getDisplayList() {
Chet Haasea1cff502012-02-21 13:43:44 -080011939 mDisplayList = getDisplayList(mDisplayList, false);
Romain Guyb051e892010-09-28 19:09:36 -070011940 return mDisplayList;
11941 }
11942
11943 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070011944 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011945 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011946 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011947 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011948 * @see #getDrawingCache(boolean)
11949 */
11950 public Bitmap getDrawingCache() {
11951 return getDrawingCache(false);
11952 }
11953
11954 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
11956 * is null when caching is disabled. If caching is enabled and the cache is not ready,
11957 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
11958 * draw from the cache when the cache is enabled. To benefit from the cache, you must
11959 * request the drawing cache by calling this method and draw it on screen if the
11960 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011961 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011962 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
11963 * this method will create a bitmap of the same size as this view. Because this bitmap
11964 * will be drawn scaled by the parent ViewGroup, the result on screen might show
11965 * scaling artifacts. To avoid such artifacts, you should call this method by setting
11966 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
11967 * size than the view. This implies that your application must be able to handle this
11968 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080011969 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011970 * @param autoScale Indicates whether the generated bitmap should be scaled based on
11971 * the current density of the screen when the application is in compatibility
11972 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011973 *
Romain Guyfbd8f692009-06-26 14:51:58 -070011974 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080011975 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 * @see #setDrawingCacheEnabled(boolean)
11977 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070011978 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011979 * @see #destroyDrawingCache()
11980 */
Romain Guyfbd8f692009-06-26 14:51:58 -070011981 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011982 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
11983 return null;
11984 }
11985 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070011986 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011987 }
Romain Guy02890fd2010-08-06 17:58:44 -070011988 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 }
11990
11991 /**
11992 * <p>Frees the resources used by the drawing cache. If you call
11993 * {@link #buildDrawingCache()} manually without calling
11994 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
11995 * should cleanup the cache with this method afterwards.</p>
11996 *
11997 * @see #setDrawingCacheEnabled(boolean)
11998 * @see #buildDrawingCache()
11999 * @see #getDrawingCache()
12000 */
12001 public void destroyDrawingCache() {
12002 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012003 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012004 mDrawingCache = null;
12005 }
Romain Guyfbd8f692009-06-26 14:51:58 -070012006 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070012007 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070012008 mUnscaledDrawingCache = null;
12009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 }
12011
12012 /**
12013 * Setting a solid background color for the drawing cache's bitmaps will improve
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070012014 * performance and memory usage. Note, though that this should only be used if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 * view will always be drawn on top of a solid color.
12016 *
12017 * @param color The background color to use for the drawing cache's bitmap
12018 *
12019 * @see #setDrawingCacheEnabled(boolean)
12020 * @see #buildDrawingCache()
12021 * @see #getDrawingCache()
12022 */
12023 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080012024 if (color != mDrawingCacheBackgroundColor) {
12025 mDrawingCacheBackgroundColor = color;
12026 mPrivateFlags &= ~DRAWING_CACHE_VALID;
12027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 }
12029
12030 /**
12031 * @see #setDrawingCacheBackgroundColor(int)
12032 *
12033 * @return The background color to used for the drawing cache's bitmap
12034 */
12035 public int getDrawingCacheBackgroundColor() {
12036 return mDrawingCacheBackgroundColor;
12037 }
12038
12039 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070012040 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012041 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012042 * @see #buildDrawingCache(boolean)
12043 */
12044 public void buildDrawingCache() {
12045 buildDrawingCache(false);
12046 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080012047
Romain Guyfbd8f692009-06-26 14:51:58 -070012048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012049 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
12050 *
12051 * <p>If you call {@link #buildDrawingCache()} manually without calling
12052 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
12053 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012054 *
Romain Guyfbd8f692009-06-26 14:51:58 -070012055 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
12056 * this method will create a bitmap of the same size as this view. Because this bitmap
12057 * will be drawn scaled by the parent ViewGroup, the result on screen might show
12058 * scaling artifacts. To avoid such artifacts, you should call this method by setting
12059 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
12060 * size than the view. This implies that your application must be able to handle this
12061 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012062 *
Romain Guy0d9275e2010-10-26 14:22:30 -070012063 * <p>You should avoid calling this method when hardware acceleration is enabled. If
12064 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080012065 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070012066 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012067 *
12068 * @see #getDrawingCache()
12069 * @see #destroyDrawingCache()
12070 */
Romain Guyfbd8f692009-06-26 14:51:58 -070012071 public void buildDrawingCache(boolean autoScale) {
12072 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070012073 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080012074 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075
12076 if (ViewDebug.TRACE_HIERARCHY) {
12077 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
12078 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012079
Romain Guy8506ab42009-06-11 17:35:47 -070012080 int width = mRight - mLeft;
12081 int height = mBottom - mTop;
12082
12083 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070012084 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070012085
Romain Guye1123222009-06-29 14:24:56 -070012086 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012087 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
12088 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070012089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090
12091 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070012092 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080012093 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012094
12095 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070012096 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080012097 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012098 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
12099 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080012100 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012101 return;
12102 }
12103
12104 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070012105 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012106
12107 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012108 Bitmap.Config quality;
12109 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080012110 // Never pick ARGB_4444 because it looks awful
12111 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012112 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
12113 case DRAWING_CACHE_QUALITY_AUTO:
12114 quality = Bitmap.Config.ARGB_8888;
12115 break;
12116 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080012117 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118 break;
12119 case DRAWING_CACHE_QUALITY_HIGH:
12120 quality = Bitmap.Config.ARGB_8888;
12121 break;
12122 default:
12123 quality = Bitmap.Config.ARGB_8888;
12124 break;
12125 }
12126 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070012127 // Optimization for translucent windows
12128 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080012129 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012130 }
12131
12132 // Try to cleanup memory
12133 if (bitmap != null) bitmap.recycle();
12134
12135 try {
12136 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070012137 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070012138 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070012139 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012140 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070012141 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070012142 }
Adam Powell26153a32010-11-08 15:22:27 -080012143 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012144 } catch (OutOfMemoryError e) {
12145 // If there is not enough memory to create the bitmap cache, just
12146 // ignore the issue as bitmap caches are not required to draw the
12147 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070012148 if (autoScale) {
12149 mDrawingCache = null;
12150 } else {
12151 mUnscaledDrawingCache = null;
12152 }
Romain Guy0211a0a2011-02-14 16:34:59 -080012153 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012154 return;
12155 }
12156
12157 clear = drawingCacheBackgroundColor != 0;
12158 }
12159
12160 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012161 if (attachInfo != null) {
12162 canvas = attachInfo.mCanvas;
12163 if (canvas == null) {
12164 canvas = new Canvas();
12165 }
12166 canvas.setBitmap(bitmap);
12167 // Temporarily clobber the cached Canvas in case one of our children
12168 // is also using a drawing cache. Without this, the children would
12169 // steal the canvas by attaching their own bitmap to it and bad, bad
12170 // thing would happen (invisible views, corrupted drawings, etc.)
12171 attachInfo.mCanvas = null;
12172 } else {
12173 // This case should hopefully never or seldom happen
12174 canvas = new Canvas(bitmap);
12175 }
12176
12177 if (clear) {
12178 bitmap.eraseColor(drawingCacheBackgroundColor);
12179 }
12180
12181 computeScroll();
12182 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080012183
Romain Guye1123222009-06-29 14:24:56 -070012184 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070012185 final float scale = attachInfo.mApplicationScale;
12186 canvas.scale(scale, scale);
12187 }
Joe Malin32736f02011-01-19 16:14:20 -080012188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012189 canvas.translate(-mScrollX, -mScrollY);
12190
Romain Guy5bcdff42009-05-14 21:27:18 -070012191 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080012192 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
12193 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070012194 mPrivateFlags |= DRAWING_CACHE_VALID;
12195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012196
12197 // Fast path for layouts with no backgrounds
12198 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12199 if (ViewDebug.TRACE_HIERARCHY) {
12200 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12201 }
Romain Guy5bcdff42009-05-14 21:27:18 -070012202 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012203 dispatchDraw(canvas);
12204 } else {
12205 draw(canvas);
12206 }
12207
12208 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012209 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012210
12211 if (attachInfo != null) {
12212 // Restore the cached Canvas for our siblings
12213 attachInfo.mCanvas = canvas;
12214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 }
12216 }
12217
12218 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012219 * Create a snapshot of the view into a bitmap. We should probably make
12220 * some form of this public, but should think about the API.
12221 */
Romain Guy223ff5c2010-03-02 17:07:47 -080012222 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012223 int width = mRight - mLeft;
12224 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012225
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012226 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070012227 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012228 width = (int) ((width * scale) + 0.5f);
12229 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080012230
Romain Guy8c11e312009-09-14 15:15:30 -070012231 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012232 if (bitmap == null) {
12233 throw new OutOfMemoryError();
12234 }
12235
Romain Guyc529d8d2011-09-06 15:01:39 -070012236 Resources resources = getResources();
12237 if (resources != null) {
12238 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
12239 }
Joe Malin32736f02011-01-19 16:14:20 -080012240
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012241 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012242 if (attachInfo != null) {
12243 canvas = attachInfo.mCanvas;
12244 if (canvas == null) {
12245 canvas = new Canvas();
12246 }
12247 canvas.setBitmap(bitmap);
12248 // Temporarily clobber the cached Canvas in case one of our children
12249 // is also using a drawing cache. Without this, the children would
12250 // steal the canvas by attaching their own bitmap to it and bad, bad
12251 // things would happen (invisible views, corrupted drawings, etc.)
12252 attachInfo.mCanvas = null;
12253 } else {
12254 // This case should hopefully never or seldom happen
12255 canvas = new Canvas(bitmap);
12256 }
12257
Romain Guy5bcdff42009-05-14 21:27:18 -070012258 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012259 bitmap.eraseColor(backgroundColor);
12260 }
12261
12262 computeScroll();
12263 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070012264 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012265 canvas.translate(-mScrollX, -mScrollY);
12266
Romain Guy5bcdff42009-05-14 21:27:18 -070012267 // Temporarily remove the dirty mask
12268 int flags = mPrivateFlags;
12269 mPrivateFlags &= ~DIRTY_MASK;
12270
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012271 // Fast path for layouts with no backgrounds
12272 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12273 dispatchDraw(canvas);
12274 } else {
12275 draw(canvas);
12276 }
12277
Romain Guy5bcdff42009-05-14 21:27:18 -070012278 mPrivateFlags = flags;
12279
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012280 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070012281 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012282
12283 if (attachInfo != null) {
12284 // Restore the cached Canvas for our siblings
12285 attachInfo.mCanvas = canvas;
12286 }
Romain Guy8506ab42009-06-11 17:35:47 -070012287
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070012288 return bitmap;
12289 }
12290
12291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292 * Indicates whether this View is currently in edit mode. A View is usually
12293 * in edit mode when displayed within a developer tool. For instance, if
12294 * this View is being drawn by a visual user interface builder, this method
12295 * should return true.
12296 *
12297 * Subclasses should check the return value of this method to provide
12298 * different behaviors if their normal behavior might interfere with the
12299 * host environment. For instance: the class spawns a thread in its
12300 * constructor, the drawing code relies on device-specific features, etc.
12301 *
12302 * This method is usually checked in the drawing code of custom widgets.
12303 *
12304 * @return True if this View is in edit mode, false otherwise.
12305 */
12306 public boolean isInEditMode() {
12307 return false;
12308 }
12309
12310 /**
12311 * If the View draws content inside its padding and enables fading edges,
12312 * it needs to support padding offsets. Padding offsets are added to the
12313 * fading edges to extend the length of the fade so that it covers pixels
12314 * drawn inside the padding.
12315 *
12316 * Subclasses of this class should override this method if they need
12317 * to draw content inside the padding.
12318 *
12319 * @return True if padding offset must be applied, false otherwise.
12320 *
12321 * @see #getLeftPaddingOffset()
12322 * @see #getRightPaddingOffset()
12323 * @see #getTopPaddingOffset()
12324 * @see #getBottomPaddingOffset()
12325 *
12326 * @since CURRENT
12327 */
12328 protected boolean isPaddingOffsetRequired() {
12329 return false;
12330 }
12331
12332 /**
12333 * Amount by which to extend the left fading region. Called only when
12334 * {@link #isPaddingOffsetRequired()} returns true.
12335 *
12336 * @return The left padding offset in pixels.
12337 *
12338 * @see #isPaddingOffsetRequired()
12339 *
12340 * @since CURRENT
12341 */
12342 protected int getLeftPaddingOffset() {
12343 return 0;
12344 }
12345
12346 /**
12347 * Amount by which to extend the right fading region. Called only when
12348 * {@link #isPaddingOffsetRequired()} returns true.
12349 *
12350 * @return The right padding offset in pixels.
12351 *
12352 * @see #isPaddingOffsetRequired()
12353 *
12354 * @since CURRENT
12355 */
12356 protected int getRightPaddingOffset() {
12357 return 0;
12358 }
12359
12360 /**
12361 * Amount by which to extend the top fading region. Called only when
12362 * {@link #isPaddingOffsetRequired()} returns true.
12363 *
12364 * @return The top padding offset in pixels.
12365 *
12366 * @see #isPaddingOffsetRequired()
12367 *
12368 * @since CURRENT
12369 */
12370 protected int getTopPaddingOffset() {
12371 return 0;
12372 }
12373
12374 /**
12375 * Amount by which to extend the bottom fading region. Called only when
12376 * {@link #isPaddingOffsetRequired()} returns true.
12377 *
12378 * @return The bottom padding offset in pixels.
12379 *
12380 * @see #isPaddingOffsetRequired()
12381 *
12382 * @since CURRENT
12383 */
12384 protected int getBottomPaddingOffset() {
12385 return 0;
12386 }
12387
12388 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070012389 * @hide
12390 * @param offsetRequired
12391 */
12392 protected int getFadeTop(boolean offsetRequired) {
12393 int top = mPaddingTop;
12394 if (offsetRequired) top += getTopPaddingOffset();
12395 return top;
12396 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012397
Romain Guyf2fc4602011-07-19 15:20:03 -070012398 /**
12399 * @hide
12400 * @param offsetRequired
12401 */
12402 protected int getFadeHeight(boolean offsetRequired) {
12403 int padding = mPaddingTop;
Philip Milne6c8ea062012-04-03 17:38:43 -070012404 if (offsetRequired) padding += getTopPaddingOffset();
Romain Guyf2fc4602011-07-19 15:20:03 -070012405 return mBottom - mTop - mPaddingBottom - padding;
12406 }
Philip Milne6c8ea062012-04-03 17:38:43 -070012407
Romain Guyf2fc4602011-07-19 15:20:03 -070012408 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012409 * <p>Indicates whether this view is attached to a hardware accelerated
Romain Guy2bffd262010-09-12 17:40:02 -070012410 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012411 *
Romain Guy2bffd262010-09-12 17:40:02 -070012412 * <p>Even if this method returns true, it does not mean that every call
12413 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
12414 * accelerated {@link android.graphics.Canvas}. For instance, if this view
Ken Wakasaf76a50c2012-03-09 19:56:35 +090012415 * is drawn onto an offscreen {@link android.graphics.Bitmap} and its
Romain Guy2bffd262010-09-12 17:40:02 -070012416 * window is hardware accelerated,
12417 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
12418 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080012419 *
Romain Guy2bffd262010-09-12 17:40:02 -070012420 * @return True if the view is attached to a window and the window is
12421 * hardware accelerated; false in any other case.
12422 */
12423 public boolean isHardwareAccelerated() {
12424 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
12425 }
Joe Malin32736f02011-01-19 16:14:20 -080012426
Romain Guy2bffd262010-09-12 17:40:02 -070012427 /**
Chet Haasebcca79a2012-02-14 08:45:14 -080012428 * Utility function, called by draw(canvas, parent, drawingTime) to handle the less common
12429 * case of an active Animation being run on the view.
12430 */
12431 private boolean drawAnimation(ViewGroup parent, long drawingTime,
12432 Animation a, boolean scalingRequired) {
12433 Transformation invalidationTransform;
12434 final int flags = parent.mGroupFlags;
12435 final boolean initialized = a.isInitialized();
12436 if (!initialized) {
Chet Haase1fb8a9e2012-04-19 09:22:34 -070012437 a.initialize(mRight - mLeft, mBottom - mTop, parent.getWidth(), parent.getHeight());
Chet Haasebcca79a2012-02-14 08:45:14 -080012438 a.initializeInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop);
12439 onAnimationStart();
12440 }
12441
12442 boolean more = a.getTransformation(drawingTime, parent.mChildTransformation, 1f);
12443 if (scalingRequired && mAttachInfo.mApplicationScale != 1f) {
12444 if (parent.mInvalidationTransformation == null) {
12445 parent.mInvalidationTransformation = new Transformation();
12446 }
12447 invalidationTransform = parent.mInvalidationTransformation;
12448 a.getTransformation(drawingTime, invalidationTransform, 1f);
12449 } else {
12450 invalidationTransform = parent.mChildTransformation;
12451 }
12452 if (more) {
12453 if (!a.willChangeBounds()) {
12454 if ((flags & (parent.FLAG_OPTIMIZE_INVALIDATE | parent.FLAG_ANIMATION_DONE)) ==
12455 parent.FLAG_OPTIMIZE_INVALIDATE) {
12456 parent.mGroupFlags |= parent.FLAG_INVALIDATE_REQUIRED;
12457 } else if ((flags & parent.FLAG_INVALIDATE_REQUIRED) == 0) {
12458 // The child need to draw an animation, potentially offscreen, so
12459 // make sure we do not cancel invalidate requests
12460 parent.mPrivateFlags |= DRAW_ANIMATION;
12461 parent.invalidate(mLeft, mTop, mRight, mBottom);
12462 }
12463 } else {
12464 if (parent.mInvalidateRegion == null) {
12465 parent.mInvalidateRegion = new RectF();
12466 }
12467 final RectF region = parent.mInvalidateRegion;
12468 a.getInvalidateRegion(0, 0, mRight - mLeft, mBottom - mTop, region,
12469 invalidationTransform);
12470
12471 // The child need to draw an animation, potentially offscreen, so
12472 // make sure we do not cancel invalidate requests
12473 parent.mPrivateFlags |= DRAW_ANIMATION;
12474
12475 final int left = mLeft + (int) region.left;
12476 final int top = mTop + (int) region.top;
12477 parent.invalidate(left, top, left + (int) (region.width() + .5f),
12478 top + (int) (region.height() + .5f));
12479 }
12480 }
12481 return more;
12482 }
12483
Chet Haasea1cff502012-02-21 13:43:44 -080012484 void setDisplayListProperties() {
12485 setDisplayListProperties(mDisplayList);
12486 }
12487
12488 /**
12489 * This method is called by getDisplayList() when a display list is created or re-rendered.
12490 * It sets or resets the current value of all properties on that display list (resetting is
12491 * necessary when a display list is being re-created, because we need to make sure that
12492 * previously-set transform values
12493 */
12494 void setDisplayListProperties(DisplayList displayList) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012495 if (displayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012496 displayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
Chet Haasedb8c9a62012-03-21 18:54:18 -070012497 displayList.setHasOverlappingRendering(hasOverlappingRendering());
Chet Haasea1cff502012-02-21 13:43:44 -080012498 if (mParent instanceof ViewGroup) {
12499 displayList.setClipChildren(
12500 (((ViewGroup)mParent).mGroupFlags & ViewGroup.FLAG_CLIP_CHILDREN) != 0);
12501 }
Chet Haase9420abd2012-03-29 16:28:32 -070012502 float alpha = 1;
12503 if (mParent instanceof ViewGroup && (((ViewGroup) mParent).mGroupFlags &
12504 ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
12505 ViewGroup parentVG = (ViewGroup) mParent;
12506 final boolean hasTransform =
12507 parentVG.getChildStaticTransformation(this, parentVG.mChildTransformation);
12508 if (hasTransform) {
12509 Transformation transform = parentVG.mChildTransformation;
12510 final int transformType = parentVG.mChildTransformation.getTransformationType();
12511 if (transformType != Transformation.TYPE_IDENTITY) {
12512 if ((transformType & Transformation.TYPE_ALPHA) != 0) {
12513 alpha = transform.getAlpha();
12514 }
12515 if ((transformType & Transformation.TYPE_MATRIX) != 0) {
12516 displayList.setStaticMatrix(transform.getMatrix());
12517 }
12518 }
12519 }
Chet Haasea1cff502012-02-21 13:43:44 -080012520 }
12521 if (mTransformationInfo != null) {
Chet Haase9420abd2012-03-29 16:28:32 -070012522 alpha *= mTransformationInfo.mAlpha;
12523 if (alpha < 1) {
12524 final int multipliedAlpha = (int) (255 * alpha);
12525 if (onSetAlpha(multipliedAlpha)) {
12526 alpha = 1;
12527 }
12528 }
12529 displayList.setTransformationInfo(alpha,
Chet Haasea1cff502012-02-21 13:43:44 -080012530 mTransformationInfo.mTranslationX, mTransformationInfo.mTranslationY,
12531 mTransformationInfo.mRotation, mTransformationInfo.mRotationX,
12532 mTransformationInfo.mRotationY, mTransformationInfo.mScaleX,
12533 mTransformationInfo.mScaleY);
Chet Haaseb85967b2012-03-26 14:37:51 -070012534 if (mTransformationInfo.mCamera == null) {
12535 mTransformationInfo.mCamera = new Camera();
12536 mTransformationInfo.matrix3D = new Matrix();
12537 }
12538 displayList.setCameraDistance(mTransformationInfo.mCamera.getLocationZ());
Chet Haasea1cff502012-02-21 13:43:44 -080012539 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == PIVOT_EXPLICITLY_SET) {
12540 displayList.setPivotX(getPivotX());
12541 displayList.setPivotY(getPivotY());
12542 }
Chet Haase9420abd2012-03-29 16:28:32 -070012543 } else if (alpha < 1) {
12544 displayList.setAlpha(alpha);
Chet Haasea1cff502012-02-21 13:43:44 -080012545 }
12546 }
12547 }
12548
Chet Haasebcca79a2012-02-14 08:45:14 -080012549 /**
Chet Haase64a48c12012-02-13 16:33:29 -080012550 * This method is called by ViewGroup.drawChild() to have each child view draw itself.
12551 * This draw() method is an implementation detail and is not intended to be overridden or
12552 * to be called from anywhere else other than ViewGroup.drawChild().
12553 */
12554 boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
Chet Haase1271e2c2012-04-20 09:54:27 -070012555 boolean useDisplayListProperties = mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
Chet Haase64a48c12012-02-13 16:33:29 -080012556 boolean more = false;
Chet Haase64a48c12012-02-13 16:33:29 -080012557 final boolean childHasIdentityMatrix = hasIdentityMatrix();
Chet Haase64a48c12012-02-13 16:33:29 -080012558 final int flags = parent.mGroupFlags;
12559
Chet Haasea1cff502012-02-21 13:43:44 -080012560 if ((flags & ViewGroup.FLAG_CLEAR_TRANSFORMATION) == ViewGroup.FLAG_CLEAR_TRANSFORMATION) {
Chet Haase64a48c12012-02-13 16:33:29 -080012561 parent.mChildTransformation.clear();
Chet Haasea1cff502012-02-21 13:43:44 -080012562 parent.mGroupFlags &= ~ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012563 }
12564
12565 Transformation transformToApply = null;
Chet Haase64a48c12012-02-13 16:33:29 -080012566 boolean concatMatrix = false;
12567
12568 boolean scalingRequired = false;
12569 boolean caching;
12570 int layerType = parent.mDrawLayers ? getLayerType() : LAYER_TYPE_NONE;
12571
12572 final boolean hardwareAccelerated = canvas.isHardwareAccelerated();
Chet Haasea1cff502012-02-21 13:43:44 -080012573 if ((flags & ViewGroup.FLAG_CHILDREN_DRAWN_WITH_CACHE) != 0 ||
12574 (flags & ViewGroup.FLAG_ALWAYS_DRAWN_WITH_CACHE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012575 caching = true;
Chet Haase9420abd2012-03-29 16:28:32 -070012576 // Auto-scaled apps are not hw-accelerated, no need to set scaling flag on DisplayList
Chet Haase64a48c12012-02-13 16:33:29 -080012577 if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired;
12578 } else {
12579 caching = (layerType != LAYER_TYPE_NONE) || hardwareAccelerated;
12580 }
12581
Chet Haasebcca79a2012-02-14 08:45:14 -080012582 final Animation a = getAnimation();
Chet Haase64a48c12012-02-13 16:33:29 -080012583 if (a != null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012584 more = drawAnimation(parent, drawingTime, a, scalingRequired);
Chet Haase64a48c12012-02-13 16:33:29 -080012585 concatMatrix = a.willChangeTransformationMatrix();
Chet Haasebcca79a2012-02-14 08:45:14 -080012586 transformToApply = parent.mChildTransformation;
Chet Haase9420abd2012-03-29 16:28:32 -070012587 } else if (!useDisplayListProperties &&
12588 (flags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012589 final boolean hasTransform =
12590 parent.getChildStaticTransformation(this, parent.mChildTransformation);
Chet Haase64a48c12012-02-13 16:33:29 -080012591 if (hasTransform) {
12592 final int transformType = parent.mChildTransformation.getTransformationType();
12593 transformToApply = transformType != Transformation.TYPE_IDENTITY ?
12594 parent.mChildTransformation : null;
12595 concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0;
12596 }
12597 }
12598
12599 concatMatrix |= !childHasIdentityMatrix;
12600
12601 // Sets the flag as early as possible to allow draw() implementations
12602 // to call invalidate() successfully when doing animations
12603 mPrivateFlags |= DRAWN;
12604
Chet Haasebcca79a2012-02-14 08:45:14 -080012605 if (!concatMatrix && canvas.quickReject(mLeft, mTop, mRight, mBottom, Canvas.EdgeType.BW) &&
Chet Haase64a48c12012-02-13 16:33:29 -080012606 (mPrivateFlags & DRAW_ANIMATION) == 0) {
12607 return more;
12608 }
12609
12610 if (hardwareAccelerated) {
12611 // Clear INVALIDATED flag to allow invalidation to occur during rendering, but
12612 // retain the flag's value temporarily in the mRecreateDisplayList flag
12613 mRecreateDisplayList = (mPrivateFlags & INVALIDATED) == INVALIDATED;
12614 mPrivateFlags &= ~INVALIDATED;
12615 }
12616
12617 computeScroll();
12618
12619 final int sx = mScrollX;
12620 final int sy = mScrollY;
12621
12622 DisplayList displayList = null;
12623 Bitmap cache = null;
12624 boolean hasDisplayList = false;
12625 if (caching) {
12626 if (!hardwareAccelerated) {
12627 if (layerType != LAYER_TYPE_NONE) {
12628 layerType = LAYER_TYPE_SOFTWARE;
12629 buildDrawingCache(true);
12630 }
12631 cache = getDrawingCache(true);
12632 } else {
12633 switch (layerType) {
12634 case LAYER_TYPE_SOFTWARE:
Chet Haaseb85967b2012-03-26 14:37:51 -070012635 if (useDisplayListProperties) {
12636 hasDisplayList = canHaveDisplayList();
12637 } else {
12638 buildDrawingCache(true);
12639 cache = getDrawingCache(true);
12640 }
Chet Haase64a48c12012-02-13 16:33:29 -080012641 break;
Chet Haasea1cff502012-02-21 13:43:44 -080012642 case LAYER_TYPE_HARDWARE:
12643 if (useDisplayListProperties) {
12644 hasDisplayList = canHaveDisplayList();
12645 }
12646 break;
Chet Haase64a48c12012-02-13 16:33:29 -080012647 case LAYER_TYPE_NONE:
12648 // Delay getting the display list until animation-driven alpha values are
12649 // set up and possibly passed on to the view
12650 hasDisplayList = canHaveDisplayList();
12651 break;
12652 }
12653 }
12654 }
Chet Haasea1cff502012-02-21 13:43:44 -080012655 useDisplayListProperties &= hasDisplayList;
Chet Haase9420abd2012-03-29 16:28:32 -070012656 if (useDisplayListProperties) {
12657 displayList = getDisplayList();
12658 if (!displayList.isValid()) {
12659 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12660 // to getDisplayList(), the display list will be marked invalid and we should not
12661 // try to use it again.
12662 displayList = null;
12663 hasDisplayList = false;
12664 useDisplayListProperties = false;
12665 }
12666 }
Chet Haase64a48c12012-02-13 16:33:29 -080012667
12668 final boolean hasNoCache = cache == null || hasDisplayList;
12669 final boolean offsetForScroll = cache == null && !hasDisplayList &&
12670 layerType != LAYER_TYPE_HARDWARE;
12671
Chet Haasea1cff502012-02-21 13:43:44 -080012672 int restoreTo = -1;
Chet Haase89b7f2e2012-03-21 11:15:37 -070012673 if (!useDisplayListProperties || transformToApply != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080012674 restoreTo = canvas.save();
12675 }
Chet Haase64a48c12012-02-13 16:33:29 -080012676 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012677 canvas.translate(mLeft - sx, mTop - sy);
Chet Haase64a48c12012-02-13 16:33:29 -080012678 } else {
Chet Haasea1cff502012-02-21 13:43:44 -080012679 if (!useDisplayListProperties) {
12680 canvas.translate(mLeft, mTop);
12681 }
Chet Haase64a48c12012-02-13 16:33:29 -080012682 if (scalingRequired) {
Chet Haasea1cff502012-02-21 13:43:44 -080012683 if (useDisplayListProperties) {
Chet Haase9420abd2012-03-29 16:28:32 -070012684 // TODO: Might not need this if we put everything inside the DL
Chet Haasea1cff502012-02-21 13:43:44 -080012685 restoreTo = canvas.save();
12686 }
Chet Haase64a48c12012-02-13 16:33:29 -080012687 // mAttachInfo cannot be null, otherwise scalingRequired == false
12688 final float scale = 1.0f / mAttachInfo.mApplicationScale;
12689 canvas.scale(scale, scale);
12690 }
12691 }
12692
Chet Haasea1cff502012-02-21 13:43:44 -080012693 float alpha = useDisplayListProperties ? 1 : getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012694 if (transformToApply != null || alpha < 1 || !hasIdentityMatrix()) {
Chet Haase64a48c12012-02-13 16:33:29 -080012695 if (transformToApply != null || !childHasIdentityMatrix) {
12696 int transX = 0;
12697 int transY = 0;
12698
12699 if (offsetForScroll) {
12700 transX = -sx;
12701 transY = -sy;
12702 }
12703
12704 if (transformToApply != null) {
12705 if (concatMatrix) {
Chet Haase9420abd2012-03-29 16:28:32 -070012706 if (useDisplayListProperties) {
12707 displayList.setAnimationMatrix(transformToApply.getMatrix());
12708 } else {
12709 // Undo the scroll translation, apply the transformation matrix,
12710 // then redo the scroll translate to get the correct result.
12711 canvas.translate(-transX, -transY);
12712 canvas.concat(transformToApply.getMatrix());
12713 canvas.translate(transX, transY);
12714 }
Chet Haasea1cff502012-02-21 13:43:44 -080012715 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012716 }
12717
12718 float transformAlpha = transformToApply.getAlpha();
Chet Haase9420abd2012-03-29 16:28:32 -070012719 if (transformAlpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012720 alpha *= transformToApply.getAlpha();
Chet Haasea1cff502012-02-21 13:43:44 -080012721 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012722 }
12723 }
12724
Chet Haasea1cff502012-02-21 13:43:44 -080012725 if (!childHasIdentityMatrix && !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012726 canvas.translate(-transX, -transY);
12727 canvas.concat(getMatrix());
12728 canvas.translate(transX, transY);
12729 }
12730 }
12731
Chet Haase9420abd2012-03-29 16:28:32 -070012732 if (alpha < 1) {
Chet Haasea1cff502012-02-21 13:43:44 -080012733 parent.mGroupFlags |= ViewGroup.FLAG_CLEAR_TRANSFORMATION;
Chet Haase64a48c12012-02-13 16:33:29 -080012734 if (hasNoCache) {
12735 final int multipliedAlpha = (int) (255 * alpha);
12736 if (!onSetAlpha(multipliedAlpha)) {
12737 int layerFlags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
Chet Haasea1cff502012-02-21 13:43:44 -080012738 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) != 0 ||
Chet Haase64a48c12012-02-13 16:33:29 -080012739 layerType != LAYER_TYPE_NONE) {
12740 layerFlags |= Canvas.CLIP_TO_LAYER_SAVE_FLAG;
12741 }
Chet Haase9420abd2012-03-29 16:28:32 -070012742 if (useDisplayListProperties) {
12743 displayList.setAlpha(alpha * getAlpha());
12744 } else if (layerType == LAYER_TYPE_NONE) {
Chet Haase89b7f2e2012-03-21 11:15:37 -070012745 final int scrollX = hasDisplayList ? 0 : sx;
12746 final int scrollY = hasDisplayList ? 0 : sy;
12747 canvas.saveLayerAlpha(scrollX, scrollY, scrollX + mRight - mLeft,
12748 scrollY + mBottom - mTop, multipliedAlpha, layerFlags);
Chet Haase64a48c12012-02-13 16:33:29 -080012749 }
12750 } else {
12751 // Alpha is handled by the child directly, clobber the layer's alpha
12752 mPrivateFlags |= ALPHA_SET;
12753 }
12754 }
12755 }
12756 } else if ((mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12757 onSetAlpha(255);
12758 mPrivateFlags &= ~ALPHA_SET;
12759 }
12760
Chet Haasea1cff502012-02-21 13:43:44 -080012761 if ((flags & ViewGroup.FLAG_CLIP_CHILDREN) == ViewGroup.FLAG_CLIP_CHILDREN &&
12762 !useDisplayListProperties) {
Chet Haase64a48c12012-02-13 16:33:29 -080012763 if (offsetForScroll) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012764 canvas.clipRect(sx, sy, sx + (mRight - mLeft), sy + (mBottom - mTop));
Chet Haase64a48c12012-02-13 16:33:29 -080012765 } else {
12766 if (!scalingRequired || cache == null) {
Chet Haasebcca79a2012-02-14 08:45:14 -080012767 canvas.clipRect(0, 0, mRight - mLeft, mBottom - mTop);
Chet Haase64a48c12012-02-13 16:33:29 -080012768 } else {
12769 canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight());
12770 }
12771 }
12772 }
12773
Chet Haase9420abd2012-03-29 16:28:32 -070012774 if (!useDisplayListProperties && hasDisplayList) {
Chet Haase64a48c12012-02-13 16:33:29 -080012775 displayList = getDisplayList();
12776 if (!displayList.isValid()) {
12777 // Uncommon, but possible. If a view is removed from the hierarchy during the call
12778 // to getDisplayList(), the display list will be marked invalid and we should not
12779 // try to use it again.
12780 displayList = null;
12781 hasDisplayList = false;
12782 }
12783 }
12784
12785 if (hasNoCache) {
12786 boolean layerRendered = false;
Chet Haasea1cff502012-02-21 13:43:44 -080012787 if (layerType == LAYER_TYPE_HARDWARE && !useDisplayListProperties) {
Michael Jurka7e52caf2012-03-06 15:57:06 -080012788 final HardwareLayer layer = getHardwareLayer();
Chet Haase64a48c12012-02-13 16:33:29 -080012789 if (layer != null && layer.isValid()) {
12790 mLayerPaint.setAlpha((int) (alpha * 255));
12791 ((HardwareCanvas) canvas).drawHardwareLayer(layer, 0, 0, mLayerPaint);
12792 layerRendered = true;
12793 } else {
12794 final int scrollX = hasDisplayList ? 0 : sx;
12795 final int scrollY = hasDisplayList ? 0 : sy;
12796 canvas.saveLayer(scrollX, scrollY,
Chet Haasebcca79a2012-02-14 08:45:14 -080012797 scrollX + mRight - mLeft, scrollY + mBottom - mTop, mLayerPaint,
Chet Haase64a48c12012-02-13 16:33:29 -080012798 Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
12799 }
12800 }
12801
12802 if (!layerRendered) {
12803 if (!hasDisplayList) {
12804 // Fast path for layouts with no backgrounds
12805 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
12806 if (ViewDebug.TRACE_HIERARCHY) {
12807 ViewDebug.trace(parent, ViewDebug.HierarchyTraceType.DRAW);
12808 }
12809 mPrivateFlags &= ~DIRTY_MASK;
12810 dispatchDraw(canvas);
12811 } else {
12812 draw(canvas);
12813 }
12814 } else {
12815 mPrivateFlags &= ~DIRTY_MASK;
Chet Haase1271e2c2012-04-20 09:54:27 -070012816 ((HardwareCanvas) canvas).drawDisplayList(displayList, null, flags);
Chet Haase64a48c12012-02-13 16:33:29 -080012817 }
12818 }
12819 } else if (cache != null) {
12820 mPrivateFlags &= ~DIRTY_MASK;
12821 Paint cachePaint;
12822
12823 if (layerType == LAYER_TYPE_NONE) {
12824 cachePaint = parent.mCachePaint;
12825 if (cachePaint == null) {
12826 cachePaint = new Paint();
12827 cachePaint.setDither(false);
12828 parent.mCachePaint = cachePaint;
12829 }
Chet Haase9420abd2012-03-29 16:28:32 -070012830 if (alpha < 1) {
Chet Haase64a48c12012-02-13 16:33:29 -080012831 cachePaint.setAlpha((int) (alpha * 255));
Chet Haasea1cff502012-02-21 13:43:44 -080012832 parent.mGroupFlags |= ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
12833 } else if ((flags & ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE) != 0) {
Chet Haase64a48c12012-02-13 16:33:29 -080012834 cachePaint.setAlpha(255);
Chet Haasea1cff502012-02-21 13:43:44 -080012835 parent.mGroupFlags &= ~ViewGroup.FLAG_ALPHA_LOWER_THAN_ONE;
Chet Haase64a48c12012-02-13 16:33:29 -080012836 }
12837 } else {
12838 cachePaint = mLayerPaint;
12839 cachePaint.setAlpha((int) (alpha * 255));
12840 }
12841 canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);
12842 }
12843
Chet Haasea1cff502012-02-21 13:43:44 -080012844 if (restoreTo >= 0) {
12845 canvas.restoreToCount(restoreTo);
12846 }
Chet Haase64a48c12012-02-13 16:33:29 -080012847
12848 if (a != null && !more) {
12849 if (!hardwareAccelerated && !a.getFillAfter()) {
12850 onSetAlpha(255);
12851 }
12852 parent.finishAnimatingView(this, a);
12853 }
12854
12855 if (more && hardwareAccelerated) {
12856 // invalidation is the trigger to recreate display lists, so if we're using
12857 // display lists to render, force an invalidate to allow the animation to
12858 // continue drawing another frame
12859 parent.invalidate(true);
12860 if (a.hasAlpha() && (mPrivateFlags & ALPHA_SET) == ALPHA_SET) {
12861 // alpha animations should cause the child to recreate its display list
12862 invalidate(true);
12863 }
12864 }
12865
12866 mRecreateDisplayList = false;
12867
12868 return more;
12869 }
12870
12871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012872 * Manually render this view (and all of its children) to the given Canvas.
12873 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070012874 * called. When implementing a view, implement
12875 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
12876 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012877 *
12878 * @param canvas The Canvas to which the View is rendered.
12879 */
12880 public void draw(Canvas canvas) {
12881 if (ViewDebug.TRACE_HIERARCHY) {
12882 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
12883 }
12884
Romain Guy5bcdff42009-05-14 21:27:18 -070012885 final int privateFlags = mPrivateFlags;
12886 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
12887 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
12888 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070012889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012890 /*
12891 * Draw traversal performs several drawing steps which must be executed
12892 * in the appropriate order:
12893 *
12894 * 1. Draw the background
12895 * 2. If necessary, save the canvas' layers to prepare for fading
12896 * 3. Draw view's content
12897 * 4. Draw children
12898 * 5. If necessary, draw the fading edges and restore layers
12899 * 6. Draw decorations (scrollbars for instance)
12900 */
12901
12902 // Step 1, draw the background, if needed
12903 int saveCount;
12904
Romain Guy24443ea2009-05-11 11:56:30 -070012905 if (!dirtyOpaque) {
Philip Milne6c8ea062012-04-03 17:38:43 -070012906 final Drawable background = mBackground;
Romain Guy24443ea2009-05-11 11:56:30 -070012907 if (background != null) {
12908 final int scrollX = mScrollX;
12909 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012910
Romain Guy24443ea2009-05-11 11:56:30 -070012911 if (mBackgroundSizeChanged) {
12912 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
12913 mBackgroundSizeChanged = false;
12914 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012915
Romain Guy24443ea2009-05-11 11:56:30 -070012916 if ((scrollX | scrollY) == 0) {
12917 background.draw(canvas);
12918 } else {
12919 canvas.translate(scrollX, scrollY);
12920 background.draw(canvas);
12921 canvas.translate(-scrollX, -scrollY);
12922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012923 }
12924 }
12925
12926 // skip step 2 & 5 if possible (common case)
12927 final int viewFlags = mViewFlags;
12928 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
12929 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
12930 if (!verticalEdges && !horizontalEdges) {
12931 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070012932 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012933
12934 // Step 4, draw the children
12935 dispatchDraw(canvas);
12936
12937 // Step 6, draw decorations (scrollbars)
12938 onDrawScrollBars(canvas);
12939
12940 // we're done...
12941 return;
12942 }
12943
12944 /*
12945 * Here we do the full fledged routine...
12946 * (this is an uncommon case where speed matters less,
12947 * this is why we repeat some of the tests that have been
12948 * done above)
12949 */
12950
12951 boolean drawTop = false;
12952 boolean drawBottom = false;
12953 boolean drawLeft = false;
12954 boolean drawRight = false;
12955
12956 float topFadeStrength = 0.0f;
12957 float bottomFadeStrength = 0.0f;
12958 float leftFadeStrength = 0.0f;
12959 float rightFadeStrength = 0.0f;
12960
12961 // Step 2, save the canvas' layers
12962 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012963
12964 final boolean offsetRequired = isPaddingOffsetRequired();
12965 if (offsetRequired) {
12966 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012967 }
12968
12969 int left = mScrollX + paddingLeft;
12970 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070012971 int top = mScrollY + getFadeTop(offsetRequired);
12972 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012973
12974 if (offsetRequired) {
12975 right += getRightPaddingOffset();
12976 bottom += getBottomPaddingOffset();
12977 }
12978
12979 final ScrollabilityCache scrollabilityCache = mScrollCache;
Philip Milne6c8ea062012-04-03 17:38:43 -070012980 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012981 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012982
12983 // clip the fade length if top and bottom fades overlap
12984 // overlapping fades produce odd-looking artifacts
12985 if (verticalEdges && (top + length > bottom - length)) {
12986 length = (bottom - top) / 2;
12987 }
12988
12989 // also clip horizontal fades if necessary
12990 if (horizontalEdges && (left + length > right - length)) {
12991 length = (right - left) / 2;
12992 }
12993
12994 if (verticalEdges) {
12995 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012996 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012997 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012998 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012999 }
13000
13001 if (horizontalEdges) {
13002 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013003 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013004 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013005 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013006 }
13007
13008 saveCount = canvas.getSaveCount();
13009
13010 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070013011 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013012 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
13013
13014 if (drawTop) {
13015 canvas.saveLayer(left, top, right, top + length, null, flags);
13016 }
13017
13018 if (drawBottom) {
13019 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
13020 }
13021
13022 if (drawLeft) {
13023 canvas.saveLayer(left, top, left + length, bottom, null, flags);
13024 }
13025
13026 if (drawRight) {
13027 canvas.saveLayer(right - length, top, right, bottom, null, flags);
13028 }
13029 } else {
13030 scrollabilityCache.setFadeColor(solidColor);
13031 }
13032
13033 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070013034 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013035
13036 // Step 4, draw the children
13037 dispatchDraw(canvas);
13038
13039 // Step 5, draw the fade effect and restore layers
13040 final Paint p = scrollabilityCache.paint;
13041 final Matrix matrix = scrollabilityCache.matrix;
13042 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013043
13044 if (drawTop) {
13045 matrix.setScale(1, fadeHeight * topFadeStrength);
13046 matrix.postTranslate(left, top);
13047 fade.setLocalMatrix(matrix);
13048 canvas.drawRect(left, top, right, top + length, p);
13049 }
13050
13051 if (drawBottom) {
13052 matrix.setScale(1, fadeHeight * bottomFadeStrength);
13053 matrix.postRotate(180);
13054 matrix.postTranslate(left, bottom);
13055 fade.setLocalMatrix(matrix);
13056 canvas.drawRect(left, bottom - length, right, bottom, p);
13057 }
13058
13059 if (drawLeft) {
13060 matrix.setScale(1, fadeHeight * leftFadeStrength);
13061 matrix.postRotate(-90);
13062 matrix.postTranslate(left, top);
13063 fade.setLocalMatrix(matrix);
13064 canvas.drawRect(left, top, left + length, bottom, p);
13065 }
13066
13067 if (drawRight) {
13068 matrix.setScale(1, fadeHeight * rightFadeStrength);
13069 matrix.postRotate(90);
13070 matrix.postTranslate(right, top);
13071 fade.setLocalMatrix(matrix);
13072 canvas.drawRect(right - length, top, right, bottom, p);
13073 }
13074
13075 canvas.restoreToCount(saveCount);
13076
13077 // Step 6, draw decorations (scrollbars)
13078 onDrawScrollBars(canvas);
13079 }
13080
13081 /**
13082 * Override this if your view is known to always be drawn on top of a solid color background,
13083 * and needs to draw fading edges. Returning a non-zero color enables the view system to
13084 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
13085 * should be set to 0xFF.
13086 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013087 * @see #setVerticalFadingEdgeEnabled(boolean)
13088 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 *
13090 * @return The known solid color background for this view, or 0 if the color may vary
13091 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013092 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013093 public int getSolidColor() {
13094 return 0;
13095 }
13096
13097 /**
13098 * Build a human readable string representation of the specified view flags.
13099 *
13100 * @param flags the view flags to convert to a string
13101 * @return a String representing the supplied flags
13102 */
13103 private static String printFlags(int flags) {
13104 String output = "";
13105 int numFlags = 0;
13106 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
13107 output += "TAKES_FOCUS";
13108 numFlags++;
13109 }
13110
13111 switch (flags & VISIBILITY_MASK) {
13112 case INVISIBLE:
13113 if (numFlags > 0) {
13114 output += " ";
13115 }
13116 output += "INVISIBLE";
13117 // USELESS HERE numFlags++;
13118 break;
13119 case GONE:
13120 if (numFlags > 0) {
13121 output += " ";
13122 }
13123 output += "GONE";
13124 // USELESS HERE numFlags++;
13125 break;
13126 default:
13127 break;
13128 }
13129 return output;
13130 }
13131
13132 /**
13133 * Build a human readable string representation of the specified private
13134 * view flags.
13135 *
13136 * @param privateFlags the private view flags to convert to a string
13137 * @return a String representing the supplied flags
13138 */
13139 private static String printPrivateFlags(int privateFlags) {
13140 String output = "";
13141 int numFlags = 0;
13142
13143 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
13144 output += "WANTS_FOCUS";
13145 numFlags++;
13146 }
13147
13148 if ((privateFlags & FOCUSED) == FOCUSED) {
13149 if (numFlags > 0) {
13150 output += " ";
13151 }
13152 output += "FOCUSED";
13153 numFlags++;
13154 }
13155
13156 if ((privateFlags & SELECTED) == SELECTED) {
13157 if (numFlags > 0) {
13158 output += " ";
13159 }
13160 output += "SELECTED";
13161 numFlags++;
13162 }
13163
13164 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
13165 if (numFlags > 0) {
13166 output += " ";
13167 }
13168 output += "IS_ROOT_NAMESPACE";
13169 numFlags++;
13170 }
13171
13172 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
13173 if (numFlags > 0) {
13174 output += " ";
13175 }
13176 output += "HAS_BOUNDS";
13177 numFlags++;
13178 }
13179
13180 if ((privateFlags & DRAWN) == DRAWN) {
13181 if (numFlags > 0) {
13182 output += " ";
13183 }
13184 output += "DRAWN";
13185 // USELESS HERE numFlags++;
13186 }
13187 return output;
13188 }
13189
13190 /**
13191 * <p>Indicates whether or not this view's layout will be requested during
13192 * the next hierarchy layout pass.</p>
13193 *
13194 * @return true if the layout will be forced during next layout pass
13195 */
13196 public boolean isLayoutRequested() {
13197 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
13198 }
13199
13200 /**
13201 * Assign a size and position to a view and all of its
13202 * descendants
13203 *
13204 * <p>This is the second phase of the layout mechanism.
13205 * (The first is measuring). In this phase, each parent calls
13206 * layout on all of its children to position them.
13207 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080013208 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013209 *
Chet Haase9c087442011-01-12 16:20:16 -080013210 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211 * Derived classes with children should override
13212 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080013213 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013214 *
13215 * @param l Left position, relative to parent
13216 * @param t Top position, relative to parent
13217 * @param r Right position, relative to parent
13218 * @param b Bottom position, relative to parent
13219 */
Romain Guy5429e1d2010-09-07 12:38:00 -070013220 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080013221 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070013222 int oldL = mLeft;
13223 int oldT = mTop;
13224 int oldB = mBottom;
13225 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013226 boolean changed = setFrame(l, t, r, b);
13227 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
13228 if (ViewDebug.TRACE_HIERARCHY) {
13229 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
13230 }
13231
13232 onLayout(changed, l, t, r, b);
13233 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070013234
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013235 ListenerInfo li = mListenerInfo;
13236 if (li != null && li.mOnLayoutChangeListeners != null) {
Chet Haase21cd1382010-09-01 17:42:29 -070013237 ArrayList<OnLayoutChangeListener> listenersCopy =
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070013238 (ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
Chet Haase21cd1382010-09-01 17:42:29 -070013239 int numListeners = listenersCopy.size();
13240 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070013241 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070013242 }
13243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013244 }
13245 mPrivateFlags &= ~FORCE_LAYOUT;
13246 }
13247
13248 /**
13249 * Called from layout when this view should
13250 * assign a size and position to each of its children.
13251 *
13252 * Derived classes with children should override
13253 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070013254 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013255 * @param changed This is a new size or position for this view
13256 * @param left Left position, relative to parent
13257 * @param top Top position, relative to parent
13258 * @param right Right position, relative to parent
13259 * @param bottom Bottom position, relative to parent
13260 */
13261 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
13262 }
13263
13264 /**
13265 * Assign a size and position to this view.
13266 *
13267 * This is called from layout.
13268 *
13269 * @param left Left position, relative to parent
13270 * @param top Top position, relative to parent
13271 * @param right Right position, relative to parent
13272 * @param bottom Bottom position, relative to parent
13273 * @return true if the new size and position are different than the
13274 * previous ones
13275 * {@hide}
13276 */
13277 protected boolean setFrame(int left, int top, int right, int bottom) {
13278 boolean changed = false;
13279
13280 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070013281 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013282 + right + "," + bottom + ")");
13283 }
13284
13285 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
13286 changed = true;
13287
13288 // Remember our drawn bit
13289 int drawn = mPrivateFlags & DRAWN;
13290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013291 int oldWidth = mRight - mLeft;
13292 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070013293 int newWidth = right - left;
13294 int newHeight = bottom - top;
13295 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
13296
13297 // Invalidate our old position
13298 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013299
13300 mLeft = left;
13301 mTop = top;
13302 mRight = right;
13303 mBottom = bottom;
Chet Haase1271e2c2012-04-20 09:54:27 -070013304 if (mDisplayList != null) {
Chet Haasea1cff502012-02-21 13:43:44 -080013305 mDisplayList.setLeftTopRightBottom(mLeft, mTop, mRight, mBottom);
13306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013307
13308 mPrivateFlags |= HAS_BOUNDS;
13309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013310
Chet Haase75755e22011-07-18 17:48:25 -070013311 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013312 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
13313 // A change in dimension means an auto-centered pivot point changes, too
Dianne Hackbornddb715b2011-09-09 14:43:39 -070013314 if (mTransformationInfo != null) {
13315 mTransformationInfo.mMatrixDirty = true;
13316 }
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013318 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
13319 }
13320
13321 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
13322 // If we are visible, force the DRAWN bit to on so that
13323 // this invalidate will go through (at least to our parent).
13324 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080013325 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013326 // the DRAWN bit.
13327 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070013328 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080013329 // parent display list may need to be recreated based on a change in the bounds
13330 // of any child
13331 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013332 }
13333
13334 // Reset drawn bit to original value (invalidate turns it off)
13335 mPrivateFlags |= drawn;
13336
13337 mBackgroundSizeChanged = true;
13338 }
13339 return changed;
13340 }
13341
13342 /**
13343 * Finalize inflating a view from XML. This is called as the last phase
13344 * of inflation, after all child views have been added.
13345 *
13346 * <p>Even if the subclass overrides onFinishInflate, they should always be
13347 * sure to call the super method, so that we get called.
13348 */
13349 protected void onFinishInflate() {
13350 }
13351
13352 /**
13353 * Returns the resources associated with this view.
13354 *
13355 * @return Resources object.
13356 */
13357 public Resources getResources() {
13358 return mResources;
13359 }
13360
13361 /**
13362 * Invalidates the specified Drawable.
13363 *
13364 * @param drawable the drawable to invalidate
13365 */
13366 public void invalidateDrawable(Drawable drawable) {
13367 if (verifyDrawable(drawable)) {
13368 final Rect dirty = drawable.getBounds();
13369 final int scrollX = mScrollX;
13370 final int scrollY = mScrollY;
13371
13372 invalidate(dirty.left + scrollX, dirty.top + scrollY,
13373 dirty.right + scrollX, dirty.bottom + scrollY);
13374 }
13375 }
13376
13377 /**
13378 * Schedules an action on a drawable to occur at a specified time.
13379 *
13380 * @param who the recipient of the action
13381 * @param what the action to run on the drawable
13382 * @param when the time at which the action must occur. Uses the
13383 * {@link SystemClock#uptimeMillis} timebase.
13384 */
13385 public void scheduleDrawable(Drawable who, Runnable what, long when) {
Adam Powell37419d72011-11-10 11:32:09 -080013386 if (verifyDrawable(who) && what != null) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013387 final long delay = when - SystemClock.uptimeMillis();
Adam Powell37419d72011-11-10 11:32:09 -080013388 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013389 mAttachInfo.mViewRootImpl.mChoreographer.postCallbackDelayed(
13390 Choreographer.CALLBACK_ANIMATION, what, who,
13391 Choreographer.subtractFrameDelay(delay));
Adam Powell37419d72011-11-10 11:32:09 -080013392 } else {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -080013393 ViewRootImpl.getRunQueue().postDelayed(what, delay);
Adam Powell37419d72011-11-10 11:32:09 -080013394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013395 }
13396 }
13397
13398 /**
13399 * Cancels a scheduled action on a drawable.
13400 *
13401 * @param who the recipient of the action
13402 * @param what the action to cancel
13403 */
13404 public void unscheduleDrawable(Drawable who, Runnable what) {
Adam Powell37419d72011-11-10 11:32:09 -080013405 if (verifyDrawable(who) && what != null) {
13406 if (mAttachInfo != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013407 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13408 Choreographer.CALLBACK_ANIMATION, what, who);
Adam Powell37419d72011-11-10 11:32:09 -080013409 } else {
13410 ViewRootImpl.getRunQueue().removeCallbacks(what);
13411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013412 }
13413 }
13414
13415 /**
13416 * Unschedule any events associated with the given Drawable. This can be
13417 * used when selecting a new Drawable into a view, so that the previous
13418 * one is completely unscheduled.
13419 *
13420 * @param who The Drawable to unschedule.
13421 *
13422 * @see #drawableStateChanged
13423 */
13424 public void unscheduleDrawable(Drawable who) {
Jeff Brown43ea54b2012-03-09 14:37:48 -080013425 if (mAttachInfo != null && who != null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -070013426 mAttachInfo.mViewRootImpl.mChoreographer.removeCallbacks(
13427 Choreographer.CALLBACK_ANIMATION, null, who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013428 }
13429 }
13430
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013431 /**
13432 * Return the layout direction of a given Drawable.
13433 *
13434 * @param who the Drawable to query
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013435 * @hide
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070013436 */
13437 public int getResolvedLayoutDirection(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013438 return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070013439 }
13440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013441 /**
13442 * If your view subclass is displaying its own Drawable objects, it should
13443 * override this function and return true for any Drawable it is
13444 * displaying. This allows animations for those drawables to be
13445 * scheduled.
13446 *
13447 * <p>Be sure to call through to the super class when overriding this
13448 * function.
13449 *
13450 * @param who The Drawable to verify. Return true if it is one you are
13451 * displaying, else return the result of calling through to the
13452 * super class.
13453 *
13454 * @return boolean If true than the Drawable is being displayed in the
13455 * view; else false and it is not allowed to animate.
13456 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013457 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
13458 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013459 */
13460 protected boolean verifyDrawable(Drawable who) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013461 return who == mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013462 }
13463
13464 /**
13465 * This function is called whenever the state of the view changes in such
13466 * a way that it impacts the state of drawables being shown.
13467 *
13468 * <p>Be sure to call through to the superclass when overriding this
13469 * function.
13470 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013471 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013472 */
13473 protected void drawableStateChanged() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013474 Drawable d = mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013475 if (d != null && d.isStateful()) {
13476 d.setState(getDrawableState());
13477 }
13478 }
13479
13480 /**
13481 * Call this to force a view to update its drawable state. This will cause
13482 * drawableStateChanged to be called on this view. Views that are interested
13483 * in the new state should call getDrawableState.
13484 *
13485 * @see #drawableStateChanged
13486 * @see #getDrawableState
13487 */
13488 public void refreshDrawableState() {
13489 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
13490 drawableStateChanged();
13491
13492 ViewParent parent = mParent;
13493 if (parent != null) {
13494 parent.childDrawableStateChanged(this);
13495 }
13496 }
13497
13498 /**
13499 * Return an array of resource IDs of the drawable states representing the
13500 * current state of the view.
13501 *
13502 * @return The current drawable state
13503 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013504 * @see Drawable#setState(int[])
13505 * @see #drawableStateChanged()
13506 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013507 */
13508 public final int[] getDrawableState() {
13509 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
13510 return mDrawableState;
13511 } else {
13512 mDrawableState = onCreateDrawableState(0);
13513 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
13514 return mDrawableState;
13515 }
13516 }
13517
13518 /**
13519 * Generate the new {@link android.graphics.drawable.Drawable} state for
13520 * this view. This is called by the view
13521 * system when the cached Drawable state is determined to be invalid. To
13522 * retrieve the current state, you should use {@link #getDrawableState}.
13523 *
13524 * @param extraSpace if non-zero, this is the number of extra entries you
13525 * would like in the returned array in which you can place your own
13526 * states.
13527 *
13528 * @return Returns an array holding the current {@link Drawable} state of
13529 * the view.
13530 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013531 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013532 */
13533 protected int[] onCreateDrawableState(int extraSpace) {
13534 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
13535 mParent instanceof View) {
13536 return ((View) mParent).onCreateDrawableState(extraSpace);
13537 }
13538
13539 int[] drawableState;
13540
13541 int privateFlags = mPrivateFlags;
13542
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013543 int viewStateIndex = 0;
13544 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
13545 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
13546 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070013547 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013548 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
13549 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070013550 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
13551 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013552 // This is set if HW acceleration is requested, even if the current
13553 // process doesn't allow it. This is just to allow app preview
13554 // windows to better match their app.
13555 viewStateIndex |= VIEW_STATE_ACCELERATED;
13556 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070013557 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013558
Christopher Tate3d4bf172011-03-28 16:16:46 -070013559 final int privateFlags2 = mPrivateFlags2;
13560 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
13561 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
13562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013563 drawableState = VIEW_STATE_SETS[viewStateIndex];
13564
13565 //noinspection ConstantIfStatement
13566 if (false) {
13567 Log.i("View", "drawableStateIndex=" + viewStateIndex);
13568 Log.i("View", toString()
13569 + " pressed=" + ((privateFlags & PRESSED) != 0)
13570 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
13571 + " fo=" + hasFocus()
13572 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070013573 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013574 + ": " + Arrays.toString(drawableState));
13575 }
13576
13577 if (extraSpace == 0) {
13578 return drawableState;
13579 }
13580
13581 final int[] fullState;
13582 if (drawableState != null) {
13583 fullState = new int[drawableState.length + extraSpace];
13584 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
13585 } else {
13586 fullState = new int[extraSpace];
13587 }
13588
13589 return fullState;
13590 }
13591
13592 /**
13593 * Merge your own state values in <var>additionalState</var> into the base
13594 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013595 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013596 *
13597 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070013598 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013599 * own additional state values.
13600 *
13601 * @param additionalState The additional state values you would like
13602 * added to <var>baseState</var>; this array is not modified.
13603 *
13604 * @return As a convenience, the <var>baseState</var> array you originally
13605 * passed into the function is returned.
13606 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013607 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013608 */
13609 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
13610 final int N = baseState.length;
13611 int i = N - 1;
13612 while (i >= 0 && baseState[i] == 0) {
13613 i--;
13614 }
13615 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
13616 return baseState;
13617 }
13618
13619 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070013620 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
13621 * on all Drawable objects associated with this view.
13622 */
13623 public void jumpDrawablesToCurrentState() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013624 if (mBackground != null) {
13625 mBackground.jumpToCurrentState();
Dianne Hackborn079e2352010-10-18 17:02:43 -070013626 }
13627 }
13628
13629 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630 * Sets the background color for this view.
13631 * @param color the color of the background
13632 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013633 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013634 public void setBackgroundColor(int color) {
Philip Milne6c8ea062012-04-03 17:38:43 -070013635 if (mBackground instanceof ColorDrawable) {
13636 ((ColorDrawable) mBackground).setColor(color);
Chet Haase70d4ba12010-10-06 09:46:45 -070013637 } else {
Philip Milne6c8ea062012-04-03 17:38:43 -070013638 setBackground(new ColorDrawable(color));
Chet Haase70d4ba12010-10-06 09:46:45 -070013639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013640 }
13641
13642 /**
13643 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070013644 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013645 * @param resid The identifier of the resource.
Philip Milne6c8ea062012-04-03 17:38:43 -070013646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013647 * @attr ref android.R.styleable#View_background
13648 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000013649 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013650 public void setBackgroundResource(int resid) {
13651 if (resid != 0 && resid == mBackgroundResource) {
13652 return;
13653 }
13654
13655 Drawable d= null;
13656 if (resid != 0) {
13657 d = mResources.getDrawable(resid);
13658 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013659 setBackground(d);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013660
13661 mBackgroundResource = resid;
13662 }
13663
13664 /**
13665 * Set the background to a given Drawable, or remove the background. If the
13666 * background has padding, this View's padding is set to the background's
13667 * padding. However, when a background is removed, this View's padding isn't
13668 * touched. If setting the padding is desired, please use
13669 * {@link #setPadding(int, int, int, int)}.
13670 *
Philip Milne6c8ea062012-04-03 17:38:43 -070013671 * @param background The Drawable to use as the background, or null to remove the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 * background
13673 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013674 public void setBackground(Drawable background) {
Romain Guyeb378892012-04-12 11:33:14 -070013675 //noinspection deprecation
Philip Milne6c8ea062012-04-03 17:38:43 -070013676 setBackgroundDrawable(background);
13677 }
13678
13679 /**
13680 * @deprecated use {@link #setBackground(Drawable)} instead
13681 */
13682 @Deprecated
13683 public void setBackgroundDrawable(Drawable background) {
13684 if (background == mBackground) {
Adam Powell4d36ec12011-07-17 16:44:16 -070013685 return;
13686 }
13687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013688 boolean requestLayout = false;
13689
13690 mBackgroundResource = 0;
13691
13692 /*
13693 * Regardless of whether we're setting a new background or not, we want
13694 * to clear the previous drawable.
13695 */
Philip Milne6c8ea062012-04-03 17:38:43 -070013696 if (mBackground != null) {
13697 mBackground.setCallback(null);
13698 unscheduleDrawable(mBackground);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013699 }
13700
Philip Milne6c8ea062012-04-03 17:38:43 -070013701 if (background != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013702 Rect padding = sThreadLocal.get();
13703 if (padding == null) {
13704 padding = new Rect();
13705 sThreadLocal.set(padding);
13706 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013707 if (background.getPadding(padding)) {
13708 switch (background.getResolvedLayoutDirectionSelf()) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013709 case LAYOUT_DIRECTION_RTL:
13710 setPadding(padding.right, padding.top, padding.left, padding.bottom);
13711 break;
13712 case LAYOUT_DIRECTION_LTR:
13713 default:
13714 setPadding(padding.left, padding.top, padding.right, padding.bottom);
13715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013716 }
13717
13718 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
13719 // if it has a different minimum size, we should layout again
Philip Milne6c8ea062012-04-03 17:38:43 -070013720 if (mBackground == null || mBackground.getMinimumHeight() != background.getMinimumHeight() ||
13721 mBackground.getMinimumWidth() != background.getMinimumWidth()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013722 requestLayout = true;
13723 }
13724
Philip Milne6c8ea062012-04-03 17:38:43 -070013725 background.setCallback(this);
13726 if (background.isStateful()) {
13727 background.setState(getDrawableState());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013728 }
Philip Milne6c8ea062012-04-03 17:38:43 -070013729 background.setVisible(getVisibility() == VISIBLE, false);
13730 mBackground = background;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731
13732 if ((mPrivateFlags & SKIP_DRAW) != 0) {
13733 mPrivateFlags &= ~SKIP_DRAW;
13734 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
13735 requestLayout = true;
13736 }
13737 } else {
13738 /* Remove the background */
Philip Milne6c8ea062012-04-03 17:38:43 -070013739 mBackground = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740
13741 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
13742 /*
13743 * This view ONLY drew the background before and we're removing
13744 * the background, so now it won't draw anything
13745 * (hence we SKIP_DRAW)
13746 */
13747 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
13748 mPrivateFlags |= SKIP_DRAW;
13749 }
13750
13751 /*
13752 * When the background is set, we try to apply its padding to this
13753 * View. When the background is removed, we don't touch this View's
13754 * padding. This is noted in the Javadocs. Hence, we don't need to
13755 * requestLayout(), the invalidate() below is sufficient.
13756 */
13757
13758 // The old background's minimum size could have affected this
13759 // View's layout, so let's requestLayout
13760 requestLayout = true;
13761 }
13762
Romain Guy8f1344f52009-05-15 16:03:59 -070013763 computeOpaqueFlags();
13764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 if (requestLayout) {
13766 requestLayout();
13767 }
13768
13769 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080013770 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013771 }
13772
13773 /**
13774 * Gets the background drawable
Philip Milne6c8ea062012-04-03 17:38:43 -070013775 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013776 * @return The drawable used as the background for this view, if any.
Philip Milne6c8ea062012-04-03 17:38:43 -070013777 *
13778 * @see #setBackground(Drawable)
13779 *
13780 * @attr ref android.R.styleable#View_background
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013781 */
13782 public Drawable getBackground() {
Philip Milne6c8ea062012-04-03 17:38:43 -070013783 return mBackground;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013784 }
13785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013786 /**
13787 * Sets the padding. The view may add on the space required to display
13788 * the scrollbars, depending on the style and visibility of the scrollbars.
13789 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
13790 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
13791 * from the values set in this call.
13792 *
13793 * @attr ref android.R.styleable#View_padding
13794 * @attr ref android.R.styleable#View_paddingBottom
13795 * @attr ref android.R.styleable#View_paddingLeft
13796 * @attr ref android.R.styleable#View_paddingRight
13797 * @attr ref android.R.styleable#View_paddingTop
13798 * @param left the left padding in pixels
13799 * @param top the top padding in pixels
13800 * @param right the right padding in pixels
13801 * @param bottom the bottom padding in pixels
13802 */
13803 public void setPadding(int left, int top, int right, int bottom) {
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013804 mUserPaddingStart = -1;
13805 mUserPaddingEnd = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013806 mUserPaddingRelative = false;
13807
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013808 internalSetPadding(left, top, right, bottom);
13809 }
13810
13811 private void internalSetPadding(int left, int top, int right, int bottom) {
Adam Powell20232d02010-12-08 21:08:53 -080013812 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013813 mUserPaddingRight = right;
13814 mUserPaddingBottom = bottom;
13815
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013816 final int viewFlags = mViewFlags;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013817 boolean changed = false;
Romain Guy8506ab42009-06-11 17:35:47 -070013818
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013819 // Common case is there are no scroll bars.
13820 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013821 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080013822 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013823 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080013824 switch (mVerticalScrollbarPosition) {
13825 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013826 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
13827 left += offset;
13828 } else {
13829 right += offset;
13830 }
13831 break;
Adam Powell20232d02010-12-08 21:08:53 -080013832 case SCROLLBAR_POSITION_RIGHT:
13833 right += offset;
13834 break;
13835 case SCROLLBAR_POSITION_LEFT:
13836 left += offset;
13837 break;
13838 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013839 }
Adam Powell20232d02010-12-08 21:08:53 -080013840 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013841 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
13842 ? 0 : getHorizontalScrollbarHeight();
13843 }
13844 }
Romain Guy8506ab42009-06-11 17:35:47 -070013845
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013846 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013847 changed = true;
13848 mPaddingLeft = left;
13849 }
13850 if (mPaddingTop != top) {
13851 changed = true;
13852 mPaddingTop = top;
13853 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013854 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013855 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013856 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013858 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013859 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070013860 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013861 }
13862
13863 if (changed) {
13864 requestLayout();
13865 }
13866 }
13867
13868 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013869 * Sets the relative padding. The view may add on the space required to display
13870 * the scrollbars, depending on the style and visibility of the scrollbars.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013871 * from the values set in this call.
13872 *
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013873 * @param start the start padding in pixels
13874 * @param top the top padding in pixels
13875 * @param end the end padding in pixels
13876 * @param bottom the bottom padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013877 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013878 */
13879 public void setPaddingRelative(int start, int top, int end, int bottom) {
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013880 mUserPaddingStart = start;
13881 mUserPaddingEnd = end;
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013882 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070013883
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013884 switch(getResolvedLayoutDirection()) {
13885 case LAYOUT_DIRECTION_RTL:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013886 internalSetPadding(end, top, start, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013887 break;
13888 case LAYOUT_DIRECTION_LTR:
13889 default:
Fabrice Di Meglio509708d2012-03-06 15:41:11 -080013890 internalSetPadding(start, top, end, bottom);
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013891 }
13892 }
13893
13894 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013895 * Returns the top padding of this view.
13896 *
13897 * @return the top padding in pixels
13898 */
13899 public int getPaddingTop() {
13900 return mPaddingTop;
13901 }
13902
13903 /**
13904 * Returns the bottom padding of this view. If there are inset and enabled
13905 * scrollbars, this value may include the space required to display the
13906 * scrollbars as well.
13907 *
13908 * @return the bottom padding in pixels
13909 */
13910 public int getPaddingBottom() {
13911 return mPaddingBottom;
13912 }
13913
13914 /**
13915 * Returns the left padding of this view. If there are inset and enabled
13916 * scrollbars, this value may include the space required to display the
13917 * scrollbars as well.
13918 *
13919 * @return the left padding in pixels
13920 */
13921 public int getPaddingLeft() {
13922 return mPaddingLeft;
13923 }
13924
13925 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013926 * Returns the start padding of this view depending on its resolved layout direction.
13927 * If there are inset and enabled scrollbars, this value may include the space
13928 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013929 *
13930 * @return the start padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013931 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013932 */
13933 public int getPaddingStart() {
13934 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13935 mPaddingRight : mPaddingLeft;
13936 }
13937
13938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013939 * Returns the right padding of this view. If there are inset and enabled
13940 * scrollbars, this value may include the space required to display the
13941 * scrollbars as well.
13942 *
13943 * @return the right padding in pixels
13944 */
13945 public int getPaddingRight() {
13946 return mPaddingRight;
13947 }
13948
13949 /**
Fabrice Di Meglio30a21e12012-03-12 13:12:19 -070013950 * Returns the end padding of this view depending on its resolved layout direction.
13951 * If there are inset and enabled scrollbars, this value may include the space
13952 * required to display the scrollbars as well.
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013953 *
13954 * @return the end padding in pixels
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013955 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013956 */
13957 public int getPaddingEnd() {
13958 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
13959 mPaddingLeft : mPaddingRight;
13960 }
13961
13962 /**
13963 * Return if the padding as been set thru relative values
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013964 * {@link #setPaddingRelative(int, int, int, int)}
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013965 *
13966 * @return true if the padding is relative or false if it is not.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070013967 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070013968 */
13969 public boolean isPaddingRelative() {
13970 return mUserPaddingRelative;
13971 }
13972
13973 /**
Philip Milne1557fd72012-04-04 23:41:34 -070013974 * @hide
13975 */
Philip Milne7a23b492012-04-24 22:12:36 -070013976 public Insets getOpticalInsets() {
Philip Milne1557fd72012-04-04 23:41:34 -070013977 if (mLayoutInsets == null) {
Philip Milnebbd51f12012-04-18 15:09:05 -070013978 mLayoutInsets = (mBackground == null) ? Insets.NONE : mBackground.getLayoutInsets();
Philip Milne1557fd72012-04-04 23:41:34 -070013979 }
13980 return mLayoutInsets;
13981 }
13982
13983 /**
13984 * @hide
13985 */
13986 public void setLayoutInsets(Insets layoutInsets) {
13987 mLayoutInsets = layoutInsets;
13988 }
13989
13990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013991 * Changes the selection state of this view. A view can be selected or not.
13992 * Note that selection is not the same as focus. Views are typically
13993 * selected in the context of an AdapterView like ListView or GridView;
13994 * the selected view is the view that is highlighted.
13995 *
13996 * @param selected true if the view must be selected, false otherwise
13997 */
13998 public void setSelected(boolean selected) {
13999 if (((mPrivateFlags & SELECTED) != 0) != selected) {
14000 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070014001 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080014002 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014003 refreshDrawableState();
14004 dispatchSetSelected(selected);
Svetoslav Ganov42138042012-03-20 11:51:39 -070014005 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
14006 notifyAccessibilityStateChanged();
14007 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014008 }
14009 }
14010
14011 /**
14012 * Dispatch setSelected to all of this View's children.
14013 *
14014 * @see #setSelected(boolean)
14015 *
14016 * @param selected The new selected state
14017 */
14018 protected void dispatchSetSelected(boolean selected) {
14019 }
14020
14021 /**
14022 * Indicates the selection state of this view.
14023 *
14024 * @return true if the view is selected, false otherwise
14025 */
14026 @ViewDebug.ExportedProperty
14027 public boolean isSelected() {
14028 return (mPrivateFlags & SELECTED) != 0;
14029 }
14030
14031 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014032 * Changes the activated state of this view. A view can be activated or not.
14033 * Note that activation is not the same as selection. Selection is
14034 * a transient property, representing the view (hierarchy) the user is
14035 * currently interacting with. Activation is a longer-term state that the
14036 * user can move views in and out of. For example, in a list view with
14037 * single or multiple selection enabled, the views in the current selection
14038 * set are activated. (Um, yeah, we are deeply sorry about the terminology
14039 * here.) The activated state is propagated down to children of the view it
14040 * is set on.
14041 *
14042 * @param activated true if the view must be activated, false otherwise
14043 */
14044 public void setActivated(boolean activated) {
14045 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
14046 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014047 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014048 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070014049 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070014050 }
14051 }
14052
14053 /**
14054 * Dispatch setActivated to all of this View's children.
14055 *
14056 * @see #setActivated(boolean)
14057 *
14058 * @param activated The new activated state
14059 */
14060 protected void dispatchSetActivated(boolean activated) {
14061 }
14062
14063 /**
14064 * Indicates the activation state of this view.
14065 *
14066 * @return true if the view is activated, false otherwise
14067 */
14068 @ViewDebug.ExportedProperty
14069 public boolean isActivated() {
14070 return (mPrivateFlags & ACTIVATED) != 0;
14071 }
14072
14073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014074 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
14075 * observer can be used to get notifications when global events, like
14076 * layout, happen.
14077 *
14078 * The returned ViewTreeObserver observer is not guaranteed to remain
14079 * valid for the lifetime of this View. If the caller of this method keeps
14080 * a long-lived reference to ViewTreeObserver, it should always check for
14081 * the return value of {@link ViewTreeObserver#isAlive()}.
14082 *
14083 * @return The ViewTreeObserver for this view's hierarchy.
14084 */
14085 public ViewTreeObserver getViewTreeObserver() {
14086 if (mAttachInfo != null) {
14087 return mAttachInfo.mTreeObserver;
14088 }
14089 if (mFloatingTreeObserver == null) {
14090 mFloatingTreeObserver = new ViewTreeObserver();
14091 }
14092 return mFloatingTreeObserver;
14093 }
14094
14095 /**
14096 * <p>Finds the topmost view in the current view hierarchy.</p>
14097 *
14098 * @return the topmost view containing this view
14099 */
14100 public View getRootView() {
14101 if (mAttachInfo != null) {
14102 final View v = mAttachInfo.mRootView;
14103 if (v != null) {
14104 return v;
14105 }
14106 }
Romain Guy8506ab42009-06-11 17:35:47 -070014107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014108 View parent = this;
14109
14110 while (parent.mParent != null && parent.mParent instanceof View) {
14111 parent = (View) parent.mParent;
14112 }
14113
14114 return parent;
14115 }
14116
14117 /**
14118 * <p>Computes the coordinates of this view on the screen. The argument
14119 * must be an array of two integers. After the method returns, the array
14120 * contains the x and y location in that order.</p>
14121 *
14122 * @param location an array of two integers in which to hold the coordinates
14123 */
14124 public void getLocationOnScreen(int[] location) {
14125 getLocationInWindow(location);
14126
14127 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070014128 if (info != null) {
14129 location[0] += info.mWindowLeft;
14130 location[1] += info.mWindowTop;
14131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014132 }
14133
14134 /**
14135 * <p>Computes the coordinates of this view in its window. The argument
14136 * must be an array of two integers. After the method returns, the array
14137 * contains the x and y location in that order.</p>
14138 *
14139 * @param location an array of two integers in which to hold the coordinates
14140 */
14141 public void getLocationInWindow(int[] location) {
14142 if (location == null || location.length < 2) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014143 throw new IllegalArgumentException("location must be an array of two integers");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014144 }
14145
Gilles Debunne6583ce52011-12-06 18:09:02 -080014146 if (mAttachInfo == null) {
14147 // When the view is not attached to a window, this method does not make sense
14148 location[0] = location[1] = 0;
14149 return;
14150 }
14151
Gilles Debunnecea45132011-11-24 02:19:27 +010014152 float[] position = mAttachInfo.mTmpTransformLocation;
14153 position[0] = position[1] = 0.0f;
14154
14155 if (!hasIdentityMatrix()) {
14156 getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014158
Gilles Debunnecea45132011-11-24 02:19:27 +010014159 position[0] += mLeft;
14160 position[1] += mTop;
14161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014162 ViewParent viewParent = mParent;
14163 while (viewParent instanceof View) {
Gilles Debunnecea45132011-11-24 02:19:27 +010014164 final View view = (View) viewParent;
14165
14166 position[0] -= view.mScrollX;
14167 position[1] -= view.mScrollY;
14168
14169 if (!view.hasIdentityMatrix()) {
14170 view.getMatrix().mapPoints(position);
Dianne Hackbornddb715b2011-09-09 14:43:39 -070014171 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014172
14173 position[0] += view.mLeft;
14174 position[1] += view.mTop;
14175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014176 viewParent = view.mParent;
Svetoslav Ganov42138042012-03-20 11:51:39 -070014177 }
Romain Guy8506ab42009-06-11 17:35:47 -070014178
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014179 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014180 // *cough*
Gilles Debunnecea45132011-11-24 02:19:27 +010014181 final ViewRootImpl vr = (ViewRootImpl) viewParent;
14182 position[1] -= vr.mCurScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014183 }
Gilles Debunnecea45132011-11-24 02:19:27 +010014184
14185 location[0] = (int) (position[0] + 0.5f);
14186 location[1] = (int) (position[1] + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014187 }
14188
14189 /**
14190 * {@hide}
14191 * @param id the id of the view to be found
14192 * @return the view of the specified id, null if cannot be found
14193 */
14194 protected View findViewTraversal(int id) {
14195 if (id == mID) {
14196 return this;
14197 }
14198 return null;
14199 }
14200
14201 /**
14202 * {@hide}
14203 * @param tag the tag of the view to be found
14204 * @return the view of specified tag, null if cannot be found
14205 */
14206 protected View findViewWithTagTraversal(Object tag) {
14207 if (tag != null && tag.equals(mTag)) {
14208 return this;
14209 }
14210 return null;
14211 }
14212
14213 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014214 * {@hide}
14215 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070014216 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080014217 * @return The first view that matches the predicate or null.
14218 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070014219 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080014220 if (predicate.apply(this)) {
14221 return this;
14222 }
14223 return null;
14224 }
14225
14226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014227 * Look for a child view with the given id. If this view has the given
14228 * id, return this view.
14229 *
14230 * @param id The id to search for.
14231 * @return The view that has the given id in the hierarchy or null
14232 */
14233 public final View findViewById(int id) {
14234 if (id < 0) {
14235 return null;
14236 }
14237 return findViewTraversal(id);
14238 }
14239
14240 /**
Svetoslav Ganov2cdedff2011-10-03 14:18:42 -070014241 * Finds a view by its unuque and stable accessibility id.
14242 *
14243 * @param accessibilityId The searched accessibility id.
14244 * @return The found view.
14245 */
14246 final View findViewByAccessibilityId(int accessibilityId) {
14247 if (accessibilityId < 0) {
14248 return null;
14249 }
14250 return findViewByAccessibilityIdTraversal(accessibilityId);
14251 }
14252
14253 /**
14254 * Performs the traversal to find a view by its unuque and stable accessibility id.
14255 *
14256 * <strong>Note:</strong>This method does not stop at the root namespace
14257 * boundary since the user can touch the screen at an arbitrary location
14258 * potentially crossing the root namespace bounday which will send an
14259 * accessibility event to accessibility services and they should be able
14260 * to obtain the event source. Also accessibility ids are guaranteed to be
14261 * unique in the window.
14262 *
14263 * @param accessibilityId The accessibility id.
14264 * @return The found view.
14265 */
14266 View findViewByAccessibilityIdTraversal(int accessibilityId) {
14267 if (getAccessibilityViewId() == accessibilityId) {
14268 return this;
14269 }
14270 return null;
14271 }
14272
14273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 * Look for a child view with the given tag. If this view has the given
14275 * tag, return this view.
14276 *
14277 * @param tag The tag to search for, using "tag.equals(getTag())".
14278 * @return The View that has the given tag in the hierarchy or null
14279 */
14280 public final View findViewWithTag(Object tag) {
14281 if (tag == null) {
14282 return null;
14283 }
14284 return findViewWithTagTraversal(tag);
14285 }
14286
14287 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080014288 * {@hide}
14289 * Look for a child view that matches the specified predicate.
14290 * If this view matches the predicate, return this view.
14291 *
14292 * @param predicate The predicate to evaluate.
14293 * @return The first view that matches the predicate or null.
14294 */
14295 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070014296 return findViewByPredicateTraversal(predicate, null);
14297 }
14298
14299 /**
14300 * {@hide}
14301 * Look for a child view that matches the specified predicate,
14302 * starting with the specified view and its descendents and then
14303 * recusively searching the ancestors and siblings of that view
14304 * until this view is reached.
14305 *
14306 * This method is useful in cases where the predicate does not match
14307 * a single unique view (perhaps multiple views use the same id)
14308 * and we are trying to find the view that is "closest" in scope to the
14309 * starting view.
14310 *
14311 * @param start The view to start from.
14312 * @param predicate The predicate to evaluate.
14313 * @return The first view that matches the predicate or null.
14314 */
14315 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
14316 View childToSkip = null;
14317 for (;;) {
14318 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
14319 if (view != null || start == this) {
14320 return view;
14321 }
14322
14323 ViewParent parent = start.getParent();
14324 if (parent == null || !(parent instanceof View)) {
14325 return null;
14326 }
14327
14328 childToSkip = start;
14329 start = (View) parent;
14330 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080014331 }
14332
14333 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014334 * Sets the identifier for this view. The identifier does not have to be
14335 * unique in this view's hierarchy. The identifier should be a positive
14336 * number.
14337 *
14338 * @see #NO_ID
Philip Milne6c8ea062012-04-03 17:38:43 -070014339 * @see #getId()
14340 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014341 *
14342 * @param id a number used to identify the view
14343 *
14344 * @attr ref android.R.styleable#View_id
14345 */
14346 public void setId(int id) {
14347 mID = id;
14348 }
14349
14350 /**
14351 * {@hide}
14352 *
14353 * @param isRoot true if the view belongs to the root namespace, false
14354 * otherwise
14355 */
14356 public void setIsRootNamespace(boolean isRoot) {
14357 if (isRoot) {
14358 mPrivateFlags |= IS_ROOT_NAMESPACE;
14359 } else {
14360 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
14361 }
14362 }
14363
14364 /**
14365 * {@hide}
14366 *
14367 * @return true if the view belongs to the root namespace, false otherwise
14368 */
14369 public boolean isRootNamespace() {
14370 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
14371 }
14372
14373 /**
14374 * Returns this view's identifier.
14375 *
14376 * @return a positive integer used to identify the view or {@link #NO_ID}
14377 * if the view has no ID
14378 *
Philip Milne6c8ea062012-04-03 17:38:43 -070014379 * @see #setId(int)
14380 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014381 * @attr ref android.R.styleable#View_id
14382 */
14383 @ViewDebug.CapturedViewProperty
14384 public int getId() {
14385 return mID;
14386 }
14387
14388 /**
14389 * Returns this view's tag.
14390 *
14391 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070014392 *
14393 * @see #setTag(Object)
14394 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014395 */
14396 @ViewDebug.ExportedProperty
14397 public Object getTag() {
14398 return mTag;
14399 }
14400
14401 /**
14402 * Sets the tag associated with this view. A tag can be used to mark
14403 * a view in its hierarchy and does not have to be unique within the
14404 * hierarchy. Tags can also be used to store data within a view without
14405 * resorting to another data structure.
14406 *
14407 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070014408 *
14409 * @see #getTag()
14410 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014411 */
14412 public void setTag(final Object tag) {
14413 mTag = tag;
14414 }
14415
14416 /**
Romain Guyd90a3312009-05-06 14:54:28 -070014417 * Returns the tag associated with this view and the specified key.
14418 *
14419 * @param key The key identifying the tag
14420 *
14421 * @return the Object stored in this view as a tag
14422 *
14423 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070014424 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070014425 */
14426 public Object getTag(int key) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014427 if (mKeyedTags != null) return mKeyedTags.get(key);
Romain Guyd90a3312009-05-06 14:54:28 -070014428 return null;
14429 }
14430
14431 /**
14432 * Sets a tag associated with this view and a key. A tag can be used
14433 * to mark a view in its hierarchy and does not have to be unique within
14434 * the hierarchy. Tags can also be used to store data within a view
14435 * without resorting to another data structure.
14436 *
14437 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070014438 * application to ensure it is unique (see the <a
14439 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
14440 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070014441 * the Android framework or not associated with any package will cause
14442 * an {@link IllegalArgumentException} to be thrown.
14443 *
14444 * @param key The key identifying the tag
14445 * @param tag An Object to tag the view with
14446 *
14447 * @throws IllegalArgumentException If they specified key is not valid
14448 *
14449 * @see #setTag(Object)
14450 * @see #getTag(int)
14451 */
14452 public void setTag(int key, final Object tag) {
14453 // If the package id is 0x00 or 0x01, it's either an undefined package
14454 // or a framework id
14455 if ((key >>> 24) < 2) {
14456 throw new IllegalArgumentException("The key must be an application-specific "
14457 + "resource id.");
14458 }
14459
Adam Powell2b2f6d62011-09-23 11:15:39 -070014460 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014461 }
14462
14463 /**
14464 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
14465 * framework id.
14466 *
14467 * @hide
14468 */
14469 public void setTagInternal(int key, Object tag) {
14470 if ((key >>> 24) != 0x1) {
14471 throw new IllegalArgumentException("The key must be a framework-specific "
14472 + "resource id.");
14473 }
14474
Adam Powell2b2f6d62011-09-23 11:15:39 -070014475 setKeyedTag(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014476 }
14477
Adam Powell2b2f6d62011-09-23 11:15:39 -070014478 private void setKeyedTag(int key, Object tag) {
Adam Powell7db82ac2011-09-22 19:44:04 -070014479 if (mKeyedTags == null) {
14480 mKeyedTags = new SparseArray<Object>();
Romain Guyd90a3312009-05-06 14:54:28 -070014481 }
14482
Adam Powell7db82ac2011-09-22 19:44:04 -070014483 mKeyedTags.put(key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070014484 }
14485
14486 /**
Romain Guy13922e02009-05-12 17:56:14 -070014487 * @param consistency The type of consistency. See ViewDebug for more information.
14488 *
14489 * @hide
14490 */
14491 protected boolean dispatchConsistencyCheck(int consistency) {
14492 return onConsistencyCheck(consistency);
14493 }
14494
14495 /**
14496 * Method that subclasses should implement to check their consistency. The type of
14497 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070014498 *
Romain Guy13922e02009-05-12 17:56:14 -070014499 * @param consistency The type of consistency. See ViewDebug for more information.
14500 *
14501 * @throws IllegalStateException if the view is in an inconsistent state.
14502 *
14503 * @hide
14504 */
14505 protected boolean onConsistencyCheck(int consistency) {
14506 boolean result = true;
14507
14508 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
14509 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
14510
14511 if (checkLayout) {
14512 if (getParent() == null) {
14513 result = false;
14514 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14515 "View " + this + " does not have a parent.");
14516 }
14517
14518 if (mAttachInfo == null) {
14519 result = false;
14520 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14521 "View " + this + " is not attached to a window.");
14522 }
14523 }
14524
14525 if (checkDrawing) {
14526 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
14527 // from their draw() method
14528
14529 if ((mPrivateFlags & DRAWN) != DRAWN &&
14530 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
14531 result = false;
14532 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
14533 "View " + this + " was invalidated but its drawing cache is valid.");
14534 }
14535 }
14536
14537 return result;
14538 }
14539
14540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014541 * Prints information about this view in the log output, with the tag
14542 * {@link #VIEW_LOG_TAG}.
14543 *
14544 * @hide
14545 */
14546 public void debug() {
14547 debug(0);
14548 }
14549
14550 /**
14551 * Prints information about this view in the log output, with the tag
14552 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
14553 * indentation defined by the <code>depth</code>.
14554 *
14555 * @param depth the indentation level
14556 *
14557 * @hide
14558 */
14559 protected void debug(int depth) {
14560 String output = debugIndent(depth - 1);
14561
14562 output += "+ " + this;
14563 int id = getId();
14564 if (id != -1) {
14565 output += " (id=" + id + ")";
14566 }
14567 Object tag = getTag();
14568 if (tag != null) {
14569 output += " (tag=" + tag + ")";
14570 }
14571 Log.d(VIEW_LOG_TAG, output);
14572
14573 if ((mPrivateFlags & FOCUSED) != 0) {
14574 output = debugIndent(depth) + " FOCUSED";
14575 Log.d(VIEW_LOG_TAG, output);
14576 }
14577
14578 output = debugIndent(depth);
14579 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
14580 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
14581 + "} ";
14582 Log.d(VIEW_LOG_TAG, output);
14583
14584 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
14585 || mPaddingBottom != 0) {
14586 output = debugIndent(depth);
14587 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
14588 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
14589 Log.d(VIEW_LOG_TAG, output);
14590 }
14591
14592 output = debugIndent(depth);
14593 output += "mMeasureWidth=" + mMeasuredWidth +
14594 " mMeasureHeight=" + mMeasuredHeight;
14595 Log.d(VIEW_LOG_TAG, output);
14596
14597 output = debugIndent(depth);
14598 if (mLayoutParams == null) {
14599 output += "BAD! no layout params";
14600 } else {
14601 output = mLayoutParams.debug(output);
14602 }
14603 Log.d(VIEW_LOG_TAG, output);
14604
14605 output = debugIndent(depth);
14606 output += "flags={";
14607 output += View.printFlags(mViewFlags);
14608 output += "}";
14609 Log.d(VIEW_LOG_TAG, output);
14610
14611 output = debugIndent(depth);
14612 output += "privateFlags={";
14613 output += View.printPrivateFlags(mPrivateFlags);
14614 output += "}";
14615 Log.d(VIEW_LOG_TAG, output);
14616 }
14617
14618 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +090014619 * Creates a string of whitespaces used for indentation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014620 *
14621 * @param depth the indentation level
14622 * @return a String containing (depth * 2 + 3) * 2 white spaces
14623 *
14624 * @hide
14625 */
14626 protected static String debugIndent(int depth) {
14627 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
14628 for (int i = 0; i < (depth * 2) + 3; i++) {
14629 spaces.append(' ').append(' ');
14630 }
14631 return spaces.toString();
14632 }
14633
14634 /**
14635 * <p>Return the offset of the widget's text baseline from the widget's top
14636 * boundary. If this widget does not support baseline alignment, this
14637 * method returns -1. </p>
14638 *
14639 * @return the offset of the baseline within the widget's bounds or -1
14640 * if baseline alignment is not supported
14641 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070014642 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014643 public int getBaseline() {
14644 return -1;
14645 }
14646
14647 /**
14648 * Call this when something has changed which has invalidated the
14649 * layout of this view. This will schedule a layout pass of the view
14650 * tree.
14651 */
14652 public void requestLayout() {
14653 if (ViewDebug.TRACE_HIERARCHY) {
14654 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
14655 }
14656
14657 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014658 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014659
Fabrice Di Meglio4a5268852012-03-30 15:56:48 -070014660 if (mLayoutParams != null) {
14661 mLayoutParams.onResolveLayoutDirection(getResolvedLayoutDirection());
14662 }
14663
14664 if (mParent != null && !mParent.isLayoutRequested()) {
14665 mParent.requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014666 }
14667 }
14668
14669 /**
14670 * Forces this view to be laid out during the next layout pass.
14671 * This method does not call requestLayout() or forceLayout()
14672 * on the parent.
14673 */
14674 public void forceLayout() {
14675 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080014676 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014677 }
14678
14679 /**
14680 * <p>
14681 * This is called to find out how big a view should be. The parent
14682 * supplies constraint information in the width and height parameters.
14683 * </p>
14684 *
14685 * <p>
Romain Guy967e2bf2012-02-07 17:04:34 -080014686 * The actual measurement work of a view is performed in
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014687 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
Romain Guy967e2bf2012-02-07 17:04:34 -080014688 * {@link #onMeasure(int, int)} can and must be overridden by subclasses.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014689 * </p>
14690 *
14691 *
14692 * @param widthMeasureSpec Horizontal space requirements as imposed by the
14693 * parent
14694 * @param heightMeasureSpec Vertical space requirements as imposed by the
14695 * parent
14696 *
14697 * @see #onMeasure(int, int)
14698 */
14699 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
14700 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
14701 widthMeasureSpec != mOldWidthMeasureSpec ||
14702 heightMeasureSpec != mOldHeightMeasureSpec) {
14703
14704 // first clears the measured dimension flag
14705 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
14706
14707 if (ViewDebug.TRACE_HIERARCHY) {
14708 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
14709 }
14710
14711 // measure ourselves, this should set the measured dimension flag back
14712 onMeasure(widthMeasureSpec, heightMeasureSpec);
14713
14714 // flag not set, setMeasuredDimension() was not invoked, we raise
14715 // an exception to warn the developer
14716 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
14717 throw new IllegalStateException("onMeasure() did not set the"
14718 + " measured dimension by calling"
14719 + " setMeasuredDimension()");
14720 }
14721
14722 mPrivateFlags |= LAYOUT_REQUIRED;
14723 }
14724
14725 mOldWidthMeasureSpec = widthMeasureSpec;
14726 mOldHeightMeasureSpec = heightMeasureSpec;
14727 }
14728
14729 /**
14730 * <p>
14731 * Measure the view and its content to determine the measured width and the
14732 * measured height. This method is invoked by {@link #measure(int, int)} and
14733 * should be overriden by subclasses to provide accurate and efficient
14734 * measurement of their contents.
14735 * </p>
14736 *
14737 * <p>
14738 * <strong>CONTRACT:</strong> When overriding this method, you
14739 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
14740 * measured width and height of this view. Failure to do so will trigger an
14741 * <code>IllegalStateException</code>, thrown by
14742 * {@link #measure(int, int)}. Calling the superclass'
14743 * {@link #onMeasure(int, int)} is a valid use.
14744 * </p>
14745 *
14746 * <p>
14747 * The base class implementation of measure defaults to the background size,
14748 * unless a larger size is allowed by the MeasureSpec. Subclasses should
14749 * override {@link #onMeasure(int, int)} to provide better measurements of
14750 * their content.
14751 * </p>
14752 *
14753 * <p>
14754 * If this method is overridden, it is the subclass's responsibility to make
14755 * sure the measured height and width are at least the view's minimum height
14756 * and width ({@link #getSuggestedMinimumHeight()} and
14757 * {@link #getSuggestedMinimumWidth()}).
14758 * </p>
14759 *
14760 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
14761 * The requirements are encoded with
14762 * {@link android.view.View.MeasureSpec}.
14763 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
14764 * The requirements are encoded with
14765 * {@link android.view.View.MeasureSpec}.
14766 *
14767 * @see #getMeasuredWidth()
14768 * @see #getMeasuredHeight()
14769 * @see #setMeasuredDimension(int, int)
14770 * @see #getSuggestedMinimumHeight()
14771 * @see #getSuggestedMinimumWidth()
14772 * @see android.view.View.MeasureSpec#getMode(int)
14773 * @see android.view.View.MeasureSpec#getSize(int)
14774 */
14775 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
14776 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
14777 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
14778 }
14779
14780 /**
14781 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
14782 * measured width and measured height. Failing to do so will trigger an
14783 * exception at measurement time.</p>
14784 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080014785 * @param measuredWidth The measured width of this view. May be a complex
14786 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14787 * {@link #MEASURED_STATE_TOO_SMALL}.
14788 * @param measuredHeight The measured height of this view. May be a complex
14789 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
14790 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014791 */
14792 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
14793 mMeasuredWidth = measuredWidth;
14794 mMeasuredHeight = measuredHeight;
14795
14796 mPrivateFlags |= MEASURED_DIMENSION_SET;
14797 }
14798
14799 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080014800 * Merge two states as returned by {@link #getMeasuredState()}.
14801 * @param curState The current state as returned from a view or the result
14802 * of combining multiple views.
14803 * @param newState The new view state to combine.
14804 * @return Returns a new integer reflecting the combination of the two
14805 * states.
14806 */
14807 public static int combineMeasuredStates(int curState, int newState) {
14808 return curState | newState;
14809 }
14810
14811 /**
14812 * Version of {@link #resolveSizeAndState(int, int, int)}
14813 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
14814 */
14815 public static int resolveSize(int size, int measureSpec) {
14816 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
14817 }
14818
14819 /**
14820 * Utility to reconcile a desired size and state, with constraints imposed
14821 * by a MeasureSpec. Will take the desired size, unless a different size
14822 * is imposed by the constraints. The returned value is a compound integer,
14823 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
14824 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
14825 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014826 *
14827 * @param size How big the view wants to be
14828 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080014829 * @return Size information bit mask as defined by
14830 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014831 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080014832 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014833 int result = size;
14834 int specMode = MeasureSpec.getMode(measureSpec);
14835 int specSize = MeasureSpec.getSize(measureSpec);
14836 switch (specMode) {
14837 case MeasureSpec.UNSPECIFIED:
14838 result = size;
14839 break;
14840 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080014841 if (specSize < size) {
14842 result = specSize | MEASURED_STATE_TOO_SMALL;
14843 } else {
14844 result = size;
14845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014846 break;
14847 case MeasureSpec.EXACTLY:
14848 result = specSize;
14849 break;
14850 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080014851 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014852 }
14853
14854 /**
14855 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070014856 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014857 * by the MeasureSpec.
14858 *
14859 * @param size Default size for this view
14860 * @param measureSpec Constraints imposed by the parent
14861 * @return The size this view should be.
14862 */
14863 public static int getDefaultSize(int size, int measureSpec) {
14864 int result = size;
14865 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070014866 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014867
14868 switch (specMode) {
14869 case MeasureSpec.UNSPECIFIED:
14870 result = size;
14871 break;
14872 case MeasureSpec.AT_MOST:
14873 case MeasureSpec.EXACTLY:
14874 result = specSize;
14875 break;
14876 }
14877 return result;
14878 }
14879
14880 /**
14881 * Returns the suggested minimum height that the view should use. This
14882 * returns the maximum of the view's minimum height
14883 * and the background's minimum height
14884 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
14885 * <p>
14886 * When being used in {@link #onMeasure(int, int)}, the caller should still
14887 * ensure the returned height is within the requirements of the parent.
14888 *
14889 * @return The suggested minimum height of the view.
14890 */
14891 protected int getSuggestedMinimumHeight() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014892 return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014894 }
14895
14896 /**
14897 * Returns the suggested minimum width that the view should use. This
14898 * returns the maximum of the view's minimum width)
14899 * and the background's minimum width
14900 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
14901 * <p>
14902 * When being used in {@link #onMeasure(int, int)}, the caller should still
14903 * ensure the returned width is within the requirements of the parent.
14904 *
14905 * @return The suggested minimum width of the view.
14906 */
14907 protected int getSuggestedMinimumWidth() {
Philip Milne6c8ea062012-04-03 17:38:43 -070014908 return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
14909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014910
Philip Milne6c8ea062012-04-03 17:38:43 -070014911 /**
14912 * Returns the minimum height of the view.
14913 *
14914 * @return the minimum height the view will try to be.
14915 *
14916 * @see #setMinimumHeight(int)
14917 *
14918 * @attr ref android.R.styleable#View_minHeight
14919 */
14920 public int getMinimumHeight() {
14921 return mMinHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014922 }
14923
14924 /**
14925 * Sets the minimum height of the view. It is not guaranteed the view will
14926 * be able to achieve this minimum height (for example, if its parent layout
14927 * constrains it with less available height).
14928 *
14929 * @param minHeight The minimum height the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014930 *
14931 * @see #getMinimumHeight()
14932 *
14933 * @attr ref android.R.styleable#View_minHeight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014934 */
14935 public void setMinimumHeight(int minHeight) {
14936 mMinHeight = minHeight;
Philip Milne6c8ea062012-04-03 17:38:43 -070014937 requestLayout();
14938 }
14939
14940 /**
14941 * Returns the minimum width of the view.
14942 *
14943 * @return the minimum width the view will try to be.
14944 *
14945 * @see #setMinimumWidth(int)
14946 *
14947 * @attr ref android.R.styleable#View_minWidth
14948 */
14949 public int getMinimumWidth() {
14950 return mMinWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014951 }
14952
14953 /**
14954 * Sets the minimum width of the view. It is not guaranteed the view will
14955 * be able to achieve this minimum width (for example, if its parent layout
14956 * constrains it with less available width).
14957 *
14958 * @param minWidth The minimum width the view will try to be.
Philip Milne6c8ea062012-04-03 17:38:43 -070014959 *
14960 * @see #getMinimumWidth()
14961 *
14962 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014963 */
14964 public void setMinimumWidth(int minWidth) {
14965 mMinWidth = minWidth;
Philip Milne6c8ea062012-04-03 17:38:43 -070014966 requestLayout();
14967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014968 }
14969
14970 /**
14971 * Get the animation currently associated with this view.
14972 *
14973 * @return The animation that is currently playing or
14974 * scheduled to play for this view.
14975 */
14976 public Animation getAnimation() {
14977 return mCurrentAnimation;
14978 }
14979
14980 /**
14981 * Start the specified animation now.
14982 *
14983 * @param animation the animation to start now
14984 */
14985 public void startAnimation(Animation animation) {
14986 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
14987 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080014988 invalidateParentCaches();
14989 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014990 }
14991
14992 /**
14993 * Cancels any animations for this view.
14994 */
14995 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080014996 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080014997 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080014998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014999 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080015000 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015001 }
15002
15003 /**
15004 * Sets the next animation to play for this view.
15005 * If you want the animation to play immediately, use
15006 * startAnimation. This method provides allows fine-grained
15007 * control over the start time and invalidation, but you
15008 * must make sure that 1) the animation has a start time set, and
15009 * 2) the view will be invalidated when the animation is supposed to
15010 * start.
15011 *
15012 * @param animation The next animation, or null.
15013 */
15014 public void setAnimation(Animation animation) {
15015 mCurrentAnimation = animation;
Romain Guyeb378892012-04-12 11:33:14 -070015016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015017 if (animation != null) {
Romain Guyeb378892012-04-12 11:33:14 -070015018 // If the screen is off assume the animation start time is now instead of
15019 // the next frame we draw. Keeping the START_ON_FIRST_FRAME start time
15020 // would cause the animation to start when the screen turns back on
15021 if (mAttachInfo != null && !mAttachInfo.mScreenOn &&
15022 animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {
15023 animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());
15024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015025 animation.reset();
15026 }
15027 }
15028
15029 /**
15030 * Invoked by a parent ViewGroup to notify the start of the animation
15031 * currently associated with this view. If you override this method,
15032 * always call super.onAnimationStart();
15033 *
15034 * @see #setAnimation(android.view.animation.Animation)
15035 * @see #getAnimation()
15036 */
15037 protected void onAnimationStart() {
15038 mPrivateFlags |= ANIMATION_STARTED;
15039 }
15040
15041 /**
15042 * Invoked by a parent ViewGroup to notify the end of the animation
15043 * currently associated with this view. If you override this method,
15044 * always call super.onAnimationEnd();
15045 *
15046 * @see #setAnimation(android.view.animation.Animation)
15047 * @see #getAnimation()
15048 */
15049 protected void onAnimationEnd() {
15050 mPrivateFlags &= ~ANIMATION_STARTED;
15051 }
15052
15053 /**
15054 * Invoked if there is a Transform that involves alpha. Subclass that can
15055 * draw themselves with the specified alpha should return true, and then
15056 * respect that alpha when their onDraw() is called. If this returns false
15057 * then the view may be redirected to draw into an offscreen buffer to
15058 * fulfill the request, which will look fine, but may be slower than if the
15059 * subclass handles it internally. The default implementation returns false.
15060 *
15061 * @param alpha The alpha (0..255) to apply to the view's drawing
15062 * @return true if the view can draw with the specified alpha.
15063 */
15064 protected boolean onSetAlpha(int alpha) {
15065 return false;
15066 }
15067
15068 /**
15069 * This is used by the RootView to perform an optimization when
15070 * the view hierarchy contains one or several SurfaceView.
15071 * SurfaceView is always considered transparent, but its children are not,
15072 * therefore all View objects remove themselves from the global transparent
15073 * region (passed as a parameter to this function).
15074 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070015075 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015076 *
15077 * @return Returns true if the effective visibility of the view at this
15078 * point is opaque, regardless of the transparent region; returns false
15079 * if it is possible for underlying windows to be seen behind the view.
15080 *
15081 * {@hide}
15082 */
15083 public boolean gatherTransparentRegion(Region region) {
15084 final AttachInfo attachInfo = mAttachInfo;
15085 if (region != null && attachInfo != null) {
15086 final int pflags = mPrivateFlags;
15087 if ((pflags & SKIP_DRAW) == 0) {
15088 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
15089 // remove it from the transparent region.
15090 final int[] location = attachInfo.mTransparentLocation;
15091 getLocationInWindow(location);
15092 region.op(location[0], location[1], location[0] + mRight - mLeft,
15093 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
Philip Milne6c8ea062012-04-03 17:38:43 -070015094 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBackground != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015095 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
15096 // exists, so we remove the background drawable's non-transparent
15097 // parts from this transparent region.
Philip Milne6c8ea062012-04-03 17:38:43 -070015098 applyDrawableToTransparentRegion(mBackground, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015099 }
15100 }
15101 return true;
15102 }
15103
15104 /**
15105 * Play a sound effect for this view.
15106 *
15107 * <p>The framework will play sound effects for some built in actions, such as
15108 * clicking, but you may wish to play these effects in your widget,
15109 * for instance, for internal navigation.
15110 *
15111 * <p>The sound effect will only be played if sound effects are enabled by the user, and
15112 * {@link #isSoundEffectsEnabled()} is true.
15113 *
15114 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
15115 */
15116 public void playSoundEffect(int soundConstant) {
15117 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
15118 return;
15119 }
15120 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
15121 }
15122
15123 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015124 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015125 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015126 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015127 *
15128 * <p>The framework will provide haptic feedback for some built in actions,
15129 * such as long presses, but you may wish to provide feedback for your
15130 * own widget.
15131 *
15132 * <p>The feedback will only be performed if
15133 * {@link #isHapticFeedbackEnabled()} is true.
15134 *
15135 * @param feedbackConstant One of the constants defined in
15136 * {@link HapticFeedbackConstants}
15137 */
15138 public boolean performHapticFeedback(int feedbackConstant) {
15139 return performHapticFeedback(feedbackConstant, 0);
15140 }
15141
15142 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015143 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070015144 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070015145 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015146 *
15147 * @param feedbackConstant One of the constants defined in
15148 * {@link HapticFeedbackConstants}
15149 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
15150 */
15151 public boolean performHapticFeedback(int feedbackConstant, int flags) {
15152 if (mAttachInfo == null) {
15153 return false;
15154 }
Romain Guyf607bdc2010-09-10 19:20:06 -070015155 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070015156 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015157 && !isHapticFeedbackEnabled()) {
15158 return false;
15159 }
Romain Guy812ccbe2010-06-01 14:07:24 -070015160 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
15161 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015162 }
15163
15164 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015165 * Request that the visibility of the status bar or other screen/window
15166 * decorations be changed.
15167 *
15168 * <p>This method is used to put the over device UI into temporary modes
15169 * where the user's attention is focused more on the application content,
15170 * by dimming or hiding surrounding system affordances. This is typically
15171 * used in conjunction with {@link Window#FEATURE_ACTION_BAR_OVERLAY
15172 * Window.FEATURE_ACTION_BAR_OVERLAY}, allowing the applications content
15173 * to be placed behind the action bar (and with these flags other system
15174 * affordances) so that smooth transitions between hiding and showing them
15175 * can be done.
15176 *
15177 * <p>Two representative examples of the use of system UI visibility is
15178 * implementing a content browsing application (like a magazine reader)
15179 * and a video playing application.
15180 *
15181 * <p>The first code shows a typical implementation of a View in a content
15182 * browsing application. In this implementation, the application goes
15183 * into a content-oriented mode by hiding the status bar and action bar,
15184 * and putting the navigation elements into lights out mode. The user can
15185 * then interact with content while in this mode. Such an application should
15186 * provide an easy way for the user to toggle out of the mode (such as to
15187 * check information in the status bar or access notifications). In the
15188 * implementation here, this is done simply by tapping on the content.
15189 *
15190 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
15191 * content}
15192 *
15193 * <p>This second code sample shows a typical implementation of a View
15194 * in a video playing application. In this situation, while the video is
15195 * playing the application would like to go into a complete full-screen mode,
15196 * to use as much of the display as possible for the video. When in this state
15197 * the user can not interact with the application; the system intercepts
15198 * touching on the screen to pop the UI out of full screen mode.
15199 *
15200 * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
15201 * content}
15202 *
15203 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15204 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15205 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15206 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015207 */
15208 public void setSystemUiVisibility(int visibility) {
Daniel Sandler70524062011-09-21 00:30:47 -040015209 if (visibility != mSystemUiVisibility) {
15210 mSystemUiVisibility = visibility;
15211 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15212 mParent.recomputeViewAttributes(this);
15213 }
Joe Onorato664644d2011-01-23 17:53:23 -080015214 }
15215 }
15216
15217 /**
Dianne Hackborn98014352012-04-05 18:31:41 -070015218 * Returns the last {@link #setSystemUiVisibility(int) that this view has requested.
15219 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE},
15220 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}, {@link #SYSTEM_UI_FLAG_FULLSCREEN},
15221 * {@link #SYSTEM_UI_FLAG_LAYOUT_STABLE}, {@link #SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION},
15222 * and {@link #SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080015223 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080015224 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080015225 return mSystemUiVisibility;
15226 }
15227
Scott Mainec6331b2011-05-24 16:55:56 -070015228 /**
Dianne Hackborn3a3a6cf2012-03-26 10:24:04 -070015229 * Returns the current system UI visibility that is currently set for
15230 * the entire window. This is the combination of the
15231 * {@link #setSystemUiVisibility(int)} values supplied by all of the
15232 * views in the window.
15233 */
15234 public int getWindowSystemUiVisibility() {
15235 return mAttachInfo != null ? mAttachInfo.mSystemUiVisibility : 0;
15236 }
15237
15238 /**
15239 * Override to find out when the window's requested system UI visibility
15240 * has changed, that is the value returned by {@link #getWindowSystemUiVisibility()}.
15241 * This is different from the callbacks recieved through
15242 * {@link #setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener)}
15243 * in that this is only telling you about the local request of the window,
15244 * not the actual values applied by the system.
15245 */
15246 public void onWindowSystemUiVisibilityChanged(int visible) {
15247 }
15248
15249 /**
15250 * Dispatch callbacks to {@link #onWindowSystemUiVisibilityChanged(int)} down
15251 * the view hierarchy.
15252 */
15253 public void dispatchWindowSystemUiVisiblityChanged(int visible) {
15254 onWindowSystemUiVisibilityChanged(visible);
15255 }
15256
15257 /**
Scott Mainec6331b2011-05-24 16:55:56 -070015258 * Set a listener to receive callbacks when the visibility of the system bar changes.
15259 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
15260 */
Joe Onorato664644d2011-01-23 17:53:23 -080015261 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015262 getListenerInfo().mOnSystemUiVisibilityChangeListener = l;
Joe Onorato664644d2011-01-23 17:53:23 -080015263 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
15264 mParent.recomputeViewAttributes(this);
15265 }
15266 }
15267
15268 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015269 * Dispatch callbacks to {@link #setOnSystemUiVisibilityChangeListener} down
15270 * the view hierarchy.
Joe Onorato664644d2011-01-23 17:53:23 -080015271 */
15272 public void dispatchSystemUiVisibilityChanged(int visibility) {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015273 ListenerInfo li = mListenerInfo;
15274 if (li != null && li.mOnSystemUiVisibilityChangeListener != null) {
15275 li.mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080015276 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080015277 }
15278 }
15279
Dianne Hackborn9a230e02011-10-06 11:51:27 -070015280 void updateLocalSystemUiVisibility(int localValue, int localChanges) {
15281 int val = (mSystemUiVisibility&~localChanges) | (localValue&localChanges);
15282 if (val != mSystemUiVisibility) {
15283 setSystemUiVisibility(val);
15284 }
15285 }
15286
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070015287 /** @hide */
15288 public void setDisabledSystemUiVisibility(int flags) {
15289 if (mAttachInfo != null) {
15290 if (mAttachInfo.mDisabledSystemUiVisibility != flags) {
15291 mAttachInfo.mDisabledSystemUiVisibility = flags;
15292 if (mParent != null) {
15293 mParent.recomputeViewAttributes(this);
15294 }
15295 }
15296 }
15297 }
15298
Joe Onorato664644d2011-01-23 17:53:23 -080015299 /**
Joe Malin32736f02011-01-19 16:14:20 -080015300 * Creates an image that the system displays during the drag and drop
15301 * operation. This is called a &quot;drag shadow&quot;. The default implementation
15302 * for a DragShadowBuilder based on a View returns an image that has exactly the same
15303 * appearance as the given View. The default also positions the center of the drag shadow
15304 * directly under the touch point. If no View is provided (the constructor with no parameters
15305 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
15306 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
15307 * default is an invisible drag shadow.
15308 * <p>
15309 * You are not required to use the View you provide to the constructor as the basis of the
15310 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
15311 * anything you want as the drag shadow.
15312 * </p>
15313 * <p>
15314 * You pass a DragShadowBuilder object to the system when you start the drag. The system
15315 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
15316 * size and position of the drag shadow. It uses this data to construct a
15317 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
15318 * so that your application can draw the shadow image in the Canvas.
15319 * </p>
Joe Fernandez558459f2011-10-13 16:47:36 -070015320 *
15321 * <div class="special reference">
15322 * <h3>Developer Guides</h3>
15323 * <p>For a guide to implementing drag and drop features, read the
15324 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
15325 * </div>
Christopher Tate2c095f32010-10-04 14:13:40 -070015326 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015327 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070015328 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070015329
15330 /**
Joe Malin32736f02011-01-19 16:14:20 -080015331 * Constructs a shadow image builder based on a View. By default, the resulting drag
15332 * shadow will have the same appearance and dimensions as the View, with the touch point
15333 * over the center of the View.
15334 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070015335 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015336 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070015337 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070015338 }
15339
Christopher Tate17ed60c2011-01-18 12:50:26 -080015340 /**
15341 * Construct a shadow builder object with no associated View. This
15342 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
15343 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
15344 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080015345 * reference to any View object. If they are not overridden, then the result is an
15346 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080015347 */
15348 public DragShadowBuilder() {
15349 mView = new WeakReference<View>(null);
15350 }
15351
15352 /**
15353 * Returns the View object that had been passed to the
15354 * {@link #View.DragShadowBuilder(View)}
15355 * constructor. If that View parameter was {@code null} or if the
15356 * {@link #View.DragShadowBuilder()}
15357 * constructor was used to instantiate the builder object, this method will return
15358 * null.
15359 *
15360 * @return The View object associate with this builder object.
15361 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070015362 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070015363 final public View getView() {
15364 return mView.get();
15365 }
15366
Christopher Tate2c095f32010-10-04 14:13:40 -070015367 /**
Joe Malin32736f02011-01-19 16:14:20 -080015368 * Provides the metrics for the shadow image. These include the dimensions of
15369 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070015370 * be centered under the touch location while dragging.
15371 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015372 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080015373 * same as the dimensions of the View itself and centers the shadow under
15374 * the touch point.
15375 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070015376 *
Joe Malin32736f02011-01-19 16:14:20 -080015377 * @param shadowSize A {@link android.graphics.Point} containing the width and height
15378 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
15379 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
15380 * image.
15381 *
15382 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
15383 * shadow image that should be underneath the touch point during the drag and drop
15384 * operation. Your application must set {@link android.graphics.Point#x} to the
15385 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070015386 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015387 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070015388 final View view = mView.get();
15389 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015390 shadowSize.set(view.getWidth(), view.getHeight());
15391 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070015392 } else {
15393 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
15394 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015395 }
15396
15397 /**
Joe Malin32736f02011-01-19 16:14:20 -080015398 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
15399 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015400 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070015401 *
Joe Malin32736f02011-01-19 16:14:20 -080015402 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070015403 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015404 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070015405 final View view = mView.get();
15406 if (view != null) {
15407 view.draw(canvas);
15408 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015409 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070015410 }
Christopher Tate2c095f32010-10-04 14:13:40 -070015411 }
15412 }
15413
15414 /**
Joe Malin32736f02011-01-19 16:14:20 -080015415 * Starts a drag and drop operation. When your application calls this method, it passes a
15416 * {@link android.view.View.DragShadowBuilder} object to the system. The
15417 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
15418 * to get metrics for the drag shadow, and then calls the object's
15419 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
15420 * <p>
15421 * Once the system has the drag shadow, it begins the drag and drop operation by sending
15422 * drag events to all the View objects in your application that are currently visible. It does
15423 * this either by calling the View object's drag listener (an implementation of
15424 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
15425 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
15426 * Both are passed a {@link android.view.DragEvent} object that has a
15427 * {@link android.view.DragEvent#getAction()} value of
15428 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
15429 * </p>
15430 * <p>
15431 * Your application can invoke startDrag() on any attached View object. The View object does not
15432 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
15433 * be related to the View the user selected for dragging.
15434 * </p>
15435 * @param data A {@link android.content.ClipData} object pointing to the data to be
15436 * transferred by the drag and drop operation.
15437 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
15438 * drag shadow.
15439 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
15440 * drop operation. This Object is put into every DragEvent object sent by the system during the
15441 * current drag.
15442 * <p>
15443 * myLocalState is a lightweight mechanism for the sending information from the dragged View
15444 * to the target Views. For example, it can contain flags that differentiate between a
15445 * a copy operation and a move operation.
15446 * </p>
15447 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
15448 * so the parameter should be set to 0.
15449 * @return {@code true} if the method completes successfully, or
15450 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
15451 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070015452 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015453 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015454 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070015455 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015456 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070015457 }
15458 boolean okay = false;
15459
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015460 Point shadowSize = new Point();
15461 Point shadowTouchPoint = new Point();
15462 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070015463
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015464 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
15465 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
15466 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070015467 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015468
Chris Tatea32dcf72010-10-14 12:13:50 -070015469 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015470 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
15471 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070015472 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015473 Surface surface = new Surface();
15474 try {
15475 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080015476 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070015477 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070015478 + " surface=" + surface);
15479 if (token != null) {
15480 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070015481 try {
Chris Tate6b391282010-10-14 15:48:59 -070015482 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015483 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070015484 } finally {
15485 surface.unlockCanvasAndPost(canvas);
15486 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015487
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070015488 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080015489
15490 // Cache the local state object for delivery with DragEvents
15491 root.setLocalDragState(myLocalState);
15492
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015493 // repurpose 'shadowSize' for the last touch point
15494 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070015495
Christopher Tatea53146c2010-09-07 11:57:52 -070015496 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080015497 shadowSize.x, shadowSize.y,
15498 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070015499 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tate8f73b5d2011-09-12 15:22:12 -070015500
15501 // Off and running! Release our local surface instance; the drag
15502 // shadow surface is now managed by the system process.
15503 surface.release();
Christopher Tatea53146c2010-09-07 11:57:52 -070015504 }
15505 } catch (Exception e) {
15506 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
15507 surface.destroy();
15508 }
15509
15510 return okay;
15511 }
15512
Christopher Tatea53146c2010-09-07 11:57:52 -070015513 /**
Joe Malin32736f02011-01-19 16:14:20 -080015514 * Handles drag events sent by the system following a call to
15515 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
15516 *<p>
15517 * When the system calls this method, it passes a
15518 * {@link android.view.DragEvent} object. A call to
15519 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
15520 * in DragEvent. The method uses these to determine what is happening in the drag and drop
15521 * operation.
15522 * @param event The {@link android.view.DragEvent} sent by the system.
15523 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
15524 * in DragEvent, indicating the type of drag event represented by this object.
15525 * @return {@code true} if the method was successful, otherwise {@code false}.
15526 * <p>
15527 * The method should return {@code true} in response to an action type of
15528 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
15529 * operation.
15530 * </p>
15531 * <p>
15532 * The method should also return {@code true} in response to an action type of
15533 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
15534 * {@code false} if it didn't.
15535 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015536 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070015537 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070015538 return false;
15539 }
15540
15541 /**
Joe Malin32736f02011-01-19 16:14:20 -080015542 * Detects if this View is enabled and has a drag event listener.
15543 * If both are true, then it calls the drag event listener with the
15544 * {@link android.view.DragEvent} it received. If the drag event listener returns
15545 * {@code true}, then dispatchDragEvent() returns {@code true}.
15546 * <p>
15547 * For all other cases, the method calls the
15548 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
15549 * method and returns its result.
15550 * </p>
15551 * <p>
15552 * This ensures that a drag event is always consumed, even if the View does not have a drag
15553 * event listener. However, if the View has a listener and the listener returns true, then
15554 * onDragEvent() is not called.
15555 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070015556 */
15557 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080015558 //noinspection SimplifiableIfStatement
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070015559 ListenerInfo li = mListenerInfo;
15560 if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
15561 && li.mOnDragListener.onDrag(this, event)) {
Chris Tate32affef2010-10-18 15:29:21 -070015562 return true;
15563 }
Christopher Tatea53146c2010-09-07 11:57:52 -070015564 return onDragEvent(event);
15565 }
15566
Christopher Tate3d4bf172011-03-28 16:16:46 -070015567 boolean canAcceptDrag() {
15568 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
15569 }
15570
Christopher Tatea53146c2010-09-07 11:57:52 -070015571 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070015572 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
15573 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070015574 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070015575 */
15576 public void onCloseSystemDialogs(String reason) {
15577 }
Joe Malin32736f02011-01-19 16:14:20 -080015578
Dianne Hackbornffa42482009-09-23 22:20:11 -070015579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015580 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070015581 * update a Region being computed for
15582 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015583 * that any non-transparent parts of the Drawable are removed from the
15584 * given transparent region.
15585 *
15586 * @param dr The Drawable whose transparency is to be applied to the region.
15587 * @param region A Region holding the current transparency information,
15588 * where any parts of the region that are set are considered to be
15589 * transparent. On return, this region will be modified to have the
15590 * transparency information reduced by the corresponding parts of the
15591 * Drawable that are not transparent.
15592 * {@hide}
15593 */
15594 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
15595 if (DBG) {
15596 Log.i("View", "Getting transparent region for: " + this);
15597 }
15598 final Region r = dr.getTransparentRegion();
15599 final Rect db = dr.getBounds();
15600 final AttachInfo attachInfo = mAttachInfo;
15601 if (r != null && attachInfo != null) {
15602 final int w = getRight()-getLeft();
15603 final int h = getBottom()-getTop();
15604 if (db.left > 0) {
15605 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
15606 r.op(0, 0, db.left, h, Region.Op.UNION);
15607 }
15608 if (db.right < w) {
15609 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
15610 r.op(db.right, 0, w, h, Region.Op.UNION);
15611 }
15612 if (db.top > 0) {
15613 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
15614 r.op(0, 0, w, db.top, Region.Op.UNION);
15615 }
15616 if (db.bottom < h) {
15617 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
15618 r.op(0, db.bottom, w, h, Region.Op.UNION);
15619 }
15620 final int[] location = attachInfo.mTransparentLocation;
15621 getLocationInWindow(location);
15622 r.translate(location[0], location[1]);
15623 region.op(r, Region.Op.INTERSECT);
15624 } else {
15625 region.op(db, Region.Op.DIFFERENCE);
15626 }
15627 }
15628
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015629 private void checkForLongClick(int delayOffset) {
15630 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
15631 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015632
Patrick Dubroye0a799a2011-05-04 16:19:22 -070015633 if (mPendingCheckForLongPress == null) {
15634 mPendingCheckForLongPress = new CheckForLongPress();
15635 }
15636 mPendingCheckForLongPress.rememberWindowAttachCount();
15637 postDelayed(mPendingCheckForLongPress,
15638 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015640 }
15641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015642 /**
15643 * Inflate a view from an XML resource. This convenience method wraps the {@link
15644 * LayoutInflater} class, which provides a full range of options for view inflation.
15645 *
15646 * @param context The Context object for your activity or application.
15647 * @param resource The resource ID to inflate
15648 * @param root A view group that will be the parent. Used to properly inflate the
15649 * layout_* parameters.
15650 * @see LayoutInflater
15651 */
15652 public static View inflate(Context context, int resource, ViewGroup root) {
15653 LayoutInflater factory = LayoutInflater.from(context);
15654 return factory.inflate(resource, root);
15655 }
Romain Guy33e72ae2010-07-17 12:40:29 -070015656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080015657 /**
Adam Powell637d3372010-08-25 14:37:03 -070015658 * Scroll the view with standard behavior for scrolling beyond the normal
15659 * content boundaries. Views that call this method should override
15660 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
15661 * results of an over-scroll operation.
15662 *
15663 * Views can use this method to handle any touch or fling-based scrolling.
15664 *
15665 * @param deltaX Change in X in pixels
15666 * @param deltaY Change in Y in pixels
15667 * @param scrollX Current X scroll value in pixels before applying deltaX
15668 * @param scrollY Current Y scroll value in pixels before applying deltaY
15669 * @param scrollRangeX Maximum content scroll range along the X axis
15670 * @param scrollRangeY Maximum content scroll range along the Y axis
15671 * @param maxOverScrollX Number of pixels to overscroll by in either direction
15672 * along the X axis.
15673 * @param maxOverScrollY Number of pixels to overscroll by in either direction
15674 * along the Y axis.
15675 * @param isTouchEvent true if this scroll operation is the result of a touch event.
15676 * @return true if scrolling was clamped to an over-scroll boundary along either
15677 * axis, false otherwise.
15678 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070015679 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070015680 protected boolean overScrollBy(int deltaX, int deltaY,
15681 int scrollX, int scrollY,
15682 int scrollRangeX, int scrollRangeY,
15683 int maxOverScrollX, int maxOverScrollY,
15684 boolean isTouchEvent) {
15685 final int overScrollMode = mOverScrollMode;
15686 final boolean canScrollHorizontal =
15687 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
15688 final boolean canScrollVertical =
15689 computeVerticalScrollRange() > computeVerticalScrollExtent();
15690 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
15691 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
15692 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
15693 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15694
15695 int newScrollX = scrollX + deltaX;
15696 if (!overScrollHorizontal) {
15697 maxOverScrollX = 0;
15698 }
15699
15700 int newScrollY = scrollY + deltaY;
15701 if (!overScrollVertical) {
15702 maxOverScrollY = 0;
15703 }
15704
15705 // Clamp values if at the limits and record
15706 final int left = -maxOverScrollX;
15707 final int right = maxOverScrollX + scrollRangeX;
15708 final int top = -maxOverScrollY;
15709 final int bottom = maxOverScrollY + scrollRangeY;
15710
15711 boolean clampedX = false;
15712 if (newScrollX > right) {
15713 newScrollX = right;
15714 clampedX = true;
15715 } else if (newScrollX < left) {
15716 newScrollX = left;
15717 clampedX = true;
15718 }
15719
15720 boolean clampedY = false;
15721 if (newScrollY > bottom) {
15722 newScrollY = bottom;
15723 clampedY = true;
15724 } else if (newScrollY < top) {
15725 newScrollY = top;
15726 clampedY = true;
15727 }
15728
15729 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
15730
15731 return clampedX || clampedY;
15732 }
15733
15734 /**
15735 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
15736 * respond to the results of an over-scroll operation.
15737 *
15738 * @param scrollX New X scroll value in pixels
15739 * @param scrollY New Y scroll value in pixels
15740 * @param clampedX True if scrollX was clamped to an over-scroll boundary
15741 * @param clampedY True if scrollY was clamped to an over-scroll boundary
15742 */
15743 protected void onOverScrolled(int scrollX, int scrollY,
15744 boolean clampedX, boolean clampedY) {
15745 // Intentionally empty.
15746 }
15747
15748 /**
15749 * Returns the over-scroll mode for this view. The result will be
15750 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15751 * (allow over-scrolling only if the view content is larger than the container),
15752 * or {@link #OVER_SCROLL_NEVER}.
15753 *
15754 * @return This view's over-scroll mode.
15755 */
15756 public int getOverScrollMode() {
15757 return mOverScrollMode;
15758 }
15759
15760 /**
15761 * Set the over-scroll mode for this view. Valid over-scroll modes are
15762 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
15763 * (allow over-scrolling only if the view content is larger than the container),
15764 * or {@link #OVER_SCROLL_NEVER}.
15765 *
15766 * Setting the over-scroll mode of a view will have an effect only if the
15767 * view is capable of scrolling.
15768 *
15769 * @param overScrollMode The new over-scroll mode for this view.
15770 */
15771 public void setOverScrollMode(int overScrollMode) {
15772 if (overScrollMode != OVER_SCROLL_ALWAYS &&
15773 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
15774 overScrollMode != OVER_SCROLL_NEVER) {
15775 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
15776 }
15777 mOverScrollMode = overScrollMode;
15778 }
15779
15780 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080015781 * Gets a scale factor that determines the distance the view should scroll
15782 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
15783 * @return The vertical scroll scale factor.
15784 * @hide
15785 */
15786 protected float getVerticalScrollFactor() {
15787 if (mVerticalScrollFactor == 0) {
15788 TypedValue outValue = new TypedValue();
15789 if (!mContext.getTheme().resolveAttribute(
15790 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
15791 throw new IllegalStateException(
15792 "Expected theme to define listPreferredItemHeight.");
15793 }
15794 mVerticalScrollFactor = outValue.getDimension(
15795 mContext.getResources().getDisplayMetrics());
15796 }
15797 return mVerticalScrollFactor;
15798 }
15799
15800 /**
15801 * Gets a scale factor that determines the distance the view should scroll
15802 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
15803 * @return The horizontal scroll scale factor.
15804 * @hide
15805 */
15806 protected float getHorizontalScrollFactor() {
15807 // TODO: Should use something else.
15808 return getVerticalScrollFactor();
15809 }
15810
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015811 /**
15812 * Return the value specifying the text direction or policy that was set with
15813 * {@link #setTextDirection(int)}.
15814 *
15815 * @return the defined text direction. It can be one of:
15816 *
15817 * {@link #TEXT_DIRECTION_INHERIT},
15818 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15819 * {@link #TEXT_DIRECTION_ANY_RTL},
15820 * {@link #TEXT_DIRECTION_LTR},
15821 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015822 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015823 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015824 */
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015825 @ViewDebug.ExportedProperty(category = "text", mapping = {
15826 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
15827 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
15828 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
15829 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
15830 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL"),
15831 @ViewDebug.IntToString(from = TEXT_DIRECTION_LOCALE, to = "LOCALE")
15832 })
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015833 public int getTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015834 return (mPrivateFlags2 & TEXT_DIRECTION_MASK) >> TEXT_DIRECTION_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015835 }
15836
15837 /**
15838 * Set the text direction.
15839 *
15840 * @param textDirection the direction to set. Should be one of:
15841 *
15842 * {@link #TEXT_DIRECTION_INHERIT},
15843 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15844 * {@link #TEXT_DIRECTION_ANY_RTL},
15845 * {@link #TEXT_DIRECTION_LTR},
15846 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015847 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015848 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015849 */
15850 public void setTextDirection(int textDirection) {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015851 if (getTextDirection() != textDirection) {
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015852 // Reset the current text direction and the resolved one
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015853 mPrivateFlags2 &= ~TEXT_DIRECTION_MASK;
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015854 resetResolvedTextDirection();
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015855 // Set the new text direction
15856 mPrivateFlags2 |= ((textDirection << TEXT_DIRECTION_MASK_SHIFT) & TEXT_DIRECTION_MASK);
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015857 // Refresh
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015858 requestLayout();
Fabrice Di Meglio827d5c02012-03-23 15:13:41 -070015859 invalidate(true);
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015860 }
15861 }
15862
15863 /**
15864 * Return the resolved text direction.
15865 *
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015866 * This needs resolution if the value is TEXT_DIRECTION_INHERIT. The resolution matches
15867 * {@link #getTextDirection()}if it is not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds
15868 * up the parent chain of the view. if there is no parent, then it will return the default
15869 * {@link #TEXT_DIRECTION_FIRST_STRONG}.
15870 *
15871 * @return the resolved text direction. Returns one of:
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015872 *
Doug Feltcb3791202011-07-07 11:57:48 -070015873 * {@link #TEXT_DIRECTION_FIRST_STRONG}
15874 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015875 * {@link #TEXT_DIRECTION_LTR},
15876 * {@link #TEXT_DIRECTION_RTL},
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015877 * {@link #TEXT_DIRECTION_LOCALE}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015878 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015879 */
15880 public int getResolvedTextDirection() {
Fabrice Di Meglio22ab7752012-03-23 16:39:26 -070015881 // The text direction will be resolved only if needed
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015882 if ((mPrivateFlags2 & TEXT_DIRECTION_RESOLVED) != TEXT_DIRECTION_RESOLVED) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015883 resolveTextDirection();
15884 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015885 return (mPrivateFlags2 & TEXT_DIRECTION_RESOLVED_MASK) >> TEXT_DIRECTION_RESOLVED_MASK_SHIFT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015886 }
15887
15888 /**
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015889 * Resolve the text direction. Will call {@link View#onResolvedTextDirectionChanged} when
15890 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015891 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015892 */
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015893 public void resolveTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015894 // Reset any previous text direction resolution
15895 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
15896
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015897 if (hasRtlSupport()) {
15898 // Set resolved text direction flag depending on text direction flag
15899 final int textDirection = getTextDirection();
15900 switch(textDirection) {
15901 case TEXT_DIRECTION_INHERIT:
15902 if (canResolveTextDirection()) {
15903 ViewGroup viewGroup = ((ViewGroup) mParent);
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015904
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015905 // Set current resolved direction to the same value as the parent's one
15906 final int parentResolvedDirection = viewGroup.getResolvedTextDirection();
15907 switch (parentResolvedDirection) {
15908 case TEXT_DIRECTION_FIRST_STRONG:
15909 case TEXT_DIRECTION_ANY_RTL:
15910 case TEXT_DIRECTION_LTR:
15911 case TEXT_DIRECTION_RTL:
15912 case TEXT_DIRECTION_LOCALE:
15913 mPrivateFlags2 |=
15914 (parentResolvedDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15915 break;
15916 default:
15917 // Default resolved direction is "first strong" heuristic
15918 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
15919 }
15920 } else {
15921 // We cannot do the resolution if there is no parent, so use the default one
15922 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015923 }
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015924 break;
15925 case TEXT_DIRECTION_FIRST_STRONG:
15926 case TEXT_DIRECTION_ANY_RTL:
15927 case TEXT_DIRECTION_LTR:
15928 case TEXT_DIRECTION_RTL:
15929 case TEXT_DIRECTION_LOCALE:
15930 // Resolved direction is the same as text direction
15931 mPrivateFlags2 |= (textDirection << TEXT_DIRECTION_RESOLVED_MASK_SHIFT);
15932 break;
15933 default:
15934 // Default resolved direction is "first strong" heuristic
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015935 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -070015936 }
15937 } else {
15938 // Default resolved direction is "first strong" heuristic
15939 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED_DEFAULT;
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015940 }
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015941
15942 // Set to resolved
15943 mPrivateFlags2 |= TEXT_DIRECTION_RESOLVED;
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015944 onResolvedTextDirectionChanged();
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015945 }
15946
15947 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015948 * Called when text direction has been resolved. Subclasses that care about text direction
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015949 * resolution should override this method.
15950 *
15951 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015952 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015953 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015954 public void onResolvedTextDirectionChanged() {
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015955 }
15956
15957 /**
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015958 * Check if text direction resolution can be done.
15959 *
15960 * @return true if text direction resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015961 * @hide
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015962 */
15963 public boolean canResolveTextDirection() {
15964 switch (getTextDirection()) {
15965 case TEXT_DIRECTION_INHERIT:
15966 return (mParent != null) && (mParent instanceof ViewGroup);
15967 default:
15968 return true;
15969 }
15970 }
15971
15972 /**
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015973 * Reset resolved text direction. Text direction can be resolved with a call to
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015974 * getResolvedTextDirection(). Will call {@link View#onResolvedTextDirectionReset} when
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015975 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015976 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015977 */
15978 public void resetResolvedTextDirection() {
Fabrice Di Megliob934db72012-03-20 14:33:01 -070015979 mPrivateFlags2 &= ~(TEXT_DIRECTION_RESOLVED | TEXT_DIRECTION_RESOLVED_MASK);
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015980 onResolvedTextDirectionReset();
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015981 }
15982
15983 /**
15984 * Called when text direction is reset. Subclasses that care about text direction reset should
15985 * override this method and do a reset of the text direction of their children. The default
15986 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070015987 * @hide
Fabrice Di Meglio6d3d5052012-02-15 20:22:56 -080015988 */
Fabrice Di Megliodd3ef2c2012-03-01 16:37:17 -080015989 public void onResolvedTextDirectionReset() {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070015990 }
15991
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070015992 /**
15993 * Return the value specifying the text alignment or policy that was set with
15994 * {@link #setTextAlignment(int)}.
15995 *
15996 * @return the defined text alignment. It can be one of:
15997 *
15998 * {@link #TEXT_ALIGNMENT_INHERIT},
15999 * {@link #TEXT_ALIGNMENT_GRAVITY},
16000 * {@link #TEXT_ALIGNMENT_CENTER},
16001 * {@link #TEXT_ALIGNMENT_TEXT_START},
16002 * {@link #TEXT_ALIGNMENT_TEXT_END},
16003 * {@link #TEXT_ALIGNMENT_VIEW_START},
16004 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016005 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016006 */
16007 @ViewDebug.ExportedProperty(category = "text", mapping = {
16008 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16009 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16010 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16011 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16012 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16013 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16014 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16015 })
16016 public int getTextAlignment() {
16017 return (mPrivateFlags2 & TEXT_ALIGNMENT_MASK) >> TEXT_ALIGNMENT_MASK_SHIFT;
16018 }
16019
16020 /**
16021 * Set the text alignment.
16022 *
16023 * @param textAlignment The text alignment to set. Should be one of
16024 *
16025 * {@link #TEXT_ALIGNMENT_INHERIT},
16026 * {@link #TEXT_ALIGNMENT_GRAVITY},
16027 * {@link #TEXT_ALIGNMENT_CENTER},
16028 * {@link #TEXT_ALIGNMENT_TEXT_START},
16029 * {@link #TEXT_ALIGNMENT_TEXT_END},
16030 * {@link #TEXT_ALIGNMENT_VIEW_START},
16031 * {@link #TEXT_ALIGNMENT_VIEW_END}
16032 *
16033 * @attr ref android.R.styleable#View_textAlignment
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016034 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016035 */
16036 public void setTextAlignment(int textAlignment) {
16037 if (textAlignment != getTextAlignment()) {
16038 // Reset the current and resolved text alignment
16039 mPrivateFlags2 &= ~TEXT_ALIGNMENT_MASK;
16040 resetResolvedTextAlignment();
16041 // Set the new text alignment
16042 mPrivateFlags2 |= ((textAlignment << TEXT_ALIGNMENT_MASK_SHIFT) & TEXT_ALIGNMENT_MASK);
16043 // Refresh
16044 requestLayout();
16045 invalidate(true);
16046 }
16047 }
16048
16049 /**
16050 * Return the resolved text alignment.
16051 *
16052 * The resolved text alignment. This needs resolution if the value is
16053 * TEXT_ALIGNMENT_INHERIT. The resolution matches {@link #setTextAlignment(int)} if it is
16054 * not TEXT_ALIGNMENT_INHERIT, otherwise resolution proceeds up the parent chain of the view.
16055 *
16056 * @return the resolved text alignment. Returns one of:
16057 *
16058 * {@link #TEXT_ALIGNMENT_GRAVITY},
16059 * {@link #TEXT_ALIGNMENT_CENTER},
16060 * {@link #TEXT_ALIGNMENT_TEXT_START},
16061 * {@link #TEXT_ALIGNMENT_TEXT_END},
16062 * {@link #TEXT_ALIGNMENT_VIEW_START},
16063 * {@link #TEXT_ALIGNMENT_VIEW_END}
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016064 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016065 */
16066 @ViewDebug.ExportedProperty(category = "text", mapping = {
16067 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_INHERIT, to = "INHERIT"),
16068 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_GRAVITY, to = "GRAVITY"),
16069 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_START, to = "TEXT_START"),
16070 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_TEXT_END, to = "TEXT_END"),
16071 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_CENTER, to = "CENTER"),
16072 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_START, to = "VIEW_START"),
16073 @ViewDebug.IntToString(from = TEXT_ALIGNMENT_VIEW_END, to = "VIEW_END")
16074 })
16075 public int getResolvedTextAlignment() {
16076 // If text alignment is not resolved, then resolve it
16077 if ((mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED) != TEXT_ALIGNMENT_RESOLVED) {
16078 resolveTextAlignment();
16079 }
16080 return (mPrivateFlags2 & TEXT_ALIGNMENT_RESOLVED_MASK) >> TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT;
16081 }
16082
16083 /**
16084 * Resolve the text alignment. Will call {@link View#onResolvedTextAlignmentChanged} when
16085 * resolution is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016086 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016087 */
16088 public void resolveTextAlignment() {
16089 // Reset any previous text alignment resolution
16090 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16091
16092 if (hasRtlSupport()) {
16093 // Set resolved text alignment flag depending on text alignment flag
16094 final int textAlignment = getTextAlignment();
16095 switch (textAlignment) {
16096 case TEXT_ALIGNMENT_INHERIT:
16097 // Check if we can resolve the text alignment
16098 if (canResolveLayoutDirection() && mParent instanceof View) {
16099 View view = (View) mParent;
16100
16101 final int parentResolvedTextAlignment = view.getResolvedTextAlignment();
16102 switch (parentResolvedTextAlignment) {
16103 case TEXT_ALIGNMENT_GRAVITY:
16104 case TEXT_ALIGNMENT_TEXT_START:
16105 case TEXT_ALIGNMENT_TEXT_END:
16106 case TEXT_ALIGNMENT_CENTER:
16107 case TEXT_ALIGNMENT_VIEW_START:
16108 case TEXT_ALIGNMENT_VIEW_END:
16109 // Resolved text alignment is the same as the parent resolved
16110 // text alignment
16111 mPrivateFlags2 |=
16112 (parentResolvedTextAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16113 break;
16114 default:
16115 // Use default resolved text alignment
16116 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16117 }
16118 }
16119 else {
16120 // We cannot do the resolution if there is no parent so use the default
16121 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16122 }
16123 break;
16124 case TEXT_ALIGNMENT_GRAVITY:
16125 case TEXT_ALIGNMENT_TEXT_START:
16126 case TEXT_ALIGNMENT_TEXT_END:
16127 case TEXT_ALIGNMENT_CENTER:
16128 case TEXT_ALIGNMENT_VIEW_START:
16129 case TEXT_ALIGNMENT_VIEW_END:
16130 // Resolved text alignment is the same as text alignment
16131 mPrivateFlags2 |= (textAlignment << TEXT_ALIGNMENT_RESOLVED_MASK_SHIFT);
16132 break;
16133 default:
16134 // Use default resolved text alignment
16135 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16136 }
16137 } else {
16138 // Use default resolved text alignment
16139 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED_DEFAULT;
16140 }
16141
16142 // Set the resolved
16143 mPrivateFlags2 |= TEXT_ALIGNMENT_RESOLVED;
16144 onResolvedTextAlignmentChanged();
16145 }
16146
16147 /**
16148 * Check if text alignment resolution can be done.
16149 *
16150 * @return true if text alignment resolution can be done otherwise return false.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016151 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016152 */
16153 public boolean canResolveTextAlignment() {
16154 switch (getTextAlignment()) {
16155 case TEXT_DIRECTION_INHERIT:
16156 return (mParent != null);
16157 default:
16158 return true;
16159 }
16160 }
16161
16162 /**
16163 * Called when text alignment has been resolved. Subclasses that care about text alignment
16164 * resolution should override this method.
16165 *
16166 * The default implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016167 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016168 */
16169 public void onResolvedTextAlignmentChanged() {
16170 }
16171
16172 /**
16173 * Reset resolved text alignment. Text alignment can be resolved with a call to
16174 * getResolvedTextAlignment(). Will call {@link View#onResolvedTextAlignmentReset} when
16175 * reset is done.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016176 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016177 */
16178 public void resetResolvedTextAlignment() {
16179 // Reset any previous text alignment resolution
16180 mPrivateFlags2 &= ~(TEXT_ALIGNMENT_RESOLVED | TEXT_ALIGNMENT_RESOLVED_MASK);
16181 onResolvedTextAlignmentReset();
16182 }
16183
16184 /**
16185 * Called when text alignment is reset. Subclasses that care about text alignment reset should
16186 * override this method and do a reset of the text alignment of their children. The default
16187 * implementation does nothing.
Fabrice Di Meglio66388dc2012-05-03 18:51:57 -070016188 * @hide
Fabrice Di Meglio9da0f8a2012-03-13 19:37:57 -070016189 */
16190 public void onResolvedTextAlignmentReset() {
16191 }
16192
Chet Haaseb39f0512011-05-24 14:36:40 -070016193 //
16194 // Properties
16195 //
16196 /**
16197 * A Property wrapper around the <code>alpha</code> functionality handled by the
16198 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
16199 */
Chet Haased47f1532011-12-16 11:18:52 -080016200 public static final Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016201 @Override
16202 public void setValue(View object, float value) {
16203 object.setAlpha(value);
16204 }
16205
16206 @Override
16207 public Float get(View object) {
16208 return object.getAlpha();
16209 }
16210 };
16211
16212 /**
16213 * A Property wrapper around the <code>translationX</code> functionality handled by the
16214 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
16215 */
Chet Haased47f1532011-12-16 11:18:52 -080016216 public static final Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016217 @Override
16218 public void setValue(View object, float value) {
16219 object.setTranslationX(value);
16220 }
16221
16222 @Override
16223 public Float get(View object) {
16224 return object.getTranslationX();
16225 }
16226 };
16227
16228 /**
16229 * A Property wrapper around the <code>translationY</code> functionality handled by the
16230 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
16231 */
Chet Haased47f1532011-12-16 11:18:52 -080016232 public static final Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016233 @Override
16234 public void setValue(View object, float value) {
16235 object.setTranslationY(value);
16236 }
16237
16238 @Override
16239 public Float get(View object) {
16240 return object.getTranslationY();
16241 }
16242 };
16243
16244 /**
16245 * A Property wrapper around the <code>x</code> functionality handled by the
16246 * {@link View#setX(float)} and {@link View#getX()} methods.
16247 */
Chet Haased47f1532011-12-16 11:18:52 -080016248 public static final Property<View, Float> X = new FloatProperty<View>("x") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016249 @Override
16250 public void setValue(View object, float value) {
16251 object.setX(value);
16252 }
16253
16254 @Override
16255 public Float get(View object) {
16256 return object.getX();
16257 }
16258 };
16259
16260 /**
16261 * A Property wrapper around the <code>y</code> functionality handled by the
16262 * {@link View#setY(float)} and {@link View#getY()} methods.
16263 */
Chet Haased47f1532011-12-16 11:18:52 -080016264 public static final Property<View, Float> Y = new FloatProperty<View>("y") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016265 @Override
16266 public void setValue(View object, float value) {
16267 object.setY(value);
16268 }
16269
16270 @Override
16271 public Float get(View object) {
16272 return object.getY();
16273 }
16274 };
16275
16276 /**
16277 * A Property wrapper around the <code>rotation</code> functionality handled by the
16278 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
16279 */
Chet Haased47f1532011-12-16 11:18:52 -080016280 public static final Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016281 @Override
16282 public void setValue(View object, float value) {
16283 object.setRotation(value);
16284 }
16285
16286 @Override
16287 public Float get(View object) {
16288 return object.getRotation();
16289 }
16290 };
16291
16292 /**
16293 * A Property wrapper around the <code>rotationX</code> functionality handled by the
16294 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
16295 */
Chet Haased47f1532011-12-16 11:18:52 -080016296 public static final Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016297 @Override
16298 public void setValue(View object, float value) {
16299 object.setRotationX(value);
16300 }
16301
16302 @Override
16303 public Float get(View object) {
16304 return object.getRotationX();
16305 }
16306 };
16307
16308 /**
16309 * A Property wrapper around the <code>rotationY</code> functionality handled by the
16310 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
16311 */
Chet Haased47f1532011-12-16 11:18:52 -080016312 public static final Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016313 @Override
16314 public void setValue(View object, float value) {
16315 object.setRotationY(value);
16316 }
16317
16318 @Override
16319 public Float get(View object) {
16320 return object.getRotationY();
16321 }
16322 };
16323
16324 /**
16325 * A Property wrapper around the <code>scaleX</code> functionality handled by the
16326 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
16327 */
Chet Haased47f1532011-12-16 11:18:52 -080016328 public static final Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016329 @Override
16330 public void setValue(View object, float value) {
16331 object.setScaleX(value);
16332 }
16333
16334 @Override
16335 public Float get(View object) {
16336 return object.getScaleX();
16337 }
16338 };
16339
16340 /**
16341 * A Property wrapper around the <code>scaleY</code> functionality handled by the
16342 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
16343 */
Chet Haased47f1532011-12-16 11:18:52 -080016344 public static final Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
Chet Haaseb39f0512011-05-24 14:36:40 -070016345 @Override
16346 public void setValue(View object, float value) {
16347 object.setScaleY(value);
16348 }
16349
16350 @Override
16351 public Float get(View object) {
16352 return object.getScaleY();
16353 }
16354 };
16355
Jeff Brown33bbfd22011-02-24 20:55:35 -080016356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016357 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
16358 * Each MeasureSpec represents a requirement for either the width or the height.
16359 * A MeasureSpec is comprised of a size and a mode. There are three possible
16360 * modes:
16361 * <dl>
16362 * <dt>UNSPECIFIED</dt>
16363 * <dd>
16364 * The parent has not imposed any constraint on the child. It can be whatever size
16365 * it wants.
16366 * </dd>
16367 *
16368 * <dt>EXACTLY</dt>
16369 * <dd>
16370 * The parent has determined an exact size for the child. The child is going to be
16371 * given those bounds regardless of how big it wants to be.
16372 * </dd>
16373 *
16374 * <dt>AT_MOST</dt>
16375 * <dd>
16376 * The child can be as large as it wants up to the specified size.
16377 * </dd>
16378 * </dl>
16379 *
16380 * MeasureSpecs are implemented as ints to reduce object allocation. This class
16381 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
16382 */
16383 public static class MeasureSpec {
16384 private static final int MODE_SHIFT = 30;
16385 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
16386
16387 /**
16388 * Measure specification mode: The parent has not imposed any constraint
16389 * on the child. It can be whatever size it wants.
16390 */
16391 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
16392
16393 /**
16394 * Measure specification mode: The parent has determined an exact size
16395 * for the child. The child is going to be given those bounds regardless
16396 * of how big it wants to be.
16397 */
16398 public static final int EXACTLY = 1 << MODE_SHIFT;
16399
16400 /**
16401 * Measure specification mode: The child can be as large as it wants up
16402 * to the specified size.
16403 */
16404 public static final int AT_MOST = 2 << MODE_SHIFT;
16405
16406 /**
16407 * Creates a measure specification based on the supplied size and mode.
16408 *
16409 * The mode must always be one of the following:
16410 * <ul>
16411 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
16412 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
16413 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
16414 * </ul>
16415 *
16416 * @param size the size of the measure specification
16417 * @param mode the mode of the measure specification
16418 * @return the measure specification based on size and mode
16419 */
16420 public static int makeMeasureSpec(int size, int mode) {
16421 return size + mode;
16422 }
16423
16424 /**
16425 * Extracts the mode from the supplied measure specification.
16426 *
16427 * @param measureSpec the measure specification to extract the mode from
16428 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
16429 * {@link android.view.View.MeasureSpec#AT_MOST} or
16430 * {@link android.view.View.MeasureSpec#EXACTLY}
16431 */
16432 public static int getMode(int measureSpec) {
16433 return (measureSpec & MODE_MASK);
16434 }
16435
16436 /**
16437 * Extracts the size from the supplied measure specification.
16438 *
16439 * @param measureSpec the measure specification to extract the size from
16440 * @return the size in pixels defined in the supplied measure specification
16441 */
16442 public static int getSize(int measureSpec) {
16443 return (measureSpec & ~MODE_MASK);
16444 }
16445
16446 /**
16447 * Returns a String representation of the specified measure
16448 * specification.
16449 *
16450 * @param measureSpec the measure specification to convert to a String
16451 * @return a String with the following format: "MeasureSpec: MODE SIZE"
16452 */
16453 public static String toString(int measureSpec) {
16454 int mode = getMode(measureSpec);
16455 int size = getSize(measureSpec);
16456
16457 StringBuilder sb = new StringBuilder("MeasureSpec: ");
16458
16459 if (mode == UNSPECIFIED)
16460 sb.append("UNSPECIFIED ");
16461 else if (mode == EXACTLY)
16462 sb.append("EXACTLY ");
16463 else if (mode == AT_MOST)
16464 sb.append("AT_MOST ");
16465 else
16466 sb.append(mode).append(" ");
16467
16468 sb.append(size);
16469 return sb.toString();
16470 }
16471 }
16472
16473 class CheckForLongPress implements Runnable {
16474
16475 private int mOriginalWindowAttachCount;
16476
16477 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070016478 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016479 && mOriginalWindowAttachCount == mWindowAttachCount) {
16480 if (performLongClick()) {
16481 mHasPerformedLongPress = true;
16482 }
16483 }
16484 }
16485
16486 public void rememberWindowAttachCount() {
16487 mOriginalWindowAttachCount = mWindowAttachCount;
16488 }
16489 }
Joe Malin32736f02011-01-19 16:14:20 -080016490
Adam Powelle14579b2009-12-16 18:39:52 -080016491 private final class CheckForTap implements Runnable {
16492 public void run() {
16493 mPrivateFlags &= ~PREPRESSED;
Adam Powell4d6f0662012-02-21 15:11:11 -080016494 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -070016495 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080016496 }
16497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016498
Adam Powella35d7682010-03-12 14:48:13 -080016499 private final class PerformClick implements Runnable {
16500 public void run() {
16501 performClick();
16502 }
16503 }
16504
Dianne Hackborn63042d62011-01-26 18:56:29 -080016505 /** @hide */
16506 public void hackTurnOffWindowResizeAnim(boolean off) {
16507 mAttachInfo.mTurnOffWindowResizeAnim = off;
16508 }
Joe Malin32736f02011-01-19 16:14:20 -080016509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016510 /**
Chet Haasea00f3862011-02-22 06:34:40 -080016511 * This method returns a ViewPropertyAnimator object, which can be used to animate
16512 * specific properties on this View.
16513 *
16514 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
16515 */
16516 public ViewPropertyAnimator animate() {
16517 if (mAnimator == null) {
16518 mAnimator = new ViewPropertyAnimator(this);
16519 }
16520 return mAnimator;
16521 }
16522
16523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016524 * Interface definition for a callback to be invoked when a key event is
16525 * dispatched to this view. The callback will be invoked before the key
16526 * event is given to the view.
16527 */
16528 public interface OnKeyListener {
16529 /**
16530 * Called when a key is dispatched to a view. This allows listeners to
16531 * get a chance to respond before the target view.
16532 *
16533 * @param v The view the key has been dispatched to.
16534 * @param keyCode The code for the physical key that was pressed
16535 * @param event The KeyEvent object containing full information about
16536 * the event.
16537 * @return True if the listener has consumed the event, false otherwise.
16538 */
16539 boolean onKey(View v, int keyCode, KeyEvent event);
16540 }
16541
16542 /**
16543 * Interface definition for a callback to be invoked when a touch event is
16544 * dispatched to this view. The callback will be invoked before the touch
16545 * event is given to the view.
16546 */
16547 public interface OnTouchListener {
16548 /**
16549 * Called when a touch event is dispatched to a view. This allows listeners to
16550 * get a chance to respond before the target view.
16551 *
16552 * @param v The view the touch event has been dispatched to.
16553 * @param event The MotionEvent object containing full information about
16554 * the event.
16555 * @return True if the listener has consumed the event, false otherwise.
16556 */
16557 boolean onTouch(View v, MotionEvent event);
16558 }
16559
16560 /**
Jeff Brown10b62902011-06-20 16:40:37 -070016561 * Interface definition for a callback to be invoked when a hover event is
16562 * dispatched to this view. The callback will be invoked before the hover
16563 * event is given to the view.
16564 */
16565 public interface OnHoverListener {
16566 /**
16567 * Called when a hover event is dispatched to a view. This allows listeners to
16568 * get a chance to respond before the target view.
16569 *
16570 * @param v The view the hover event has been dispatched to.
16571 * @param event The MotionEvent object containing full information about
16572 * the event.
16573 * @return True if the listener has consumed the event, false otherwise.
16574 */
16575 boolean onHover(View v, MotionEvent event);
16576 }
16577
16578 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080016579 * Interface definition for a callback to be invoked when a generic motion event is
16580 * dispatched to this view. The callback will be invoked before the generic motion
16581 * event is given to the view.
16582 */
16583 public interface OnGenericMotionListener {
16584 /**
16585 * Called when a generic motion event is dispatched to a view. This allows listeners to
16586 * get a chance to respond before the target view.
16587 *
16588 * @param v The view the generic motion event has been dispatched to.
16589 * @param event The MotionEvent object containing full information about
16590 * the event.
16591 * @return True if the listener has consumed the event, false otherwise.
16592 */
16593 boolean onGenericMotion(View v, MotionEvent event);
16594 }
16595
16596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016597 * Interface definition for a callback to be invoked when a view has been clicked and held.
16598 */
16599 public interface OnLongClickListener {
16600 /**
16601 * Called when a view has been clicked and held.
16602 *
16603 * @param v The view that was clicked and held.
16604 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080016605 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016606 */
16607 boolean onLongClick(View v);
16608 }
16609
16610 /**
Chris Tate32affef2010-10-18 15:29:21 -070016611 * Interface definition for a callback to be invoked when a drag is being dispatched
16612 * to this view. The callback will be invoked before the hosting view's own
16613 * onDrag(event) method. If the listener wants to fall back to the hosting view's
16614 * onDrag(event) behavior, it should return 'false' from this callback.
Joe Fernandez558459f2011-10-13 16:47:36 -070016615 *
16616 * <div class="special reference">
16617 * <h3>Developer Guides</h3>
16618 * <p>For a guide to implementing drag and drop features, read the
16619 * <a href="{@docRoot}guide/topics/ui/drag-drop.html">Drag and Drop</a> developer guide.</p>
16620 * </div>
Chris Tate32affef2010-10-18 15:29:21 -070016621 */
16622 public interface OnDragListener {
16623 /**
16624 * Called when a drag event is dispatched to a view. This allows listeners
16625 * to get a chance to override base View behavior.
16626 *
Joe Malin32736f02011-01-19 16:14:20 -080016627 * @param v The View that received the drag event.
16628 * @param event The {@link android.view.DragEvent} object for the drag event.
16629 * @return {@code true} if the drag event was handled successfully, or {@code false}
16630 * if the drag event was not handled. Note that {@code false} will trigger the View
16631 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070016632 */
16633 boolean onDrag(View v, DragEvent event);
16634 }
16635
16636 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016637 * Interface definition for a callback to be invoked when the focus state of
16638 * a view changed.
16639 */
16640 public interface OnFocusChangeListener {
16641 /**
16642 * Called when the focus state of a view has changed.
16643 *
16644 * @param v The view whose state has changed.
16645 * @param hasFocus The new focus state of v.
16646 */
16647 void onFocusChange(View v, boolean hasFocus);
16648 }
16649
16650 /**
16651 * Interface definition for a callback to be invoked when a view is clicked.
16652 */
16653 public interface OnClickListener {
16654 /**
16655 * Called when a view has been clicked.
16656 *
16657 * @param v The view that was clicked.
16658 */
16659 void onClick(View v);
16660 }
16661
16662 /**
16663 * Interface definition for a callback to be invoked when the context menu
16664 * for this view is being built.
16665 */
16666 public interface OnCreateContextMenuListener {
16667 /**
16668 * Called when the context menu for this view is being built. It is not
16669 * safe to hold onto the menu after this method returns.
16670 *
16671 * @param menu The context menu that is being built
16672 * @param v The view for which the context menu is being built
16673 * @param menuInfo Extra information about the item for which the
16674 * context menu should be shown. This information will vary
16675 * depending on the class of v.
16676 */
16677 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
16678 }
16679
Joe Onorato664644d2011-01-23 17:53:23 -080016680 /**
16681 * Interface definition for a callback to be invoked when the status bar changes
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016682 * visibility. This reports <strong>global</strong> changes to the system UI
16683 * state, not just what the application is requesting.
Joe Onorato664644d2011-01-23 17:53:23 -080016684 *
Philip Milne6c8ea062012-04-03 17:38:43 -070016685 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080016686 */
16687 public interface OnSystemUiVisibilityChangeListener {
16688 /**
16689 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070016690 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080016691 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040016692 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016693 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}. This tells you the
16694 * <strong>global</strong> state of the UI visibility flags, not what your
16695 * app is currently applying.
Joe Onorato664644d2011-01-23 17:53:23 -080016696 */
16697 public void onSystemUiVisibilityChange(int visibility);
16698 }
16699
Adam Powell4afd62b2011-02-18 15:02:18 -080016700 /**
16701 * Interface definition for a callback to be invoked when this view is attached
16702 * or detached from its window.
16703 */
16704 public interface OnAttachStateChangeListener {
16705 /**
16706 * Called when the view is attached to a window.
16707 * @param v The view that was attached
16708 */
16709 public void onViewAttachedToWindow(View v);
16710 /**
16711 * Called when the view is detached from a window.
16712 * @param v The view that was detached
16713 */
16714 public void onViewDetachedFromWindow(View v);
16715 }
16716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016717 private final class UnsetPressedState implements Runnable {
16718 public void run() {
16719 setPressed(false);
16720 }
16721 }
16722
16723 /**
16724 * Base class for derived classes that want to save and restore their own
16725 * state in {@link android.view.View#onSaveInstanceState()}.
16726 */
16727 public static class BaseSavedState extends AbsSavedState {
16728 /**
16729 * Constructor used when reading from a parcel. Reads the state of the superclass.
16730 *
16731 * @param source
16732 */
16733 public BaseSavedState(Parcel source) {
16734 super(source);
16735 }
16736
16737 /**
16738 * Constructor called by derived classes when creating their SavedState objects
16739 *
16740 * @param superState The state of the superclass of this view
16741 */
16742 public BaseSavedState(Parcelable superState) {
16743 super(superState);
16744 }
16745
16746 public static final Parcelable.Creator<BaseSavedState> CREATOR =
16747 new Parcelable.Creator<BaseSavedState>() {
16748 public BaseSavedState createFromParcel(Parcel in) {
16749 return new BaseSavedState(in);
16750 }
16751
16752 public BaseSavedState[] newArray(int size) {
16753 return new BaseSavedState[size];
16754 }
16755 };
16756 }
16757
16758 /**
16759 * A set of information given to a view when it is attached to its parent
16760 * window.
16761 */
16762 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016763 interface Callbacks {
16764 void playSoundEffect(int effectId);
16765 boolean performHapticFeedback(int effectId, boolean always);
16766 }
16767
16768 /**
16769 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
16770 * to a Handler. This class contains the target (View) to invalidate and
16771 * the coordinates of the dirty rectangle.
16772 *
16773 * For performance purposes, this class also implements a pool of up to
16774 * POOL_LIMIT objects that get reused. This reduces memory allocations
16775 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016776 */
Romain Guyd928d682009-03-31 17:52:16 -070016777 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016778 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070016779 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
16780 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070016781 public InvalidateInfo newInstance() {
16782 return new InvalidateInfo();
16783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016784
Romain Guyd928d682009-03-31 17:52:16 -070016785 public void onAcquired(InvalidateInfo element) {
16786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016787
Romain Guyd928d682009-03-31 17:52:16 -070016788 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070016789 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070016790 }
16791 }, POOL_LIMIT)
16792 );
16793
16794 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016795 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016796
16797 View target;
16798
16799 int left;
16800 int top;
16801 int right;
16802 int bottom;
16803
Romain Guyd928d682009-03-31 17:52:16 -070016804 public void setNextPoolable(InvalidateInfo element) {
16805 mNext = element;
16806 }
16807
16808 public InvalidateInfo getNextPoolable() {
16809 return mNext;
16810 }
16811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016812 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070016813 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016814 }
16815
16816 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070016817 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016818 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070016819
16820 public boolean isPooled() {
16821 return mIsPooled;
16822 }
16823
16824 public void setPooled(boolean isPooled) {
16825 mIsPooled = isPooled;
16826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016827 }
16828
16829 final IWindowSession mSession;
16830
16831 final IWindow mWindow;
16832
16833 final IBinder mWindowToken;
16834
16835 final Callbacks mRootCallbacks;
16836
Romain Guy59a12ca2011-06-09 17:48:21 -070016837 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080016838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016839 /**
16840 * The top view of the hierarchy.
16841 */
16842 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070016843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016844 IBinder mPanelParentWindowToken;
16845 Surface mSurface;
16846
Romain Guyb051e892010-09-28 19:09:36 -070016847 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080016848 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070016849 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080016850
Romain Guy7e4e5612012-03-05 14:37:29 -080016851 boolean mScreenOn;
16852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016853 /**
Romain Guy8506ab42009-06-11 17:35:47 -070016854 * Scale factor used by the compatibility mode
16855 */
16856 float mApplicationScale;
16857
16858 /**
16859 * Indicates whether the application is in compatibility mode
16860 */
16861 boolean mScalingRequired;
16862
16863 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016864 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080016865 */
16866 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080016867
Dianne Hackborn63042d62011-01-26 18:56:29 -080016868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016869 * Left position of this view's window
16870 */
16871 int mWindowLeft;
16872
16873 /**
16874 * Top position of this view's window
16875 */
16876 int mWindowTop;
16877
16878 /**
Adam Powell26153a32010-11-08 15:22:27 -080016879 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070016880 */
Adam Powell26153a32010-11-08 15:22:27 -080016881 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070016882
16883 /**
Dianne Hackborn3556c9a2012-05-04 16:31:25 -070016884 * Describes the parts of the window that are currently completely
16885 * obscured by system UI elements.
16886 */
16887 final Rect mSystemInsets = new Rect();
16888
16889 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016890 * For windows that are full-screen but using insets to layout inside
16891 * of the screen decorations, these are the current insets for the
16892 * content of the window.
16893 */
16894 final Rect mContentInsets = new Rect();
16895
16896 /**
16897 * For windows that are full-screen but using insets to layout inside
16898 * of the screen decorations, these are the current insets for the
16899 * actual visible parts of the window.
16900 */
16901 final Rect mVisibleInsets = new Rect();
16902
16903 /**
16904 * The internal insets given by this window. This value is
16905 * supplied by the client (through
16906 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
16907 * be given to the window manager when changed to be used in laying
16908 * out windows behind it.
16909 */
16910 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
16911 = new ViewTreeObserver.InternalInsetsInfo();
16912
16913 /**
16914 * All views in the window's hierarchy that serve as scroll containers,
16915 * used to determine if the window can be resized or must be panned
16916 * to adjust for a soft input area.
16917 */
16918 final ArrayList<View> mScrollContainers = new ArrayList<View>();
16919
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070016920 final KeyEvent.DispatcherState mKeyDispatchState
16921 = new KeyEvent.DispatcherState();
16922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016923 /**
16924 * Indicates whether the view's window currently has the focus.
16925 */
16926 boolean mHasWindowFocus;
16927
16928 /**
16929 * The current visibility of the window.
16930 */
16931 int mWindowVisibility;
16932
16933 /**
16934 * Indicates the time at which drawing started to occur.
16935 */
16936 long mDrawingTime;
16937
16938 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070016939 * Indicates whether or not ignoring the DIRTY_MASK flags.
16940 */
16941 boolean mIgnoreDirtyState;
16942
16943 /**
Romain Guy02ccac62011-06-24 13:20:23 -070016944 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
16945 * to avoid clearing that flag prematurely.
16946 */
16947 boolean mSetIgnoreDirtyState = false;
16948
16949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016950 * Indicates whether the view's window is currently in touch mode.
16951 */
16952 boolean mInTouchMode;
16953
16954 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070016955 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016956 * the next time it performs a traversal
16957 */
16958 boolean mRecomputeGlobalAttributes;
16959
16960 /**
Dianne Hackborn9a230e02011-10-06 11:51:27 -070016961 * Always report new attributes at next traversal.
16962 */
16963 boolean mForceReportNewAttributes;
16964
16965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016966 * Set during a traveral if any views want to keep the screen on.
16967 */
16968 boolean mKeepScreenOn;
16969
16970 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016971 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
16972 */
16973 int mSystemUiVisibility;
16974
16975 /**
Dianne Hackborn139e5aa2012-05-05 20:36:38 -070016976 * Hack to force certain system UI visibility flags to be cleared.
16977 */
16978 int mDisabledSystemUiVisibility;
16979
16980 /**
Joe Onorato664644d2011-01-23 17:53:23 -080016981 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
16982 * attached.
16983 */
16984 boolean mHasSystemUiListeners;
16985
16986 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016987 * Set if the visibility of any views has changed.
16988 */
16989 boolean mViewVisibilityChanged;
16990
16991 /**
16992 * Set to true if a view has been scrolled.
16993 */
16994 boolean mViewScrollChanged;
16995
16996 /**
16997 * Global to the view hierarchy used as a temporary for dealing with
16998 * x/y points in the transparent region computations.
16999 */
17000 final int[] mTransparentLocation = new int[2];
17001
17002 /**
17003 * Global to the view hierarchy used as a temporary for dealing with
17004 * x/y points in the ViewGroup.invalidateChild implementation.
17005 */
17006 final int[] mInvalidateChildLocation = new int[2];
17007
Chet Haasec3aa3612010-06-17 08:50:37 -070017008
17009 /**
17010 * Global to the view hierarchy used as a temporary for dealing with
17011 * x/y location when view is transformed.
17012 */
17013 final float[] mTmpTransformLocation = new float[2];
17014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017015 /**
17016 * The view tree observer used to dispatch global events like
17017 * layout, pre-draw, touch mode change, etc.
17018 */
17019 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
17020
17021 /**
17022 * A Canvas used by the view hierarchy to perform bitmap caching.
17023 */
17024 Canvas mCanvas;
17025
17026 /**
Jeff Browna175a5b2012-02-15 19:18:31 -080017027 * The view root impl.
17028 */
17029 final ViewRootImpl mViewRootImpl;
17030
17031 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070017032 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017033 * handler can be used to pump events in the UI events queue.
17034 */
17035 final Handler mHandler;
17036
17037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017038 * Temporary for use in computing invalidate rectangles while
17039 * calling up the hierarchy.
17040 */
17041 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070017042
17043 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070017044 * Temporary for use in computing hit areas with transformed views
17045 */
17046 final RectF mTmpTransformRect = new RectF();
17047
17048 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070017049 * Temporary list for use in collecting focusable descendents of a view.
17050 */
Svetoslav Ganov42138042012-03-20 11:51:39 -070017051 final ArrayList<View> mTempArrayList = new ArrayList<View>(24);
svetoslavganov75986cf2009-05-14 22:28:01 -070017052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017053 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070017054 * The id of the window for accessibility purposes.
17055 */
17056 int mAccessibilityWindowId = View.NO_ID;
17057
17058 /**
Svetoslav Ganov42138042012-03-20 11:51:39 -070017059 * Whether to ingore not exposed for accessibility Views when
17060 * reporting the view tree to accessibility services.
17061 */
17062 boolean mIncludeNotImportantViews;
17063
17064 /**
17065 * The drawable for highlighting accessibility focus.
17066 */
17067 Drawable mAccessibilityFocusDrawable;
17068
17069 /**
Philip Milne10ca24a2012-04-23 15:38:27 -070017070 * Show where the margins, bounds and layout bounds are for each view.
17071 */
Romain Guy4b8c4f82012-04-27 15:48:35 -070017072 final boolean mDebugLayout = SystemProperties.getBoolean(DEBUG_LAYOUT_PROPERTY, false);
Philip Milne10ca24a2012-04-23 15:38:27 -070017073
17074 /**
Romain Guyab4c4f4f2012-05-06 13:11:24 -070017075 * Point used to compute visible regions.
17076 */
17077 final Point mPoint = new Point();
17078
17079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017080 * Creates a new set of attachment information with the specified
17081 * events handler and thread.
17082 *
17083 * @param handler the events handler the view must use
17084 */
17085 AttachInfo(IWindowSession session, IWindow window,
Jeff Browna175a5b2012-02-15 19:18:31 -080017086 ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017087 mSession = session;
17088 mWindow = window;
17089 mWindowToken = window.asBinder();
Jeff Browna175a5b2012-02-15 19:18:31 -080017090 mViewRootImpl = viewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017091 mHandler = handler;
17092 mRootCallbacks = effectPlayer;
17093 }
17094 }
17095
17096 /**
17097 * <p>ScrollabilityCache holds various fields used by a View when scrolling
17098 * is supported. This avoids keeping too many unused fields in most
17099 * instances of View.</p>
17100 */
Mike Cleronf116bf82009-09-27 19:14:12 -070017101 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080017102
Mike Cleronf116bf82009-09-27 19:14:12 -070017103 /**
17104 * Scrollbars are not visible
17105 */
17106 public static final int OFF = 0;
17107
17108 /**
17109 * Scrollbars are visible
17110 */
17111 public static final int ON = 1;
17112
17113 /**
17114 * Scrollbars are fading away
17115 */
17116 public static final int FADING = 2;
17117
17118 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080017119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017120 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070017121 public int scrollBarDefaultDelayBeforeFade;
17122 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017123
17124 public int scrollBarSize;
17125 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070017126 public float[] interpolatorValues;
17127 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017128
17129 public final Paint paint;
17130 public final Matrix matrix;
17131 public Shader shader;
17132
Mike Cleronf116bf82009-09-27 19:14:12 -070017133 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
17134
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017135 private static final float[] OPAQUE = { 255 };
17136 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080017137
Mike Cleronf116bf82009-09-27 19:14:12 -070017138 /**
17139 * When fading should start. This time moves into the future every time
17140 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
17141 */
17142 public long fadeStartTime;
17143
17144
17145 /**
17146 * The current state of the scrollbars: ON, OFF, or FADING
17147 */
17148 public int state = OFF;
17149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017150 private int mLastColor;
17151
Mike Cleronf116bf82009-09-27 19:14:12 -070017152 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017153 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
17154 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070017155 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
17156 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017157
17158 paint = new Paint();
17159 matrix = new Matrix();
17160 // use use a height of 1, and then wack the matrix each time we
17161 // actually use it.
17162 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017164 paint.setShader(shader);
17165 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070017166 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017167 }
Romain Guy8506ab42009-06-11 17:35:47 -070017168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017169 public void setFadeColor(int color) {
17170 if (color != 0 && color != mLastColor) {
17171 mLastColor = color;
17172 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070017173
Romain Guye55e1a72009-08-27 10:42:26 -070017174 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17175 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070017176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017177 paint.setShader(shader);
17178 // Restore the default transfer mode (src_over)
17179 paint.setXfermode(null);
17180 }
17181 }
Joe Malin32736f02011-01-19 16:14:20 -080017182
Mike Cleronf116bf82009-09-27 19:14:12 -070017183 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070017184 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070017185 if (now >= fadeStartTime) {
17186
17187 // the animation fades the scrollbars out by changing
17188 // the opacity (alpha) from fully opaque to fully
17189 // transparent
17190 int nextFrame = (int) now;
17191 int framesCount = 0;
17192
17193 Interpolator interpolator = scrollBarInterpolator;
17194
17195 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017196 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070017197
17198 // End transparent
17199 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080017200 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070017201
17202 state = FADING;
17203
17204 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080017205 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070017206 }
17207 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070017208 }
Mike Cleronf116bf82009-09-27 19:14:12 -070017209
Svetoslav Ganova0156172011-06-26 17:55:44 -070017210 /**
17211 * Resuable callback for sending
17212 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
17213 */
17214 private class SendViewScrolledAccessibilityEvent implements Runnable {
17215 public volatile boolean mIsPending;
17216
17217 public void run() {
17218 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
17219 mIsPending = false;
17220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017221 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017222
17223 /**
17224 * <p>
17225 * This class represents a delegate that can be registered in a {@link View}
17226 * to enhance accessibility support via composition rather via inheritance.
17227 * It is specifically targeted to widget developers that extend basic View
17228 * classes i.e. classes in package android.view, that would like their
17229 * applications to be backwards compatible.
17230 * </p>
Joe Fernandeze1302ed2012-02-06 14:30:15 -080017231 * <div class="special reference">
17232 * <h3>Developer Guides</h3>
17233 * <p>For more information about making applications accessible, read the
17234 * <a href="{@docRoot}guide/topics/ui/accessibility/index.html">Accessibility</a>
17235 * developer guide.</p>
17236 * </div>
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017237 * <p>
17238 * A scenario in which a developer would like to use an accessibility delegate
17239 * is overriding a method introduced in a later API version then the minimal API
17240 * version supported by the application. For example, the method
17241 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} is not available
17242 * in API version 4 when the accessibility APIs were first introduced. If a
17243 * developer would like his application to run on API version 4 devices (assuming
17244 * all other APIs used by the application are version 4 or lower) and take advantage
17245 * of this method, instead of overriding the method which would break the application's
17246 * backwards compatibility, he can override the corresponding method in this
17247 * delegate and register the delegate in the target View if the API version of
17248 * the system is high enough i.e. the API version is same or higher to the API
17249 * version that introduced
17250 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)}.
17251 * </p>
17252 * <p>
17253 * Here is an example implementation:
17254 * </p>
17255 * <code><pre><p>
17256 * if (Build.VERSION.SDK_INT >= 14) {
17257 * // If the API version is equal of higher than the version in
17258 * // which onInitializeAccessibilityNodeInfo was introduced we
17259 * // register a delegate with a customized implementation.
17260 * View view = findViewById(R.id.view_id);
17261 * view.setAccessibilityDelegate(new AccessibilityDelegate() {
17262 * public void onInitializeAccessibilityNodeInfo(View host,
17263 * AccessibilityNodeInfo info) {
17264 * // Let the default implementation populate the info.
17265 * super.onInitializeAccessibilityNodeInfo(host, info);
17266 * // Set some other information.
17267 * info.setEnabled(host.isEnabled());
17268 * }
17269 * });
17270 * }
17271 * </code></pre></p>
17272 * <p>
17273 * This delegate contains methods that correspond to the accessibility methods
17274 * in View. If a delegate has been specified the implementation in View hands
17275 * off handling to the corresponding method in this delegate. The default
17276 * implementation the delegate methods behaves exactly as the corresponding
17277 * method in View for the case of no accessibility delegate been set. Hence,
17278 * to customize the behavior of a View method, clients can override only the
17279 * corresponding delegate method without altering the behavior of the rest
17280 * accessibility related methods of the host view.
17281 * </p>
17282 */
17283 public static class AccessibilityDelegate {
17284
17285 /**
17286 * Sends an accessibility event of the given type. If accessibility is not
17287 * enabled this method has no effect.
17288 * <p>
17289 * The default implementation behaves as {@link View#sendAccessibilityEvent(int)
17290 * View#sendAccessibilityEvent(int)} for the case of no accessibility delegate
17291 * been set.
17292 * </p>
17293 *
17294 * @param host The View hosting the delegate.
17295 * @param eventType The type of the event to send.
17296 *
17297 * @see View#sendAccessibilityEvent(int) View#sendAccessibilityEvent(int)
17298 */
17299 public void sendAccessibilityEvent(View host, int eventType) {
17300 host.sendAccessibilityEventInternal(eventType);
17301 }
17302
17303 /**
17304 * Sends an accessibility event. This method behaves exactly as
17305 * {@link #sendAccessibilityEvent(View, int)} but takes as an argument an
17306 * empty {@link AccessibilityEvent} and does not perform a check whether
17307 * accessibility is enabled.
17308 * <p>
17309 * The default implementation behaves as
17310 * {@link View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17311 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)} for
17312 * the case of no accessibility delegate been set.
17313 * </p>
17314 *
17315 * @param host The View hosting the delegate.
17316 * @param event The event to send.
17317 *
17318 * @see View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17319 * View#sendAccessibilityEventUnchecked(AccessibilityEvent)
17320 */
17321 public void sendAccessibilityEventUnchecked(View host, AccessibilityEvent event) {
17322 host.sendAccessibilityEventUncheckedInternal(event);
17323 }
17324
17325 /**
17326 * Dispatches an {@link AccessibilityEvent} to the host {@link View} first and then
17327 * to its children for adding their text content to the event.
17328 * <p>
17329 * The default implementation behaves as
17330 * {@link View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17331 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)} for
17332 * the case of no accessibility delegate been set.
17333 * </p>
17334 *
17335 * @param host The View hosting the delegate.
17336 * @param event The event.
17337 * @return True if the event population was completed.
17338 *
17339 * @see View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17340 * View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
17341 */
17342 public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17343 return host.dispatchPopulateAccessibilityEventInternal(event);
17344 }
17345
17346 /**
17347 * Gives a chance to the host View to populate the accessibility event with its
17348 * text content.
17349 * <p>
17350 * The default implementation behaves as
17351 * {@link View#onPopulateAccessibilityEvent(AccessibilityEvent)
17352 * View#onPopulateAccessibilityEvent(AccessibilityEvent)} for
17353 * the case of no accessibility delegate been set.
17354 * </p>
17355 *
17356 * @param host The View hosting the delegate.
17357 * @param event The accessibility event which to populate.
17358 *
17359 * @see View#onPopulateAccessibilityEvent(AccessibilityEvent)
17360 * View#onPopulateAccessibilityEvent(AccessibilityEvent)
17361 */
17362 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
17363 host.onPopulateAccessibilityEventInternal(event);
17364 }
17365
17366 /**
17367 * Initializes an {@link AccessibilityEvent} with information about the
17368 * the host View which is the event source.
17369 * <p>
17370 * The default implementation behaves as
17371 * {@link View#onInitializeAccessibilityEvent(AccessibilityEvent)
17372 * View#onInitializeAccessibilityEvent(AccessibilityEvent)} for
17373 * the case of no accessibility delegate been set.
17374 * </p>
17375 *
17376 * @param host The View hosting the delegate.
17377 * @param event The event to initialize.
17378 *
17379 * @see View#onInitializeAccessibilityEvent(AccessibilityEvent)
17380 * View#onInitializeAccessibilityEvent(AccessibilityEvent)
17381 */
17382 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
17383 host.onInitializeAccessibilityEventInternal(event);
17384 }
17385
17386 /**
17387 * Initializes an {@link AccessibilityNodeInfo} with information about the host view.
17388 * <p>
17389 * The default implementation behaves as
17390 * {@link View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17391 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} for
17392 * the case of no accessibility delegate been set.
17393 * </p>
17394 *
17395 * @param host The View hosting the delegate.
17396 * @param info The instance to initialize.
17397 *
17398 * @see View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17399 * View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
17400 */
17401 public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
17402 host.onInitializeAccessibilityNodeInfoInternal(info);
17403 }
17404
17405 /**
17406 * Called when a child of the host View has requested sending an
17407 * {@link AccessibilityEvent} and gives an opportunity to the parent (the host)
17408 * to augment the event.
17409 * <p>
17410 * The default implementation behaves as
17411 * {@link ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17412 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)} for
17413 * the case of no accessibility delegate been set.
17414 * </p>
17415 *
17416 * @param host The View hosting the delegate.
17417 * @param child The child which requests sending the event.
17418 * @param event The event to be sent.
17419 * @return True if the event should be sent
17420 *
17421 * @see ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17422 * ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
17423 */
17424 public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child,
17425 AccessibilityEvent event) {
17426 return host.onRequestSendAccessibilityEventInternal(child, event);
17427 }
Svetoslav Ganov02107852011-10-03 17:06:56 -070017428
17429 /**
17430 * Gets the provider for managing a virtual view hierarchy rooted at this View
17431 * and reported to {@link android.accessibilityservice.AccessibilityService}s
17432 * that explore the window content.
17433 * <p>
17434 * The default implementation behaves as
17435 * {@link View#getAccessibilityNodeProvider() View#getAccessibilityNodeProvider()} for
17436 * the case of no accessibility delegate been set.
17437 * </p>
17438 *
17439 * @return The provider.
17440 *
17441 * @see AccessibilityNodeProvider
17442 */
17443 public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
17444 return null;
17445 }
Svetoslav Ganov031d9c12011-09-09 16:41:13 -070017446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017447}